From f4988b253ca5252c84a8beffdc3461647ce63fb7 Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Tue, 31 Oct 2023 16:41:58 -0600 Subject: [PATCH] api: refactor web3index --- api/api.go | 85 ---------------------------------- api/types.go | 24 ---------- api/web3index.go | 117 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 109 deletions(-) delete mode 100644 api/types.go create mode 100644 api/web3index.go diff --git a/api/api.go b/api/api.go index b249fd8..adb2313 100644 --- a/api/api.go +++ b/api/api.go @@ -96,91 +96,6 @@ func (a *api) handleGetRevenuePeriods(c jape.Context) { c.Encode(revenue) } -func (a *api) handleGetWeb3Index(c jape.Context) { - var resp Web3IndexResp - - now := time.Now() - revenue, err := a.sp.Metrics(now) - if err != nil { - c.Error(err, http.StatusInternalServerError) - return - } - resp.Revenue.Now = revenue.Revenue.USD.InexactFloat64() - - oneDayAgo := now.AddDate(0, 0, -1) - revenue, err = a.sp.Metrics(oneDayAgo) - if err != nil { - c.Error(err, http.StatusInternalServerError) - return - } - resp.Revenue.OneDayAgo = revenue.Revenue.USD.InexactFloat64() - - twoDaysAgo := now.AddDate(0, 0, -2) - revenue, err = a.sp.Metrics(twoDaysAgo) - if err != nil { - c.Error(err, http.StatusInternalServerError) - return - } - resp.Revenue.TwoDaysAgo = revenue.Revenue.USD.InexactFloat64() - - oneWeekAgo := now.AddDate(0, 0, -7) - revenue, err = a.sp.Metrics(oneWeekAgo) - if err != nil { - c.Error(err, http.StatusInternalServerError) - return - } - resp.Revenue.OneWeekAgo = revenue.Revenue.USD.InexactFloat64() - - twoWeeksAgo := now.AddDate(0, 0, -14) - revenue, err = a.sp.Metrics(twoWeeksAgo) - if err != nil { - c.Error(err, http.StatusInternalServerError) - return - } - resp.Revenue.TwoWeeksAgo = revenue.Revenue.USD.InexactFloat64() - - thirtyDaysAgo := now.AddDate(0, 0, -30) - revenue, err = a.sp.Metrics(thirtyDaysAgo) - if err != nil { - c.Error(err, http.StatusInternalServerError) - return - } - resp.Revenue.ThirtyDaysAgo = revenue.Revenue.USD.InexactFloat64() - - sixtyDaysAgo := now.AddDate(0, 0, -60) - revenue, err = a.sp.Metrics(sixtyDaysAgo) - if err != nil { - c.Error(err, http.StatusInternalServerError) - return - } - resp.Revenue.SixtyDaysAgo = revenue.Revenue.USD.InexactFloat64() - - ninetyDaysAgo := now.AddDate(0, 0, -90) - revenue, err = a.sp.Metrics(ninetyDaysAgo) - if err != nil { - c.Error(err, http.StatusInternalServerError) - return - } - resp.Revenue.NinetyDaysAgo = revenue.Revenue.USD.InexactFloat64() - - start := now.AddDate(-1, 0, 0) - start = start.AddDate(0, 0, -int(start.Weekday()+1)) - days, err := a.sp.Periods(start, now, stats.PeriodDaily) - if err != nil { - c.Error(err, http.StatusInternalServerError) - return - } - - for i := len(days) - 1; i > 0; i-- { - current, prev := days[i], days[i-1] - resp.Days = append(resp.Days, Web3IndexDay{ - Date: current.Timestamp.Unix(), - Revenue: current.Revenue.USD.Sub(prev.Revenue.USD).InexactFloat64(), - }) - } - c.Encode(resp) -} - // NewServer returns an http.Handler that serves the API. func NewServer(sp StatProvider, log *zap.Logger) http.Handler { a := &api{ diff --git a/api/types.go b/api/types.go deleted file mode 100644 index 14ed7da..0000000 --- a/api/types.go +++ /dev/null @@ -1,24 +0,0 @@ -package api - -type ( - Web3IndexDay struct { - Date int64 `json:"date"` - Revenue float64 `json:"revenue"` - } - - Web3IndexRevenue struct { - Now float64 `json:"now"` - OneDayAgo float64 `json:"oneDayAgo"` - TwoDaysAgo float64 `json:"twoDaysAgo"` - OneWeekAgo float64 `json:"oneWeekAgo"` - TwoWeeksAgo float64 `json:"twoWeeksAgo"` - ThirtyDaysAgo float64 `json:"thirtyDaysAgo"` - SixtyDaysAgo float64 `json:"sixtyDaysAgo"` - NinetyDaysAgo float64 `json:"ninetyDaysAgo"` - } - - Web3IndexResp struct { - Days []Web3IndexDay `json:"days"` - Revenue Web3IndexRevenue `json:"revenue"` - } -) diff --git a/api/web3index.go b/api/web3index.go new file mode 100644 index 0000000..dff6844 --- /dev/null +++ b/api/web3index.go @@ -0,0 +1,117 @@ +package api + +import ( + "net/http" + "time" + + "go.sia.tech/host-revenue-api/stats" + "go.sia.tech/jape" +) + +type ( + Web3IndexDay struct { + Date int64 `json:"date"` + Revenue float64 `json:"revenue"` + } + + Web3IndexRevenue struct { + Now float64 `json:"now"` + OneDayAgo float64 `json:"oneDayAgo"` + TwoDaysAgo float64 `json:"twoDaysAgo"` + OneWeekAgo float64 `json:"oneWeekAgo"` + TwoWeeksAgo float64 `json:"twoWeeksAgo"` + ThirtyDaysAgo float64 `json:"thirtyDaysAgo"` + SixtyDaysAgo float64 `json:"sixtyDaysAgo"` + NinetyDaysAgo float64 `json:"ninetyDaysAgo"` + } + + Web3IndexResp struct { + Days []Web3IndexDay `json:"days"` + Revenue Web3IndexRevenue `json:"revenue"` + } +) + +func (a *api) handleGetWeb3Index(c jape.Context) { + var resp Web3IndexResp + + now := time.Now() + revenue, err := a.sp.Metrics(now) + if err != nil { + c.Error(err, http.StatusInternalServerError) + return + } + resp.Revenue.Now = revenue.Revenue.USD.InexactFloat64() + + oneDayAgo := now.AddDate(0, 0, -1) + revenue, err = a.sp.Metrics(oneDayAgo) + if err != nil { + c.Error(err, http.StatusInternalServerError) + return + } + resp.Revenue.OneDayAgo = revenue.Revenue.USD.InexactFloat64() + + twoDaysAgo := now.AddDate(0, 0, -2) + revenue, err = a.sp.Metrics(twoDaysAgo) + if err != nil { + c.Error(err, http.StatusInternalServerError) + return + } + resp.Revenue.TwoDaysAgo = revenue.Revenue.USD.InexactFloat64() + + oneWeekAgo := now.AddDate(0, 0, -7) + revenue, err = a.sp.Metrics(oneWeekAgo) + if err != nil { + c.Error(err, http.StatusInternalServerError) + return + } + resp.Revenue.OneWeekAgo = revenue.Revenue.USD.InexactFloat64() + + twoWeeksAgo := now.AddDate(0, 0, -14) + revenue, err = a.sp.Metrics(twoWeeksAgo) + if err != nil { + c.Error(err, http.StatusInternalServerError) + return + } + resp.Revenue.TwoWeeksAgo = revenue.Revenue.USD.InexactFloat64() + + thirtyDaysAgo := now.AddDate(0, 0, -30) + revenue, err = a.sp.Metrics(thirtyDaysAgo) + if err != nil { + c.Error(err, http.StatusInternalServerError) + return + } + resp.Revenue.ThirtyDaysAgo = revenue.Revenue.USD.InexactFloat64() + + sixtyDaysAgo := now.AddDate(0, 0, -60) + revenue, err = a.sp.Metrics(sixtyDaysAgo) + if err != nil { + c.Error(err, http.StatusInternalServerError) + return + } + resp.Revenue.SixtyDaysAgo = revenue.Revenue.USD.InexactFloat64() + + ninetyDaysAgo := now.AddDate(0, 0, -90) + revenue, err = a.sp.Metrics(ninetyDaysAgo) + if err != nil { + c.Error(err, http.StatusInternalServerError) + return + } + resp.Revenue.NinetyDaysAgo = revenue.Revenue.USD.InexactFloat64() + + start := now.AddDate(-1, 0, 0) + start = start.AddDate(0, 0, -int(start.Weekday()+1)) + days, err := a.sp.Periods(start, now, stats.PeriodDaily) + if err != nil { + c.Error(err, http.StatusInternalServerError) + return + } + + for i := len(days) - 1; i > 0; i-- { + current, prev := days[i], days[i-1] + resp.Days = append(resp.Days, Web3IndexDay{ + Date: current.Timestamp.Unix(), + Revenue: current.Revenue.USD.Sub(prev.Revenue.USD).InexactFloat64(), + }) + } + c.Encode(resp) +}