diff --git a/services/website/webserver.go b/services/website/webserver.go index 0147c37..272a4d8 100644 --- a/services/website/webserver.go +++ b/services/website/webserver.go @@ -9,6 +9,7 @@ import ( "fmt" "net/http" _ "net/http/pprof" + "os" "strings" "sync" "text/template" @@ -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 @@ -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() { @@ -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"} @@ -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) @@ -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 @@ -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) @@ -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) {