Skip to content

Commit 76420c8

Browse files
committed
ack PR comment
1 parent 8c2d1bc commit 76420c8

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

apps/sim/lib/billing/core/usage-log.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ export async function logWorkflowUsageBatch(params: LogWorkflowUsageBatchParams)
180180
executionId: string | null
181181
}> = []
182182

183-
// Add base execution fee entry
184183
if (params.baseExecutionCharge && params.baseExecutionCharge > 0) {
185184
entries.push({
186185
id: crypto.randomUUID(),
@@ -196,7 +195,6 @@ export async function logWorkflowUsageBatch(params: LogWorkflowUsageBatchParams)
196195
})
197196
}
198197

199-
// Add per-model entries
200198
if (params.models) {
201199
for (const [modelName, modelData] of Object.entries(params.models)) {
202200
if (modelData.total > 0) {
@@ -299,7 +297,6 @@ export async function getUserUsageLogs(
299297
const { source, workspaceId, startDate, endDate, limit = 50, cursor } = options
300298

301299
try {
302-
// Build conditions
303300
const conditions = [eq(usageLog.userId, userId)]
304301

305302
if (source) {
@@ -319,33 +316,29 @@ export async function getUserUsageLogs(
319316
}
320317

321318
if (cursor) {
322-
// For cursor-based pagination, get logs older than the cursor
323319
const cursorLog = await db
324320
.select({ createdAt: usageLog.createdAt })
325321
.from(usageLog)
326322
.where(eq(usageLog.id, cursor))
327323
.limit(1)
328324

329325
if (cursorLog.length > 0) {
330-
conditions.push(lte(usageLog.createdAt, cursorLog[0].createdAt))
331-
// Exclude the cursor itself
332-
conditions.push(sql`${usageLog.id} != ${cursor}`)
326+
conditions.push(
327+
sql`(${usageLog.createdAt} < ${cursorLog[0].createdAt} OR (${usageLog.createdAt} = ${cursorLog[0].createdAt} AND ${usageLog.id} < ${cursor}))`
328+
)
333329
}
334330
}
335331

336-
// Fetch logs with one extra to check if there are more
337332
const logs = await db
338333
.select()
339334
.from(usageLog)
340335
.where(and(...conditions))
341-
.orderBy(desc(usageLog.createdAt))
336+
.orderBy(desc(usageLog.createdAt), desc(usageLog.id))
342337
.limit(limit + 1)
343338

344-
// Check if there are more results
345339
const hasMore = logs.length > limit
346340
const resultLogs = hasMore ? logs.slice(0, limit) : logs
347341

348-
// Transform to response format
349342
const transformedLogs: UsageLogEntry[] = resultLogs.map((log) => ({
350343
id: log.id,
351344
createdAt: log.createdAt.toISOString(),
@@ -359,7 +352,6 @@ export async function getUserUsageLogs(
359352
...(log.executionId ? { executionId: log.executionId } : {}),
360353
}))
361354

362-
// Calculate summary (for the filtered period, not just this page)
363355
const summaryConditions = [eq(usageLog.userId, userId)]
364356
if (source) summaryConditions.push(eq(usageLog.source, source))
365357
if (workspaceId) summaryConditions.push(eq(usageLog.workspaceId, workspaceId))

0 commit comments

Comments
 (0)