Skip to content

Commit d0331af

Browse files
committed
before input data
1 parent 0afbd56 commit d0331af

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

coderd/notifications/reports/generator.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package reports
33
import (
44
"context"
55
"database/sql"
6+
"fmt"
67
"io"
78
"slices"
89
"sort"
@@ -12,9 +13,10 @@ import (
1213

1314
"cdr.dev/slog"
1415

15-
"github.com/coder/quartz"
1616
"github.com/google/uuid"
1717

18+
"github.com/coder/quartz"
19+
1820
"github.com/coder/coder/v2/coderd/database"
1921
"github.com/coder/coder/v2/coderd/database/dbauthz"
2022
"github.com/coder/coder/v2/coderd/database/dbtime"
@@ -114,12 +116,12 @@ func reportFailedWorkspaceBuilds(ctx context.Context, logger slog.Logger, db dat
114116
Since: dbtime.Time(clk.Now()).UTC(),
115117
})
116118
if err != nil {
117-
logger.Error(ctx, "unable to fetch failed workspace builds", slog.F("template_id", template.ID), slog.Error(err))
119+
logger.Error(ctx, "unable to fetch failed workspace builds", slog.F("template_id", stats.TemplateID), slog.Error(err))
118120
continue
119121
}
120122

121-
// TODO Lazy-render the report.
122-
reportData = map[string]any{}
123+
// There are some failed builds, so we have to prepare input data for the report.
124+
reportData = buildDataForReportFailedWorkspaceBuilds(frequencyDays, stats, failedBuilds)
123125
}
124126

125127
templateAdmins, err := findTemplateAdmins(ctx, db, stats)
@@ -129,8 +131,6 @@ func reportFailedWorkspaceBuilds(ctx context.Context, logger slog.Logger, db dat
129131
}
130132

131133
for _, templateAdmin := range templateAdmins {
132-
// TODO Check if report is enabled for the person.
133-
134134
reportLog, err := db.GetReportGeneratorLogByUserAndTemplate(ctx, database.GetReportGeneratorLogByUserAndTemplateParams{
135135
UserID: templateAdmin.ID,
136136
NotificationTemplateID: notifications.TemplateWorkspaceBuildsFailedReport,
@@ -201,10 +201,27 @@ func reportFailedWorkspaceBuilds(ctx context.Context, logger slog.Logger, db dat
201201
return nil
202202
}
203203

204-
func buildDataForReportFailedWorkspaceBuilds() map[string]any {
205-
// TODO Lazy-render the report.
206-
reportData := map[string]any{}
204+
func buildDataForReportFailedWorkspaceBuilds(frequencyDays int, stats database.GetWorkspaceBuildStatsByTemplatesRow, failedBuilds []database.WorkspaceBuild) map[string]any {
205+
// Format frequency label
206+
var frequencyLabel string
207+
if frequencyDays == 7 {
208+
frequencyLabel = "week"
209+
} else {
210+
var plural string
211+
if frequencyDays > 1 {
212+
plural = "s"
213+
}
214+
frequencyLabel = fmt.Sprintf("%d day%s", frequencyDays, plural)
215+
}
207216

217+
reportData := map[string]any{
218+
"failed_builds": stats.FailedBuilds,
219+
"total_builds": stats.TotalBuilds,
220+
"report_frequency": frequencyLabel,
221+
"template_version": map[string]any{
222+
// TODO
223+
},
224+
}
208225
return reportData
209226
}
210227

0 commit comments

Comments
 (0)