Skip to content

Commit

Permalink
BED-4947: Fix Last Analysis Run At Bug (#907)
Browse files Browse the repository at this point in the history
* fix: logic bug causing last analysis run at to update every time we changed status

* fix: test was reliant on bug, fixed test to be in line with reality
  • Loading branch information
superlinkx authored Oct 17, 2024
1 parent 43264b2 commit b22b6bb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions cmd/api/src/database/datapipestatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,22 @@ import (
)

func (s *BloodhoundDB) SetDatapipeStatus(ctx context.Context, status model.DatapipeStatus, updateAnalysisTime bool) error {

updateSql := "UPDATE datapipe_status SET status = ?, updated_at = ?, last_analysis_run_at = ?"
now := time.Now().UTC()
// All queries will update the status and table update time
updateSql := "UPDATE datapipe_status SET status = ?, updated_at = ?"

if updateAnalysisTime {
if status == model.DatapipeStatusAnalyzing {
// Updates last run anytime we start analysis
updateSql += ", last_analysis_run_at = ?;"
return s.db.WithContext(ctx).Exec(updateSql, status, now, now).Error
} else if updateAnalysisTime {
// Updates last completed when analysis is set to complete
updateSql += ", last_complete_analysis_at = ?;"
return s.db.WithContext(ctx).Exec(updateSql, status, now, now, now).Error
return s.db.WithContext(ctx).Exec(updateSql, status, now, now).Error
} else {
// Otherwise, only update status and last update to the table
updateSql += ";"
return s.db.WithContext(ctx).Exec(updateSql, status, now, now).Error
return s.db.WithContext(ctx).Exec(updateSql, status, now).Error
}

}
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/src/database/datapipestatus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestDatapipeStatus(t *testing.T) {

// when `SetDatapipeStatus` is called with `true` for the `updateAnalysisTime` parameter, assert that the time is no longer null
require.True(t, status.LastCompleteAnalysisAt.IsZero())
err = db.SetDatapipeStatus(testCtx, model.DatapipeStatusAnalyzing, true)
err = db.SetDatapipeStatus(testCtx, model.DatapipeStatusIdle, true)
require.Nil(t, err)
status, err = db.GetDatapipeStatus(testCtx)
require.Nil(t, err)
Expand Down

0 comments on commit b22b6bb

Please sign in to comment.