Skip to content

Commit

Permalink
test: api to look up extradata
Browse files Browse the repository at this point in the history
  • Loading branch information
metachris committed Jul 30, 2024
1 parent 377a99c commit 70c9b91
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 5 deletions.
19 changes: 19 additions & 0 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,22 @@ func (s *DatabaseService) GetLastDailyBuilderStatsEntry(filterType string) (*Bui
err := s.DB.Get(entry, query, filterType)
return entry, err
}

func (s *DatabaseService) GetRecentPayloadsForExtraData(extraData []string, limit int) (resp []*TmpPayloadsForExtraDataEntry, err error) {
query := `
SELECT
DISTINCT ON (slot) slot, extra_data, inserted_at, block_timestamp
FROM ` + vars.TableDataAPIPayloadDelivered + `
wHERE extra_data IN (?)
ORDER BY slot DESC
LIMIT ?;`

// See also https://jmoiron.github.io/sqlx/#inQueries
query, args, err := sqlx.In(query, extraData, limit)
if err != nil {
return nil, err
}
query = s.DB.Rebind(query)
err = s.DB.Select(&resp, query, args...)
return resp, err
}
16 changes: 11 additions & 5 deletions database/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"time"
)

var (
BuilderStatsEntryTypeExtraData = "extra_data"
BuilderStatsEntryTypeBuilderPubkey = "builder_pubkey"
)

func NewNullBool(b bool) sql.NullBool {
return sql.NullBool{
Bool: b,
Expand Down Expand Up @@ -184,8 +189,9 @@ type BuilderStatsEntry struct {
BuilderPubkeys string `db:"builder_pubkeys" json:"builder_pubkeys"`
BlocksIncluded int `db:"blocks_included" json:"blocks_included"`
}

var (
BuilderStatsEntryTypeExtraData = "extra_data"
BuilderStatsEntryTypeBuilderPubkey = "builder_pubkey"
)
type TmpPayloadsForExtraDataEntry struct {
Slot uint64 `db:"slot"`
ExtraData string `db:"extra_data"`
InsertedAt time.Time `db:"inserted_at"`
BlockTimestamp sql.NullTime `db:"block_timestamp"`
}
63 changes: 63 additions & 0 deletions services/website/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
_ "net/http/pprof"
"os"
"strconv"
"strings"
"sync"
"text/template"
Expand Down Expand Up @@ -141,6 +142,7 @@ func (srv *Webserver) getRouter() http.Handler {
r.HandleFunc("/stats/cowstats", srv.handleCowstatsJSON).Methods(http.MethodGet)
r.HandleFunc("/stats/day/{day:[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}}", srv.handleDailyStats).Methods(http.MethodGet)
r.HandleFunc("/stats/day/{day:[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}}/json", srv.handleDailyStatsJSON).Methods(http.MethodGet)
r.HandleFunc("/stats/_test/extradata-payloads", srv.handleExtraDataPayloads).Methods(http.MethodGet)

r.HandleFunc("/livez", srv.handleLivenessCheck)
r.HandleFunc("/healthz", srv.handleHealthCheck)
Expand Down Expand Up @@ -408,6 +410,67 @@ func (srv *Webserver) handleHealthCheck(w http.ResponseWriter, r *http.Request)
srv.RespondOK(w, resp)
}

// Return recent payloads for a given extra data string
func (srv *Webserver) handleExtraDataPayloads(w http.ResponseWriter, r *http.Request) {
type apiRespEntry struct {
Slot uint64 `json:"slot"`
SlotTime string `json:"slot_time"`
ExtraData string `json:"extra_data"`
PrevSlot uint64 `json:"prev_slot"`
MinSincePrevSlot uint64 `json:"min_since_prev_slot"`
}

extraData := r.URL.Query().Get("extra_data")
extraDataSlice := []string{extraData}
if extraData == "fb" {
extraDataSlice = []string{"Illuminate Dmocratize Dstribute", "Illuminate Dmocrtz Dstrib Prtct"}
}

limit := 20
limitArg := r.URL.Query().Get("limit")
if limitArg != "" {
l, err := strconv.Atoi(limitArg)
if err != nil {
srv.RespondError(w, http.StatusBadRequest, "invalid limit")
return
}
limit = l
}

payloads, err := srv.db.GetRecentPayloadsForExtraData(extraDataSlice, limit+1)
if err != nil {
srv.log.WithError(err).Error("error getting payloads")
srv.RespondError(w, http.StatusInternalServerError, "error getting payloads")
return
}

entries := make([]apiRespEntry, len(payloads))
var prevSlot uint64
var prevSlotTime time.Time

// iterate over payloads in reverse
for i := len(payloads) - 1; i >= 0; i-- {
payload := payloads[i]
slotStartTime := common.SlotToTime(payload.Slot)
entry := apiRespEntry{
Slot: payload.Slot,
SlotTime: slotStartTime.Format("2006-01-02 15:04:05"),
ExtraData: payload.ExtraData,
}
if prevSlot > 0 {
entry.PrevSlot = prevSlot
entry.MinSincePrevSlot = uint64(slotStartTime.Sub(prevSlotTime).Abs().Minutes())
}
entries[i] = entry
prevSlotTime = slotStartTime
prevSlot = payload.Slot
}

// trim last entry
entries = entries[:len(entries)-1]
srv.RespondOK(w, entries)
}

// func (srv *Webserver) updateHTML() {
// var err error
// srv.log.Info("Updating HTML data...")
Expand Down

0 comments on commit 70c9b91

Please sign in to comment.