Files
crush/internal/db/sql/stats.sql
0eee061660 feat: crush stats (#1920)
* wip: stats

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* wip

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* wip

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* fixup! wip

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* fix: css

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* fix: cleanup

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* wip

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* wip

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* logo

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* fix: cast

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* wip

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* wip

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* wip

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* wip

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* wip

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* wip

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* cleanup

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* improvements

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* provider donut

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* fix: improvements

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* wip

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* wip

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* wip

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* wip

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* jetbrains mono

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* fixes

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* fix: rm border

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* chore: update footer/header

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* fix: footer class

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* refactor: move stuff around

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* refactor: improving

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* fix: anims

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* fix: rename vars

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* chore: remove all card borders

* chore: adjust easing

* fix: fail if no sessions

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* fix: improvements

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* fix: generated by

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

* fix: header hazy

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
Co-authored-by: Christian Rocha <christian@rocha.is>
2026-01-26 12:01:49 -03:00

94 lines
2.7 KiB
SQL

-- name: GetUsageByDay :many
SELECT
date(created_at, 'unixepoch') as day,
SUM(prompt_tokens) as prompt_tokens,
SUM(completion_tokens) as completion_tokens,
SUM(cost) as cost,
COUNT(*) as session_count
FROM sessions
WHERE parent_session_id IS NULL
GROUP BY date(created_at, 'unixepoch')
ORDER BY day DESC;
-- name: GetUsageByModel :many
SELECT
COALESCE(model, 'unknown') as model,
COALESCE(provider, 'unknown') as provider,
COUNT(*) as message_count
FROM messages
WHERE role = 'assistant'
GROUP BY model, provider
ORDER BY message_count DESC;
-- name: GetUsageByHour :many
SELECT
CAST(strftime('%H', created_at, 'unixepoch') AS INTEGER) as hour,
COUNT(*) as session_count
FROM sessions
WHERE parent_session_id IS NULL
GROUP BY hour
ORDER BY hour;
-- name: GetUsageByDayOfWeek :many
SELECT
CAST(strftime('%w', created_at, 'unixepoch') AS INTEGER) as day_of_week,
COUNT(*) as session_count,
SUM(prompt_tokens) as prompt_tokens,
SUM(completion_tokens) as completion_tokens
FROM sessions
WHERE parent_session_id IS NULL
GROUP BY day_of_week
ORDER BY day_of_week;
-- name: GetTotalStats :one
SELECT
COUNT(*) as total_sessions,
COALESCE(SUM(prompt_tokens), 0) as total_prompt_tokens,
COALESCE(SUM(completion_tokens), 0) as total_completion_tokens,
COALESCE(SUM(cost), 0) as total_cost,
COALESCE(SUM(message_count), 0) as total_messages,
COALESCE(AVG(prompt_tokens + completion_tokens), 0) as avg_tokens_per_session,
COALESCE(AVG(message_count), 0) as avg_messages_per_session
FROM sessions
WHERE parent_session_id IS NULL;
-- name: GetRecentActivity :many
SELECT
date(created_at, 'unixepoch') as day,
COUNT(*) as session_count,
SUM(prompt_tokens + completion_tokens) as total_tokens,
SUM(cost) as cost
FROM sessions
WHERE parent_session_id IS NULL
AND created_at >= strftime('%s', 'now', '-30 days')
GROUP BY date(created_at, 'unixepoch')
ORDER BY day ASC;
-- name: GetAverageResponseTime :one
SELECT
CAST(COALESCE(AVG(finished_at - created_at), 0) AS INTEGER) as avg_response_seconds
FROM messages
WHERE role = 'assistant'
AND finished_at IS NOT NULL
AND finished_at > created_at;
-- name: GetToolUsage :many
SELECT
json_extract(value, '$.data.name') as tool_name,
COUNT(*) as call_count
FROM messages, json_each(parts)
WHERE json_extract(value, '$.type') = 'tool_call'
AND json_extract(value, '$.data.name') IS NOT NULL
GROUP BY tool_name
ORDER BY call_count DESC;
-- name: GetHourDayHeatmap :many
SELECT
CAST(strftime('%w', created_at, 'unixepoch') AS INTEGER) as day_of_week,
CAST(strftime('%H', created_at, 'unixepoch') AS INTEGER) as hour,
COUNT(*) as session_count
FROM sessions
WHERE parent_session_id IS NULL
GROUP BY day_of_week, hour
ORDER BY day_of_week, hour;