(
billingEntity: BillingEntity,
billingPeriod: { start: Date; end: Date },
source?: UsageLogSource | UsageLogSource[],
executor: DbClient = db
)
| 181 | * Legacy pre-cutover usage remains in userStats as a baseline until reset. |
| 182 | */ |
| 183 | export async function getBillingPeriodUsageCost( |
| 184 | billingEntity: BillingEntity, |
| 185 | billingPeriod: { start: Date; end: Date }, |
| 186 | source?: UsageLogSource | UsageLogSource[], |
| 187 | executor: DbClient = db |
| 188 | ): Promise<number> { |
| 189 | const conditions = [ |
| 190 | eq(usageLog.billingEntityType, billingEntity.type), |
| 191 | eq(usageLog.billingEntityId, billingEntity.id), |
| 192 | eq(usageLog.billingPeriodStart, billingPeriod.start), |
| 193 | eq(usageLog.billingPeriodEnd, billingPeriod.end), |
| 194 | ] |
| 195 | if (source) { |
| 196 | conditions.push( |
| 197 | Array.isArray(source) ? inArray(usageLog.source, source) : eq(usageLog.source, source) |
| 198 | ) |
| 199 | } |
| 200 | |
| 201 | const [row] = await executor |
| 202 | .select({ |
| 203 | cost: sql<string>`COALESCE(SUM(${usageLog.cost}), 0)`, |
| 204 | }) |
| 205 | .from(usageLog) |
| 206 | .where(and(...conditions)) |
| 207 | |
| 208 | return Number.parseFloat(row?.cost ?? '0') |
| 209 | } |
| 210 | |
| 211 | export async function getBillingPeriodUsageCostByUser( |
| 212 | billingEntity: BillingEntity, |
no test coverage detected