Skip to content

Commit

Permalink
feat(eventindexer): Track transition proved / contested event by tier…
Browse files Browse the repository at this point in the history
…s, generate chart data for each tier, update http API to be able to fetch data by tier (#15458)
  • Loading branch information
cyberhorsey authored Jan 6, 2024
1 parent 933b1f0 commit 227ac59
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 36 deletions.
1 change: 1 addition & 0 deletions packages/eventindexer/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ type ChartRepository interface {
start string,
end string,
feeTokenAddress string,
tier string,
) (*ChartResponse, error)
}
2 changes: 2 additions & 0 deletions packages/eventindexer/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Event struct {
ContractAddress string `json:"contractAddress"`
FeeTokenAddress string `json:"feeTokenAddress"`
TransactedAt time.Time `json:"transactedAt"`
Tier sql.NullInt16 `json:"tier"`
}

// SaveEventOpts
Expand All @@ -63,6 +64,7 @@ type SaveEventOpts struct {
ContractAddress *string
FeeTokenAddress *string
TransactedAt time.Time
Tier *uint16
}

type UniqueProversResponse struct {
Expand Down
235 changes: 226 additions & 9 deletions packages/eventindexer/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,216 @@ func (g *Generator) queryByTask(task string, date time.Time) error {
var err error

switch task {
case tasks.TotalTransitionProvedByTier:
var tiers []uint16 = make([]uint16, 0)

query := "SELECT DISTINCT(tier) FROM events WHERE name = ?"

err = g.db.GormDB().
Raw(query, eventindexer.EventNameTransitionProved).
Scan(&tiers).Error
if err != nil {
return err
}

slog.Info("tiers", "tiers", tiers)

for _, tier := range tiers {
t := tier

var dailyCountByTier decimal.NullDecimal

// nolint: lll
query := "SELECT COUNT(*) FROM events WHERE event = ? AND DATE(transacted_at) = ? AND tier = ?"
err = g.db.GormDB().
Raw(query, eventindexer.EventNameTransitionProved, dateString, t).
Scan(&dailyCountByTier).Error

if err != nil {
return err
}

tsdResult, err := g.previousDayTsdResultByTask(task, date, nil, &t)
if err != nil {
return err
}

result := tsdResult.Decimal.Add(dailyCountByTier.Decimal)

slog.Info("Query successful",
"task", task,
"date", dateString,
"result", result.String(),
"tier", t,
)

insertStmt := `
INSERT INTO time_series_data(task, value, date, tier)
VALUES (?, ?, ?, ?)`

err = g.db.GormDB().Exec(insertStmt, task, result, dateString, t).Error
if err != nil {
slog.Info("Insert failed", "task", task, "date", dateString, "error", err.Error())
return err
}
}

// return early for array processing data
return nil
case tasks.TransitionProvedByTierPerDay:
var tiers []uint16 = make([]uint16, 0)

query := "SELECT DISTINCT(tier) FROM events WHERE name = ?"

err = g.db.GormDB().
Raw(query, eventindexer.EventNameTransitionProved).
Scan(&tiers).Error
if err != nil {
return err
}

slog.Info("tiers", "tiers", tiers)

for _, tier := range tiers {
t := tier

var dailyCountByTier decimal.NullDecimal

// nolint: lll
query := "SELECT COUNT(*) FROM events WHERE event = ? AND DATE(transacted_at) = ? AND tier = ?"
err = g.db.GormDB().
Raw(query, eventindexer.EventNameTransitionProved, dateString, t).
Scan(&dailyCountByTier).Error

if err != nil {
return err
}

slog.Info("Query successful",
"task", task,
"date", dateString,
"result", result.String(),
"tier", t,
)

insertStmt := `
INSERT INTO time_series_data(task, value, date, tier)
VALUES (?, ?, ?, ?)`

err = g.db.GormDB().Exec(insertStmt, task, result, dateString, t).Error
if err != nil {
slog.Info("Insert failed", "task", task, "date", dateString, "error", err.Error())
return err
}
}

// return early for array processing data
return nil
case tasks.TransitionContestedByTierPerDay:
var tiers []uint16 = make([]uint16, 0)

query := "SELECT DISTINCT(tier) FROM events WHERE name = ?"

err = g.db.GormDB().
Raw(query, eventindexer.EventNameTransitionContested).
Scan(&tiers).Error
if err != nil {
return err
}

slog.Info("tiers", "tiers", tiers)

for _, tier := range tiers {
t := tier

var dailyCountByTier decimal.NullDecimal

// nolint: lll
query := "SELECT COUNT(*) FROM events WHERE event = ? AND DATE(transacted_at) = ? AND tier = ?"
err = g.db.GormDB().
Raw(query, eventindexer.EventNameTransitionContested, dateString, t).
Scan(&dailyCountByTier).Error

if err != nil {
return err
}

slog.Info("Query successful",
"task", task,
"date", dateString,
"result", result.String(),
"tier", t,
)

insertStmt := `
INSERT INTO time_series_data(task, value, date, tier)
VALUES (?, ?, ?, ?)`

err = g.db.GormDB().Exec(insertStmt, task, result, dateString, t).Error
if err != nil {
slog.Info("Insert failed", "task", task, "date", dateString, "error", err.Error())
return err
}
}

// return early for array processing data
return nil
case tasks.TotalTransitionContestedByTier:
var tiers []uint16 = make([]uint16, 0)

query := "SELECT DISTINCT(tier) FROM events WHERE name = ?"

err = g.db.GormDB().
Raw(query, eventindexer.EventNameTransitionContested).
Scan(&tiers).Error
if err != nil {
return err
}

slog.Info("tiers", "tiers", tiers)

for _, tier := range tiers {
t := tier

var dailyCountByTier decimal.NullDecimal

// nolint: lll
query := "SELECT COUNT(*) FROM events WHERE event = ? AND DATE(transacted_at) = ? AND tier = ?"
err = g.db.GormDB().
Raw(query, eventindexer.EventNameTransitionContested, dateString, t).
Scan(&dailyCountByTier).Error

if err != nil {
return err
}

tsdResult, err := g.previousDayTsdResultByTask(task, date, nil, &t)
if err != nil {
return err
}

result := tsdResult.Decimal.Add(dailyCountByTier.Decimal)

slog.Info("Query successful",
"task", task,
"date", dateString,
"result", result.String(),
"tier", t,
)

insertStmt := `
INSERT INTO time_series_data(task, value, date, tier)
VALUES (?, ?, ?, ?)`

err = g.db.GormDB().Exec(insertStmt, task, result, dateString, t).Error
if err != nil {
slog.Info("Insert failed", "task", task, "date", dateString, "error", err.Error())
return err
}
}

// return early for array processing data
return nil
case tasks.TotalProofRewards:
var feeTokenAddresses []string = make([]string, 0)
// get unique fee token addresses
Expand Down Expand Up @@ -222,7 +432,7 @@ func (g *Generator) queryByTask(task string, date time.Time) error {
return err
}

tsdResult, err := g.previousDayTsdResultByTask(task, date, &f)
tsdResult, err := g.previousDayTsdResultByTask(task, date, &f, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -307,7 +517,7 @@ func (g *Generator) queryByTask(task string, date time.Time) error {
return err
}

tsdResult, err := g.previousDayTsdResultByTask(task, date, nil)
tsdResult, err := g.previousDayTsdResultByTask(task, date, nil, nil)
if err != nil {
return err
}
Expand All @@ -323,7 +533,7 @@ func (g *Generator) queryByTask(task string, date time.Time) error {
return err
}

tsdResult, err := g.previousDayTsdResultByTask(task, date, nil)
tsdResult, err := g.previousDayTsdResultByTask(task, date, nil, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -378,7 +588,7 @@ func (g *Generator) queryByTask(task string, date time.Time) error {
return err
}

tsdResult, err := g.previousDayTsdResultByTask(task, date, nil)
tsdResult, err := g.previousDayTsdResultByTask(task, date, nil, nil)
if err != nil {
return err
}
Expand All @@ -403,7 +613,7 @@ func (g *Generator) queryByTask(task string, date time.Time) error {
return err
}

tsdResult, err := g.previousDayTsdResultByTask(task, date, nil)
tsdResult, err := g.previousDayTsdResultByTask(task, date, nil, nil)
if err != nil {
return err
}
Expand All @@ -422,7 +632,7 @@ func (g *Generator) queryByTask(task string, date time.Time) error {
return err
}

tsdResult, err := g.previousDayTsdResultByTask(task, date, nil)
tsdResult, err := g.previousDayTsdResultByTask(task, date, nil, nil)
if err != nil {
return err
}
Expand All @@ -441,7 +651,7 @@ func (g *Generator) queryByTask(task string, date time.Time) error {
return err
}

tsdResult, err := g.previousDayTsdResultByTask(task, date, nil)
tsdResult, err := g.previousDayTsdResultByTask(task, date, nil, nil)
if err != nil {
return err
}
Expand All @@ -462,7 +672,7 @@ func (g *Generator) queryByTask(task string, date time.Time) error {
return err
}

tsdResult, err := g.previousDayTsdResultByTask(task, date, nil)
tsdResult, err := g.previousDayTsdResultByTask(task, date, nil, nil)
if err != nil {
return err
}
Expand All @@ -482,7 +692,7 @@ func (g *Generator) queryByTask(task string, date time.Time) error {
return err
}

tsdResult, err := g.previousDayTsdResultByTask(task, date, nil)
tsdResult, err := g.previousDayTsdResultByTask(task, date, nil, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -517,6 +727,7 @@ func (g *Generator) previousDayTsdResultByTask(
task string,
date time.Time,
feeTokenAddress *string,
tier *uint16,
) (decimal.NullDecimal, error) {
var tsdResult decimal.NullDecimal

Expand All @@ -531,6 +742,12 @@ func (g *Generator) previousDayTsdResultByTask(
Raw(tsdQuery, task, date.AddDate(0, 0, -1).Format("2006-01-02"), *feeTokenAddress)
}

if tier != nil {
tsdQuery = `SELECT value FROM time_series_data WHERE task = ? AND date = ? AND tier = ?`
q = g.db.GormDB().
Raw(tsdQuery, task, date.AddDate(0, 0, -1).Format("2006-01-02"), *tier)
}

err := q.
Scan(&tsdResult).Error
if err != nil {
Expand Down
9 changes: 7 additions & 2 deletions packages/eventindexer/http/get_chart_by_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
// @Success 200 {object} eventindexer.ChartResponse
// @Router /chartByTask [get]
func (srv *Server) GetChartByTask(c echo.Context) error {
cached, found := srv.cache.Get(c.QueryParam("task") + c.QueryParam("fee_token_address"))
cached, found := srv.cache.Get(c.QueryParam("task") + c.QueryParam("fee_token_address") + c.QueryParam("tier"))

var chart *eventindexer.ChartResponse

Expand All @@ -36,12 +36,17 @@ func (srv *Server) GetChartByTask(c echo.Context) error {
c.QueryParam("start"),
c.QueryParam("end"),
c.QueryParam("fee_token_address"),
c.QueryParam("tier"),
)
if err != nil {
return webutils.LogAndRenderErrors(c, http.StatusUnprocessableEntity, err)
}

srv.cache.Set(c.QueryParam("task")+c.QueryParam("fee_token_address"), chart, cache.DefaultExpiration)
srv.cache.Set(
c.QueryParam("task")+c.QueryParam("fee_token_address")+c.QueryParam("tier"),
chart,
cache.DefaultExpiration,
)
}

return c.JSON(http.StatusOK, chart)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (indxr *Indexer) saveTransitionProvedEvent(
Address: event.Prover.Hex(),
BlockID: &blockID,
TransactedAt: time.Unix(int64(block.Time()), 0),
Tier: &event.Tier,
})
if err != nil {
return errors.Wrap(err, "indxr.eventRepo.Save")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- +goose Up
-- +goose StatementBegin
ALTER TABLE events
ADD COLUMN tier int DEFAULT NULL;

-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE tier;
-- +goose StatementEnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- +goose Up
-- +goose StatementBegin
ALTER TABLE time_series_data
ADD COLUMN tier int DEFAULT NULL;

-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE tier;
-- +goose StatementEnd
Loading

2 comments on commit 227ac59

@vercel
Copy link

@vercel vercel bot commented on 227ac59 Jan 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

bridge-ui-v2-internal – ./packages/bridge-ui-v2

bridge-ui-v2-internal-taikoxyz.vercel.app
bridge-ui-v2-internal.vercel.app
bridge-ui-v2-internal-git-alpha-6-taikoxyz.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 227ac59 Jan 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

bridge-ui-v2-a6 – ./packages/bridge-ui-v2

bridge-ui-v2-a6-taikoxyz.vercel.app
bridge-ui-v2-a6-git-alpha-6-taikoxyz.vercel.app
bridge-ui-v2-a6.vercel.app
bridge.katla.taiko.xyz

Please sign in to comment.