Skip to content
This repository has been archived by the owner on May 5, 2022. It is now read-only.

Remove db requests #435

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func init() {
log.Fatal(err)
}

charts = chartscontroller.NewController(redisCache, memoryCache, database, chartsPriority, m.ChartsAPIs, configuration)
charts = chartscontroller.NewController(redisCache, memoryCache, chartsPriority, m.ChartsAPIs)
info = infocontroller.NewController(database, memoryCache, coinInfoPriority, ratesPriority, m.ChartsAPIs)
tickers = tickerscontroller.NewController(database, memoryCache, ratesPriority, tickerPriority, configuration)
rates = ratescontroller.NewController(database, memoryCache, ratesPriority, configuration)
Expand Down
29 changes: 3 additions & 26 deletions services/controllers/charts/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (
"errors"
"fmt"
"github.com/trustwallet/golibs/asset"
"github.com/trustwallet/watchmarket/config"
"github.com/trustwallet/watchmarket/db/models"
"strings"

log "github.com/sirupsen/logrus"
"github.com/trustwallet/watchmarket/db"
"github.com/trustwallet/watchmarket/pkg/watchmarket"
"github.com/trustwallet/watchmarket/services/cache"
"github.com/trustwallet/watchmarket/services/controllers"
Expand All @@ -22,33 +20,26 @@ const charts = "charts"
type Controller struct {
redisCache cache.Provider
memoryCache cache.Provider
database db.Instance
availableProviders []string
api markets.ChartsAPIs
useMemoryCache bool
}

func NewController(
redisCache cache.Provider,
memoryCache cache.Provider,
database db.Instance,
chartsPriority []string,
api markets.ChartsAPIs,
configuration config.Configuration,
) Controller {
return Controller{
redisCache,
memoryCache,
database,
chartsPriority,
api,
configuration.RestAPI.UseMemoryCache,
}
}

// ChartsController interface implementation
func (c Controller) HandleChartsRequest(request controllers.ChartRequest) (chart watchmarket.Chart, err error) {

if !c.hasTickers(request.Asset) {
return chart, nil
}
Expand All @@ -73,25 +64,11 @@ func (c Controller) HandleChartsRequest(request controllers.ChartRequest) (chart
}

func (c Controller) hasTickers(assetData controllers.Asset) bool {
var tickers []models.Ticker
var err error

if c.useMemoryCache {
if tickers, err = c.getChartsFromMemory(assetData); err != nil {
return false
}
if tickers, err := c.getChartsFromMemory(assetData); err != nil {
return false
} else {
dbTickers, err := c.database.GetTickers([]controllers.Asset{assetData})
if err != nil {
return false
}
for _, t := range dbTickers {
if t.ShowOption != 2 { // TODO: 2 to constants
tickers = append(tickers, t)
}
}
return len(tickers) > 0
}
return len(tickers) > 0
}

func (c Controller) getChartsFromApi(data controllers.ChartRequest) (ch watchmarket.Chart, err error) {
Expand Down
227 changes: 0 additions & 227 deletions services/controllers/charts/base_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion services/controllers/info/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (c Controller) getFromCache(request controllers.DetailsRequest) (controller

cachedDetails, err := c.cache.Get(key)
if err != nil || len(cachedDetails) <= 0 {
return controllers.InfoResponse{}, errors.New("cache is empty")
return controllers.InfoResponse{}, errors.New(watchmarket.ErrNotFound)
}
var infoResponse controllers.InfoResponse
err = json.Unmarshal(cachedDetails, &infoResponse)
Expand Down
2 changes: 1 addition & 1 deletion services/controllers/info/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func TestController_HandleDetailsRequest(t *testing.T) {
cm := getChartsMock()
wantedD := watchmarket.CoinDetails{
Provider: watchmarket.CoinMarketCap,
Provider: "coinmarketcap",
Info: &watchmarket.Info{
Name: "2",
Website: "2",
Expand Down
46 changes: 6 additions & 40 deletions services/controllers/rates/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"encoding/json"
"errors"

log "github.com/sirupsen/logrus"
"github.com/trustwallet/watchmarket/config"
"github.com/trustwallet/watchmarket/db"
"github.com/trustwallet/watchmarket/db/models"
"github.com/trustwallet/watchmarket/pkg/watchmarket"
"github.com/trustwallet/watchmarket/services/cache"
"github.com/trustwallet/watchmarket/services/controllers"
Expand Down Expand Up @@ -67,45 +65,13 @@ func (c Controller) GetFiatRates() (controllers.FiatRates, error) {
}

func (c Controller) getRateByCurrency(currency string) (watchmarket.Rate, error) {
if c.configuration.RestAPI.UseMemoryCache {
rawResult, err := c.dataCache.Get(currency)
if err != nil {
return watchmarket.Rate{}, err
}
var result watchmarket.Rate
if err = json.Unmarshal(rawResult, &result); err != nil {
return watchmarket.Rate{}, err
}
return result, nil
}
emptyRate := watchmarket.Rate{}
rates, err := c.database.GetRates(currency)
rawResult, err := c.dataCache.Get(currency)
if err != nil {
log.Error(err, "getRateByPriority")
return emptyRate, err
return watchmarket.Rate{}, err
}

providers := c.ratesPriority
var result models.Rate
ProvidersLoop:
for _, p := range providers {
for _, r := range rates {
if p == r.Provider {
result = r
break ProvidersLoop
}
}
var result watchmarket.Rate
if err = json.Unmarshal(rawResult, &result); err != nil {
return watchmarket.Rate{}, err
}

if result.Currency == "" || result.Rate == 0 {
return emptyRate, errors.New(watchmarket.ErrNotFound)
}

return watchmarket.Rate{
Currency: result.Currency,
PercentChange24h: result.PercentChange24h,
Provider: result.Provider,
Rate: result.Rate,
Timestamp: result.LastUpdated.Unix(),
}, nil
return result, nil
}
Loading