-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 增加 token 用量统计接口
- Loading branch information
Showing
5 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
migrations/supabase/migrations/20240912023152_remote_schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
set check_function_bodies = off; | ||
|
||
CREATE OR REPLACE FUNCTION public.get_user_stats(filter_user_id text, start_date date, end_date date) | ||
RETURNS TABLE(usage_date date, input_tokens bigint, output_tokens bigint, total_tokens bigint) | ||
LANGUAGE plpgsql | ||
AS $function$ | ||
BEGIN | ||
RETURN QUERY | ||
SELECT | ||
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.user_id = filter_user_id -- 使用别名来引用参数 | ||
AND u.date >= start_date | ||
AND u.date <= end_date | ||
GROUP BY u.date; | ||
END; | ||
$function$ | ||
; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters