2023-05-10 14:18:32 -07:00
|
|
|
package transactions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-05-12 06:54:29 -07:00
|
|
|
// 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 = `
|
2023-05-10 14:18:32 -07:00
|
|
|
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")
|
2023-05-12 06:54:29 -07:00
|
|
|
|> range(start: %s)
|
2023-05-10 14:18:32 -07:00
|
|
|
|> filter(fn: (r) => r["_measurement"] == "vaa_count_1h")
|
2023-05-12 06:54:29 -07:00
|
|
|
union(tables: [aggregatesVaaCount, lastVaaCount])
|
|
|
|
|> group()
|
|
|
|
|> sort(columns: ["_time"], desc: true)
|
|
|
|
`
|
2023-05-10 14:18:32 -07:00
|
|
|
|
2023-05-12 06:54:29 -07:00
|
|
|
// 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)
|
2023-05-10 14:18:32 -07:00
|
|
|
union(tables: [aggregatesVaaCount, lastVaaCount])
|
|
|
|
|> group()
|
2023-05-12 06:54:29 -07:00
|
|
|
|> aggregateWindow(every: 1d, fn: sum, createEmpty: true)
|
2023-05-10 14:18:32 -07:00
|
|
|
|> sort(columns: ["_time"], desc: true)
|
|
|
|
`
|
|
|
|
|
|
|
|
func buildLastTrxQuery(bucket string, tm time.Time, q *TransactionCountQuery) string {
|
2023-05-12 06:54:29 -07:00
|
|
|
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)
|
2023-05-10 14:18:32 -07:00
|
|
|
}
|
|
|
|
|
2023-05-12 06:54:29 -07:00
|
|
|
func createRangeQuery(t time.Time, timeSpan string) (string, string) {
|
2023-05-10 14:18:32 -07:00
|
|
|
|
|
|
|
const format = time.RFC3339Nano
|
|
|
|
|
2023-05-12 06:54:29 -07:00
|
|
|
var startLastVaa, startAggregatesVaa time.Time
|
2023-05-10 14:18:32 -07:00
|
|
|
|
|
|
|
switch timeSpan {
|
|
|
|
case "1w":
|
2023-05-12 06:54:29 -07:00
|
|
|
startLastVaa = t.Truncate(time.Hour * 24)
|
2023-05-10 14:18:32 -07:00
|
|
|
startAggregatesVaa = startLastVaa.Add(-time.Hour * 24 * 7)
|
|
|
|
case "1mo":
|
2023-05-12 06:54:29 -07:00
|
|
|
startLastVaa = t.Truncate(time.Hour * 24)
|
2023-05-10 14:18:32 -07:00
|
|
|
startAggregatesVaa = startLastVaa.Add(-time.Hour * 24 * 30)
|
|
|
|
default:
|
2023-05-12 06:54:29 -07:00
|
|
|
startLastVaa = t.Truncate(time.Hour * 1)
|
2023-05-10 14:18:32 -07:00
|
|
|
startAggregatesVaa = startLastVaa.Add(-time.Hour * 24)
|
|
|
|
}
|
|
|
|
|
2023-05-12 06:54:29 -07:00
|
|
|
return startLastVaa.Format(format), startAggregatesVaa.Format(format)
|
2023-05-10 14:18:32 -07:00
|
|
|
}
|
2023-05-15 11:15:12 -07:00
|
|
|
|
|
|
|
const queryTemplateTotalTrxCount = `
|
|
|
|
current = from(bucket: "%s")
|
|
|
|
|> range(start: %s)
|
|
|
|
|> filter(fn: (r) => r["_measurement"] == "vaa_volume")
|
|
|
|
|> filter(fn: (r) => r["_field"] == "volume")
|
|
|
|
|> group()
|
|
|
|
|> count()
|
|
|
|
last = from(bucket: "%s")
|
|
|
|
|> range(start: -1mo)
|
|
|
|
|> filter(fn: (r) => r["_measurement"] == "total_tx_count")
|
|
|
|
|> last()
|
|
|
|
union(tables: [current, last])
|
|
|
|
|> group()
|
|
|
|
|> sum()
|
|
|
|
`
|
|
|
|
|
|
|
|
func buildTotalTrxCountQuery(bucketForever, bucket30Days string, t time.Time) string {
|
|
|
|
start := t.Truncate(time.Hour * 24).Format(time.RFC3339Nano)
|
|
|
|
return fmt.Sprintf(queryTemplateTotalTrxCount, bucketForever, start, bucket30Days)
|
|
|
|
}
|
|
|
|
|
|
|
|
const queryTemplateTotalTrxVolume = `
|
|
|
|
current = from(bucket: "%s")
|
|
|
|
|> range(start: %s)
|
|
|
|
|> filter(fn: (r) => r["_measurement"] == "vaa_volume")
|
|
|
|
|> filter(fn: (r) => r["_field"] == "volume")
|
|
|
|
|> group()
|
|
|
|
|> sum()
|
|
|
|
last = from(bucket: "%s")
|
|
|
|
|> range(start: -1mo)
|
|
|
|
|> filter(fn: (r) => r["_measurement"] == "total_tx_volume")
|
|
|
|
|> last()
|
|
|
|
union(tables: [current, last])
|
|
|
|
|> group()
|
|
|
|
|> sum()
|
|
|
|
`
|
|
|
|
|
|
|
|
func buildTotalTrxVolumeQuery(bucketForever, bucket30Days string, t time.Time) string {
|
|
|
|
start := t.Truncate(time.Hour * 24).Format(time.RFC3339Nano)
|
|
|
|
return fmt.Sprintf(queryTemplateTotalTrxVolume, bucketForever, start, bucket30Days)
|
|
|
|
}
|