def test_aggregates(self): self.assertEqual(repr(Avg('a')), "Avg(F(a))") self.assertEqual(repr(Count('a')), "Count(F(a), distinct=False)") self.assertEqual(repr(Max('a')), "Max(F(a))") self.assertEqual(repr(Min('a')), "Min(F(a))") self.assertEqual(repr(StdDev('a')), "StdDev(F(a), sample=False)") self.assertEqual(repr(Sum('a')), "Sum(F(a))") self.assertEqual(repr(Variance('a', sample=True)), "Variance(F(a), sample=True)")
def get_registrations(self, interval, days, graph_key, select_box_value): """ Returns an array with new users count per interval.""" try: conf_data = DashboardStats.objects.get(graph_key=graph_key) model_name = apps.get_model(conf_data.model_app_name, conf_data.model_name) kwargs = {} for i in conf_data.criteria.all(): # fixed mapping value passed info kwargs if i.criteria_fix_mapping: for key in i.criteria_fix_mapping: # value => i.criteria_fix_mapping[key] kwargs[key] = i.criteria_fix_mapping[key] # dynamic mapping value passed info kwargs if i.dynamic_criteria_field_name and select_box_value: kwargs[i.dynamic_criteria_field_name] = select_box_value aggregate = None if conf_data.type_operation_field_name and conf_data.operation_field_name: operation = { 'Count': Count(conf_data.operation_field_name), 'Sum': Sum(conf_data.operation_field_name), 'Avg': Avg(conf_data.operation_field_name), 'StdDev': StdDev(conf_data.operation_field_name), 'Max': Max(conf_data.operation_field_name), 'Min': Min(conf_data.operation_field_name), 'Variance': Variance(conf_data.operation_field_name), } aggregate = operation[conf_data.type_operation_field_name] stats = QuerySetStats(model_name.objects.filter(**kwargs), conf_data.date_field_name, aggregate) # stats = QuerySetStats(User.objects.filter(is_active=True), 'date_joined') today = now() if days == 24: begin = today - timedelta(hours=days - 1) return stats.time_series(begin, today + timedelta(hours=1), interval) begin = today - timedelta(days=days - 1) return stats.time_series(begin, today + timedelta(days=1), interval) except (LookupError, FieldError, TypeError) as e: self.error_message = str(e) User = get_user_model() stats = QuerySetStats(User.objects.filter(is_active=True), 'date_joined') today = now() if days == 24: begin = today - timedelta(hours=days - 1) return stats.time_series(begin, today + timedelta(hours=1), interval) begin = today - timedelta(days=days - 1) return stats.time_series(begin, today + timedelta(days=1), interval)
def get_operation(self, dkwargs=None): if not self.type_operation_field_name: self.type_operation_field_name = 'Count' if not self.operation_field_name: self.operation_field_name = 'id' operation = { 'AvgCountPerInstance': lambda field_name, distinct, dkwargs: ExpressionWrapper( 1.0 * Count(field_name, distinct=distinct, filter=dkwargs) / Count('id', distinct=True, filter=Q(**{field_name + "__isnull": False})), output_field=models.FloatField(), ), 'Count': lambda field_name, distinct, dkwargs: Count( field_name, distinct=distinct, filter=dkwargs), 'Sum': lambda field_name, distinct, dkwargs: Sum( field_name, distinct=distinct, filter=dkwargs), 'Avg': lambda field_name, distinct, dkwargs: Avg( field_name, distinct=distinct, filter=dkwargs), 'StdDev': lambda field_name, distinct, dkwargs: StdDev(field_name, filter=dkwargs), 'Max': lambda field_name, distinct, dkwargs: Max(field_name, filter=dkwargs), 'Min': lambda field_name, distinct, dkwargs: Min(field_name, filter=dkwargs), 'Variance': lambda field_name, distinct, dkwargs: Variance(field_name, filter=dkwargs), } return operation[self.type_operation_field_name]( self.operation_field_name, self.distinct, dkwargs)
def get_time_series(self, dynamic_criteria, all_criteria, request, time_since, time_until, interval): """ Get the stats time series """ model_name = apps.get_model(self.model_app_name, self.model_name) kwargs = {} dynamic_kwargs = [] if request and not request.user.is_superuser and self.user_field_name: kwargs[self.user_field_name] = request.user for m2m in all_criteria: criteria = m2m.criteria # fixed mapping value passed info kwargs if criteria.criteria_fix_mapping: for key in criteria.criteria_fix_mapping: # value => criteria.criteria_fix_mapping[key] kwargs[key] = criteria.criteria_fix_mapping[key] # dynamic mapping value passed info kwargs dynamic_key = "select_box_dynamic_%i" % m2m.id if dynamic_key in dynamic_criteria: if dynamic_criteria[dynamic_key] != '': dynamic_values = dynamic_criteria[dynamic_key] dynamic_field_name = m2m.get_dynamic_criteria_field_name() criteria_key = 'id' if dynamic_field_name == '' else dynamic_field_name if isinstance(dynamic_values, (list, tuple)): single_value = False else: dynamic_values = (dynamic_values, ) single_value = True for dynamic_value in dynamic_values: criteria_value = m2m.get_dynamic_choices( )[dynamic_value] if isinstance(criteria_value, (list, tuple)): criteria_value = criteria_value[0] else: criteria_value = dynamic_value if single_value: kwargs[criteria_key] = criteria_value else: dynamic_kwargs.append( Q(**{criteria_key: criteria_value})) aggregate_dict = {} i = 0 if not dynamic_kwargs: dynamic_kwargs = [None] for dkwargs in dynamic_kwargs: i += 1 if not self.type_operation_field_name: self.type_operation_field_name = 'Count' if not self.operation_field_name: self.operation_field_name = 'id' operation = { 'AvgCountPerInstance': lambda field_name, distinct, dkwargs: ExpressionWrapper( 1.0 * Count(field_name, distinct=distinct, filter=dkwargs) / Count('id', distinct=True, filter=Q(**{field_name + "__isnull": False})), output_field=models.FloatField()), 'Count': lambda field_name, distinct, dkwargs: Count( field_name, distinct=distinct, filter=dkwargs), 'Sum': lambda field_name, distinct, dkwargs: Sum( field_name, distinct=distinct, filter=dkwargs), 'Avg': lambda field_name, distinct, dkwargs: Avg( field_name, distinct=distinct, filter=dkwargs), 'StdDev': lambda field_name, distinct, dkwargs: StdDev(field_name, filter=dkwargs), 'Max': lambda field_name, distinct, dkwargs: Max(field_name, filter=dkwargs), 'Min': lambda field_name, distinct, dkwargs: Min(field_name, filter=dkwargs), 'Variance': lambda field_name, distinct, dkwargs: Variance(field_name, filter=dkwargs), } aggregate_dict['agg_%i' % i] = operation[self.type_operation_field_name]( self.operation_field_name, self.distinct, dkwargs) # TODO: maybe backport values_list support back to django-qsstats-magic and use it again for the query time_range = { '%s__range' % self.date_field_name: (time_since, time_until) } qs = model_name.objects qs = qs.filter(**time_range) qs = qs.filter(**kwargs) if hasattr(time_since, 'tzinfo') and time_since.tzinfo: tzinfo = {'tzinfo': time_since.tzinfo} else: tzinfo = {} qs = qs.annotate(d=Trunc(self.date_field_name, interval, **tzinfo)) qs = qs.values_list('d') qs = qs.order_by('d') qs = qs.annotate(**aggregate_dict) return qs