dcramer/django-sentry — Issue #243: Add "trends" capability
Author: dcramer
Created: 2011-10-11 20:38:19+00:00
State: open

We want to be able to notify (and display) trends, which can be thought of as "accelerated events" based on certain conditions.

Several factors which should probably go into the calculation for the score:

- The current acceleration of an event
- How old the event is (first_seen)
- The total number of times an event has seen (might not be that useful?)

Some example code which we're using to prototype the behavior::

import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'disqus.conf.settings.dblog_viewer'

def get_trends(group_id=None):
from sentry.models import GroupedMessage, MessageCountByMinute
from django.db.models import Sum
mcbm_tbl = MessageCountByMinute._meta.db_table
qs = GroupedMessage.objects.extra(where=["%s.date >= now() - interval '6 minutes'" % mcbm_tbl]).annotate(x=Sum('messagecountbyminute__times_seen'))
if group_id:
qs = qs.filter(id=group_id)
sql, params = qs.query.get_compiler(qs.db).as_sql()
before_select, after_select = str(sql).split('SELECT ', 1)
before_where, after_where = after_select.split(' WHERE ', 1)
before_group, after_group = after_where.split(' GROUP BY ', 1)
query = """SELECT (SUM(%(mcbm_tbl)s.times_seen) + 1.0) / (COALESCE(z.freq, 0) + 1.0) as zfreq, SUM(%(mcbm_tbl)s.times_seen) as nfreq, z.freq as pfreq, %(before_where)s LEFT JOIN (SELECT a.group_id, SUM(a.times_seen) / 11.0 as freq FROM %(mcbm_tbl)s as a WHERE a.date BETWEEN now() - interval '1 hour' AND now() - interval '5 minutes' GROUP BY a.group_id) as z ON z.group_id = %(mcbm_tbl)s.group_id WHERE %(before_group)s GROUP BY pfreq, %(after_group)s ORDER BY zfreq DESC LIMIT 50
""" % dict(mcbm_tbl=mcbm_tbl, before_where=before_where, before_group=before_group, after_group=after_group)
for r in GroupedMessage.objects.raw(query, params)[:15]:
print '\t'.join(map(str, [r.zfreq, r.nfreq, r.pfreq, r.id, r.message]))

if __name__ == '__main__':
get_trends(sys.argv[-1] if sys.argv[-1].isdigit() else None)