Skip to content

Commit 6813b73

Browse files
committed
fix dbmem query logic replication
1 parent f3bc067 commit 6813b73

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

coderd/database/dbmem/dbmem.go

+15-13
Original file line numberDiff line numberDiff line change
@@ -3058,7 +3058,7 @@ func (q *FakeQuerier) GetTemplateAppInsights(ctx context.Context, arg database.G
30583058
// FROM
30593059
for _, stat := range q.templateUsageStats {
30603060
// WHERE
3061-
if stat.StartTime.Before(arg.StartTime) || stat.StartTime.After(arg.EndTime) {
3061+
if stat.StartTime.Before(arg.StartTime) || stat.EndTime.After(arg.EndTime) {
30623062
continue
30633063
}
30643064
if len(arg.TemplateIDs) > 0 && !slices.Contains(arg.TemplateIDs, stat.TemplateID) {
@@ -3118,20 +3118,20 @@ func (q *FakeQuerier) GetTemplateAppInsights(ctx context.Context, arg database.G
31183118
}
31193119

31203120
templateRows := make(map[appGroupBy]templateRow)
3121-
for _, row := range appInsightRows {
3121+
for _, aiRow := range appInsightRows {
31223122
key := appGroupBy{
3123-
AppName: row.AppName,
3124-
DisplayName: row.DisplayName,
3125-
Icon: row.Icon,
3126-
IsApp: row.IsApp,
3123+
AppName: aiRow.AppName,
3124+
DisplayName: aiRow.DisplayName,
3125+
Icon: aiRow.Icon,
3126+
IsApp: aiRow.IsApp,
31273127
}
31283128
row, ok := templateRows[key]
31293129
if !ok {
31303130
row = templateRow{
31313131
appGroupBy: key,
31323132
}
31333133
}
3134-
row.TemplateIDs = append(row.TemplateIDs, row.TemplateIDs...)
3134+
row.TemplateIDs = uniqueSortedUUIDs(append(row.TemplateIDs, aiRow.TemplateIDs...))
31353135
templateRows[key] = row
31363136
}
31373137

@@ -3171,18 +3171,20 @@ func (q *FakeQuerier) GetTemplateAppInsights(ctx context.Context, arg database.G
31713171
IsApp: aiRow.IsApp,
31723172
}
31733173
row := groupedRows[key]
3174-
row.TemplateIDs = append(row.TemplateIDs, aiRow.TemplateIDs...)
31753174
row.ActiveUserIDs = append(row.ActiveUserIDs, aiRow.UserID)
31763175
row.UsageSeconds += aiRow.AppUsageMins * 60
3176+
groupedRows[key] = row
31773177
}
31783178

31793179
var rows []database.GetTemplateAppInsightsRow
3180-
for k, gr := range groupedRows {
3180+
for key, gr := range groupedRows {
31813181
rows = append(rows, database.GetTemplateAppInsightsRow{
3182-
TemplateIDs: uniqueSortedUUIDs(gr.TemplateIDs),
3182+
TemplateIDs: templateRows[key].TemplateIDs,
31833183
ActiveUsers: int64(len(uniqueSortedUUIDs(gr.ActiveUserIDs))),
3184-
DisplayName: k.DisplayName,
3185-
SlugOrPort: k.AppName,
3184+
SlugOrPort: key.AppName,
3185+
DisplayName: key.DisplayName,
3186+
Icon: key.Icon,
3187+
IsApp: key.IsApp,
31863188
UsageSeconds: gr.UsageSeconds,
31873189
})
31883190
}
@@ -8354,7 +8356,7 @@ func (q *FakeQuerier) UpsertTemplateUsageStats(ctx context.Context) error {
83548356
return err
83558357
}
83568358
// CROSS JOIN generate_series
8357-
for t := was.SessionStartedAt; t.Before(was.SessionEndedAt); t = t.Add(time.Minute) {
8359+
for t := was.SessionStartedAt.Truncate(time.Minute); t.Before(was.SessionEndedAt); t = t.Add(time.Minute) {
83588360
// WHERE
83598361
if t.Before(latestStart) || t.After(now) || t.Equal(now) {
83608362
continue

0 commit comments

Comments
 (0)