diff --git a/apps/server/src/services/flux-transaction.ts b/apps/server/src/services/flux-transaction.ts index 73d3d5601..748c331b2 100644 --- a/apps/server/src/services/flux-transaction.ts +++ b/apps/server/src/services/flux-transaction.ts @@ -1,7 +1,7 @@ import type { Database } from '../libs/db' import { useLogger } from '@guiiai/logg' -import { desc, eq, sql } from 'drizzle-orm' +import { and, desc, eq, inArray } from 'drizzle-orm' import * as schema from '../schemas/flux-transaction' @@ -48,27 +48,22 @@ export function createFluxTransactionService(db: Database) { }, async getStats(userId: string) { - const rows = await db.select({ - type: schema.fluxTransaction.type, - total: sql`sum(${schema.fluxTransaction.amount})`.mapWith(Number), + // Get the balance right after the most recent credit/initial transaction + // as the "capacity" for the progress bar + const [latestCredit] = await db.select({ + balanceAfter: schema.fluxTransaction.balanceAfter, }) .from(schema.fluxTransaction) - .where(eq(schema.fluxTransaction.userId, userId)) - .groupBy(schema.fluxTransaction.type) + .where( + and( + eq(schema.fluxTransaction.userId, userId), + inArray(schema.fluxTransaction.type, ['credit', 'initial']), + ), + ) + .orderBy(desc(schema.fluxTransaction.createdAt)) + .limit(1) - let totalReceived = 0 - let totalConsumed = 0 - - for (const row of rows) { - if (row.type === 'credit' || row.type === 'initial') { - totalReceived += row.total - } - else if (row.type === 'debit') { - totalConsumed += row.total - } - } - - return { totalReceived, totalConsumed } + return { capacity: latestCredit?.balanceAfter ?? 0 } }, } } diff --git a/packages/stage-pages/src/pages/settings/flux.vue b/packages/stage-pages/src/pages/settings/flux.vue index 78bbde768..5c225b6f0 100644 --- a/packages/stage-pages/src/pages/settings/flux.vue +++ b/packages/stage-pages/src/pages/settings/flux.vue @@ -68,14 +68,12 @@ const auditHasMore = ref(false) const auditOffset = ref(0) const AUDIT_PAGE_SIZE = 20 -const totalReceived = ref(0) -const totalConsumed = ref(0) +const capacity = ref(0) const fluxPercentage = computed(() => { - if (totalReceived.value === 0) + if (capacity.value === 0) return credits.value > 0 ? 100 : 0 - const remaining = Math.max(0, totalReceived.value - totalConsumed.value) - return Math.min(100, Math.round((remaining / totalReceived.value) * 100)) + return Math.min(100, Math.round((credits.value / capacity.value) * 100)) }) async function fetchStats() { @@ -83,8 +81,7 @@ async function fetchStats() { const res = await client.api.v1.flux.stats.$get() if (res.ok) { const data = await res.json() - totalReceived.value = data.totalReceived - totalConsumed.value = data.totalConsumed + capacity.value = data.capacity } } catch { @@ -173,7 +170,7 @@ const groupedRows = computed(() => { model: (record.metadata?.model as string) || '', count: group.length, totalAmount: group.reduce((sum, r) => sum + r.amount, 0), - firstTime: group.at(-1).createdAt, + firstTime: group.at(-1)!.createdAt, lastTime: group[0].createdAt, records: group, })