Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

perf: optimize GetTemplateAppInsightsByTemplate by pre-filtering apps based on start/end times#20669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
cstyan wants to merge2 commits intomain
base:main
Choose a base branch
Loading
fromcallum/app-insights-optimize
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletionscoderd/database/queries.sql.go
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

35 changes: 25 additions & 10 deletionscoderd/database/queries/insights.sql
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -350,52 +350,67 @@ GROUP BY
-- GetTemplateAppInsightsByTemplate is used for Prometheus metrics. Keep
-- in sync with GetTemplateAppInsights and UpsertTemplateUsageStats.
WITH
filtered_stats AS (
SELECT
was.workspace_id,
was.user_id,
was.agent_id,
was.access_method,
was.slug_or_port,
was.session_started_at,
was.session_ended_at
FROM
workspace_app_stats AS was
WHERE
was.session_ended_at >= @start_time::timestamptz
AND was.session_started_at < @end_time::timestamptz
),
-- This CTE is used to explode app usage into minute buckets, then
-- flatten the users app usage within the template so that usage in
-- multiple workspaces under one template is only counted once for
-- every minute.
app_insights AS (
SELECT
w.template_id,
was.user_id,
fs.user_id,
-- Both app stats and agent stats track web terminal usage, but
-- by different means. The app stats value should be more
-- accurate so we don't want to discard it just yet.
CASE
WHENwas.access_method = 'terminal'
WHENfs.access_method = 'terminal'
THEN '[terminal]' -- Unique name, app names can't contain brackets.
ELSEwas.slug_or_port
ELSEfs.slug_or_port
END::text AS app_name,
COALESCE(wa.display_name, '') AS display_name,
(wa.slug IS NOT NULL)::boolean AS is_app,
COUNT(DISTINCT s.minute_bucket) AS app_minutes
FROM
workspace_app_stats ASwas
filtered_stats ASfs
JOIN
workspaces AS w
ON
w.id =was.workspace_id
w.id =fs.workspace_id
-- We do a left join here because we want to include user IDs that have used
-- e.g. ports when counting active users.
LEFT JOIN
workspace_apps wa
ON
wa.agent_id =was.agent_id
AND wa.slug =was.slug_or_port
wa.agent_id =fs.agent_id
AND wa.slug =fs.slug_or_port
-- Generate a series of minute buckets for each session for computing the
-- mintes/bucket.
CROSS JOIN
generate_series(
date_trunc('minute',was.session_started_at),
date_trunc('minute',fs.session_started_at),
-- Subtract 1 μs to avoid creating an extra series.
date_trunc('minute',was.session_ended_at - '1 microsecond'::interval),
date_trunc('minute',fs.session_ended_at - '1 microsecond'::interval),
'1 minute'::interval
) AS s(minute_bucket)
WHERE
s.minute_bucket >= @start_time::timestamptz
AND s.minute_bucket < @end_time::timestamptz
GROUP BY
w.template_id,was.user_id,was.access_method,was.slug_or_port, wa.display_name, wa.slug
w.template_id,fs.user_id,fs.access_method,fs.slug_or_port, wa.display_name, wa.slug
)

SELECT
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp