Permalink
Browse files

Merge branch 'new-trends'

  • Loading branch information...
2 parents 92bc3c0 + 77bd8ae commit d0a948aeb0216b0391a13ddec36c67e478d795d3 @dcramer dcramer committed Sep 20, 2012
View
@@ -708,16 +708,26 @@ def get_accelerated(self, queryset=None, minutes=15):
if queryset is None:
queryset = self
- assert minutes >= settings.MINUTE_NORMALIZATION
+ normalization = float(settings.MINUTE_NORMALIZATION)
+ assert minutes >= normalization
engine = get_db_engine(queryset.db)
+ # We technically only support mysql and postgresql, since there seems to be no standard
+ # way to get the epoch from a datetime/interval
if engine.startswith('mysql'):
minute_clause = "interval %s minute"
+ epoch_clause = "unix_timestamp(now()) - unix_timestamp(%(mcbm_tbl)s.date)"
else:
minute_clause = "interval '%s minutes'"
+ epoch_clause = "extract(epoch from now()) - extract(epoch from %(mcbm_tbl)s.date)"
+
+ epoch_clause = epoch_clause % dict(mcbm_tbl=mcbm_tbl)
queryset = queryset.extra(
- where=["%s.date >= now() - %s" % (mcbm_tbl, minute_clause % (minutes, ))],
+ where=[
+ "%s.date >= now() - %s" % (mcbm_tbl, minute_clause % (minutes + 1, )),
+ "%s.date <= now() - %s" % (mcbm_tbl, minute_clause % (1, ))
+ ],
).annotate(x=Sum('messagecountbyminute__times_seen')).order_by('id')
sql, params = queryset.query.get_compiler(queryset.db).as_sql()
@@ -729,26 +739,31 @@ def get_accelerated(self, queryset=None, minutes=15):
after_group = after_group.split(' ORDER BY ')[0]
query = """
- SELECT (SUM(%(mcbm_tbl)s.times_seen) + 1.0) / (COALESCE(z.accel, 0) + 1.0) as accel,
- z.accel as prev_accel,
+ SELECT (SUM(%(mcbm_tbl)s.times_seen) * (%(norm)f / (%(epoch_clause)s / 60)) + 1.0) / (COALESCE(z.rate, 0) + 1.0) as accel,
+ (COALESCE(z.rate, 0) + 1.0) as prev_rate,
%(before_where)s
- LEFT JOIN (SELECT a.group_id, SUM(a.times_seen) / 3.0 as accel
+ LEFT JOIN (SELECT a.group_id, SUM(a.times_seen) / COUNT(a.times_seen) / %(norm)f as rate
FROM %(mcbm_tbl)s as a
WHERE a.date BETWEEN now() - %(max_time)s
AND now() - %(min_time)s
GROUP BY a.group_id) as z
ON z.group_id = %(mcbm_tbl)s.group_id
- WHERE %(before_group)s
- GROUP BY prev_accel, %(after_group)s
+ WHERE %(mcbm_tbl)s.date BETWEEN now() - %(min_time)s
+ AND now() - %(offset_time)s
+ AND %(before_group)s
+ GROUP BY prev_rate, %(mcbm_tbl)s.date, %(after_group)s
HAVING SUM(%(mcbm_tbl)s.times_seen) > 0
ORDER BY accel DESC
""" % dict(
mcbm_tbl=mcbm_tbl,
before_where=before_where,
before_group=before_group,
after_group=after_group,
+ offset_time=minute_clause % (1,),
min_time=minute_clause % (minutes + 1,),
- max_time=minute_clause % (minutes * 4,),
+ max_time=minute_clause % (minutes * (60 / normalization),),
+ norm=normalization,
+ epoch_clause=epoch_clause,
)
return RawQuerySet(self, query, params)
@@ -93,7 +93,6 @@ def backwards(self, orm):
'culprit': ('django.db.models.fields.CharField', [], {'max_length': '200', 'null': 'True', 'db_column': "'view'", 'blank': 'True'}),
'data': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
'first_seen': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now', 'db_index': 'True'}),
- 'group_id': ('django.db.models.fields.CharField', [], {'max_length': '32', 'unique': 'True', 'null': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'is_public': ('django.db.models.fields.NullBooleanField', [], {'default': 'False', 'null': 'True', 'blank': 'True'}),
'last_seen': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now', 'db_index': 'True'}),
Oops, something went wrong.

0 comments on commit d0a948a

Please sign in to comment.