def test__get_report(self): inc_value = 10 expected_value = 10 for item in self.date_ranges: date_range = item['date_range'] selected_date_range = date_range[:10] self.inc_all_counters(date_range, inc_value) self._test_get_report(item['base_model'], item['begin_date'], item['end_date'], selected_date_range, SUMMARY_EVENTS, ALL_DIMENSIONS.keys(), expected_value, summary=False) self._test_get_report(item['base_model'], item['begin_date'], item['end_date'], selected_date_range, SUMMARY_EVENTS, ALL_DIMENSIONS.keys(), expected_value, summary=True) self.drop_collections()
def get(self, request): params = request.GET.dict() for key in self.mandatory_fields: if key not in params.keys(): raise ValidationError('Mandatory field: "%s" is missing' % key) try: site = Site.objects.get(pk=params['site']) except Site.DoesNotExist as ex: raise NotFound(ex.message) if site.user != request.user: raise PermissionDenied() begin_date = datetime.fromtimestamp(int(params['begin_date'])) end_date = datetime.fromtimestamp(int(params['end_date'])) aggregation = params.get('aggregation', 'day') events = params['events'].split(',') dimensions = params.get('dimensions') if dimensions: dimensions = dimensions.split(',') else: dimensions = ALL_DIMENSIONS.keys() report = Report(site.pk, begin_date, end_date) report_data = report.get_report(aggregation, events, dimensions) return JsonResponse(report_data, safe=False)
def test__get_report_empty(self): site_id = self.data['site'] models = [BaseDaily, BaseMonthly, BaseYearly] begin_date = datetime(2015, 1, 1) end_date = datetime(2015, 1, 2) all_dimensions = ALL_DIMENSIONS.keys() for model in models: report_data = model().get_report(site_id, begin_date, end_date, SUMMARY_EVENTS, all_dimensions) self.assertEqual(report_data, []) report_data = model().get_report(site_id, begin_date, end_date, SUMMARY_EVENTS, all_dimensions, summary=True) self.assertEqual(report_data, []) report_data = model().get_report(site_id, begin_date, end_date, SUMMARY_EVENTS, all_dimensions, aggregate_by_values=True) self.assertEqual(report_data, [])
def handle(self, *args, **options): verbosity = int(options.get('verbosity', 1)) show_indexes = options['show'] create_indexes = options['create'] drop_indexes = options['drop'] model_list = [] collection_models = {} for dimension in ALL_DIMENSIONS.values(): for model in dimension.models: model_list.append(model) model_list += [OutsideShares, UpdateCounters, GeoIPModel] for model in model_list: collection_models.setdefault(model.collection_name, []).append(model) collection_models = OrderedDict(sorted(collection_models.items())) if verbosity >= 1: if create_indexes: self.stdout.write('Creating indexes...') elif drop_indexes: self.stdout.write('Dropping indexes...') for collection_name, models in collection_models.items(): if verbosity >= 1 and show_indexes: self.stdout.write('collection %s:' % collection_name) for model in models: model_instance = model() self.print_indexes(model_instance) if create_indexes: model_instance.create_indexes() elif drop_indexes: model_instance.drop_indexes() if verbosity >= 1 and (create_indexes or drop_indexes): self.stdout.write('Done.')
def test__get_totals_empty(self): begin_date = datetime(2015, 1, 1) end_date = datetime(2015, 1, 2) for dimension in ALL_DIMENSIONS.values(): for model_class in dimension.models: totals = model_class().get_totals(self.data['site'], begin_date, end_date) self.assertEqual(totals, [])
def test__get_report_aggregate_by_values(self): inc_value = 10 expected_value = 10 for item in self.date_ranges_with_values_granularity: date_range = item['date_range'] self.inc_all_counters(date_range, inc_value) self._test_get_report_aggregate_by_values(item['base_model'], item['begin_date'], item['end_date'], SUMMARY_EVENTS, ALL_DIMENSIONS.keys(), expected_value, summary=False) self._test_get_report_aggregate_by_values(item['base_model'], item['begin_date'], item['end_date'], SUMMARY_EVENTS, ALL_DIMENSIONS.keys(), expected_value, summary=True) self.drop_collections()
def _test_get_totals(self, base_model, begin_date, end_date, expected_value, aggregate_by_values=False): for dimension in ALL_DIMENSIONS.values(): for model_class in dimension.models: model_instance = model_class() if isinstance(model_instance, base_model): events = model_instance.get_events() totals = model_instance.get_totals( self.data['site'], begin_date, end_date, events=events, aggregate_by_values=aggregate_by_values)[0] for data_field_name in model_instance.data_fields: if data_field_name in self.data: self.assertEqual(totals[data_field_name], self.data[data_field_name]) for event in events: self.assertEqual(totals[event], expected_value)