Skip to content
Merged
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
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ services:
- GECKO_URL=http://wiremock:8080
- COINRANKING_URL=http://wiremock:8080
- COINRANKING_TOKEN=suchtoken
- PROMETHEUS_URL=http://wiremock:8080
- PROMETHEUS_USERNAME=prometheus
- PROMETHEUS_PASSWORD=prometheus

db:
image: postgres:13-alpine
Expand Down
81 changes: 81 additions & 0 deletions price/pricingbyservice/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func (cpd *ConfigProviderDB) fetchConfig() (Config, error) {
type Config struct {
BasePrices PriceByTypeUSD `json:"base_prices"`
CountryModifiers map[ISO3166CountryCode]Modifier `json:"country_modifiers"`
DemandBoost *DemandBoostConfig `json:"demand_boost,omitempty"`
}

func (c Config) Validate() error {
Expand All @@ -107,6 +108,86 @@ func (c Config) Validate() error {
}
}

if c.DemandBoost != nil {
if err := c.DemandBoost.Validate(); err != nil {
return fmt.Errorf("demand boost invalid: %w", err)
}
}

return nil
}

type DemandBoostConfig struct {
Countries map[ISO3166CountryCode]DemandBoostCountryCfg `json:"countries"`
}

func (d DemandBoostConfig) Validate() error {
for country, cfg := range d.Countries {
if err := country.Validate(); err != nil {
return err
}
if err := cfg.Validate(); err != nil {
return fmt.Errorf("country %v contains invalid demand boost: %w", country, err)
}
}

return nil
}

type DemandBoostCountryCfg struct {
TargetDemandIndex float64 `json:"target_demand_index"`
MaxBonus float64 `json:"max_bonus"`
ServiceTypes []ServiceType `json:"service_types,omitempty"`
}

func (d DemandBoostCountryCfg) Validate() error {
if d.TargetDemandIndex <= 0 {
return errors.New("target demand index should be higher than 0")
}
if d.MaxBonus < 0 {
return errors.New("max bonus should be non negative")
}
for _, serviceType := range d.ServiceTypes {
if err := serviceType.Validate(); err != nil {
return err
}
}
return nil
}

type ServiceType string

const (
ServiceTypeWireguard ServiceType = "wireguard"
ServiceTypeScraping ServiceType = "scraping"
ServiceTypeQUICScraping ServiceType = "quic_scraping"
ServiceTypeDataTransfer ServiceType = "data_transfer"
ServiceTypeDVPN ServiceType = "dvpn"
ServiceTypeMonitoring ServiceType = "monitoring"
)

var validServiceTypes = map[ServiceType]struct{}{
ServiceTypeWireguard: {},
ServiceTypeScraping: {},
ServiceTypeQUICScraping: {},
ServiceTypeDataTransfer: {},
ServiceTypeDVPN: {},
ServiceTypeMonitoring: {},
}

var allServiceTypes = []ServiceType{
ServiceTypeWireguard,
ServiceTypeScraping,
ServiceTypeQUICScraping,
ServiceTypeDataTransfer,
ServiceTypeDVPN,
ServiceTypeMonitoring,
}

func (s ServiceType) Validate() error {
if _, ok := validServiceTypes[s]; !ok {
return fmt.Errorf("%v is an invalid service type", s)
}
return nil
}

Expand Down
66 changes: 66 additions & 0 deletions price/pricingbyservice/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,69 @@ func TestConfig_Validate(t *testing.T) {
})
}
}

func TestDemandBoostConfig_Validate(t *testing.T) {
tests := []struct {
name string
cfg DemandBoostConfig
wantErr bool
}{
{
name: "accepts valid config",
cfg: DemandBoostConfig{
Countries: map[ISO3166CountryCode]DemandBoostCountryCfg{
"PL": {TargetDemandIndex: 0.1, MaxBonus: 0.5},
},
},
},
{
name: "rejects invalid country",
cfg: DemandBoostConfig{
Countries: map[ISO3166CountryCode]DemandBoostCountryCfg{
"pl": {TargetDemandIndex: 0.1, MaxBonus: 0.5},
},
},
wantErr: true,
},
{
name: "rejects negative max bonus",
cfg: DemandBoostConfig{
Countries: map[ISO3166CountryCode]DemandBoostCountryCfg{
"PL": {TargetDemandIndex: 0.1, MaxBonus: -0.1},
},
},
wantErr: true,
},
{
name: "rejects unset target demand index",
cfg: DemandBoostConfig{
Countries: map[ISO3166CountryCode]DemandBoostCountryCfg{
"PL": {MaxBonus: 0.5},
},
},
wantErr: true,
},
{
name: "rejects invalid service type",
cfg: DemandBoostConfig{
Countries: map[ISO3166CountryCode]DemandBoostCountryCfg{
"PL": {
TargetDemandIndex: 0.1,
MaxBonus: 0.5,
ServiceTypes: []ServiceType{"bad_service"},
},
},
},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.cfg.Validate()
if (err != nil) != tt.wantErr {
t.Errorf("DemandBoostConfig.Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
Loading
Loading