Skip to content

Commit

Permalink
feat: move check details query to duty
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Dec 29, 2023
1 parent 832d45c commit 402df03
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 414 deletions.
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/flanksource/canary-checker/checks"
"github.com/flanksource/canary-checker/pkg"
"github.com/flanksource/canary-checker/pkg/cache"
"github.com/flanksource/canary-checker/pkg/db"
"github.com/flanksource/canary-checker/pkg/jobs/canary"
"github.com/flanksource/canary-checker/pkg/prometheus"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/flanksource/commons/logger"
"github.com/flanksource/duty"
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/query"
gomplate "github.com/flanksource/gomplate/v3"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -103,12 +103,12 @@ func ServerFlags(flags *pflag.FlagSet) {
flags.StringSliceVar(&runner.IncludeCanaries, "include-check", []string{}, "Run matching canaries - useful for debugging")
flags.StringSliceVar(&runner.IncludeTypes, "include-type", []string{}, "Check type to disable")
flags.StringSliceVar(&runner.IncludeNamespaces, "include-namespace", []string{}, "Check type to disable")
flags.IntVar(&cache.DefaultCacheCount, "maxStatusCheckCount", 5, "Maximum number of past checks in the in memory cache")
flags.IntVar(&query.DefaultCacheCount, "maxStatusCheckCount", 5, "Maximum number of past checks in the in memory cache")
flags.StringVar(&runner.RunnerName, "name", "local", "Server name shown in aggregate dashboard")
flags.StringVar(&prometheus.PrometheusURL, "prometheus", "", "URL of the prometheus server that is scraping this instance")
flags.StringVar(&db.ConnectionString, "db", "DB_URL", "Connection string for the postgres database. Use embedded:///path/to/dir to use the embedded database")
flags.IntVar(&db.DefaultExpiryDays, "cache-timeout", 90, "Cache timeout in days")
flags.StringVarP(&cache.DefaultWindow, "default-window", "", "1h", "Default search window")
flags.StringVarP(&query.DefaultCheckQueryWindow, "default-window", "", "1h", "Default search window")
flags.IntVar(&db.CheckStatusRetention, "check-status-retention-period", db.CheckStatusRetention, "Check status retention period in days")
flags.IntVar(&topology.CheckRetentionDays, "check-retention-period", topology.DefaultCheckRetentionDays, "Check retention period in days")
flags.IntVar(&topology.CanaryRetentionDays, "canary-retention-period", topology.DefaultCanaryRetentionDays, "Canary retention period in days")
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,4 @@ require (
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)

// replace github.com/flanksource/commons => ../commons

// replace github.com/flanksource/duty => ../duty
// replace "github.com/flanksource/duty" => ../duty
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -827,10 +827,6 @@ github.com/flanksource/artifacts v1.0.3 h1:Ci26mDhVFyxPefX96tgPGTe5fR0wfntXotSxa
github.com/flanksource/artifacts v1.0.3/go.mod h1:KfDDG7B4wsiGqJYOamcRU19u5hBvpYjlru3GfcORvko=
github.com/flanksource/commons v1.19.3 h1:J4s5WWicUMNavEDCC5AIJQWBDaaloJ/tgztWUXTtRXc=
github.com/flanksource/commons v1.19.3/go.mod h1:ZUgFy0Wwrm2LxV/AltBx36FShzXcn7wPmXJldK8Aa0E=
github.com/flanksource/duty v1.0.259 h1:PbMqiF1Uk4nHjxlEJtNke23QXZlAyZpBY/Rj5sF13UU=
github.com/flanksource/duty v1.0.259/go.mod h1:tXMyMlcSGOXvXoP5KZApwzSDFs07NDQGhnJi9ArjWf4=
github.com/flanksource/duty v1.0.263 h1:e4mEioMJGlpCZ8Dzr5pAZqKsAy0gSHNhP0ffgP0C0/I=
github.com/flanksource/duty v1.0.263/go.mod h1:cfaBnuz1o5WqJ7mKOe03R7so6dHzz8vtdEYqDAN+nwE=
github.com/flanksource/duty v1.0.264 h1:PQIevmIWFrMYN1VfQjgxqDJG6NCut+K30rIPkA7w7dI=
github.com/flanksource/duty v1.0.264/go.mod h1:cfaBnuz1o5WqJ7mKOe03R7so6dHzz8vtdEYqDAN+nwE=
github.com/flanksource/gomplate/v3 v3.20.4/go.mod h1:27BNWhzzSjDed1z8YShO6W+z6G9oZXuxfNFGd/iGSdc=
Expand Down
4 changes: 0 additions & 4 deletions hack/generate-schemas/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -719,10 +719,6 @@ github.com/exaring/otelpgx v0.5.2/go.mod h1:4dBiAqwzDNmpj3TwX5Syti1/Nw2bIoDQItdL
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/flanksource/commons v1.19.3 h1:J4s5WWicUMNavEDCC5AIJQWBDaaloJ/tgztWUXTtRXc=
github.com/flanksource/commons v1.19.3/go.mod h1:ZUgFy0Wwrm2LxV/AltBx36FShzXcn7wPmXJldK8Aa0E=
github.com/flanksource/duty v1.0.259 h1:PbMqiF1Uk4nHjxlEJtNke23QXZlAyZpBY/Rj5sF13UU=
github.com/flanksource/duty v1.0.259/go.mod h1:tXMyMlcSGOXvXoP5KZApwzSDFs07NDQGhnJi9ArjWf4=
github.com/flanksource/duty v1.0.263 h1:e4mEioMJGlpCZ8Dzr5pAZqKsAy0gSHNhP0ffgP0C0/I=
github.com/flanksource/duty v1.0.263/go.mod h1:cfaBnuz1o5WqJ7mKOe03R7so6dHzz8vtdEYqDAN+nwE=
github.com/flanksource/duty v1.0.264 h1:PQIevmIWFrMYN1VfQjgxqDJG6NCut+K30rIPkA7w7dI=
github.com/flanksource/duty v1.0.264/go.mod h1:cfaBnuz1o5WqJ7mKOe03R7so6dHzz8vtdEYqDAN+nwE=
github.com/flanksource/gomplate/v3 v3.20.4/go.mod h1:27BNWhzzSjDed1z8YShO6W+z6G9oZXuxfNFGd/iGSdc=
Expand Down
60 changes: 2 additions & 58 deletions pkg/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,62 +64,6 @@ func (s CheckStatus) GetTime() (time.Time, error) {
return time.Parse("2006-01-02 15:04:05", s.Time)
}

type Latency struct {
Percentile99 float64 `json:"p99,omitempty" db:"p99"`
Percentile97 float64 `json:"p97,omitempty" db:"p97"`
Percentile95 float64 `json:"p95,omitempty" db:"p95"`
Rolling1H float64 `json:"rolling1h"`
}

func (l Latency) String() string {
s := ""
if l.Percentile99 != 0 {
s += fmt.Sprintf("p99=%s", utils.Age(time.Duration(l.Percentile99)*time.Millisecond))
}
if l.Percentile95 != 0 {
s += fmt.Sprintf("p95=%s", utils.Age(time.Duration(l.Percentile95)*time.Millisecond))
}
if l.Percentile97 != 0 {
s += fmt.Sprintf("p97=%s", utils.Age(time.Duration(l.Percentile97)*time.Millisecond))
}
if l.Rolling1H != 0 {
s += fmt.Sprintf("rolling1h=%s", utils.Age(time.Duration(l.Rolling1H)*time.Millisecond))
}
return s
}

type Uptime struct {
Passed int `json:"passed"`
Failed int `json:"failed"`
P100 float64 `json:"p100,omitempty"`
LastPass *time.Time `json:"last_pass,omitempty"`
LastFail *time.Time `json:"last_fail,omitempty"`
}

func (u Uptime) String() string {
if u.Passed == 0 && u.Failed == 0 {
return ""
}
if u.Passed == 0 {
return fmt.Sprintf("0/%d 0%%", u.Failed)
}
percentage := 100.0 * (1 - (float64(u.Failed) / float64(u.Passed+u.Failed)))
return fmt.Sprintf("%d/%d (%0.1f%%)", u.Passed, u.Passed+u.Failed, percentage)
}

type Timeseries struct {
Key string `json:"key,omitempty"`
Time string `json:"time,omitempty"`
Status bool `json:"status,omitempty"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
Duration int `json:"duration"`
// Count is the number of times the check has been run in the specified time window
Count int `json:"count,omitempty"`
Passed int `json:"passed,omitempty"`
Failed int `json:"failed,omitempty"`
}

type Canary struct {
ID uuid.UUID `gorm:"default:generate_ulid()"`
AgentID uuid.UUID
Expand Down Expand Up @@ -212,8 +156,8 @@ type Check struct {
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:"-"`
Uptime types.Uptime `json:"uptime" gorm:"-"`
Latency types.Latency `json:"latency" gorm:"-"`
Statuses []CheckStatus `json:"checkStatuses" gorm:"-"`
Owner string `json:"owner,omitempty"`
Severity string `json:"severity,omitempty"`
Expand Down
36 changes: 11 additions & 25 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/query"
"github.com/flanksource/duty/types"
"github.com/labstack/echo/v4"

"github.com/flanksource/canary-checker/pkg"
Expand Down Expand Up @@ -44,11 +45,11 @@ type Response struct {
}

type DetailResponse struct {
Duration int `json:"duration,omitempty"`
RunnerName string `json:"runnerName"`
Status []pkg.Timeseries `json:"status"`
Latency pkg.Latency `json:"latency"`
Uptime pkg.Uptime `json:"uptime"`
Duration int `json:"duration,omitempty"`
RunnerName string `json:"runnerName"`
Status []query.Timeseries `json:"status"`
Latency types.Latency `json:"latency"`
Uptime types.Uptime `json:"uptime"`
}

func About(c echo.Context) error {
Expand All @@ -60,30 +61,15 @@ func About(c echo.Context) error {
}

func CheckDetails(c echo.Context) error {
q, err := cache.ParseQuery(c)
if err != nil {
ctx := c.Request().Context().(context.Context)

var q query.CheckQueryParams
if err := q.Init(c.QueryParams()); err != nil {
return errorResponse(c, err, http.StatusBadRequest)
}

start := time.Now()

end := q.GetEndTime()
since := q.GetStartTime()
timeRange := end.Sub(*since)

if timeRange <= time.Hour*2 {
q.WindowDuration = time.Minute
} else if timeRange >= time.Hour*24 {
q.WindowDuration = time.Minute * 15
} else if timeRange >= time.Hour*24*7 {
q.WindowDuration = time.Minute * 60
} else {
q.WindowDuration = time.Hour * 4
}

ctx := c.Request().Context().(context.Context)

results, uptime, latency, err := q.ExecuteDetails(ctx, ctx.Pool())
results, uptime, latency, err := q.ExecuteDetails(ctx)
if err != nil {
return errorResponse(c, err, http.StatusInternalServerError)
}
Expand Down
135 changes: 0 additions & 135 deletions pkg/cache/cache.go

This file was deleted.

Loading

0 comments on commit 402df03

Please sign in to comment.