From 08e0c675f92f492fc36194ee80862abf0453876e Mon Sep 17 00:00:00 2001 From: soffokl Date: Mon, 1 Jul 2024 10:32:59 +0600 Subject: [PATCH] Remove country filter for quality information --- proposal/metrics/enhancer.go | 9 +++------ proposal/service.go | 6 +++--- quality/oracleapi/oracle.go | 4 ++-- quality/service.go | 38 ++++++++++++++++++++++++------------ 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/proposal/metrics/enhancer.go b/proposal/metrics/enhancer.go index 61ea63f..42fddc1 100644 --- a/proposal/metrics/enhancer.go +++ b/proposal/metrics/enhancer.go @@ -12,13 +12,10 @@ type OracleResponses struct { QualityResponse map[string]*oracleapi.DetailedQuality } -func (or *OracleResponses) Load(qualityService *quality.Service, fromCountry string) { - if len(fromCountry) != 2 { - fromCountry = "NL" - } - qRes, err := qualityService.Quality(fromCountry) +func (or *OracleResponses) Load(qualityService *quality.Service) { + qRes, err := qualityService.Quality() if err != nil { - log.Error().Err(err).Msgf("Could not fetch quality for consumer (country=%s)", fromCountry) + log.Error().Err(err).Msgf("Could not fetch quality for consumer") } or.QualityResponse = qRes } diff --git a/proposal/service.go b/proposal/service.go index 255ce85..76ef642 100644 --- a/proposal/service.go +++ b/proposal/service.go @@ -59,7 +59,7 @@ func (s *Service) List(opts ListOpts, limited bool) []v3.Proposal { }, limited) or := &metrics.OracleResponses{} - or.Load(s.qualityService, opts.from) + or.Load(s.qualityService) return metrics.EnhanceWithMetrics(proposals, or.QualityResponse, metrics.Filters{ IncludeMonitoringFailed: opts.includeMonitoringFailed, @@ -72,7 +72,7 @@ func (s *Service) List(opts ListOpts, limited bool) []v3.Proposal { func (s *Service) Metadata(opts repoMetadataOpts) []v3.Metadata { or := &metrics.OracleResponses{} - or.Load(s.qualityService, "US") + or.Load(s.qualityService) return s.Repository.Metadata(opts, or.QualityResponse) } @@ -105,7 +105,7 @@ func (s *Service) ListCountriesNumbers(opts ListOpts, limited bool) map[string]i }, limited) or := &metrics.OracleResponses{} - or.Load(s.qualityService, opts.from) + or.Load(s.qualityService) eps := metrics.EnhanceWithMetrics(proposals, or.QualityResponse, metrics.Filters{ IncludeMonitoringFailed: opts.includeMonitoringFailed, diff --git a/quality/oracleapi/oracle.go b/quality/oracleapi/oracle.go index 0f80953..e384a70 100644 --- a/quality/oracleapi/oracle.go +++ b/quality/oracleapi/oracle.go @@ -25,8 +25,8 @@ func New(url string) *API { } } -func (a *API) Quality(country string) (map[string]*DetailedQuality, error) { - resp, err := a.client.Get(fmt.Sprintf("%s/api/v2/providers/detailed?country=%s", a.url, country)) +func (a *API) Quality() (map[string]*DetailedQuality, error) { + resp, err := a.client.Get(fmt.Sprintf("%s/api/v2/providers/detailed?country=US", a.url)) if err != nil { return nil, fmt.Errorf("request failed: %w", err) } diff --git a/quality/service.go b/quality/service.go index 40a8029..0d5de03 100644 --- a/quality/service.go +++ b/quality/service.go @@ -5,6 +5,8 @@ package quality import ( + "fmt" + "sync" "time" "github.com/ReneKroon/ttlcache/v2" @@ -25,26 +27,36 @@ func newCacheInstance(cacheTTL time.Duration) *ttlcache.Cache { } type Service struct { - qualityAPI *oracleapi.API - qualityCache *ttlcache.Cache + qualityAPI *oracleapi.API + qualityCache map[string]*oracleapi.DetailedQuality + qualityLastUpdated time.Time + ttl time.Duration + mu sync.Mutex } func NewService(qualityAPI *oracleapi.API, cacheTTL time.Duration) *Service { - service := &Service{ + return &Service{ qualityAPI: qualityAPI, - qualityCache: newCacheInstance(cacheTTL), + qualityCache: make(map[string]*oracleapi.DetailedQuality), + ttl: cacheTTL, } - service.qualityCache.SetLoaderFunction(func(key string) (interface{}, time.Duration, error) { - res, err := service.qualityAPI.Quality(key) - return res, cacheTTL, err - }) - return service } -func (s *Service) Quality(fromCountry string) (map[string]*oracleapi.DetailedQuality, error) { - res, err := s.qualityCache.Get(fromCountry) +func (s *Service) Quality() (map[string]*oracleapi.DetailedQuality, error) { + s.mu.Lock() + defer s.mu.Unlock() + + if time.Since(s.qualityLastUpdated) < s.ttl { + return s.qualityCache, nil + } + + quality, err := s.qualityAPI.Quality() if err != nil { - return nil, err + return nil, fmt.Errorf("failed to get quality: %w", err) } - return res.(map[string]*oracleapi.DetailedQuality), nil + + s.qualityCache = quality + s.qualityLastUpdated = time.Now() + + return quality, nil }