26 lines
826 B
TypeScript
26 lines
826 B
TypeScript
|
|
import { query } from "./db";
|
|
|
|
export type QuotaState = {
|
|
keyDaily: number;
|
|
keyMonthly: number;
|
|
globalDaily: number;
|
|
};
|
|
|
|
export async function quotaState(keyId: string): Promise<QuotaState> {
|
|
const { rows } = await query<{ key_daily: string; key_monthly: string; global_daily: string }>(
|
|
`select
|
|
count(*) filter (where api_key_id = $1 and created_at >= date_trunc('day', now())) as key_daily,
|
|
count(*) filter (where api_key_id = $1 and created_at >= date_trunc('month', now())) as key_monthly,
|
|
count(*) filter (where created_at >= date_trunc('day', now())) as global_daily
|
|
from messages`,
|
|
[keyId],
|
|
);
|
|
const row = rows[0];
|
|
return {
|
|
keyDaily: Number(row?.key_daily ?? 0),
|
|
keyMonthly: Number(row?.key_monthly ?? 0),
|
|
globalDaily: Number(row?.global_daily ?? 0),
|
|
};
|
|
}
|