from models.performance_breakdown_option import BreakdownOptions from performance.exceptions import InvalidMetricsDatePeriod, InvalidBreakdownOption from performance.repository import BreakdownOptionsRepository from performance.schemas import PerformanceMetricsParamsSchema from services.territory.constants import GLOBAL_TERRITORY_ID class MetricsParametersValidator: breakdown_options_repository: BreakdownOptionsRepository = BreakdownOptionsRepository() def validate(self, model: PerformanceMetricsParamsSchema): if model.breakdown is None: raise InvalidBreakdownOption() breakdown_option = self.breakdown_options_repository.get_breakdown_option_by_id(model.breakdown) breakdowns_without_country_filtering = [ BreakdownOptions.AGES.value, BreakdownOptions.GENDERS.value, BreakdownOptions.PLATFORMS.value, ] if ( model.territoryId == GLOBAL_TERRITORY_ID and breakdown_option in breakdowns_without_country_filtering ) or breakdown_option is None: raise InvalidBreakdownOption() if not model.disableDateValidation: days_range = (model.endDate - model.startDate).days + 1 if days_range > breakdown_option.days_range: raise InvalidMetricsDatePeriod(max_period="{} days".format(breakdown_option.days_range))