Skip to content

Commit 4970782

Browse files
feat(drift-analysis): add total projects errored to analysis run
1 parent 0f9642a commit 4970782

9 files changed

Lines changed: 39 additions & 8 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE drift_analysis_run
2+
ADD COLUMN total_projects_errored INT NOT NULL DEFAULT 0;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- Backfill total_projects_errored from existing project data
2+
UPDATE drift_analysis_run dar
3+
SET total_projects_errored = (SELECT COUNT(*)
4+
FROM drift_analysis_project dap
5+
WHERE dap.drift_analysis_run_id = dar.uuid
6+
AND dap.succeeded = false);

pkg/model/dto/analysis_run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type DriftAnalysisRunDTO struct {
77
RepositoryId int64 `json:"repository_id"`
88
TotalProjects int32 `json:"total_projects"`
99
TotalProjectsDrifted int32 `json:"total_projects_drifted"`
10+
TotalProjectsErrored int32 `json:"total_projects_errored"`
1011
DurationMillis int64 `json:"duration_millis"`
1112
CreatedAt time.Time `json:"created_at"`
1213
UpdatedAt time.Time `json:"updated_at"`

pkg/repository/queries/drift_analysis.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- name: CreateDriftAnalysisRun :one
2-
INSERT INTO drift_analysis_run (uuid, repository_id, total_projects, total_projects_drifted, analysis_duration_millis)
3-
VALUES (@uuid, @repository_id, @total_projects, @total_projects_drifted, @analysis_duration_millis)
2+
INSERT INTO drift_analysis_run (uuid, repository_id, total_projects, total_projects_drifted, total_projects_errored, analysis_duration_millis)
3+
VALUES (@uuid, @repository_id, @total_projects, @total_projects_drifted, @total_projects_errored, @analysis_duration_millis)
44
RETURNING *;
55

66
-- name: CreateDriftAnalysisProject :one

pkg/repository/queries/drift_analysis.sql.go

Lines changed: 12 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/repository/queries/models.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/usecase/drift_stream/api.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,26 @@ func (d *DriftStateHandler) HandleUpdate(c *fiber.Ctx) error {
9393

9494
log.Debugf("Received drift state update: %v", state)
9595

96+
// Use sent value or calculate errored count from project results as fallback
97+
var totalErrored int32
98+
if state.TotalErrored != nil {
99+
totalErrored = *state.TotalErrored
100+
} else {
101+
for _, project := range state.ProjectResults {
102+
if !project.Succeeded {
103+
totalErrored++
104+
}
105+
}
106+
}
107+
96108
var runUUID uuid.UUID
97109
err = d.driftAnalysisRepository.WithTx(c.Context(), func(ctx context.Context) error {
98110
params := queries.CreateDriftAnalysisRunParams{
99111
Uuid: uuid.New(),
100112
RepositoryID: repo.ID,
101113
TotalProjects: state.TotalProjects,
102114
TotalProjectsDrifted: state.TotalDrifted,
115+
TotalProjectsErrored: totalErrored,
103116
AnalysisDurationMillis: state.Duration.Milliseconds(),
104117
}
105118

pkg/usecase/drift_stream/model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type DriftProjectResult struct {
4646
type DriftDetectionResult struct {
4747
ProjectResults []DriftProjectResult `json:"project_results"`
4848
TotalDrifted int32 `json:"total_drifted"`
49+
TotalErrored *int32 `json:"total_errored,omitempty"`
4950
TotalProjects int32 `json:"total_projects"`
5051
TotalChecked int32 `json:"total_checked"`
5152
Duration time.Duration `json:"duration"`

pkg/usecase/utils/parsing/drift_analysis_run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ func ToDriftAnalysisRunDTO(run queries.DriftAnalysisRun) dto.DriftAnalysisRunDTO
1111
RepositoryId: run.RepositoryID,
1212
TotalProjects: run.TotalProjects,
1313
TotalProjectsDrifted: run.TotalProjectsDrifted,
14+
TotalProjectsErrored: run.TotalProjectsErrored,
1415
DurationMillis: run.AnalysisDurationMillis,
1516
CreatedAt: run.CreatedAt,
1617
UpdatedAt: run.UpdatedAt,

0 commit comments

Comments
 (0)