Skip to content

Commit

Permalink
feat: add bot token usage analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
RaoHai committed Dec 24, 2024
1 parent 045d608 commit 9d9a4fe
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions migrations/supabase/migrations/20241224030938_remote_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
set check_function_bodies = off;

CREATE OR REPLACE FUNCTION public.analyze_user_token_usage(start_date date, end_date date)
RETURNS TABLE(bot_id text, usage_date date, input_tokens bigint, output_tokens bigint, total_tokens bigint)
LANGUAGE plpgsql
AS $function$
BEGIN
RETURN QUERY
SELECT
u.bot_id AS bot_id,
u.date AS usage_date, -- 使用别名来避免歧义
SUM(u.input_token)::BIGINT AS input_tokens, -- 将结果转换为 BIGINT
SUM(u.output_token)::BIGINT AS output_tokens, -- 将结果转换为 BIGINT
SUM(u.total_token)::BIGINT AS total_tokens -- 将结果转换为 BIGINT
FROM user_token_usage u
WHERE
u.date >= start_date AND
u.date <= end_date
GROUP BY u.date, u.bot_id;
END;
$function$
;


0 comments on commit 9d9a4fe

Please sign in to comment.