Skip to content

Commit

Permalink
fix query from last-tx endpoint (#311)
Browse files Browse the repository at this point in the history
Co-authored-by: ftocal <fert1335@gmail.com>
  • Loading branch information
walker-16 and ftocal committed May 12, 2023
1 parent eb414c9 commit 25a675f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 28 deletions.
40 changes: 29 additions & 11 deletions api/handlers/transactions/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
51 changes: 34 additions & 17 deletions api/handlers/transactions/queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,51 @@ 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
tm: time.Date(2023, 5, 4, 12, 25, 48, 112233445, time.UTC),
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")
|> range(start: 2023-05-04T18:00:00Z)
|> 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)
Expand All @@ -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)
}

0 comments on commit 25a675f

Please sign in to comment.