Skip to content

Commit

Permalink
chore: switch to duty const for severity
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Jul 3, 2023
1 parent 2430cb9 commit 3d06d31
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 40 deletions.
22 changes: 12 additions & 10 deletions checks/runchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/flanksource/canary-checker/pkg"
"github.com/flanksource/canary-checker/pkg/db"
"github.com/flanksource/commons/logger"
"github.com/flanksource/duty/models"
)

func RunChecks(ctx *context.Context) []*pkg.CheckResult {
Expand Down Expand Up @@ -127,29 +128,30 @@ func processTemplates(ctx *context.Context, r *pkg.CheckResult) *pkg.CheckResult
return r
}

func measureTestSeverity(ctx *context.Context, threshold *v1.TestThreshold) pkg.Severity {
func measureTestSeverity(ctx *context.Context, threshold *v1.TestThreshold) models.Severity {
if threshold == nil {
return pkg.SeverityInfo
return models.SeverityInfo
}

thresholds := []struct {
severity pkg.Severity
severity models.Severity
expr string
}{
{pkg.SeverityCritical, threshold.Critical},
{pkg.SeverityHigh, threshold.High},
{pkg.SeverityMedium, threshold.Medium},
{pkg.SeverityLow, threshold.Low},
{pkg.SeverityInfo, threshold.Info},
{models.SeverityCritical, threshold.Critical},
{models.SeverityHigh, threshold.High},
{models.SeverityMedium, threshold.Medium},
{models.SeverityLow, threshold.Low},
{models.SeverityInfo, threshold.Info},
}

for _, t := range thresholds {
if res, err := template(ctx, v1.Template{Expression: t.expr}); err != nil {
return pkg.SeverityInfo
logger.Errorf("failed to run expression for severity: %s", t.severity)
continue
} else if res == "true" {
return t.severity
}
}

return pkg.SeverityInfo
return models.SeverityInfo
}
22 changes: 11 additions & 11 deletions checks/runchecks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/flanksource/canary-checker/api/context"
v1 "github.com/flanksource/canary-checker/api/v1"
"github.com/flanksource/canary-checker/pkg"
"github.com/flanksource/duty/models"
)

func Test_measureTestSeverity(t *testing.T) {
Expand All @@ -17,7 +17,7 @@ func Test_measureTestSeverity(t *testing.T) {
tests := []struct {
name string
args args
want pkg.Severity
want models.Severity
}{
{
name: "simple - critical",
Expand All @@ -27,7 +27,7 @@ func Test_measureTestSeverity(t *testing.T) {
Critical: "duration > 1500",
},
},
want: pkg.SeverityCritical,
want: models.SeverityCritical,
},
{
name: "simple - high",
Expand All @@ -38,7 +38,7 @@ func Test_measureTestSeverity(t *testing.T) {
High: "duration > 1000",
},
},
want: pkg.SeverityHigh,
want: models.SeverityHigh,
},
{
name: "simple - medium",
Expand All @@ -51,7 +51,7 @@ func Test_measureTestSeverity(t *testing.T) {
Low: "duration > 500",
},
},
want: pkg.SeverityMedium,
want: models.SeverityMedium,
},
{
name: "simple - low",
Expand All @@ -63,7 +63,7 @@ func Test_measureTestSeverity(t *testing.T) {
Low: "duration > 500",
},
},
want: pkg.SeverityLow,
want: models.SeverityLow,
},
{
name: "complex expression",
Expand All @@ -75,14 +75,14 @@ func Test_measureTestSeverity(t *testing.T) {
Low: "duration > 500 && duration < 1000",
},
},
want: pkg.SeverityInfo,
want: models.SeverityInfo,
},
{
name: "no threshold defined",
args: args{
duration: 600,
},
want: pkg.SeverityInfo,
want: models.SeverityInfo,
},
{
name: "no severity match",
Expand All @@ -94,7 +94,7 @@ func Test_measureTestSeverity(t *testing.T) {
Low: "duration > 500",
},
},
want: pkg.SeverityInfo,
want: models.SeverityInfo,
},
{
name: "invalid expression",
Expand All @@ -106,7 +106,7 @@ func Test_measureTestSeverity(t *testing.T) {
Low: "duration > 500",
},
},
want: pkg.SeverityInfo,
want: models.SeverityInfo,
},
{
name: "use of undefined var",
Expand All @@ -118,7 +118,7 @@ func Test_measureTestSeverity(t *testing.T) {
Low: "Duration > 500",
},
},
want: pkg.SeverityInfo,
want: models.SeverityInfo,
},
}
for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/elastic/go-elasticsearch/v8 v8.1.0
github.com/fergusstrange/embedded-postgres v1.21.0
github.com/flanksource/commons v1.10.0
github.com/flanksource/duty v1.0.113
github.com/flanksource/duty v1.0.116
github.com/flanksource/kommons v0.31.1
github.com/friendsofgo/errors v0.9.2
github.com/go-ldap/ldap/v3 v3.4.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1091,8 +1091,6 @@ github.com/fergusstrange/embedded-postgres v1.21.0 h1:Sups0nR31+OB4iOZ0ZU4IwUDsB
github.com/fergusstrange/embedded-postgres v1.21.0/go.mod h1:wL562t1V+iuFwq0UcgMi2e9rp8CROY9wxWZEfP8Y874=
github.com/flanksource/commons v1.10.0 h1:Mc+fzxq1rOJ08lapF0cc0bo9ZKNDxA7/81q2D/5jt0Q=
github.com/flanksource/commons v1.10.0/go.mod h1:HpVjPtNe7v0UPk97kO/uUhOrYQ8yFD/mGglrTCkc9Ag=
github.com/flanksource/duty v1.0.113 h1:iQeMhn/Vjz+Rap4TpniGEwLOXcFeg9+X1GtcUsWp3+g=
github.com/flanksource/duty v1.0.113/go.mod h1:RJ/kcZ7dbL8/52tem757szVIA3IomS8bOAZIK0xb4rk=
github.com/flanksource/gomplate/v3 v3.20.1/go.mod h1:LPpzujBIg9HBXRUngDKK/zNmEjHpEUieKa/2oRjkCzI=
github.com/flanksource/gomplate/v3 v3.20.3 h1:mnNaO37uwvv8Kvi4Xswj1tHKP5UMre3FLNaKvAwqhSw=
github.com/flanksource/gomplate/v3 v3.20.3/go.mod h1:l4iCvp30TdhZk89eRGuNkPwlRjJLXdUSblr+VyuZfAk=
Expand Down Expand Up @@ -3621,3 +3619,5 @@ sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU=
github.com/flanksource/duty v1.0.116 h1:BU1Go53xj/P3LfZFrusP48e3lAo1KCJk0qcp+ye/p7s=
github.com/flanksource/duty v1.0.116/go.mod h1:RJ/kcZ7dbL8/52tem757szVIA3IomS8bOAZIK0xb4rk=
2 changes: 1 addition & 1 deletion hack/generate-schemas/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/dustin/gojson v0.0.0-20160307161227-2e71ec9dd5ad // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/flanksource/duty v1.0.113 // indirect
github.com/flanksource/duty v1.0.116 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-openapi/inflect v0.19.0 // indirect
Expand Down
6 changes: 3 additions & 3 deletions hack/generate-schemas/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -811,8 +811,8 @@ github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSw
github.com/fergusstrange/embedded-postgres v1.21.0 h1:Sups0nR31+OB4iOZ0ZU4IwUDsB+dVGmcqj4S2ko0qTI=
github.com/flanksource/commons v1.10.0 h1:Mc+fzxq1rOJ08lapF0cc0bo9ZKNDxA7/81q2D/5jt0Q=
github.com/flanksource/commons v1.10.0/go.mod h1:HpVjPtNe7v0UPk97kO/uUhOrYQ8yFD/mGglrTCkc9Ag=
github.com/flanksource/duty v1.0.113 h1:iQeMhn/Vjz+Rap4TpniGEwLOXcFeg9+X1GtcUsWp3+g=
github.com/flanksource/duty v1.0.113/go.mod h1:RJ/kcZ7dbL8/52tem757szVIA3IomS8bOAZIK0xb4rk=
github.com/flanksource/duty v1.0.116 h1:BU1Go53xj/P3LfZFrusP48e3lAo1KCJk0qcp+ye/p7s=
github.com/flanksource/duty v1.0.116/go.mod h1:RJ/kcZ7dbL8/52tem757szVIA3IomS8bOAZIK0xb4rk=
github.com/flanksource/gomplate/v3 v3.20.1/go.mod h1:LPpzujBIg9HBXRUngDKK/zNmEjHpEUieKa/2oRjkCzI=
github.com/flanksource/gomplate/v3 v3.20.3 h1:mnNaO37uwvv8Kvi4Xswj1tHKP5UMre3FLNaKvAwqhSw=
github.com/flanksource/gomplate/v3 v3.20.3/go.mod h1:l4iCvp30TdhZk89eRGuNkPwlRjJLXdUSblr+VyuZfAk=
Expand Down Expand Up @@ -1481,8 +1481,8 @@ github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
github.com/onsi/ginkgo v1.16.2/go.mod h1:CObGmKUOKaSC0RjmoAK7tKyn4Azo5P2IWuoMnvwxz1E=
github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc=
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/ginkgo/v2 v2.1.4/go.mod h1:um6tUpWM/cxCK3/FK8BXqEiUMUwRgSM4JXG47RKZmLU=
github.com/onsi/ginkgo/v2 v2.1.6/go.mod h1:MEH45j8TBi6u9BMogfbp0stKC5cdGjumZj5Y7AG4VIk=
Expand Down
15 changes: 3 additions & 12 deletions pkg/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/flanksource/canary-checker/pkg/utils"
"github.com/flanksource/commons/console"
"github.com/flanksource/commons/logger"
"github.com/flanksource/duty/models"
"github.com/google/uuid"
"github.com/lib/pq"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -53,7 +54,7 @@ type CheckStatus struct {
Error string `json:"error,omitempty"`
Detail interface{} `json:"-"`
Check *external.Check `json:"check,omitempty"`
TestSeverity Severity `json:"test_severity,omitempty"`
TestSeverity models.Severity `json:"severity,omitempty"`
}

func (s CheckStatus) GetTime() (time.Time, error) {
Expand Down Expand Up @@ -394,16 +395,6 @@ type URL struct {

type SystemResult struct{}

type Severity int

const (
SeverityInfo Severity = iota
SeverityLow
SeverityMedium
SeverityHigh
SeverityCritical
)

type CheckResult struct {
Start time.Time
Pass bool
Expand All @@ -418,7 +409,7 @@ type CheckResult struct {
Metrics []Metric
Check external.Check // Check is the configuration
Canary v1.Canary
Severity Severity
Severity models.Severity
}

func (result CheckResult) GetDescription() string {
Expand Down

0 comments on commit 3d06d31

Please sign in to comment.