From 2966c04d4554bcc61cc1b2e5081099779e927bef Mon Sep 17 00:00:00 2001 From: Aditya Date: Tue, 16 May 2023 11:28:01 +0545 Subject: [PATCH] create TestThreshold struct. Don't use from duty. [skip ci] --- api/v1/common.go | 15 ++++++---- checks/runchecks.go | 3 +- checks/runchecks_test.go | 18 +++++------ pkg/api.go | 55 +++++++++++++++++----------------- pkg/jobs/canary/canary_jobs.go | 2 ++ 5 files changed, 49 insertions(+), 44 deletions(-) diff --git a/api/v1/common.go b/api/v1/common.go index 6c1edb258..80d1f3c35 100644 --- a/api/v1/common.go +++ b/api/v1/common.go @@ -10,7 +10,6 @@ import ( "github.com/c2h5oh/datasize" "github.com/flanksource/commons/duration" - "github.com/flanksource/duty/types" "github.com/flanksource/kommons" ) @@ -217,6 +216,12 @@ type Template struct { Javascript string `yaml:"javascript,omitempty" json:"javascript,omitempty"` } +type TestThreshold struct { + High string `yaml:"high,omitempty" json:"high,omitempty"` + Low string `yaml:"low,omitempty" json:"low,omitempty"` + Critical string `yaml:"critical,omitempty" json:"critical,omitempty"` +} + func (t Template) IsEmpty() bool { return t.Template == "" && t.JSONPath == "" && t.Expression == "" && t.Javascript == "" } @@ -229,7 +234,7 @@ type DisplayTemplate interface { // +k8s:deepcopy-gen=false type TestFunction interface { GetTestFunction() Template - GetTestThreshold() *types.TestThreshold + GetTestThreshold() *TestThreshold } // +k8s:deepcopy-gen=false @@ -238,8 +243,8 @@ type Transformer interface { } type TestTemplate struct { - Template `yaml:",inline" json:",inline"` - *types.TestThreshold `yaml:",inline" json:",inline"` + Template `yaml:",inline" json:",inline"` + *TestThreshold `yaml:",inline" json:",inline"` } type Templatable struct { @@ -252,7 +257,7 @@ func (t Templatable) GetTestFunction() Template { return t.Test.Template } -func (t Templatable) GetTestThreshold() *types.TestThreshold { +func (t Templatable) GetTestThreshold() *TestThreshold { return t.Test.TestThreshold } diff --git a/checks/runchecks.go b/checks/runchecks.go index 09ecfaf74..5d2b20160 100644 --- a/checks/runchecks.go +++ b/checks/runchecks.go @@ -7,7 +7,6 @@ import ( v1 "github.com/flanksource/canary-checker/api/v1" "github.com/flanksource/canary-checker/pkg" "github.com/flanksource/commons/logger" - "github.com/flanksource/duty/types" ) func RunChecks(ctx *context.Context) []*pkg.CheckResult { @@ -116,7 +115,7 @@ func processTemplates(ctx *context.Context, r *pkg.CheckResult) *pkg.CheckResult return r } -func measureTestSeverity(ctx *context.Context, threshold *types.TestThreshold) pkg.TestSeverity { +func measureTestSeverity(ctx *context.Context, threshold *v1.TestThreshold) pkg.TestSeverity { if threshold == nil { return pkg.TestSeverityUnknown } diff --git a/checks/runchecks_test.go b/checks/runchecks_test.go index 2bda274d8..63ac6e4b3 100644 --- a/checks/runchecks_test.go +++ b/checks/runchecks_test.go @@ -5,14 +5,14 @@ import ( "testing" "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/types" ) func Test_measureTestSeverity(t *testing.T) { type args struct { duration int - threshold *types.TestThreshold + threshold *v1.TestThreshold } tests := []struct { name string @@ -23,7 +23,7 @@ func Test_measureTestSeverity(t *testing.T) { name: "simple - critical", args: args{ duration: 2000, - threshold: &types.TestThreshold{ + threshold: &v1.TestThreshold{ Critical: "duration > 1500", }, }, @@ -33,7 +33,7 @@ func Test_measureTestSeverity(t *testing.T) { name: "simple - high", args: args{ duration: 1200, - threshold: &types.TestThreshold{ + threshold: &v1.TestThreshold{ Critical: "duration > 1500", High: "duration > 1000", }, @@ -44,7 +44,7 @@ func Test_measureTestSeverity(t *testing.T) { name: "simple - low", args: args{ duration: 600, - threshold: &types.TestThreshold{ + threshold: &v1.TestThreshold{ Critical: "duration > 1500", High: "duration > 1000", Low: "duration > 500", @@ -56,7 +56,7 @@ func Test_measureTestSeverity(t *testing.T) { name: "complex expression", args: args{ duration: 2100, - threshold: &types.TestThreshold{ + threshold: &v1.TestThreshold{ Critical: "duration > 1500 && duration < 2000", High: "duration > 1000 && duration < 1500", Low: "duration > 500 && duration < 1000", @@ -75,7 +75,7 @@ func Test_measureTestSeverity(t *testing.T) { name: "no severity match", args: args{ duration: 400, - threshold: &types.TestThreshold{ + threshold: &v1.TestThreshold{ Critical: "duration > 1500", High: "duration > 1000", Low: "duration > 500", @@ -87,7 +87,7 @@ func Test_measureTestSeverity(t *testing.T) { name: "invalid expression", args: args{ duration: 400, - threshold: &types.TestThreshold{ + threshold: &v1.TestThreshold{ Critical: "duration >>> 1500", High: "duration > 1000", Low: "duration > 500", @@ -99,7 +99,7 @@ func Test_measureTestSeverity(t *testing.T) { name: "use of undefined var", args: args{ duration: 400, - threshold: &types.TestThreshold{ + threshold: &v1.TestThreshold{ Critical: "Duration > 1500", High: "Duration > 1000", Low: "Duration > 500", diff --git a/pkg/api.go b/pkg/api.go index 4771d741d..8bbb30589 100644 --- a/pkg/api.go +++ b/pkg/api.go @@ -12,7 +12,6 @@ import ( "github.com/flanksource/canary-checker/pkg/utils" "github.com/flanksource/commons/console" "github.com/flanksource/commons/logger" - dutyTypes "github.com/flanksource/duty/types" "github.com/google/uuid" "github.com/lib/pq" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -177,33 +176,33 @@ func CanaryFromV1(canary v1.Canary) (Canary, error) { } type Check struct { - ID uuid.UUID `json:"id" gorm:"default:generate_ulid()"` - CanaryID uuid.UUID `json:"canary_id"` - Spec types.JSON `json:"-"` - Type string `json:"type"` - Name string `json:"name"` - CanaryName string `json:"canary_name" gorm:"-"` - Namespace string `json:"namespace" gorm:"-"` - Labels types.JSONStringMap `json:"labels" gorm:"type:jsonstringmap"` - TestThreshold *dutyTypes.TestThreshold `json:"testThreshold,omitempty" gorm:"column:test_threshold"` - Description string `json:"description,omitempty"` - Status string `json:"status,omitempty"` - Uptime Uptime `json:"uptime" gorm:"-"` - Latency Latency `json:"latency" gorm:"-"` - Statuses []CheckStatus `json:"checkStatuses" gorm:"-"` - Owner string `json:"owner,omitempty"` - Severity string `json:"severity,omitempty"` - Icon string `json:"icon,omitempty"` - DisplayType string `json:"displayType,omitempty" gorm:"-"` - Transformed bool `json:"transformed,omitempty"` - LastRuntime *time.Time `json:"lastRuntime,omitempty"` - LastTransitionTime *time.Time `json:"lastTransitionTime,omitempty"` - NextRuntime *time.Time `json:"nextRuntime,omitempty"` - UpdatedAt *time.Time `json:"updatedAt,omitempty"` - CreatedAt *time.Time `json:"createdAt,omitempty"` - DeletedAt *time.Time `json:"deletedAt,omitempty"` - SilencedAt *time.Time `json:"silencedAt,omitempty"` - Canary *v1.Canary `json:"-" gorm:"-"` + ID uuid.UUID `json:"id" gorm:"default:generate_ulid()"` + CanaryID uuid.UUID `json:"canary_id"` + Spec types.JSON `json:"-"` + Type string `json:"type"` + Name string `json:"name"` + CanaryName string `json:"canary_name" gorm:"-"` + Namespace string `json:"namespace" gorm:"-"` + Labels types.JSONStringMap `json:"labels" gorm:"type:jsonstringmap"` + TestThreshold *v1.TestThreshold `json:"testThreshold,omitempty" gorm:"-"` + Description string `json:"description,omitempty"` + Status string `json:"status,omitempty"` + Uptime Uptime `json:"uptime" gorm:"-"` + Latency Latency `json:"latency" gorm:"-"` + Statuses []CheckStatus `json:"checkStatuses" gorm:"-"` + Owner string `json:"owner,omitempty"` + Severity string `json:"severity,omitempty"` + Icon string `json:"icon,omitempty"` + DisplayType string `json:"displayType,omitempty" gorm:"-"` + Transformed bool `json:"transformed,omitempty"` + LastRuntime *time.Time `json:"lastRuntime,omitempty"` + LastTransitionTime *time.Time `json:"lastTransitionTime,omitempty"` + NextRuntime *time.Time `json:"nextRuntime,omitempty"` + UpdatedAt *time.Time `json:"updatedAt,omitempty"` + CreatedAt *time.Time `json:"createdAt,omitempty"` + DeletedAt *time.Time `json:"deletedAt,omitempty"` + SilencedAt *time.Time `json:"silencedAt,omitempty"` + Canary *v1.Canary `json:"-" gorm:"-"` } func FromExternalCheck(canary Canary, check external.Check) Check { diff --git a/pkg/jobs/canary/canary_jobs.go b/pkg/jobs/canary/canary_jobs.go index 36a9bc26f..7d4ddee6a 100644 --- a/pkg/jobs/canary/canary_jobs.go +++ b/pkg/jobs/canary/canary_jobs.go @@ -273,6 +273,8 @@ func SyncCanaryJob(canary v1.Canary) error { } func SyncCanaryJobs() { + time.Sleep(time.Second * 3) + logger.Debugf("Syncing canary jobs") jobHistory := models.NewJobHistory("CanarySync", "canary", "").Start()