Skip to content

Commit

Permalink
do not peform aggregation if the number of check statuses in the time…
Browse files Browse the repository at this point in the history
… range is

  within the limit.
  • Loading branch information
adityathebe committed Jul 5, 2023
1 parent 9cd5c89 commit b1d1dca
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
20 changes: 8 additions & 12 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
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"
)
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -81,17 +81,23 @@ func ParseQuery(c echo.Context) (*QueryParams, error) {
} else {
cacheCount = int64(DefaultCacheCount)
}

since := queryParams.Get("since")
if since == "" {
since = queryParams.Get("start")
}
if since == "" {
since = DefaultWindow
}

until := queryParams.Get("until")
if until == "" {
until = queryParams.Get("end")
}
if until == "" {
until = "0s"
}

q := QueryParams{
Start: since,
End: until,
Expand Down
12 changes: 9 additions & 3 deletions pkg/cache/postgres_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit b1d1dca

Please sign in to comment.