Skip to content

Commit

Permalink
fix: invalid uuid (#1472)
Browse files Browse the repository at this point in the history
* fix: invalid uuid

* chore: address review comment
  • Loading branch information
adityathebe authored Nov 30, 2023
1 parent 4fa6e09 commit 4e9a75d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
13 changes: 13 additions & 0 deletions pkg/db/canary.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ func GetTransformedCheckIDs(ctx context.Context, canaryID string, excludeTypes .
return ids, err
}

func LatestCheckStatus(ctx context.Context, checkID string) (*models.CheckStatus, error) {
var status models.CheckStatus
if err := ctx.DB().Select("time, created_at, status").Where("check_id = ?", checkID).Order("created_at DESC").Find(&status).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}

return nil, err
}

return &status, nil
}

func AddCheckStatuses(ctx context.Context, ids []string, status models.CheckHealthStatus) error {
if len(ids) == 0 {
return nil
Expand Down
26 changes: 16 additions & 10 deletions pkg/jobs/canary/canary_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (j CanaryJob) Run(ctx dutyjob.JobRuntime) error {
checkIDDeleteStrategyMap[checkID] = result.Check.GetTransformDeleteStrategy()
}
}
updateCanaryStatusAndEvent(j.Canary, results)
updateCanaryStatusAndEvent(ctx.Context, j.Canary, results)

checkDeleteStrategyGroup := make(map[string][]string)
checksToRemove := utils.SetDifference(existingTransformedChecks, transformedChecksCreated)
Expand Down Expand Up @@ -170,7 +170,7 @@ func (j CanaryJob) Run(ctx dutyjob.JobRuntime) error {
return nil
}

func updateCanaryStatusAndEvent(canary v1.Canary, results []*pkg.CheckResult) {
func updateCanaryStatusAndEvent(ctx context.Context, canary v1.Canary, results []*pkg.CheckResult) {
if CanaryStatusChannel == nil {
return
}
Expand All @@ -192,8 +192,8 @@ func updateCanaryStatusAndEvent(canary v1.Canary, results []*pkg.CheckResult) {

// Set uptime and latency
uptime, latency := metrics.Record(canary, result)
checkKey := canary.GetKey(result.Check)
checkStatus[checkKey] = &v1.CheckStatus{
checkID := canary.Status.Checks[result.Check.GetName()]
checkStatus[checkID] = &v1.CheckStatus{
Uptime1H: uptime.String(),
Latency1H: latency.String(),
}
Expand All @@ -208,19 +208,25 @@ func updateCanaryStatusAndEvent(canary v1.Canary, results []*pkg.CheckResult) {
}

// Transition
q := cache.QueryParams{Check: checkKey, StatusCount: 1}
q := cache.QueryParams{Check: checkID, StatusCount: 1}
if canary.Status.LastTransitionedTime != nil {
q.Start = canary.Status.LastTransitionedTime.Format(time.RFC3339)
}
lastStatus, err := cache.PostgresCache.Query(q)
if err != nil || len(lastStatus) == 0 || len(lastStatus[0].Statuses) == 0 {

latestCheckStatus, err := db.LatestCheckStatus(ctx, checkID)
if err != nil || latestCheckStatus == nil {
transitioned = true
} else if len(lastStatus) > 0 && (lastStatus[0].Statuses[0].Status != result.Pass) {
} else if latestCheckStatus.Status != result.Pass {
transitioned = true
}
if transitioned {
checkStatus[checkKey].LastTransitionedTime = &metav1.Time{Time: time.Now()}
lastTransitionedTime = &metav1.Time{Time: time.Now()}
transitionTime := time.Now()
if latestCheckStatus != nil {
transitionTime = latestCheckStatus.CreatedAt
}

checkStatus[checkID].LastTransitionedTime = &metav1.Time{Time: transitionTime}
lastTransitionedTime = &metav1.Time{Time: transitionTime}
}

// TODO Why is this here ?
Expand Down

0 comments on commit 4e9a75d

Please sign in to comment.