Skip to content

Commit

Permalink
webserver: skip 7d with SKIP_7D_STATS env var
Browse files Browse the repository at this point in the history
  • Loading branch information
metachris committed Jun 13, 2024
1 parent 36c98b1 commit 3b017ce
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions services/website/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"strings"
"sync"
"text/template"
Expand All @@ -24,7 +25,10 @@ import (
uberatomic "go.uber.org/atomic"
)

var ErrServerAlreadyStarted = errors.New("server was already started")
var (
ErrServerAlreadyStarted = errors.New("server was already started")
envSkip7dStats = os.Getenv("SKIP_7D_STATS") != ""
)

type WebserverOpts struct {
ListenAddress string
Expand Down Expand Up @@ -97,6 +101,10 @@ func (srv *Webserver) StartServer() (err error) {
return ErrServerAlreadyStarted
}

if envSkip7dStats {
srv.log.Warn("SKIP_7D_STATS - Skipping 7d stats")
}

// Start background task to regularly update status HTML data
srv.updateHTML()
go func() {
Expand Down Expand Up @@ -209,10 +217,10 @@ func (srv *Webserver) updateHTML() {
// Now generate the HTML
// htmlDefault := bytes.Buffer{}

now := time.Now().UTC()
startTime := time.Now().UTC()
htmlData := HTMLData{} //nolint:exhaustruct
htmlData.GeneratedAt = now
htmlData.LastUpdateTime = now.Format("2006-01-02 15:04")
htmlData.GeneratedAt = startTime
htmlData.LastUpdateTime = startTime.Format("2006-01-02 15:04")
htmlData.TimeSpans = []string{"7d", "24h", "12h", "1h"}
// htmlData.TimeSpans = []string{"24h", "12h"}

Expand All @@ -239,7 +247,11 @@ func (srv *Webserver) updateHTML() {
}
srv.log.WithField("duration", time.Since(startUpdate).String()).Info("updated 24h stats")

if !srv.opts.Only24h {
if srv.opts.Only24h {
stats["1h"] = NewStats()
stats["12h"] = NewStats()
stats["7d"] = NewStats()
} else {
startUpdate = time.Now()
srv.log.Info("updating 12h stats...")
stats["12h"], err = srv.getStatsForHours(12 * time.Hour)
Expand All @@ -258,14 +270,18 @@ func (srv *Webserver) updateHTML() {
}
srv.log.WithField("duration", time.Since(startUpdate).String()).Info("updated 1h stats")

startUpdate = time.Now()
srv.log.Info("updating 7d stats...")
stats["7d"], err = srv.getStatsForHours(7 * 24 * time.Hour)
if err != nil {
srv.log.WithError(err).Error("Failed to get stats for 24h")
return
if envSkip7dStats {
stats["7d"] = NewStats()
} else {
startUpdate = time.Now()
srv.log.Info("updating 7d stats...")
stats["7d"], err = srv.getStatsForHours(7 * 24 * time.Hour)
if err != nil {
srv.log.WithError(err).Error("Failed to get stats for 24h")
return
}
srv.log.WithField("duration", time.Since(startUpdate).String()).Info("updated 7d stats")
}
srv.log.WithField("duration", time.Since(startUpdate).String()).Info("updated 7d stats")
}

// Save the html data
Expand All @@ -278,14 +294,14 @@ func (srv *Webserver) updateHTML() {
stats24h := stats["24h"]

// create overviewMd markdown
overviewMd := fmt.Sprintf("Top relays - 24h, %s UTC, via relayscan.io \n\n```\n", now.Format("2006-01-02 15:04"))
overviewMd := fmt.Sprintf("Top relays - 24h, %s UTC, via relayscan.io \n\n```\n", startTime.Format("2006-01-02 15:04"))
overviewMd += relayTable(stats24h.TopRelays)
overviewMd += fmt.Sprintf("```\n\nTop builders - 24h, %s UTC, via relayscan.io \n\n```\n", now.Format("2006-01-02 15:04"))
overviewMd += fmt.Sprintf("```\n\nTop builders - 24h, %s UTC, via relayscan.io \n\n```\n", startTime.Format("2006-01-02 15:04"))
overviewMd += builderTable(stats24h.TopBuilders)
overviewMd += "```"
overviewMdBytes := []byte(overviewMd)

builderProfitMd := fmt.Sprintf("Builder profits - 24h, %s UTC, via relayscan.io/builder-profit \n\n```\n", now.Format("2006-01-02 15:04"))
builderProfitMd := fmt.Sprintf("Builder profits - 24h, %s UTC, via relayscan.io/builder-profit \n\n```\n", startTime.Format("2006-01-02 15:04"))
builderProfitMd += builderProfitTable(stats24h.BuilderProfits)
builderProfitMd += "```"
builderProfitMdBytes := []byte(builderProfitMd)
Expand All @@ -310,7 +326,8 @@ func (srv *Webserver) updateHTML() {
// srv.statsAPIResp = &respBytes
// }
// srv.statsAPIRespLock.Unlock()
srv.log.Info("Updating HTML data complete.")
duration := time.Since(startTime)
srv.log.WithField("duration", duration.String()).Info("Updating HTML data complete.")
}

func (srv *Webserver) RespondError(w http.ResponseWriter, code int, message string) {
Expand Down

0 comments on commit 3b017ce

Please sign in to comment.