Skip to content

Commit

Permalink
persist checks test threshold
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
adityathebe committed May 16, 2023
1 parent 3df5e9f commit 739b486
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 49 deletions.
15 changes: 5 additions & 10 deletions api/v1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

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

Expand Down Expand Up @@ -228,23 +229,17 @@ type DisplayTemplate interface {
// +k8s:deepcopy-gen=false
type TestFunction interface {
GetTestFunction() Template
GetTestThreshold() *TestThreshold
GetTestThreshold() *types.TestThreshold
}

// +k8s:deepcopy-gen=false
type Transformer interface {
GetTransformer() Template
}

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"`
}

type TestTemplate struct {
Template `yaml:",inline" json:",inline"`
*TestThreshold `yaml:",inline" json:",inline"`
Template `yaml:",inline" json:",inline"`
*types.TestThreshold `yaml:",inline" json:",inline"`
}

type Templatable struct {
Expand All @@ -257,7 +252,7 @@ func (t Templatable) GetTestFunction() Template {
return t.Test.Template
}

func (t Templatable) GetTestThreshold() *TestThreshold {
func (t Templatable) GetTestThreshold() *types.TestThreshold {
return t.Test.TestThreshold
}

Expand Down
3 changes: 2 additions & 1 deletion checks/runchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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 @@ -115,7 +116,7 @@ func processTemplates(ctx *context.Context, r *pkg.CheckResult) *pkg.CheckResult
return r
}

func measureTestSeverity(ctx *context.Context, threshold *v1.TestThreshold) pkg.TestSeverity {
func measureTestSeverity(ctx *context.Context, threshold *types.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 *v1.TestThreshold
threshold *types.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: &v1.TestThreshold{
threshold: &types.TestThreshold{
Critical: "duration > 1500",
},
},
Expand All @@ -33,7 +33,7 @@ func Test_measureTestSeverity(t *testing.T) {
name: "simple - high",
args: args{
duration: 1200,
threshold: &v1.TestThreshold{
threshold: &types.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: &v1.TestThreshold{
threshold: &types.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: &v1.TestThreshold{
threshold: &types.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: &v1.TestThreshold{
threshold: &types.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: &v1.TestThreshold{
threshold: &types.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: &v1.TestThreshold{
threshold: &types.TestThreshold{
Critical: "Duration > 1500",
High: "Duration > 1000",
Low: "Duration > 500",
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,5 @@ require (
sigs.k8s.io/kustomize/kyaml v0.13.9 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)

replace github.com/flanksource/duty => ../duty
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1086,8 +1086,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.84 h1:chDSTw9n1YLFhQSbTtDWoDjzKkT9t3oZWkFa+5YV9Uc=
github.com/flanksource/duty v1.0.84/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
62 changes: 35 additions & 27 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"
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 @@ -175,36 +176,37 @@ 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"`
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 *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:"-"`
}

func FromExternalCheck(canary Canary, check external.Check) Check {
return Check{
c := Check{
CanaryID: canary.ID,
Type: check.GetType(),
Icon: check.GetIcon(),
Expand All @@ -214,6 +216,12 @@ func FromExternalCheck(canary Canary, check external.Check) Check {
CanaryName: canary.Name,
Labels: labels.FilterLabels(canary.Labels),
}

if testable, ok := check.(v1.TestFunction); ok {
c.TestThreshold = testable.GetTestThreshold()
}

return c
}

func FromResult(result CheckResult) CheckStatus {
Expand Down

0 comments on commit 739b486

Please sign in to comment.