fix query from last-tx endpoint (#311)
Co-authored-by: ftocal <fert1335@gmail.com>
This commit is contained in:
parent
eb414c9f65
commit
25a675f99a
|
@ -5,44 +5,62 @@ import (
|
||||||
"time"
|
"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")
|
lastVaaCount = from(bucket: "%s")
|
||||||
|> range(start: %s)
|
|> range(start: %s)
|
||||||
|> filter(fn: (r) => r["_measurement"] == "vaa_count")
|
|> filter(fn: (r) => r["_measurement"] == "vaa_count")
|
||||||
|> group()
|
|> group()
|
||||||
|> aggregateWindow(every: %s, fn: count, createEmpty: true)
|
|> aggregateWindow(every: %s, fn: count, createEmpty: true)
|
||||||
|
|
||||||
aggregatesVaaCount = from(bucket: "%s")
|
aggregatesVaaCount = from(bucket: "%s")
|
||||||
|> range(start: %s , stop: %s)
|
|> range(start: %s)
|
||||||
|> filter(fn: (r) => r["_measurement"] == "vaa_count_1h")
|
|> filter(fn: (r) => r["_measurement"] == "vaa_count_1h")
|
||||||
|> aggregateWindow(every: %s, fn: count, createEmpty: true)
|
|
||||||
|
|
||||||
union(tables: [aggregatesVaaCount, lastVaaCount])
|
union(tables: [aggregatesVaaCount, lastVaaCount])
|
||||||
|> group()
|
|> group()
|
||||||
|> sort(columns: ["_time"], desc: true)
|
|> 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 {
|
func buildLastTrxQuery(bucket string, tm time.Time, q *TransactionCountQuery) string {
|
||||||
startLastVaa, startAggregatesVaa, stopAggregatesVaa := createRangeQuery(tm, q.TimeSpan)
|
startLastVaa, startAggregatesVaa := createRangeQuery(tm, q.TimeSpan)
|
||||||
return fmt.Sprintf(queryTemplateVaaCount, bucket, startLastVaa, q.SampleRate, bucket, startAggregatesVaa, stopAggregatesVaa, q.SampleRate)
|
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
|
const format = time.RFC3339Nano
|
||||||
|
|
||||||
startLastVaa := t.Truncate(time.Hour * 1)
|
var startLastVaa, startAggregatesVaa time.Time
|
||||||
stopAggregatesVaa := startLastVaa.Add(time.Nanosecond * 1)
|
|
||||||
var startAggregatesVaa time.Time
|
|
||||||
|
|
||||||
switch timeSpan {
|
switch timeSpan {
|
||||||
case "1w":
|
case "1w":
|
||||||
|
startLastVaa = t.Truncate(time.Hour * 24)
|
||||||
startAggregatesVaa = startLastVaa.Add(-time.Hour * 24 * 7)
|
startAggregatesVaa = startLastVaa.Add(-time.Hour * 24 * 7)
|
||||||
case "1mo":
|
case "1mo":
|
||||||
|
startLastVaa = t.Truncate(time.Hour * 24)
|
||||||
startAggregatesVaa = startLastVaa.Add(-time.Hour * 24 * 30)
|
startAggregatesVaa = startLastVaa.Add(-time.Hour * 24 * 30)
|
||||||
default:
|
default:
|
||||||
|
startLastVaa = t.Truncate(time.Hour * 1)
|
||||||
startAggregatesVaa = startLastVaa.Add(-time.Hour * 24)
|
startAggregatesVaa = startLastVaa.Add(-time.Hour * 24)
|
||||||
}
|
}
|
||||||
|
|
||||||
return startLastVaa.Format(format), startAggregatesVaa.Format(format), stopAggregatesVaa.Format(format)
|
return startLastVaa.Format(format), startAggregatesVaa.Format(format)
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,9 +10,9 @@ import (
|
||||||
|
|
||||||
func TestQueries_createRangeQuery(t *testing.T) {
|
func TestQueries_createRangeQuery(t *testing.T) {
|
||||||
var tests = []struct {
|
var tests = []struct {
|
||||||
tm time.Time
|
tm time.Time
|
||||||
ts string
|
ts string
|
||||||
wantStartLastVaa, wantStartAggregatesVaa, wantStopAggregatesVaa string
|
wantStartLastVaa, wantStartAggregatesVaa string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
//2023-05-04T12:25:48.112233445Z
|
//2023-05-04T12:25:48.112233445Z
|
||||||
|
@ -20,35 +20,31 @@ func TestQueries_createRangeQuery(t *testing.T) {
|
||||||
ts: "1d",
|
ts: "1d",
|
||||||
wantStartLastVaa: "2023-05-04T12:00:00Z",
|
wantStartLastVaa: "2023-05-04T12:00:00Z",
|
||||||
wantStartAggregatesVaa: "2023-05-03T12:00:00Z",
|
wantStartAggregatesVaa: "2023-05-03T12:00:00Z",
|
||||||
wantStopAggregatesVaa: "2023-05-04T12:00:00.000000001Z",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
//2023-05-04T20:59:17.992233445Z
|
//2023-05-04T20:59:17.992233445Z
|
||||||
tm: time.Date(2023, 5, 4, 20, 59, 17, 992233445, time.UTC),
|
tm: time.Date(2023, 5, 4, 20, 59, 17, 992233445, time.UTC),
|
||||||
ts: "1w",
|
ts: "1w",
|
||||||
wantStartLastVaa: "2023-05-04T20:00:00Z",
|
wantStartLastVaa: "2023-05-04T00:00:00Z",
|
||||||
wantStartAggregatesVaa: "2023-04-27T20:00:00Z",
|
wantStartAggregatesVaa: "2023-04-27T00:00:00Z",
|
||||||
wantStopAggregatesVaa: "2023-05-04T20:00:00.000000001Z",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
//2023-05-04T17:09:33.987654321Z
|
//2023-05-04T17:09:33.987654321Z
|
||||||
tm: time.Date(2023, 5, 4, 17, 9, 33, 987654321, time.UTC),
|
tm: time.Date(2023, 5, 4, 17, 9, 33, 987654321, time.UTC),
|
||||||
ts: "1mo",
|
ts: "1mo",
|
||||||
wantStartLastVaa: "2023-05-04T17:00:00Z",
|
wantStartLastVaa: "2023-05-04T00:00:00Z",
|
||||||
wantStartAggregatesVaa: "2023-04-04T17:00:00Z",
|
wantStartAggregatesVaa: "2023-04-04T00:00:00Z",
|
||||||
wantStopAggregatesVaa: "2023-05-04T17:00:00.000000001Z",
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
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.wantStartLastVaa, startLastVaa)
|
||||||
assert.Equal(t, tt.wantStartAggregatesVaa, startAggregatesVaa)
|
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 := `
|
expected := `
|
||||||
lastVaaCount = from(bucket: "wormscan-1month")
|
lastVaaCount = from(bucket: "wormscan-1month")
|
||||||
|
@ -56,12 +52,9 @@ lastVaaCount = from(bucket: "wormscan-1month")
|
||||||
|> filter(fn: (r) => r["_measurement"] == "vaa_count")
|
|> filter(fn: (r) => r["_measurement"] == "vaa_count")
|
||||||
|> group()
|
|> group()
|
||||||
|> aggregateWindow(every: 1h, fn: count, createEmpty: true)
|
|> aggregateWindow(every: 1h, fn: count, createEmpty: true)
|
||||||
|
|
||||||
aggregatesVaaCount = from(bucket: "wormscan-1month")
|
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")
|
|> filter(fn: (r) => r["_measurement"] == "vaa_count_1h")
|
||||||
|> aggregateWindow(every: 1h, fn: count, createEmpty: true)
|
|
||||||
|
|
||||||
union(tables: [aggregatesVaaCount, lastVaaCount])
|
union(tables: [aggregatesVaaCount, lastVaaCount])
|
||||||
|> group()
|
|> group()
|
||||||
|> sort(columns: ["_time"], desc: true)
|
|> sort(columns: ["_time"], desc: true)
|
||||||
|
@ -73,3 +66,27 @@ union(tables: [aggregatesVaaCount, lastVaaCount])
|
||||||
fmt.Println(actual)
|
fmt.Println(actual)
|
||||||
assert.Equal(t, expected, 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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue