( dailyCosts: DailyCosts["daily"] | null, timeRangeFilter: number, )
| 168 | } |
| 169 | |
| 170 | export function getTimeRangeSlice( |
| 171 | dailyCosts: DailyCosts["daily"] | null, |
| 172 | timeRangeFilter: number, |
| 173 | ): DailyCosts["daily"] | null { |
| 174 | if (dailyCosts === null || dailyCosts.length <= timeRangeFilter) { |
| 175 | return dailyCosts; |
| 176 | } |
| 177 | const lastStartDate = parseISO(dailyCosts[dailyCosts.length - 1].startDate); |
| 178 | // Determine the starting date for the time range (subtract one because |
| 179 | // we're starting from the beginning of the last day). |
| 180 | const firstStartDate = subDays(lastStartDate, timeRangeFilter - 1); |
| 181 | let startOffset = -1 * timeRangeFilter; |
| 182 | // Seed the list with the known-minimum number of records from the tail. |
| 183 | const slices = dailyCosts.slice(startOffset); |
| 184 | startOffset--; |
| 185 | // ... then walk backwards from where we left off, prepending all records that |
| 186 | // fall on or after the start of the time range. Since we're counting back |
| 187 | // from the end of the Array, this is an inclusive guard (otherwise we'd omit |
| 188 | // the last element when calling `.at(-n)`). |
| 189 | while (Math.abs(startOffset) <= dailyCosts.length) { |
| 190 | const slice = dailyCosts.at(startOffset); |
| 191 | if (!slice) break; |
| 192 | const sliceStart = parseISO(slice.startDate); |
| 193 | if (firstStartDate.getTime() <= sliceStart.getTime()) { |
| 194 | slices.unshift(slice); |
| 195 | } else { |
| 196 | break; |
| 197 | } |
| 198 | startOffset--; |
| 199 | } |
| 200 | return slices; |
| 201 | } |
| 202 | |
| 203 | export function summarizeResourceCosts( |
| 204 | dailyCosts: DailyCosts["daily"], |
no test coverage detected