Skip to content

Commit

Permalink
create TestThreshold struct. Don't use from duty.
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
adityathebe committed May 16, 2023
1 parent e346b41 commit 2966c04
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 44 deletions.
15 changes: 10 additions & 5 deletions api/v1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/c2h5oh/datasize"
"github.com/flanksource/commons/duration"
"github.com/flanksource/duty/types"
"github.com/flanksource/kommons"
)

Expand Down Expand Up @@ -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 == ""
}
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
}

Expand Down
3 changes: 1 addition & 2 deletions checks/runchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 9 additions & 9 deletions checks/runchecks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
},
},
Expand All @@ -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",
},
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
55 changes: 27 additions & 28 deletions pkg/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions pkg/jobs/canary/canary_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 2966c04

Please sign in to comment.