Skip to content

Commit

Permalink
Remove country filter for quality information
Browse files Browse the repository at this point in the history
  • Loading branch information
soffokl committed Jul 1, 2024
1 parent e75c9a1 commit 08e0c67
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
9 changes: 3 additions & 6 deletions proposal/metrics/enhancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions proposal/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions quality/oracleapi/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
38 changes: 25 additions & 13 deletions quality/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package quality

import (
"fmt"
"sync"
"time"

"github.com/ReneKroon/ttlcache/v2"
Expand All @@ -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
}

0 comments on commit 08e0c67

Please sign in to comment.