Skip to content

Commit

Permalink
calculate the window duration
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Jul 5, 2023
1 parent 5d0197e commit 9cd5c89
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
4 changes: 4 additions & 0 deletions pkg/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ type Uptime struct {
LastFail *time.Time `json:"last_fail,omitempty"`
}

func (u Uptime) Total() int {
return u.Passed + u.Failed
}

func (u Uptime) String() string {
if u.Passed == 0 && u.Failed == 0 {
return ""
Expand Down
33 changes: 33 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package api

import (
"math"
"net/http"
"time"

"github.com/flanksource/canary-checker/pkg/cache"
"github.com/flanksource/canary-checker/pkg/runner"
"github.com/flanksource/duty/models"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"

"github.com/flanksource/canary-checker/pkg"
)

// The maximum data points returned by the graph API
const maxCheckStatuses = 100

var DefaultWindow = "1h"

type Response struct {
Expand All @@ -20,10 +25,13 @@ type Response struct {
Checks pkg.Checks `json:"checks"`
ChecksSummary models.Checks `json:"checks_summary,omitempty"`
}

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

func About(c echo.Context) error {
Expand All @@ -41,6 +49,28 @@ func CheckDetails(c echo.Context) error {
}

start := time.Now()

summary, err := cache.PostgresCache.Query(*q)
if err != nil {
return errorResonse(c, err, http.StatusInternalServerError)
}
checkSummary := summary[0]

totalChecks := checkSummary.Uptime.Total()
if totalChecks <= maxCheckStatuses {
q.WindowDuration = time.Second // TODO: Maybe do not window at all
} else {
startTime := q.GetStartTime()
if startTime == nil {
return errorResonse(c, errors.New("start time must be a duration or RFC3339 timestamp"), http.StatusBadRequest)
}

startDuration := time.Since(*startTime)

windowsCount := int(math.Ceil(float64(totalChecks) / float64(maxCheckStatuses)))
q.WindowDuration = startDuration / time.Duration(windowsCount)
}

results, err := cache.PostgresCache.QueryStatus(c.Request().Context(), *q)
if err != nil {
return errorResonse(c, err, http.StatusInternalServerError)
Expand All @@ -50,7 +80,10 @@ func CheckDetails(c echo.Context) error {
RunnerName: runner.RunnerName,
Status: results,
Duration: int(time.Since(start).Milliseconds()),
Latency: checkSummary.Latency,
Uptime: checkSummary.Uptime,
}

return c.JSON(http.StatusOK, apiResponse)
}

Expand Down
1 change: 1 addition & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type QueryParams struct {
StatusCount int
Labels map[string]string
Trace bool
WindowDuration time.Duration
}

func (q QueryParams) Validate() error {
Expand Down
11 changes: 2 additions & 9 deletions pkg/cache/postgres_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,10 @@ FROM
GROUP BY time
`

dur, err := duration.ParseDuration(q.Start)
if err != nil {
return nil, err
}

start := time.Now().Add(-time.Duration(dur)).Format(time.RFC3339)
start := q.GetStartTime().Format(time.RFC3339)
end := time.Now().Format(time.RFC3339) // TODO: connect with the new date range picker

// TODO: Find the perfect window duration
windowDurationSeconds := (time.Minute * 15).Seconds()
rows, err := db.Query(ctx, query, windowDurationSeconds/2, windowDurationSeconds, start, end, q.Check)
rows, err := db.Query(ctx, query, q.WindowDuration.Seconds()/2, q.WindowDuration.Seconds(), start, end, q.Check)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 9cd5c89

Please sign in to comment.