Skip to content

Commit

Permalink
fix: latency calculation in check
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Feb 3, 2025
1 parent 85a43a6 commit 122523e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/RaveNoX/go-jsonmerge v1.0.0
github.com/TomOnTime/utfutil v0.0.0-20230223141146-125e65197b36
github.com/WinterYukky/gorm-extra-clause-plugin v0.2.0
github.com/asecurityteam/rolling v2.0.4+incompatible
github.com/aws/aws-sdk-go-v2 v1.31.0
github.com/aws/aws-sdk-go-v2/config v1.27.36
github.com/aws/aws-sdk-go-v2/credentials v1.17.34
Expand Down Expand Up @@ -70,6 +69,7 @@ require (
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f
golang.org/x/oauth2 v0.23.0
golang.org/x/sync v0.10.0
gonum.org/v1/gonum v0.15.1
google.golang.org/api v0.198.0
google.golang.org/grpc v1.66.2
gopkg.in/yaml.v2 v2.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/asecurityteam/rolling v2.0.4+incompatible h1:WOSeokINZT0IDzYGc5BVcjLlR9vPol08RvI2GAsmB0s=
github.com/asecurityteam/rolling v2.0.4+incompatible/go.mod h1:2D4ba5ZfYCWrIMleUgTvc8pmLExEuvu3PDwl+vnG58Q=
github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU=
github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
github.com/aws/aws-sdk-go-v2 v1.31.0 h1:3V05LbxTSItI5kUqNwhJrrrY1BAXxXt0sN0l72QmG5U=
Expand Down Expand Up @@ -1139,6 +1137,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20240716161551-93cc26a95ae9 h1:LLhsEBxRTBLuKlQxFBYUOU8xyFgXv6cOTp2HASDlsDk=
golang.org/x/xerrors v0.0.0-20240716161551-93cc26a95ae9/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
gonum.org/v1/gonum v0.15.1 h1:FNy7N6OUZVUaWG9pTiD+jlhdQ3lMP+/LcTpJ6+a8sQ0=
gonum.org/v1/gonum v0.15.1/go.mod h1:eZTZuRFrzu5pcyjN5wJhcIhnUdNijYxX1T2IcrOGY0o=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg=
Expand Down
17 changes: 10 additions & 7 deletions query/check_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package query
import (
"fmt"
"net/url"
"slices"
"strings"
"time"

"github.com/asecurityteam/rolling"
"github.com/flanksource/commons/duration"
"github.com/flanksource/duty/context"
"github.com/flanksource/duty/types"
"github.com/samber/lo"
"gonum.org/v1/gonum/stat"
)

// Default search window
Expand Down Expand Up @@ -137,7 +138,7 @@ func (q CheckQueryParams) ExecuteDetails(ctx context.Context) ([]Timeseries, typ
end := q.GetEndTime().Format(time.RFC3339)

query := `
With grouped_by_window AS (
WITH grouped_by_window AS (
SELECT
duration,
status,
Expand Down Expand Up @@ -172,7 +173,7 @@ ORDER BY time
args = []any{start, end, q.Check}
}
uptime := types.Uptime{}
latencies := rolling.NewPointPolicy(rolling.NewWindow(100))
var latencies []float64

rows, err := ctx.Pool().Query(ctx, query, args...)
if err != nil {
Expand All @@ -189,15 +190,17 @@ ORDER BY time
}
uptime.Failed += datapoint.Failed
uptime.Passed += datapoint.Passed
latencies.Append(float64(datapoint.Duration))
latencies = append(latencies, float64(datapoint.Duration))
datapoint.Time = ts.Format(time.RFC3339)
results = append(results, datapoint)
}

// Sorting is required before calculating latencies else Quantile panics
slices.Sort(latencies)
latency := types.Latency{
Percentile99: latencies.Reduce(rolling.Percentile(99)),
Percentile97: latencies.Reduce(rolling.Percentile(97)),
Percentile95: latencies.Reduce(rolling.Percentile(95)),
Percentile99: stat.Quantile(0.99, stat.Empirical, latencies, nil),
Percentile97: stat.Quantile(0.97, stat.Empirical, latencies, nil),
Percentile95: stat.Quantile(0.95, stat.Empirical, latencies, nil),
}

return results, uptime, latency, nil
Expand Down
8 changes: 4 additions & 4 deletions tests/query_check_details_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ var _ = ginkgo.Describe("CheckDetails", ginkgo.Ordered, func() {
}

testData := []testRecord{
{since: "1w", statuses: 2, passed: 56, failed: 14, latency: types.Latency{Percentile99: 670, Percentile97: 0, Percentile95: 0}},
{since: "1d", statuses: 6, passed: 56, failed: 14, latency: types.Latency{Percentile99: 1305, Percentile97: 770, Percentile95: 205}},
{since: "1h", statuses: 61, passed: 48, failed: 13, latency: types.Latency{Percentile99: 1210, Percentile97: 1170, Percentile95: 1130}},
{since: "30m", statuses: 31, passed: 24, failed: 7, latency: types.Latency{Percentile99: 610, Percentile97: 570, Percentile95: 530}},
{since: "1w", statuses: 2, passed: 56, failed: 14, latency: types.Latency{Percentile99: 1020, Percentile97: 1020, Percentile95: 1020}},
{since: "1d", statuses: 6, passed: 56, failed: 14, latency: types.Latency{Percentile99: 1390, Percentile97: 1390, Percentile95: 1390}},
{since: "1h", statuses: 61, passed: 48, failed: 13, latency: types.Latency{Percentile99: 1220, Percentile97: 1200, Percentile95: 1160}},
{since: "30m", statuses: 31, passed: 24, failed: 7, latency: types.Latency{Percentile99: 620, Percentile97: 620, Percentile95: 600}},
}

refTime := time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
Expand Down

0 comments on commit 122523e

Please sign in to comment.