Skip to content

Commit

Permalink
fix job spec grafana counts (#10913)
Browse files Browse the repository at this point in the history
  • Loading branch information
eutopian authored Oct 13, 2023
1 parent 96f89be commit 5132532
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 68 deletions.
4 changes: 3 additions & 1 deletion core/services/feeds/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,12 @@ type JobProposalCounts struct {
// toMetrics transforms JobProposalCounts into a map with float64 values for setting metrics
// in prometheus.
func (jpc *JobProposalCounts) toMetrics() map[JobProposalStatus]float64 {
metrics := make(map[JobProposalStatus]float64, 4)
metrics := make(map[JobProposalStatus]float64, 6)
metrics[JobProposalStatusPending] = float64(jpc.Pending)
metrics[JobProposalStatusApproved] = float64(jpc.Approved)
metrics[JobProposalStatusCancelled] = float64(jpc.Cancelled)
metrics[JobProposalStatusRejected] = float64(jpc.Rejected)
metrics[JobProposalStatusRevoked] = float64(jpc.Revoked)
metrics[JobProposalStatusDeleted] = float64(jpc.Deleted)
return metrics
}
25 changes: 25 additions & 0 deletions core/services/feeds/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,28 @@ func Test_JobProposal_CanEditDefinition(t *testing.T) {
})
}
}

// Test_toMetrics tests the toMetrics method
func Test_toMetrics(t *testing.T) {
t.Parallel()

jpCounts := JobProposalCounts{
Cancelled: 0,
Pending: 1,
Approved: 2,
Rejected: 3,
Deleted: 4,
Revoked: 5,
}

metrics := jpCounts.toMetrics()

assert.Equal(t, metrics, map[JobProposalStatus]float64{
JobProposalStatusCancelled: 0,
JobProposalStatusPending: 1,
JobProposalStatusApproved: 2,
JobProposalStatusRejected: 3,
JobProposalStatusDeleted: 4,
JobProposalStatusRevoked: 5,
})
}
34 changes: 25 additions & 9 deletions core/services/feeds/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,23 +561,39 @@ SELECT exists (
return exists, errors.Wrap(err, "JobProposalSpecVersionExists failed")
}

// DeleteProposal performs a soft delete of the job proposal by setting the status to deleted and
// update the status to deleted
// DeleteProposal performs a soft delete of the job proposal by setting the status to deleted
func (o *orm) DeleteProposal(id int64, qopts ...pg.QOpt) error {
// Get the latest spec for the proposal.
stmt := `
SELECT id, definition, version, status, job_proposal_id, status_updated_at, created_at, updated_at
FROM job_proposal_specs
WHERE (job_proposal_id, version) IN
(
SELECT job_proposal_id, MAX(version)
FROM job_proposal_specs
GROUP BY job_proposal_id
)
AND job_proposal_id = $1
`

var spec JobProposalSpec
err := o.q.WithOpts(qopts...).Get(&spec, stmt, id)
if err != nil {
return err
}

// Set pending update to true only if the latest proposal is approved so that any running jobs
// are reminded to be cancelled.
pendingUpdate := spec.Status == SpecStatusApproved
stmt = `
UPDATE job_proposals
SET status = $1,
pending_update = (
CASE
WHEN status = 'approved' THEN true
ELSE false
END
),
pending_update = $3,
updated_at = NOW()
WHERE id = $2;
`

result, err := o.q.WithOpts(qopts...).Exec(stmt, JobProposalStatusDeleted, id)
result, err := o.q.WithOpts(qopts...).Exec(stmt, JobProposalStatusDeleted, id, pendingUpdate)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 5132532

Please sign in to comment.