MCPcopy Index your code
hub / github.com/simstudioai/sim / getBillingPeriodUsageCost

Function getBillingPeriodUsageCost

apps/sim/lib/billing/core/usage-log.ts:183–209  ·  view source on GitHub ↗
(
  billingEntity: BillingEntity,
  billingPeriod: { start: Date; end: Date },
  source?: UsageLogSource | UsageLogSource[],
  executor: DbClient = db
)

Source from the content-addressed store, hash-verified

181 * Legacy pre-cutover usage remains in userStats as a baseline until reset.
182 */
183export 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
211export async function getBillingPeriodUsageCostByUser(
212 billingEntity: BillingEntity,

Calls 2

eqFunction · 0.50
pushMethod · 0.45

Tested by

no test coverage detected