From 09b9f4593ee562586b9c2fccdf198ec1540ce6ab Mon Sep 17 00:00:00 2001 From: Aditya Date: Wed, 10 May 2023 16:42:04 +0545 Subject: [PATCH] finalize saving to severity in check status [skip ci] --- checks/runchecks.go | 6 +++--- pkg/api.go | 34 ++++++++++++++++++---------------- pkg/cache/postgres.go | 7 ++++--- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/checks/runchecks.go b/checks/runchecks.go index 849466d35..09ecfaf74 100644 --- a/checks/runchecks.go +++ b/checks/runchecks.go @@ -93,6 +93,9 @@ func processTemplates(ctx *context.Context, r *pkg.CheckResult) *pkg.CheckResult switch v := r.Check.(type) { case v1.TestFunction: + data := map[string]any{"duration": r.Duration} + r.TestSeverity = measureTestSeverity(ctx.New(data), v.GetTestThreshold()) + tpl := v.GetTestFunction() if tpl.IsEmpty() { break @@ -108,9 +111,6 @@ func processTemplates(ctx *context.Context, r *pkg.CheckResult) *pkg.CheckResult r.Failf("Test expression failed. Expecting true from: %v", tpl.Expression) } } - - data := map[string]any{"duration": r.Duration} - r.TestSeverity = measureTestSeverity(ctx.New(data), v.GetTestThreshold()) } return r diff --git a/pkg/api.go b/pkg/api.go index 43ad8bcaa..4771d741d 100644 --- a/pkg/api.go +++ b/pkg/api.go @@ -45,14 +45,15 @@ func (t *JSONTime) UnmarshalJSON(b []byte) error { } type CheckStatus struct { - Status bool `json:"status"` - Invalid bool `json:"invalid,omitempty"` - Time string `json:"time"` - Duration int `json:"duration"` - Message string `json:"message,omitempty"` - Error string `json:"error,omitempty"` - Detail interface{} `json:"-"` - Check *external.Check `json:"check,omitempty"` + Status bool `json:"status"` + Invalid bool `json:"invalid,omitempty"` + Time string `json:"time"` + Duration int `json:"duration"` + Message string `json:"message,omitempty"` + Error string `json:"error,omitempty"` + Detail interface{} `json:"-"` + Check *external.Check `json:"check,omitempty"` + TestSeverity TestSeverity `json:"test_severity,omitempty"` } func (s CheckStatus) GetTime() (time.Time, error) { @@ -226,14 +227,15 @@ func FromExternalCheck(canary Canary, check external.Check) Check { func FromResult(result CheckResult) CheckStatus { return CheckStatus{ - Status: result.Pass, - Invalid: result.Invalid, - Duration: int(result.Duration), - Time: time.Now().UTC().Format(time.RFC3339), - Message: result.Message, - Error: result.Error, - Detail: result.Detail, - Check: &result.Check, + Status: result.Pass, + Invalid: result.Invalid, + Duration: int(result.Duration), + Time: time.Now().UTC().Format(time.RFC3339), + Message: result.Message, + Error: result.Error, + Detail: result.Detail, + Check: &result.Check, + TestSeverity: result.TestSeverity, } } func FromV1(canary v1.Canary, check external.Check, statuses ...CheckStatus) Check { diff --git a/pkg/cache/postgres.go b/pkg/cache/postgres.go index 24a1a8abc..1642ac691 100644 --- a/pkg/cache/postgres.go +++ b/pkg/cache/postgres.go @@ -79,12 +79,12 @@ func (c *postgresCache) AddCheckStatus(check pkg.Check, status pkg.CheckStatus) invalid, message, status, + severity, time, created_at ) - VALUES($1,$2,$3,$4,$5,$6,$7,$8,NOW()) - ON CONFLICT (check_id,time) DO NOTHING; - `, + VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,NOW()) + ON CONFLICT (check_id,time) DO NOTHING;`, checks[0].ID, string(jsonDetails), status.Duration, @@ -92,6 +92,7 @@ func (c *postgresCache) AddCheckStatus(check pkg.Check, status pkg.CheckStatus) status.Invalid, status.Message, status.Status, + status.TestSeverity, status.Time, ) if err != nil {