def get_chart_data_for_group(self, instances, max_days=90): if not instances: return [] if hasattr(instances[0], '_state'): db = instances[0]._state.db or 'default' else: db = 'default' if not has_charts(db): return [] hours = max_days * 24 today = datetime.datetime.now().replace(microsecond=0, second=0, minute=0) min_date = today - datetime.timedelta(hours=hours) method = get_sql_date_trunc('date', db) field = self.model.messagecountbyminute_set.related column = field.field.name chart_qs = list(field.model.objects.filter(**{ '%s__in' % column: instances, 'date__gte': min_date, }).extra( select={ 'grouper': method, } ).values('grouper').annotate( num=Sum('times_seen'), ).values_list('grouper', 'num')) rows = dict(chart_qs) return [rows.get(today - datetime.timedelta(hours=d), 0) for d in xrange(hours, -1, -1)]
def get_chart_data(self, instance, max_days=90): if hasattr(instance, '_state'): db = instance._state.db else: db = 'default' if not has_charts(db): return [] hours = max_days * 24 today = datetime.datetime.now().replace(microsecond=0, second=0, minute=0) min_date = today - datetime.timedelta(hours=hours) method = get_sql_date_trunc('date', db) chart_qs = list(instance.messagecountbyminute_set\ .filter(date__gte=min_date)\ .extra(select={'grouper': method}).values('grouper')\ .annotate(num=Sum('times_seen')).values_list('grouper', 'num')\ .order_by('grouper')) if not chart_qs: return [] rows = dict(chart_qs) #just skip zeroes first_seen = hours while not rows.get(today - datetime.timedelta(hours=first_seen)) and first_seen > 24: first_seen -= 1 return [rows.get(today - datetime.timedelta(hours=d), 0) for d in xrange(first_seen, -1, -1)]
def get_chart_data(self, instance, max_days=90): if hasattr(instance, '_state'): db = instance._state.db or 'default' else: db = 'default' if not has_charts(db): return [] hours = max_days * 24 today = datetime.datetime.now().replace(microsecond=0, second=0, minute=0) min_date = today - datetime.timedelta(hours=hours) method = get_sql_date_trunc('date', db) chart_qs = list(instance.messagecountbyminute_set\ .filter(date__gte=min_date)\ .extra(select={'grouper': method}).values('grouper')\ .annotate(num=Sum('times_seen')).values_list('grouper', 'num')\ .order_by('grouper')) if not chart_qs: return [] rows = dict(chart_qs) #just skip zeroes first_seen = hours while not rows.get(today - datetime.timedelta(hours=first_seen)) and first_seen > 24: first_seen -= 1 return [rows.get(today - datetime.timedelta(hours=d), 0) for d in xrange(first_seen, -1, -1)]
def _get_chart_data(self, queryset, max_days=90, db='default'): if not has_charts(db): return [] today = timezone.now().replace(microsecond=0, second=0, minute=0) min_date = today - datetime.timedelta(days=max_days) if max_days > 30: g_type = 'hour' d_type = 'hours' points = max_days * 24 modifier = 1 else: g_type = 'minute' d_type = 'minutes' points = max_days * 24 * 15 modifier = 5 method = get_sql_date_trunc('date', db, grouper=g_type) chart_qs = list(queryset.filter(date__gte=min_date) .extra(select={'grouper': method}).values('grouper') .annotate(num=Sum('times_seen')).values_list('grouper', 'num') .order_by('grouper')) rows = dict(chart_qs) results = [] for point in xrange(points, -1, -1): dt = today - datetime.timedelta(**{d_type: point * modifier}) results.append((int((dt).strftime('%s')) * 1000, rows.get(dt, 0))) return results
def _get_chart_data(self, queryset, max_days=90, db='default'): if not has_charts(db): return [] today = timezone.now().replace(microsecond=0, second=0, minute=0) min_date = today - datetime.timedelta(days=max_days) if max_days > 30: g_type = 'hour' d_type = 'hours' points = max_days * 24 modifier = 1 else: g_type = 'minute' d_type = 'minutes' points = max_days * 24 * 15 modifier = 5 method = get_sql_date_trunc('date', db, grouper=g_type) chart_qs = list( queryset.filter(date__gte=min_date).extra(select={ 'grouper': method }).values('grouper').annotate(num=Sum('times_seen')).values_list( 'grouper', 'num').order_by('grouper')) rows = dict(chart_qs) results = [] for point in xrange(points, -1, -1): dt = today - datetime.timedelta(**{d_type: point * modifier}) results.append((int((dt).strftime('%s')) * 1000, rows.get(dt, 0))) return results
def get_chart_data_for_group(self, instances, max_days=90): if not instances: return [] if hasattr(instances[0], '_state'): db = instances[0]._state.db or 'default' else: db = 'default' if not has_charts(db): return [] hours = max_days * 24 today = datetime.datetime.now().replace(microsecond=0, second=0, minute=0) min_date = today - datetime.timedelta(hours=hours) method = get_sql_date_trunc('date', db) field = self.model.messagecountbyminute_set.related column = field.field.name chart_qs = list( field.model.objects.filter(**{ '%s__in' % column: instances, 'date__gte': min_date, }).extra(select={ 'grouper': method, }).values('grouper').annotate(num=Sum('times_seen'), ).values_list( 'grouper', 'num')) rows = dict(chart_qs) return [ rows.get(today - datetime.timedelta(hours=d), 0) for d in xrange(hours, -1, -1) ]
def _get_chart_data(self, queryset, max_days=90, db="default", key=None): if not has_charts(db): if key is None: return [] return {} today = timezone.now().replace(microsecond=0, second=0) # the last interval is not accurate, so we exclude it # TODO: it'd be ideal to normalize the last datapoint so that we can include it # and not have ~inaccurate data for up to MINUTE_NORMALIZATION today -= datetime.timedelta(minutes=settings.MINUTE_NORMALIZATION) if max_days >= 30: g_type = "date" d_type = "days" points = max_days modifier = 1 today = today.replace(hour=0) elif max_days >= 1: g_type = "hour" d_type = "hours" points = max_days * 24 modifier = 1 today = today.replace(minute=0) else: g_type = "minute" d_type = "minutes" modifier = settings.MINUTE_NORMALIZATION points = max_days * 24 * (60 / modifier) min_date = today - datetime.timedelta(days=max_days) method = get_sql_date_trunc("date", db, grouper=g_type) chart_qs = queryset.filter(date__gte=min_date).extra(select={"grouper": method}) if key: chart_qs = chart_qs.values("grouper", key) else: chart_qs = chart_qs.values("grouper") chart_qs = chart_qs.annotate(num=Sum("times_seen")) if key: chart_qs = chart_qs.values_list(key, "grouper", "num").order_by(key, "grouper") else: chart_qs = chart_qs.values_list("grouper", "num").order_by("grouper") if key is None: rows = {None: dict(chart_qs)} else: rows = {} for item, grouper, num in chart_qs: if item not in rows: rows[item] = {} rows[item][grouper] = num results = {} for item, tsdata in rows.iteritems(): results[item] = [] for point in xrange(points, -1, -1): dt = today - datetime.timedelta(**{d_type: point * modifier}) results[item].append((int((dt).strftime("%s")) * 1000, tsdata.get(dt, 0))) if key is None: return results[None] return results
def _get_chart_data(self, queryset, max_days=90, db='default', key=None): if not has_charts(db): if key is None: return [] return {} today = timezone.now().replace(microsecond=0, second=0) # the last interval is not accurate, so we exclude it # TODO: it'd be ideal to normalize the last datapoint so that we can include it # and not have ~inaccurate data for up to MINUTE_NORMALIZATION today -= datetime.timedelta(minutes=settings.MINUTE_NORMALIZATION) if max_days >= 30: g_type = 'date' d_type = 'days' points = max_days modifier = 1 today = today.replace(hour=0) elif max_days >= 1: g_type = 'hour' d_type = 'hours' points = max_days * 24 modifier = 1 today = today.replace(minute=0) else: g_type = 'minute' d_type = 'minutes' modifier = settings.MINUTE_NORMALIZATION points = max_days * 24 * (60 / modifier) min_date = today - datetime.timedelta(days=max_days) method = get_sql_date_trunc('date', db, grouper=g_type) chart_qs = queryset.filter(date__gte=min_date, ).extra( select={'grouper': method}, ) if key: chart_qs = chart_qs.values('grouper', key) else: chart_qs = chart_qs.values('grouper') chart_qs = chart_qs.annotate(num=Sum('times_seen'), ) if key: chart_qs = chart_qs.values_list(key, 'grouper', 'num').order_by(key, 'grouper') else: chart_qs = chart_qs.values_list('grouper', 'num').order_by('grouper') if key is None: rows = {None: dict(chart_qs)} else: rows = {} for item, grouper, num in chart_qs: if item not in rows: rows[item] = {} rows[item][grouper] = num results = {} for item, tsdata in rows.iteritems(): results[item] = [] for point in xrange(points, -1, -1): dt = today - datetime.timedelta(**{d_type: point * modifier}) results[item].append((int( (dt).strftime('%s')) * 1000, tsdata.get(dt, 0))) if key is None: return results[None] return results
def _get_chart_data(self, queryset, max_days=90, db='default', key=None): if not has_charts(db): if key is None: return [] return {} today = timezone.now().replace(microsecond=0, second=0) # the last interval is not accurate, so we exclude it # TODO: it'd be ideal to normalize the last datapoint so that we can include it # and not have ~inaccurate data for up to MINUTE_NORMALIZATION today -= datetime.timedelta(minutes=MINUTE_NORMALIZATION) if max_days >= 30: g_type = 'date' d_type = 'days' points = max_days modifier = 1 today = today.replace(hour=0) elif max_days >= 1: g_type = 'hour' d_type = 'hours' points = max_days * 24 modifier = 1 today = today.replace(minute=0) else: g_type = 'minute' d_type = 'minutes' modifier = MINUTE_NORMALIZATION points = max_days * 24 * (60 / modifier) min_date = today - datetime.timedelta(days=max_days) method = get_sql_date_trunc('date', db, grouper=g_type) chart_qs = queryset.filter( date__gte=min_date, ).extra( select={'grouper': method}, ) if key: chart_qs = chart_qs.values('grouper', key) else: chart_qs = chart_qs.values('grouper') chart_qs = chart_qs.annotate( num=Sum('times_seen'), ) if key: chart_qs = chart_qs.values_list(key, 'grouper', 'num').order_by(key, 'grouper') else: chart_qs = chart_qs.values_list('grouper', 'num').order_by('grouper') if key is None: rows = {None: dict(chart_qs)} else: rows = {} for item, grouper, num in chart_qs: if item not in rows: rows[item] = {} rows[item][grouper] = num results = {} for item, tsdata in rows.iteritems(): results[item] = [] for point in xrange(points, -1, -1): dt = today - datetime.timedelta(**{d_type: point * modifier}) results[item].append((int(time.mktime((dt).timetuple())) * 1000, tsdata.get(dt, 0))) if key is None: return results[None] return results