Skip to content

Commit

Permalink
Merge pull request #34 from flashbots/cowstats
Browse files Browse the repository at this point in the history
cowstats
  • Loading branch information
metachris authored May 29, 2024
2 parents ede391c + 059cb35 commit 12f3ed8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
12 changes: 12 additions & 0 deletions services/website/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/big"
"sort"
"strings"
"time"

"github.com/flashbots/relayscan/database"
"github.com/flashbots/relayscan/vars"
Expand Down Expand Up @@ -244,3 +245,14 @@ func prepareRelaysEntries(relays []*database.TopRelayEntry) []*database.TopRelay
}
return resp
}

func getLastWednesday() time.Time {
now := time.Now().UTC()
dayOffset := now.Weekday() - time.Wednesday
targetDate := now.AddDate(0, 0, -int(dayOffset))
targetDate = time.Date(targetDate.Year(), targetDate.Month(), targetDate.Day(), 0, 0, 0, 0, targetDate.Location()).UTC()
if targetDate.After(now) {
targetDate = targetDate.AddDate(0, 0, -7)
}
return targetDate
}
31 changes: 30 additions & 1 deletion services/website/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (srv *Webserver) StartServer() (err error) {

ReadTimeout: 600 * time.Millisecond,
ReadHeaderTimeout: 400 * time.Millisecond,
WriteTimeout: 3 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 3 * time.Second,
}

Expand All @@ -134,6 +134,7 @@ func (srv *Webserver) getRouter() http.Handler {
r.HandleFunc("/overview/md", srv.handleOverviewMarkdown).Methods(http.MethodGet)
r.HandleFunc("/builder-profit/md", srv.handleBuilderProfitMarkdown).Methods(http.MethodGet)

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("/api/stats", srv.handleStatsAPI).Methods(http.MethodGet)
Expand Down Expand Up @@ -510,3 +511,31 @@ func (srv *Webserver) handleDailyStatsJSON(w http.ResponseWriter, req *http.Requ

srv.RespondOK(w, resp)
}

func (srv *Webserver) handleCowstatsJSON(w http.ResponseWriter, req *http.Request) {
// builder stats for wednesday utc 00:00 to next wednesday 00:00
type apiResp struct {
DateFrom string `json:"date_from"`
DateTo string `json:"date_to"`
TopBuilders []*database.TopBuilderEntry `json:"top_builders"`
}

wednesday1 := getLastWednesday()
wednesday2 := wednesday1.AddDate(0, 0, -7)

startTime := time.Now()
srv.log.WithField("from", wednesday2).WithField("to", wednesday1).Info("[cowstats] getting top builders...")
topBuilders, err := srv.db.GetTopBuilders(wednesday2, wednesday1, "")
if err != nil {
srv.log.WithError(err).Error("Failed to get top builders")
return
}
srv.log.WithField("duration", time.Since(startTime).String()).WithField("nBuilders", len(topBuilders)).Info("[cowstats] got top builders")

resp := apiResp{
DateFrom: wednesday2.String(),
DateTo: wednesday1.String(),
TopBuilders: consolidateBuilderEntries(topBuilders),
}
srv.RespondOK(w, resp)
}

0 comments on commit 12f3ed8

Please sign in to comment.