@@ -3,6 +3,7 @@ package reports
3
3
import (
4
4
"context"
5
5
"database/sql"
6
+ "fmt"
6
7
"io"
7
8
"slices"
8
9
"sort"
@@ -12,9 +13,10 @@ import (
12
13
13
14
"cdr.dev/slog"
14
15
15
- "github.com/coder/quartz"
16
16
"github.com/google/uuid"
17
17
18
+ "github.com/coder/quartz"
19
+
18
20
"github.com/coder/coder/v2/coderd/database"
19
21
"github.com/coder/coder/v2/coderd/database/dbauthz"
20
22
"github.com/coder/coder/v2/coderd/database/dbtime"
@@ -114,12 +116,12 @@ func reportFailedWorkspaceBuilds(ctx context.Context, logger slog.Logger, db dat
114
116
Since : dbtime .Time (clk .Now ()).UTC (),
115
117
})
116
118
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 ))
118
120
continue
119
121
}
120
122
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 )
123
125
}
124
126
125
127
templateAdmins , err := findTemplateAdmins (ctx , db , stats )
@@ -129,8 +131,6 @@ func reportFailedWorkspaceBuilds(ctx context.Context, logger slog.Logger, db dat
129
131
}
130
132
131
133
for _ , templateAdmin := range templateAdmins {
132
- // TODO Check if report is enabled for the person.
133
-
134
134
reportLog , err := db .GetReportGeneratorLogByUserAndTemplate (ctx , database.GetReportGeneratorLogByUserAndTemplateParams {
135
135
UserID : templateAdmin .ID ,
136
136
NotificationTemplateID : notifications .TemplateWorkspaceBuildsFailedReport ,
@@ -201,10 +201,27 @@ func reportFailedWorkspaceBuilds(ctx context.Context, logger slog.Logger, db dat
201
201
return nil
202
202
}
203
203
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
+ }
207
216
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
+ }
208
225
return reportData
209
226
}
210
227
0 commit comments