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 Jan 3, 2024
1 parent 9cd884f commit 86d42e1
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 410 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"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -97,12 +97,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
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ require (
github.com/samber/lo v1.39.0
github.com/sevennt/echo-pprof v0.1.1-0.20220616082843-66a461746b5f
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/timberio/go-datemath v0.1.0
go.mongodb.org/mongo-driver v1.12.1
go.opentelemetry.io/otel v1.19.0
Expand Down Expand Up @@ -221,6 +220,7 @@ require (
github.com/sergi/go-diff v1.3.1 // indirect
github.com/sethvargo/go-retry v0.2.4 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stoewer/go-strcase v1.3.0 // indirect
github.com/tidwall/gjson v1.17.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
Expand Down Expand Up @@ -285,6 +285,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
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 86d42e1

Please sign in to comment.