( day: DailyCosts["daily"][0], slice: DailyCosts["daily"][0], )
| 52 | } |
| 53 | |
| 54 | function rollupSlice( |
| 55 | day: DailyCosts["daily"][0], |
| 56 | slice: DailyCosts["daily"][0], |
| 57 | ): DailyCosts["daily"][0] { |
| 58 | day.subtotal = sumStringFloats(day.subtotal, slice.subtotal); |
| 59 | day.total = sumStringFloats(day.total, slice.total); |
| 60 | for (const costKey of ["storage", "compute"] as const) { |
| 61 | day.costs[costKey].subtotal = sumStringFloats( |
| 62 | day.costs[costKey].subtotal, |
| 63 | slice.costs[costKey].subtotal, |
| 64 | ); |
| 65 | day.costs[costKey].total = sumStringFloats( |
| 66 | day.costs[costKey].total, |
| 67 | slice.costs[costKey].total, |
| 68 | ); |
| 69 | for (const slicePrice of slice.costs[costKey].prices) { |
| 70 | const dayPrice = day.costs[costKey].prices.find((p) => { |
| 71 | return ( |
| 72 | p.regionId === slicePrice.regionId && |
| 73 | // compare replicaIds for compute prices, otherwise just regionId |
| 74 | ("replicaId" in slicePrice && "replicaId" in p |
| 75 | ? slicePrice.replicaId === p.replicaId |
| 76 | : true) |
| 77 | ); |
| 78 | }); |
| 79 | if (dayPrice) { |
| 80 | dayPrice.subtotal = sumStringFloats( |
| 81 | dayPrice.subtotal, |
| 82 | slicePrice.subtotal, |
| 83 | ); |
| 84 | // If the rate is changed, poison the unit amount so we don't attempt |
| 85 | // to do any calculations based on it. if we end up needing this we can |
| 86 | // update the data structure. |
| 87 | dayPrice.unitAmount = |
| 88 | dayPrice.unitAmount === slicePrice.unitAmount |
| 89 | ? dayPrice.unitAmount |
| 90 | : "-1"; |
| 91 | } else { |
| 92 | (day.costs[costKey].prices as Prices[]).push( |
| 93 | structuredClone(slicePrice), |
| 94 | ); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | return day; |
| 99 | } |
| 100 | |
| 101 | function sumStringFloats(left: string, right: string): string { |
| 102 | return (parseFloat(left) + parseFloat(right)).toString(); |
no test coverage detected