Skip to content
Browse files

Support mysql (and postgresql) in trends

  • Loading branch information...
1 parent 4da9d98 commit 4b13cb91f377779f591ae197fd1b39c165c55ab2 @dcramer dcramer committed
Showing with 9 additions and 5 deletions.
  1. +8 −4 src/sentry/manager.py
  2. +1 −1 src/sentry/utils/db.py
View
12 src/sentry/manager.py
@@ -707,10 +707,16 @@ def get_accelerated(self, queryset=None, minutes=15):
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=[
@@ -727,11 +733,8 @@ def get_accelerated(self, queryset=None, minutes=15):
# Ensure we remove any ordering clause
after_group = after_group.split(' ORDER BY ')[0]
- # TODO: extract(epoch) is only available in pgsql
- # TODO: the subquery needs some conditions from the base query for efficiency
- # such as project IN (x) or group_id IN (x)
query = """
- SELECT (SUM(%(mcbm_tbl)s.times_seen) * (%(norm)f / (extract(epoch from now() - %(mcbm_tbl)s.date) / 60)) + 1.0) / (COALESCE(z.rate, 0) + 1.0) as 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) / COUNT(a.times_seen) / %(norm)f as rate
@@ -755,6 +758,7 @@ def get_accelerated(self, queryset=None, minutes=15):
min_time=minute_clause % (minutes + 1,),
max_time=minute_clause % (minutes * (60 / normalization),),
norm=normalization,
+ epoch_clause=epoch_clause,
)
return RawQuerySet(self, query, params)
View
2 src/sentry/utils/db.py
@@ -81,7 +81,7 @@ def get_db_engine(alias='default'):
def has_trending(alias='default'):
- return not get_db_engine('default').startswith('sqlite')
+ return get_db_engine('default').startswith(('mysql', 'postgres'))
def has_charts(db):

0 comments on commit 4b13cb9

Please sign in to comment.
Something went wrong with that request. Please try again.