Skip to content

feat(coderd/database): use template_usage_stats in GetTemplateAppInsights query #12669

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 25, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
fix dbmem query logic replication
  • Loading branch information
mafredri committed Mar 25, 2024
commit 6813b738659345f25a2c45b7b0ad71a7945b81cc
28 changes: 15 additions & 13 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -3058,7 +3058,7 @@ func (q *FakeQuerier) GetTemplateAppInsights(ctx context.Context, arg database.G
// FROM
for _, stat := range q.templateUsageStats {
// WHERE
if stat.StartTime.Before(arg.StartTime) || stat.StartTime.After(arg.EndTime) {
if stat.StartTime.Before(arg.StartTime) || stat.EndTime.After(arg.EndTime) {
continue
}
if len(arg.TemplateIDs) > 0 && !slices.Contains(arg.TemplateIDs, stat.TemplateID) {
Expand Down Expand Up @@ -3118,20 +3118,20 @@ func (q *FakeQuerier) GetTemplateAppInsights(ctx context.Context, arg database.G
}

templateRows := make(map[appGroupBy]templateRow)
for _, row := range appInsightRows {
for _, aiRow := range appInsightRows {
key := appGroupBy{
AppName: row.AppName,
DisplayName: row.DisplayName,
Icon: row.Icon,
IsApp: row.IsApp,
AppName: aiRow.AppName,
DisplayName: aiRow.DisplayName,
Icon: aiRow.Icon,
IsApp: aiRow.IsApp,
}
row, ok := templateRows[key]
if !ok {
row = templateRow{
appGroupBy: key,
}
}
row.TemplateIDs = append(row.TemplateIDs, row.TemplateIDs...)
row.TemplateIDs = uniqueSortedUUIDs(append(row.TemplateIDs, aiRow.TemplateIDs...))
templateRows[key] = row
}

Expand Down Expand Up @@ -3171,18 +3171,20 @@ func (q *FakeQuerier) GetTemplateAppInsights(ctx context.Context, arg database.G
IsApp: aiRow.IsApp,
}
row := groupedRows[key]
row.TemplateIDs = append(row.TemplateIDs, aiRow.TemplateIDs...)
row.ActiveUserIDs = append(row.ActiveUserIDs, aiRow.UserID)
row.UsageSeconds += aiRow.AppUsageMins * 60
groupedRows[key] = row
}

var rows []database.GetTemplateAppInsightsRow
for k, gr := range groupedRows {
for key, gr := range groupedRows {
rows = append(rows, database.GetTemplateAppInsightsRow{
TemplateIDs: uniqueSortedUUIDs(gr.TemplateIDs),
TemplateIDs: templateRows[key].TemplateIDs,
ActiveUsers: int64(len(uniqueSortedUUIDs(gr.ActiveUserIDs))),
DisplayName: k.DisplayName,
SlugOrPort: k.AppName,
SlugOrPort: key.AppName,
DisplayName: key.DisplayName,
Icon: key.Icon,
IsApp: key.IsApp,
UsageSeconds: gr.UsageSeconds,
})
}
Expand Down Expand Up @@ -8354,7 +8356,7 @@ func (q *FakeQuerier) UpsertTemplateUsageStats(ctx context.Context) error {
return err
}
// CROSS JOIN generate_series
for t := was.SessionStartedAt; t.Before(was.SessionEndedAt); t = t.Add(time.Minute) {
for t := was.SessionStartedAt.Truncate(time.Minute); t.Before(was.SessionEndedAt); t = t.Add(time.Minute) {
// WHERE
if t.Before(latestStart) || t.After(now) || t.Equal(now) {
continue
Expand Down