diff --git a/api/handlers/transactions/queries.go b/api/handlers/transactions/queries.go index 1721c5c8..060c98e8 100644 --- a/api/handlers/transactions/queries.go +++ b/api/handlers/transactions/queries.go @@ -5,44 +5,62 @@ import ( "time" ) -const queryTemplateVaaCount = ` +// queryTemplateVaaCount1d1h is the query used to get the last VAA count and the aggregated VAA count for the last 24 hours by hour. +const queryTemplateVaaCount1d1h = ` lastVaaCount = from(bucket: "%s") |> range(start: %s) |> filter(fn: (r) => r["_measurement"] == "vaa_count") |> group() |> aggregateWindow(every: %s, fn: count, createEmpty: true) - aggregatesVaaCount = from(bucket: "%s") - |> range(start: %s , stop: %s) + |> range(start: %s) |> filter(fn: (r) => r["_measurement"] == "vaa_count_1h") - |> aggregateWindow(every: %s, fn: count, createEmpty: true) - union(tables: [aggregatesVaaCount, lastVaaCount]) |> group() |> sort(columns: ["_time"], desc: true) ` +// queryTemplateVaaCount1d1h is the query used to get the last VAA count and the aggregated VAA count for 1 week or month by day. +const queryTemplateVaaCount = ` +lastVaaCount = from(bucket: "%s") + |> range(start: %s) + |> filter(fn: (r) => r["_measurement"] == "vaa_count") + |> group() +aggregatesVaaCount = from(bucket: "%s") + |> range(start: %s) + |> filter(fn: (r) => r["_measurement"] == "vaa_count_1h") + |> aggregateWindow(every: 1h, fn: sum, createEmpty: true) +union(tables: [aggregatesVaaCount, lastVaaCount]) + |> group() + |> aggregateWindow(every: 1d, fn: sum, createEmpty: true) + |> sort(columns: ["_time"], desc: true) +` + func buildLastTrxQuery(bucket string, tm time.Time, q *TransactionCountQuery) string { - startLastVaa, startAggregatesVaa, stopAggregatesVaa := createRangeQuery(tm, q.TimeSpan) - return fmt.Sprintf(queryTemplateVaaCount, bucket, startLastVaa, q.SampleRate, bucket, startAggregatesVaa, stopAggregatesVaa, q.SampleRate) + startLastVaa, startAggregatesVaa := createRangeQuery(tm, q.TimeSpan) + if q.TimeSpan == "1d" && q.SampleRate == "1h" { + return fmt.Sprintf(queryTemplateVaaCount1d1h, bucket, startLastVaa, q.SampleRate, bucket, startAggregatesVaa) + } + return fmt.Sprintf(queryTemplateVaaCount, bucket, startLastVaa, bucket, startAggregatesVaa) } -func createRangeQuery(t time.Time, timeSpan string) (string, string, string) { +func createRangeQuery(t time.Time, timeSpan string) (string, string) { const format = time.RFC3339Nano - startLastVaa := t.Truncate(time.Hour * 1) - stopAggregatesVaa := startLastVaa.Add(time.Nanosecond * 1) - var startAggregatesVaa time.Time + var startLastVaa, startAggregatesVaa time.Time switch timeSpan { case "1w": + startLastVaa = t.Truncate(time.Hour * 24) startAggregatesVaa = startLastVaa.Add(-time.Hour * 24 * 7) case "1mo": + startLastVaa = t.Truncate(time.Hour * 24) startAggregatesVaa = startLastVaa.Add(-time.Hour * 24 * 30) default: + startLastVaa = t.Truncate(time.Hour * 1) startAggregatesVaa = startLastVaa.Add(-time.Hour * 24) } - return startLastVaa.Format(format), startAggregatesVaa.Format(format), stopAggregatesVaa.Format(format) + return startLastVaa.Format(format), startAggregatesVaa.Format(format) } diff --git a/api/handlers/transactions/queries_test.go b/api/handlers/transactions/queries_test.go index af0e4231..e3b6b485 100644 --- a/api/handlers/transactions/queries_test.go +++ b/api/handlers/transactions/queries_test.go @@ -10,9 +10,9 @@ import ( func TestQueries_createRangeQuery(t *testing.T) { var tests = []struct { - tm time.Time - ts string - wantStartLastVaa, wantStartAggregatesVaa, wantStopAggregatesVaa string + tm time.Time + ts string + wantStartLastVaa, wantStartAggregatesVaa string }{ { //2023-05-04T12:25:48.112233445Z @@ -20,35 +20,31 @@ func TestQueries_createRangeQuery(t *testing.T) { ts: "1d", wantStartLastVaa: "2023-05-04T12:00:00Z", wantStartAggregatesVaa: "2023-05-03T12:00:00Z", - wantStopAggregatesVaa: "2023-05-04T12:00:00.000000001Z", }, { //2023-05-04T20:59:17.992233445Z tm: time.Date(2023, 5, 4, 20, 59, 17, 992233445, time.UTC), ts: "1w", - wantStartLastVaa: "2023-05-04T20:00:00Z", - wantStartAggregatesVaa: "2023-04-27T20:00:00Z", - wantStopAggregatesVaa: "2023-05-04T20:00:00.000000001Z", + wantStartLastVaa: "2023-05-04T00:00:00Z", + wantStartAggregatesVaa: "2023-04-27T00:00:00Z", }, { //2023-05-04T17:09:33.987654321Z tm: time.Date(2023, 5, 4, 17, 9, 33, 987654321, time.UTC), ts: "1mo", - wantStartLastVaa: "2023-05-04T17:00:00Z", - wantStartAggregatesVaa: "2023-04-04T17:00:00Z", - wantStopAggregatesVaa: "2023-05-04T17:00:00.000000001Z", + wantStartLastVaa: "2023-05-04T00:00:00Z", + wantStartAggregatesVaa: "2023-04-04T00:00:00Z", }, } for _, tt := range tests { - startLastVaa, startAggregatesVaa, stopAggregatesVaa := createRangeQuery(tt.tm, tt.ts) + startLastVaa, startAggregatesVaa := createRangeQuery(tt.tm, tt.ts) assert.Equal(t, tt.wantStartLastVaa, startLastVaa) assert.Equal(t, tt.wantStartAggregatesVaa, startAggregatesVaa) - assert.Equal(t, tt.wantStopAggregatesVaa, stopAggregatesVaa) } } -func TestQueries_buildLastTrxQuery(t *testing.T) { +func TestQueries_buildLastTrxQuery1d1h(t *testing.T) { expected := ` lastVaaCount = from(bucket: "wormscan-1month") @@ -56,12 +52,9 @@ lastVaaCount = from(bucket: "wormscan-1month") |> filter(fn: (r) => r["_measurement"] == "vaa_count") |> group() |> aggregateWindow(every: 1h, fn: count, createEmpty: true) - aggregatesVaaCount = from(bucket: "wormscan-1month") - |> range(start: 2023-05-03T18:00:00Z , stop: 2023-05-04T18:00:00.000000001Z) + |> range(start: 2023-05-03T18:00:00Z) |> filter(fn: (r) => r["_measurement"] == "vaa_count_1h") - |> aggregateWindow(every: 1h, fn: count, createEmpty: true) - union(tables: [aggregatesVaaCount, lastVaaCount]) |> group() |> sort(columns: ["_time"], desc: true) @@ -73,3 +66,27 @@ union(tables: [aggregatesVaaCount, lastVaaCount]) fmt.Println(actual) assert.Equal(t, expected, actual) } + +func TestQueries_buildLastTrxQuery1w1d(t *testing.T) { + + expected := ` +lastVaaCount = from(bucket: "wormscan-1month") + |> range(start: 2023-05-04T00:00:00Z) + |> filter(fn: (r) => r["_measurement"] == "vaa_count") + |> group() +aggregatesVaaCount = from(bucket: "wormscan-1month") + |> range(start: 2023-04-27T00:00:00Z) + |> filter(fn: (r) => r["_measurement"] == "vaa_count_1h") + |> aggregateWindow(every: 1h, fn: sum, createEmpty: true) +union(tables: [aggregatesVaaCount, lastVaaCount]) + |> group() + |> aggregateWindow(every: 1d, fn: sum, createEmpty: true) + |> sort(columns: ["_time"], desc: true) +` + //2023-05-04T18:39:10.985Z + tm := time.Date(2023, 5, 4, 18, 39, 10, 985, time.UTC) + actual := buildLastTrxQuery("wormscan-1month", tm, &TransactionCountQuery{TimeSpan: "1w", SampleRate: "1d"}) + fmt.Println(actual) + fmt.Println(actual) + assert.Equal(t, expected, actual) +}