From b1d1dca0189fdb5b7aa71eae08cf8ea37ae1409f Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Wed, 5 Jul 2023 13:34:51 +0545 Subject: [PATCH] do not peform aggregation if the number of check statuses in the time range is within the limit. --- pkg/api/api.go | 20 ++++++++------------ pkg/cache/cache.go | 12 +++++++++--- pkg/cache/postgres_query.go | 12 +++++++++--- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index 8c1eb05d5..b8f15546b 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -1,7 +1,6 @@ package api import ( - "math" "net/http" "time" @@ -9,7 +8,6 @@ import ( "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" ) @@ -54,21 +52,19 @@ func CheckDetails(c echo.Context) error { if err != nil { return errorResonse(c, err, http.StatusInternalServerError) } - checkSummary := summary[0] + if len(summary) == 0 { + return c.JSON(http.StatusOK, DetailResponse{}) + } + checkSummary := *summary[0] totalChecks := checkSummary.Uptime.Total() if totalChecks <= maxCheckStatuses { - q.WindowDuration = time.Second // TODO: Maybe do not window at all + q.WindowDuration = 0 // No need to perform window aggregation } 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) + endTime := q.GetEndTime() + rangeDuration := endTime.Sub(*startTime) + q.WindowDuration = rangeDuration / time.Duration(maxCheckStatuses) } results, err := cache.PostgresCache.QueryStatus(c.Request().Context(), *q) diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index 08ac56f84..699e7de16 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -58,10 +58,10 @@ func (q QueryParams) GetStartTime() *time.Time { func (q QueryParams) GetEndTime() *time.Time { if q._end != nil || q.End == "" { - return q._start + return q._end } - q._start, _ = timeV(q.Start) - return q._start + q._end, _ = timeV(q.End) + return q._end } func (q QueryParams) String() string { @@ -81,6 +81,7 @@ func ParseQuery(c echo.Context) (*QueryParams, error) { } else { cacheCount = int64(DefaultCacheCount) } + since := queryParams.Get("since") if since == "" { since = queryParams.Get("start") @@ -88,10 +89,15 @@ func ParseQuery(c echo.Context) (*QueryParams, error) { if since == "" { since = DefaultWindow } + until := queryParams.Get("until") if until == "" { until = queryParams.Get("end") } + if until == "" { + until = "0s" + } + q := QueryParams{ Start: since, End: until, diff --git a/pkg/cache/postgres_query.go b/pkg/cache/postgres_query.go index f62099b60..ba0d8de8e 100644 --- a/pkg/cache/postgres_query.go +++ b/pkg/cache/postgres_query.go @@ -80,6 +80,9 @@ func (q QueryParams) GetWhereClause() (string, map[string]interface{}, error) { } func (q QueryParams) ExecuteDetails(ctx context.Context, db Querier) ([]pkg.Timeseries, error) { + start := q.GetStartTime().Format(time.RFC3339) + end := q.GetEndTime().Format(time.RFC3339) + query := ` With grouped_by_window AS ( SELECT @@ -100,11 +103,14 @@ FROM grouped_by_window GROUP BY time ` + args := []any{q.WindowDuration.Seconds() / 2, q.WindowDuration.Seconds(), start, end, q.Check} - start := q.GetStartTime().Format(time.RFC3339) - end := time.Now().Format(time.RFC3339) // TODO: connect with the new date range picker + if q.WindowDuration == 0 { + query = `SELECT time, status, duration FROM check_statuses WHERE time >= $1 AND time <= $2 AND check_id = $3` + args = []any{start, end, q.Check} + } - rows, err := db.Query(ctx, query, q.WindowDuration.Seconds()/2, q.WindowDuration.Seconds(), start, end, q.Check) + rows, err := db.Query(ctx, query, args...) if err != nil { return nil, err }