Skip to content

Commit

Permalink
Exposed /stats endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
avalkov committed Aug 8, 2022
1 parent 2a0c0ba commit 225c949
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 24 deletions.
1 change: 1 addition & 0 deletions cmd/stats-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func main() {
http.HandleFunc("/cosmos/bank/v1beta1/supply", handlers.GetSupplyHandler(cfg, keyValueStorage))
http.HandleFunc("/circulating-supply", handlers.GetCircSupplyTextHandler(cfg, keyValueStorage))
http.HandleFunc("/json/circulating-supply", handlers.GetCircSupplyJSONHandler(cfg, keyValueStorage))
http.HandleFunc("/stats", handlers.GetStatsHandler(cfg, keyValueStorage))

log.Info().Msg(fmt.Sprintf("Listening on port: %d", cfg.Port))

Expand Down
4 changes: 4 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ calculation:
inflation_since_days: 50
storage:
apr_key: apr
apr_height_key: apr_height
annual_provisions_key: annual_provisions
inflation_key: inflation
inflation_height_key: inflation_height
all_tokens_supply_key: all_tokens_supply
supply_key: supply
supply_height_key: supply_height

3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ type Config struct {
} `yaml:"calculation"`
Storage struct {
APRKey string `yaml:"apr_key"`
APRHeightKey string `yaml:"apr_height_key"`
AnnualProvisionsKey string `yaml:"annual_provisions"`
InflationKey string `yaml:"inflation_key"`
InflationHeightKey string `yaml:"inflation_height_key"`
AllTokensSupplyKey string `yaml:"all_tokens_supply_key"`
SupplyKey string `yaml:"supply_key"`
SupplyHeightKey string `yaml:"supply_height_key"`
} `yaml:"storage"`
}
112 changes: 88 additions & 24 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,21 @@ func GetCircSupplyTextHandler(cfg config.Config, storage keyValueStorage) func(h
return func(w http.ResponseWriter, r *http.Request) {
supply, err := storage.GetValue(cfg.Storage.SupplyKey)
if err != nil {
log.Error().Err(err).Send()
w.WriteHeader(http.StatusBadRequest)
badRequest(w, err)
return
}

formattedSupply, err := formatSupply(supply)
if err != nil {
log.Error().Err(err).Send()
w.WriteHeader(http.StatusBadRequest)
badRequest(w, err)
return
}

w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)

if _, err := w.Write([]byte(formattedSupply)); err != nil {
log.Error().Err(err).Send()
w.WriteHeader(http.StatusBadRequest)
badRequest(w, err)
return
}
}
Expand All @@ -41,22 +38,76 @@ func GetCircSupplyJSONHandler(cfg config.Config, storage keyValueStorage) func(h
return func(w http.ResponseWriter, r *http.Request) {
supply, err := storage.GetValue(cfg.Storage.SupplyKey)
if err != nil {
log.Error().Err(err).Send()
w.WriteHeader(http.StatusBadRequest)
badRequest(w, err)
return
}

formattedSupply, err := formatSupply(supply)
if err != nil {
log.Error().Err(err).Send()
w.WriteHeader(http.StatusBadRequest)
badRequest(w, err)
return
}

setHeaders(w)

if err := json.NewEncoder(w).Encode(supplyResponse{Supply: formattedSupply}); err != nil {
log.Error().Err(err).Send()
badRequest(w, err)
}
}
}

func GetStatsHandler(cfg config.Config, storage keyValueStorage) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
supply, err := storage.GetValue(cfg.Storage.SupplyKey)
if err != nil {
badRequest(w, err)
return
}

formattedSupply, err := formatSupply(supply)
if err != nil {
badRequest(w, err)
return
}

supplyHeight, err := storage.GetInt64Value(cfg.Storage.SupplyHeightKey)
if err != nil {
badRequest(w, err)
return
}

inflation, err := storage.GetValue(cfg.Storage.InflationKey)
if err != nil {
badRequest(w, err)
return
}

inflationHeight, err := storage.GetInt64Value(cfg.Storage.InflationHeightKey)
if err != nil {
badRequest(w, err)
return
}

apr, err := storage.GetValue(cfg.Storage.APRKey)
if err != nil {
badRequest(w, err)
return
}

aprHeight, err := storage.GetInt64Value(cfg.Storage.APRHeightKey)
if err != nil {
badRequest(w, err)
return
}

setHeaders(w)

if err := json.NewEncoder(w).Encode(statsResponse{
Inflation: valueAtHeight{Value: inflation, Height: inflationHeight},
APR: valueAtHeight{Value: apr, Height: aprHeight},
Supply: valueAtHeight{Value: formattedSupply, Height: supplyHeight},
}); err != nil {
badRequest(w, err)
}
}
}
Expand All @@ -65,15 +116,14 @@ func GetSupplyHandler(cfg config.Config, storage keyValueStorage) func(http.Resp
return func(w http.ResponseWriter, r *http.Request) {
supply, err := storage.GetValue(cfg.Storage.AllTokensSupplyKey)
if err != nil {
log.Error().Err(err).Send()
w.WriteHeader(http.StatusBadRequest)
badRequest(w, err)
return
}

setHeaders(w)

if _, err := w.Write([]byte(supply)); err != nil {
log.Error().Err(err).Send()
badRequest(w, err)
}
}
}
Expand All @@ -82,15 +132,14 @@ func GetAPRHandler(cfg config.Config, storage keyValueStorage) func(http.Respons
return func(w http.ResponseWriter, r *http.Request) {
apr, err := storage.GetValue(cfg.Storage.APRKey)
if err != nil {
log.Error().Err(err).Send()
w.WriteHeader(http.StatusBadRequest)
badRequest(w, err)
return
}

setHeaders(w)

if err := json.NewEncoder(w).Encode(aprResponse{APR: apr}); err != nil {
log.Error().Err(err).Send()
badRequest(w, err)
}
}
}
Expand All @@ -99,15 +148,14 @@ func GetAnnualProvisionsHandler(cfg config.Config, storage keyValueStorage) func
return func(w http.ResponseWriter, r *http.Request) {
annualProvisions, err := storage.GetValue(cfg.Storage.AnnualProvisionsKey)
if err != nil {
log.Error().Err(err).Send()
w.WriteHeader(http.StatusBadRequest)
badRequest(w, err)
return
}

setHeaders(w)

if err := json.NewEncoder(w).Encode(annualProvisionsResponse{AnnualProvisions: annualProvisions}); err != nil {
log.Error().Err(err).Send()
badRequest(w, err)
}
}
}
Expand All @@ -116,15 +164,14 @@ func GetInflationHandler(cfg config.Config, storage keyValueStorage) func(http.R
return func(w http.ResponseWriter, r *http.Request) {
inflation, err := storage.GetValue(cfg.Storage.InflationKey)
if err != nil {
log.Error().Err(err).Send()
w.WriteHeader(http.StatusBadRequest)
badRequest(w, err)
return
}

setHeaders(w)

if err := json.NewEncoder(w).Encode(inflationResponse{Inflation: inflation}); err != nil {
log.Error().Err(err).Send()
badRequest(w, err)
}
}
}
Expand All @@ -143,7 +190,7 @@ func GetParamsHandler(cfg config.Config) func(http.ResponseWriter, *http.Request
BlocksPerYear: cfg.Genesis.BlocksPerDay,
},
}); err != nil {
log.Error().Err(err).Send()
badRequest(w, err)
}
}
}
Expand All @@ -161,6 +208,11 @@ func formatSupply(supply string) (string, error) {
return formattedSupply.String(), nil
}

func badRequest(w http.ResponseWriter, err error) {
log.Error().Err(err).Send()
w.WriteHeader(http.StatusBadRequest)
}

func setHeaders(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -195,7 +247,19 @@ type supplyResponse struct {
Supply string `json:"supply"`
}

type statsResponse struct {
Inflation valueAtHeight `json:"inflation"`
APR valueAtHeight `json:"apr"`
Supply valueAtHeight `json:"supply"`
}

type valueAtHeight struct {
Value string `json:"value"`
Height int64 `json:"height"`
}

type keyValueStorage interface {
SetValue(key, value string) error
GetValue(key string) (string, error)
GetInt64Value(key string) (int64, error)
}
13 changes: 13 additions & 0 deletions internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package storage

import (
"errors"
"strconv"
)

type storage struct {
Expand Down Expand Up @@ -36,3 +37,15 @@ func (s *storage) GetOrDefaultValue(key, defaultValue string) (string, error) {
}
return value, err
}

func (s *storage) SetInt64Value(key string, value int64) error {
return s.SetValue(key, strconv.FormatInt(value, 10))
}

func (s *storage) GetInt64Value(key string) (int64, error) {
value, err := s.GetValue(key)
if err != nil {
return 0, err
}
return strconv.ParseInt(value, 10, 64)
}
4 changes: 4 additions & 0 deletions internal/tasks/apr.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func getCalculateAPRHandler(genesisState cudoMintTypes.GenesisState, cfg config.
return fmt.Errorf("failed to set value %s for key %s", apr.String(), cfg.Storage.APRKey)
}

if err := storage.SetInt64Value(cfg.Storage.APRHeightKey, latestBlockHeight); err != nil {
return fmt.Errorf("failed to set value %d for key %s", latestBlockHeight, cfg.Storage.APRHeightKey)
}

annualProvisions := mintAmountInt.ToDec().MulInt64(12)

if err := storage.SetValue(cfg.Storage.AnnualProvisionsKey, annualProvisions.String()); err != nil {
Expand Down
8 changes: 8 additions & 0 deletions internal/tasks/inflation.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ func getCalculateInflationHandler(genesisState cudoMintTypes.GenesisState, cfg c
return fmt.Errorf("failed to set value %s for key %s", inflation.String(), cfg.Storage.InflationKey)
}

if err := storage.SetInt64Value(cfg.Storage.InflationHeightKey, latestCudosBlock); err != nil {
return fmt.Errorf("failed to set value %d for key %s", latestCudosBlock, cfg.Storage.InflationHeightKey)
}

ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*10)
defer cancelFunc()

Expand Down Expand Up @@ -100,6 +104,10 @@ func getCalculateInflationHandler(genesisState cudoMintTypes.GenesisState, cfg c
return fmt.Errorf("failed to set value %s for key %s", currentTotalSupply.String(), cfg.Storage.SupplyKey)
}

if err := storage.SetInt64Value(cfg.Storage.SupplyHeightKey, latestCudosBlock); err != nil {
return fmt.Errorf("failed to set value %d for key %s", latestCudosBlock, cfg.Storage.SupplyHeightKey)
}

return nil
}
}
Expand Down
1 change: 1 addition & 0 deletions internal/tasks/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ const maxSupply = "10000000000000000000000000000" // 10 billion

type keyValueStorage interface {
SetValue(key, value string) error
SetInt64Value(key string, value int64) error
GetOrDefaultValue(key, defaultValue string) (string, error)
}

Expand Down

0 comments on commit 225c949

Please sign in to comment.