Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework GET /api/v1/last-txs #629

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions analytics/metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ func (m *Metric) vaaCountMeasurement(ctx context.Context, vaa *sdk.VAA) error {
}
m.metrics.IncSuccessfulMeasurement(vaaCountMeasurement)

m.logger.Info("generated a data point for the vaa count metric")

return nil
}

Expand Down
6 changes: 4 additions & 2 deletions analytics/scripts/vaa_count_1h.flux
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ option task = {

start = date.truncate(t: -1h, unit: 1h)
stop = date.truncate(t: now(), unit: 1h)
sourceBucket = "wormscan-30days"
destinationBucket = "wormscan"

from(bucket: "wormscan-30days")
from(bucket: sourceBucket)
|> range(start: start, stop: stop)
|> filter(fn: (r) => r["_measurement"] == "vaa_count")
|> group()
|> aggregateWindow(every: 1h, fn: count, createEmpty: true)
|> set(key: "_measurement", value: "vaa_count_1h")
|> to(bucket: "wormscan-30days", fieldFn: (r) => ({"count": r._value}))
|> to(bucket: destinationBucket, fieldFn: (r) => ({"count": r._value}))
16 changes: 13 additions & 3 deletions api/handlers/transactions/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ aggregatesVaaCount = from(bucket: "%s")
|> filter(fn: (r) => r["_measurement"] == "vaa_count_1h")
union(tables: [aggregatesVaaCount, lastVaaCount])
|> group()
|> aggregateWindow(every: 1h, fn: sum, createEmpty: true)
|> sort(columns: ["_time"], desc: true)
`

Expand All @@ -36,12 +37,18 @@ union(tables: [aggregatesVaaCount, lastVaaCount])
|> sort(columns: ["_time"], desc: true)
`

func buildLastTrxQuery(bucket string, tm time.Time, q *TransactionCountQuery) string {
func buildLastTrxQuery(
dataPointsBucket string,
aggregationsBucket string,
tm time.Time,
q *TransactionCountQuery,
) string {

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(queryTemplateVaaCount1d1h, dataPointsBucket, startLastVaa, q.SampleRate, aggregationsBucket, startAggregatesVaa)
}
return fmt.Sprintf(queryTemplateVaaCount, bucket, startLastVaa, bucket, startAggregatesVaa)
return fmt.Sprintf(queryTemplateVaaCount, dataPointsBucket, startLastVaa, aggregationsBucket, startAggregatesVaa)
}

func createRangeQuery(t time.Time, timeSpan string) (string, string) {
Expand All @@ -57,6 +64,9 @@ func createRangeQuery(t time.Time, timeSpan string) (string, string) {
case "1mo":
startLastVaa = t.Truncate(time.Hour * 24)
startAggregatesVaa = startLastVaa.Add(-time.Hour * 24 * 30)
case "3mo":
startLastVaa = t.Truncate(time.Hour * 24)
startAggregatesVaa = startLastVaa.Add(-time.Hour * 24 * 90)
default:
startLastVaa = t.Truncate(time.Hour * 1)
startAggregatesVaa = startLastVaa.Add(-time.Hour * 24)
Expand Down
9 changes: 5 additions & 4 deletions api/handlers/transactions/queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,17 @@ 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")
aggregatesVaaCount = from(bucket: "wormscan")
|> range(start: 2023-05-03T18:00:00Z)
|> filter(fn: (r) => r["_measurement"] == "vaa_count_1h")
union(tables: [aggregatesVaaCount, lastVaaCount])
|> group()
|> aggregateWindow(every: 1h, 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: "1d", SampleRate: "1h"})
actual := buildLastTrxQuery("wormscan-1month", "wormscan", tm, &TransactionCountQuery{TimeSpan: "1d", SampleRate: "1h"})
assert.Equal(t, expected, actual)
}

Expand All @@ -71,7 +72,7 @@ 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")
aggregatesVaaCount = from(bucket: "wormscan")
|> range(start: 2023-04-27T00:00:00Z)
|> filter(fn: (r) => r["_measurement"] == "vaa_count_1h")
|> aggregateWindow(every: 1h, fn: sum, createEmpty: true)
Expand All @@ -82,7 +83,7 @@ union(tables: [aggregatesVaaCount, lastVaaCount])
`
//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"})
actual := buildLastTrxQuery("wormscan-1month", "wormscan", tm, &TransactionCountQuery{TimeSpan: "1w", SampleRate: "1d"})
assert.Equal(t, expected, actual)
}

Expand Down
6 changes: 4 additions & 2 deletions api/handlers/transactions/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,14 +623,16 @@ func (r *Repository) getVolume24h(ctx context.Context) (string, error) {

// GetTransactionCount get the last transactions.
func (r *Repository) GetTransactionCount(ctx context.Context, q *TransactionCountQuery) ([]TransactionCountResult, error) {
query := buildLastTrxQuery(r.bucket30DaysRetention, time.Now(), q)

query := buildLastTrxQuery(r.bucket30DaysRetention, r.bucketInfiniteRetention, time.Now(), q)
result, err := r.queryAPI.Query(ctx, query)
if err != nil {
return nil, err
}
if result.Err() != nil {
return nil, result.Err()
}

response := []TransactionCountResult{}
for result.Next() {
var row TransactionCountResult
Expand All @@ -644,7 +646,7 @@ func (r *Repository) GetTransactionCount(ctx context.Context, q *TransactionCoun
// https://github.com/wormhole-foundation/wormhole-explorer/issues/406
for i := range response {
if i > 0 {
if q.TimeSpan == "1w" || q.TimeSpan == "1mo" {
if q.TimeSpan == "1w" || q.TimeSpan == "1mo" || q.TimeSpan == "3mo" {
response[i].Time = response[i].Time.AddDate(0, 0, -1)
} else if q.TimeSpan == "1d" {
response[i].Time = response[i].Time.Add(-1 * time.Hour)
Expand Down
8 changes: 7 additions & 1 deletion api/middleware/extract_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func ExtractTimeSpan(c *fiber.Ctx, l *zap.Logger) (string, error) {

// isValidTimeSpan check that the timeSpan is valid.
func isValidTimeSpan(timeSpan string) bool {
return regexp.MustCompile(`^1d$|^1w$|^1mo$`).MatchString(timeSpan)
return regexp.MustCompile(`^1d$|^1w$|^1mo$|^3mo$`).MatchString(timeSpan)
}

func ExtractSampleRate(c *fiber.Ctx, l *zap.Logger) (string, error) {
Expand All @@ -297,10 +297,12 @@ func isValidSampleRate(sampleRate string) bool {
}

func ExtractTimeSpanAndSampleRate(c *fiber.Ctx, l *zap.Logger) (string, string, error) {

timeSpan, err := ExtractTimeSpan(c, l)
if err != nil {
return "", "", err
}

sampleRate, err := ExtractSampleRate(c, l)
if err != nil {
return "", "", err
Expand All @@ -319,6 +321,10 @@ func ExtractTimeSpanAndSampleRate(c *fiber.Ctx, l *zap.Logger) (string, string,
if sampleRate != "1d" {
return "", "", response.NewInvalidQueryParamError(c, "INVALID CONFIGURATION <timeSpan>, <sampleRate> QUERY PARAMETERS", nil)
}
case "3mo":
if sampleRate != "1d" {
return "", "", response.NewInvalidQueryParamError(c, "INVALID CONFIGURATION <timeSpan>, <sampleRate> QUERY PARAMETERS", nil)
}
}

return timeSpan, sampleRate, nil
Expand Down
1 change: 1 addition & 0 deletions api/routes/wormscan/transactions/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewController(transactionsService *transactions.Service, logger *zap.Logger
// @Failure 500
// @Router /api/v1/last-txs [get]
func (c *Controller) GetLastTransactions(ctx *fiber.Ctx) error {

timeSpan, sampleRate, err := middleware.ExtractTimeSpanAndSampleRate(ctx, c.logger)
if err != nil {
return err
Expand Down
Loading