Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ select
, sum(mu.total_cache_hit_tokens) as "sum_cache_hit_tokens"
, sum(mu.request_count) as "sum_request_count"
, sum(mu.error_count) as "sum_error_count"
from kilo_dw.dbt_prod.microdollar_usage_daily as mu
from microdollar_usage_daily as mu
where
mu.usage_date >= dateadd(week, -1, current_date())
and mu.usage_date < current_date()
Expand Down Expand Up @@ -157,9 +157,9 @@ function parseAndAggregateUsage(rows: string[][]): ModelProviderUsage[] {
costPerMillionTokens: ratio(usage.sumCost, usage.sumTokens),
cacheRatio: ratio(usage.sumCacheHitTokens, usage.sumInputTokens),
errorRate: ratio(usage.sumErrorCount, usage.sumRequestCount),
percentageOfModel:
ratio(usage.sumTokens, totalTokensByModel.get(usage.model) ?? 0) * 100,
})).filter(usage => usage.errorRate < MAXIMUM_ERROR_RATE);
percentageOfModel: ratio(usage.sumTokens, totalTokensByModel.get(usage.model) ?? 0) * 100,
}))
.filter(usage => usage.errorRate < MAXIMUM_ERROR_RATE);
}

export const GET = createPublicSnowflakeReport({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ select
else null
end as mode
, sum(coalesce(mu.total_input_tokens, 0)) + sum(coalesce(mu.total_output_tokens, 0)) as "tokens"
from kilo_dw.dbt_prod.microdollar_usage_daily as mu
from microdollar_usage_daily as mu
where
mu.usage_date >= dateadd(week, -1, current_date())
and mu.usage_date < current_date()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@ jest.mock('@/lib/snowflake', () => ({

type SnowflakeConfig = ReturnType<typeof resolveSnowflakeConfig>;

const FAKE_CONFIG = { accountHost: 'test.snowflakecomputing.com' } as SnowflakeConfig;
const FAKE_CONFIG = {
accountHost: 'test.snowflakecomputing.com',
jwtAccountIdentifier: 'TEST_ACCOUNT',
username: 'TEST_USER',
role: 'TEST_ROLE',
warehouse: 'TEST_WAREHOUSE',
database: 'TEST_DATABASE',
schema: 'TEST_SCHEMA',
privateKeyPem: 'test-private-key',
publicKeyFingerprint: 'SHA256:test-fingerprint',
} satisfies NonNullable<SnowflakeConfig>;

// The route builds a module-level in-process cache (1h TTL) at import time, so
// each test loads it in an isolated module registry to keep that cache from
Expand Down Expand Up @@ -96,6 +106,44 @@ describe('GET /api/public/leaderboard-provider-race', () => {
});
});

describe('leaderboard Snowflake database and schema', () => {
test.each([
{
route: 'leaderboard-model-usage',
loadRoute: () => import('../leaderboard-model-usage/route'),
table: 'microdollar_usage_daily',
},
{
route: 'leaderboard-model-provider-usage',
loadRoute: () => import('../leaderboard-model-provider-usage/route'),
table: 'microdollar_usage_daily',
},
{
route: 'leaderboard-provider-race',
loadRoute: () => import('./route'),
table: 'usage_daily',
},
])('$route uses the configured database and schema', async ({ loadRoute, table }) => {
await jest.isolateModulesAsync(async () => {
const snowflake = await import('@/lib/snowflake');
jest.mocked(snowflake.resolveSnowflakeConfig).mockReturnValue(FAKE_CONFIG);
const executeStatement = jest.mocked(snowflake.executeSnowflakeStatement);
executeStatement.mockReset().mockResolvedValue([]);
const { GET } = await loadRoute();

const response = await GET();

expect(response.status).toBe(200);
expect(executeStatement).toHaveBeenCalledTimes(1);
expect(executeStatement).toHaveBeenCalledWith({
config: FAKE_CONFIG,
statement: expect.stringMatching(new RegExp(`\\bfrom\\s+${table}\\s+as\\b`, 'i')),
timeoutSeconds: 30,
});
});
});
});

describe('OPTIONS /api/public/leaderboard-provider-race', () => {
test('returns 204 with CORS headers', async () => {
const { OPTIONS } = await import('./route');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ select
, ud.model_provider_company as provider
, ud.is_open_weights
, sum(ud.total_tokens) as tokens
from kilo_dw.dbt_prod.usage_daily as ud
from usage_daily as ud
where
ud.usage_date >= '2025-07-01'
and date_trunc('week', ud.usage_date) < date_trunc('week', current_date())
Expand Down