feat: harden analytics dashboard
This commit is contained in:
287
lib/query-details.ts
Normal file
287
lib/query-details.ts
Normal file
@@ -0,0 +1,287 @@
|
||||
import { query } from "./db";
|
||||
import { CACHE_CREATION, CACHE_READ, REAL_MODEL, TOKEN_NAME, cacheKey, cached, getChannelNames, getDisplayNames, timeWhere } from "./query-shared";
|
||||
|
||||
// ── 下钻详情 ──────────────────────────────────────────────────
|
||||
|
||||
export interface DetailBreakdown {
|
||||
name: string;
|
||||
calls: number;
|
||||
total_tokens: number;
|
||||
quota: number;
|
||||
}
|
||||
export interface TokenDetailBreakdown extends DetailBreakdown {
|
||||
models: DetailBreakdown[];
|
||||
}
|
||||
|
||||
export function getUserDetail(
|
||||
username: string,
|
||||
startTs?: number,
|
||||
endTs?: number
|
||||
) {
|
||||
return cached(cacheKey("userDetail", username, startTs, endTs), async () => {
|
||||
const params: (string | number | boolean | null)[] = [];
|
||||
const where = timeWhere(params, startTs, endTs);
|
||||
params.push(username);
|
||||
|
||||
// 用户总览
|
||||
const overview = await query(
|
||||
`SELECT COUNT(*)::int as calls,
|
||||
COALESCE(SUM(prompt_tokens),0)::bigint as prompt,
|
||||
COALESCE(SUM(completion_tokens),0)::bigint as completion,
|
||||
COALESCE(SUM(${CACHE_CREATION}),0)::bigint as cache_creation,
|
||||
COALESCE(SUM(${CACHE_READ}),0)::bigint as cache_read,
|
||||
COALESCE(SUM(quota),0)::bigint as quota
|
||||
FROM logs WHERE ${where} AND username = $${params.length}`,
|
||||
params
|
||||
);
|
||||
|
||||
// 用户的模型分布
|
||||
const params2: (string | number | boolean | null)[] = [];
|
||||
const where2 = timeWhere(params2, startTs, endTs);
|
||||
params2.push(username);
|
||||
|
||||
const models = await query(
|
||||
`SELECT ${REAL_MODEL} as model,
|
||||
COUNT(*)::int as calls,
|
||||
COALESCE(SUM(prompt_tokens + completion_tokens),0)::bigint as tokens,
|
||||
COALESCE(SUM(${CACHE_CREATION}),0)::bigint as cache_creation,
|
||||
COALESCE(SUM(${CACHE_READ}),0)::bigint as cache_read,
|
||||
COALESCE(SUM(quota),0)::bigint as quota
|
||||
FROM logs WHERE ${where2} AND username = $${params2.length}
|
||||
GROUP BY model
|
||||
ORDER BY tokens DESC LIMIT 20`,
|
||||
params2
|
||||
);
|
||||
|
||||
const params3: (string | number | boolean | null)[] = [];
|
||||
const where3 = timeWhere(params3, startTs, endTs);
|
||||
params3.push(username);
|
||||
|
||||
const tokens = await query(
|
||||
`SELECT ${TOKEN_NAME} as token_name,
|
||||
COUNT(*)::int as calls,
|
||||
COALESCE(SUM(prompt_tokens + completion_tokens),0)::bigint as tokens,
|
||||
COALESCE(SUM(${CACHE_CREATION}),0)::bigint as cache_creation,
|
||||
COALESCE(SUM(${CACHE_READ}),0)::bigint as cache_read,
|
||||
COALESCE(SUM(quota),0)::bigint as quota
|
||||
FROM logs WHERE ${where3} AND username = $${params3.length}
|
||||
GROUP BY token_name
|
||||
ORDER BY COALESCE(SUM(prompt_tokens),0) + COALESCE(SUM(completion_tokens),0) + COALESCE(SUM(${CACHE_CREATION}),0) + COALESCE(SUM(${CACHE_READ}),0) DESC, token_name ASC
|
||||
LIMIT 50`,
|
||||
params3
|
||||
);
|
||||
|
||||
const params4: (string | number | boolean | null)[] = [];
|
||||
const where4 = timeWhere(params4, startTs, endTs);
|
||||
params4.push(username);
|
||||
|
||||
const tokenModels = await query(
|
||||
`WITH filtered_logs AS (
|
||||
SELECT ${TOKEN_NAME} as token_name,
|
||||
${REAL_MODEL} as model,
|
||||
prompt_tokens,
|
||||
completion_tokens,
|
||||
${CACHE_CREATION} as cache_creation,
|
||||
${CACHE_READ} as cache_read,
|
||||
quota
|
||||
FROM logs WHERE ${where4} AND username = $${params4.length}
|
||||
),
|
||||
top_tokens AS (
|
||||
SELECT token_name
|
||||
FROM filtered_logs
|
||||
GROUP BY token_name
|
||||
ORDER BY COALESCE(SUM(prompt_tokens),0) + COALESCE(SUM(completion_tokens),0) + COALESCE(SUM(cache_creation),0) + COALESCE(SUM(cache_read),0) DESC, token_name ASC
|
||||
LIMIT 50
|
||||
),
|
||||
token_models AS (
|
||||
SELECT filtered_logs.token_name,
|
||||
filtered_logs.model,
|
||||
COUNT(*)::int as calls,
|
||||
COALESCE(SUM(prompt_tokens + completion_tokens),0)::bigint as tokens,
|
||||
COALESCE(SUM(cache_creation),0)::bigint as cache_creation,
|
||||
COALESCE(SUM(cache_read),0)::bigint as cache_read,
|
||||
COALESCE(SUM(quota),0)::bigint as quota
|
||||
FROM filtered_logs
|
||||
INNER JOIN top_tokens ON filtered_logs.token_name = top_tokens.token_name
|
||||
GROUP BY filtered_logs.token_name, filtered_logs.model
|
||||
),
|
||||
ranked AS (
|
||||
SELECT *,
|
||||
ROW_NUMBER() OVER (PARTITION BY token_name ORDER BY tokens + cache_creation + cache_read DESC, model ASC) as rn
|
||||
FROM token_models
|
||||
)
|
||||
SELECT token_name, model, calls, tokens, cache_creation, cache_read, quota
|
||||
FROM ranked
|
||||
WHERE rn <= 10
|
||||
ORDER BY token_name ASC, tokens + cache_creation + cache_read DESC, model ASC`,
|
||||
params4
|
||||
);
|
||||
|
||||
const modelsByToken = new Map<string, DetailBreakdown[]>();
|
||||
for (const row of tokenModels) {
|
||||
const tokenName = String(row.token_name ?? "");
|
||||
const rows = modelsByToken.get(tokenName) ?? [];
|
||||
rows.push({
|
||||
name: String(row.model || "(unknown)"),
|
||||
calls: Number(row.calls),
|
||||
total_tokens: Number(row.tokens) + Number(row.cache_creation) + Number(row.cache_read),
|
||||
quota: Number(row.quota),
|
||||
});
|
||||
modelsByToken.set(tokenName, rows);
|
||||
}
|
||||
|
||||
const displayNames = await getDisplayNames();
|
||||
const userRow = await query(
|
||||
"SELECT id FROM users WHERE username = $1 LIMIT 1",
|
||||
[username]
|
||||
);
|
||||
const displayName =
|
||||
userRow.length > 0 ? displayNames[userRow[0].id] || username : username;
|
||||
|
||||
const o = overview[0];
|
||||
return {
|
||||
display_name: displayName,
|
||||
calls: Number(o.calls),
|
||||
prompt_tokens: Number(o.prompt),
|
||||
completion_tokens: Number(o.completion),
|
||||
cache_creation_tokens: Number(o.cache_creation),
|
||||
cache_read_tokens: Number(o.cache_read),
|
||||
total_tokens: Number(o.prompt) + Number(o.completion) + Number(o.cache_creation) + Number(o.cache_read),
|
||||
quota: Number(o.quota),
|
||||
models: models.map((m) => ({
|
||||
name: m.model,
|
||||
calls: Number(m.calls),
|
||||
total_tokens: Number(m.tokens) + Number(m.cache_creation) + Number(m.cache_read),
|
||||
quota: Number(m.quota),
|
||||
})),
|
||||
tokens: tokens.map((token): TokenDetailBreakdown => {
|
||||
const name = String(token.token_name ?? "");
|
||||
return {
|
||||
name,
|
||||
calls: Number(token.calls),
|
||||
total_tokens: Number(token.tokens) + Number(token.cache_creation) + Number(token.cache_read),
|
||||
quota: Number(token.quota),
|
||||
models: modelsByToken.get(name) ?? [],
|
||||
};
|
||||
}),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function getModelDetail(
|
||||
model: string,
|
||||
startTs?: number,
|
||||
endTs?: number
|
||||
) {
|
||||
return cached(cacheKey("modelDetail", model, startTs, endTs), async () => {
|
||||
const params: (string | number | boolean | null)[] = [];
|
||||
const where = timeWhere(params, startTs, endTs);
|
||||
params.push(model);
|
||||
|
||||
const overview = await query(
|
||||
`SELECT COUNT(*)::int as calls,
|
||||
COALESCE(SUM(prompt_tokens),0)::bigint as prompt,
|
||||
COALESCE(SUM(completion_tokens),0)::bigint as completion,
|
||||
COALESCE(SUM(${CACHE_CREATION}),0)::bigint as cache_creation,
|
||||
COALESCE(SUM(${CACHE_READ}),0)::bigint as cache_read,
|
||||
COALESCE(SUM(quota),0)::bigint as quota
|
||||
FROM logs WHERE ${where} AND ${REAL_MODEL} = $${params.length}`,
|
||||
params
|
||||
);
|
||||
|
||||
const params2: (string | number | boolean | null)[] = [];
|
||||
const where2 = timeWhere(params2, startTs, endTs);
|
||||
params2.push(model);
|
||||
|
||||
const displayNames = await getDisplayNames();
|
||||
const users = await query(
|
||||
`SELECT user_id, username,
|
||||
COUNT(*)::int as calls,
|
||||
COALESCE(SUM(prompt_tokens + completion_tokens),0)::bigint as tokens,
|
||||
COALESCE(SUM(${CACHE_CREATION}),0)::bigint as cache_creation,
|
||||
COALESCE(SUM(${CACHE_READ}),0)::bigint as cache_read,
|
||||
COALESCE(SUM(quota),0)::bigint as quota
|
||||
FROM logs WHERE ${where2} AND ${REAL_MODEL} = $${params2.length}
|
||||
GROUP BY user_id, username
|
||||
ORDER BY tokens DESC LIMIT 20`,
|
||||
params2
|
||||
);
|
||||
|
||||
const o = overview[0];
|
||||
return {
|
||||
calls: Number(o.calls),
|
||||
prompt_tokens: Number(o.prompt),
|
||||
completion_tokens: Number(o.completion),
|
||||
cache_creation_tokens: Number(o.cache_creation),
|
||||
cache_read_tokens: Number(o.cache_read),
|
||||
total_tokens: Number(o.prompt) + Number(o.completion) + Number(o.cache_creation) + Number(o.cache_read),
|
||||
quota: Number(o.quota),
|
||||
users: users.map((u) => ({
|
||||
name: displayNames[u.user_id] || u.username,
|
||||
calls: Number(u.calls),
|
||||
total_tokens: Number(u.tokens) + Number(u.cache_creation) + Number(u.cache_read),
|
||||
quota: Number(u.quota),
|
||||
})),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function getChannelDetail(
|
||||
channelId: number,
|
||||
startTs?: number,
|
||||
endTs?: number
|
||||
) {
|
||||
return cached(cacheKey("channelDetail", channelId, startTs, endTs), async () => {
|
||||
const params: (string | number | boolean | null)[] = [];
|
||||
const where = timeWhere(params, startTs, endTs);
|
||||
params.push(channelId);
|
||||
|
||||
const channelNames = await getChannelNames();
|
||||
|
||||
const overview = await query(
|
||||
`SELECT COUNT(*)::int as calls,
|
||||
COALESCE(SUM(prompt_tokens),0)::bigint as prompt,
|
||||
COALESCE(SUM(completion_tokens),0)::bigint as completion,
|
||||
COALESCE(SUM(${CACHE_CREATION}),0)::bigint as cache_creation,
|
||||
COALESCE(SUM(${CACHE_READ}),0)::bigint as cache_read,
|
||||
COALESCE(SUM(quota),0)::bigint as quota
|
||||
FROM logs WHERE ${where} AND channel_id = $${params.length}`,
|
||||
params
|
||||
);
|
||||
|
||||
const params2: (string | number | boolean | null)[] = [];
|
||||
const where2 = timeWhere(params2, startTs, endTs);
|
||||
params2.push(channelId);
|
||||
|
||||
const models = await query(
|
||||
`SELECT ${REAL_MODEL} as model,
|
||||
COUNT(*)::int as calls,
|
||||
COALESCE(SUM(prompt_tokens + completion_tokens),0)::bigint as tokens,
|
||||
COALESCE(SUM(${CACHE_CREATION}),0)::bigint as cache_creation,
|
||||
COALESCE(SUM(${CACHE_READ}),0)::bigint as cache_read,
|
||||
COALESCE(SUM(quota),0)::bigint as quota
|
||||
FROM logs WHERE ${where2} AND channel_id = $${params2.length}
|
||||
GROUP BY model
|
||||
ORDER BY tokens DESC LIMIT 20`,
|
||||
params2
|
||||
);
|
||||
|
||||
const o = overview[0];
|
||||
return {
|
||||
channel_name: channelNames[channelId] || `已删除(${channelId})`,
|
||||
calls: Number(o.calls),
|
||||
prompt_tokens: Number(o.prompt),
|
||||
completion_tokens: Number(o.completion),
|
||||
cache_creation_tokens: Number(o.cache_creation),
|
||||
cache_read_tokens: Number(o.cache_read),
|
||||
total_tokens: Number(o.prompt) + Number(o.completion) + Number(o.cache_creation) + Number(o.cache_read),
|
||||
quota: Number(o.quota),
|
||||
models: models.map((m) => ({
|
||||
name: m.model,
|
||||
calls: Number(m.calls),
|
||||
total_tokens: Number(m.tokens) + Number(m.cache_creation) + Number(m.cache_read),
|
||||
quota: Number(m.quota),
|
||||
})),
|
||||
};
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user