counts.py: Move DataCollector declarations into CountStat declarations.

The previous zerver_* names were unwieldy and not very readable. This also
puts more of the useful information in one place; in particular, makes it
easier to skim a CountStat declaration and see if we're collecting it at a
user/stream granularity or a realm granularity.
This commit is contained in:
Rishi Gupta
2017-04-01 17:28:03 -07:00
committed by Tim Abbott
parent c20e79ab1f
commit f9e56ad25d

View File

@@ -260,8 +260,6 @@ count_user_by_realm_query = """
zerver_userprofile.is_active = TRUE
GROUP BY zerver_realm.id %(group_by_clause)s
"""
zerver_count_user_by_realm = DataCollector(RealmCount, count_user_by_realm_query,
(UserProfile, 'is_bot'))
# currently .sender_id is only Message specific thing
count_message_by_user_query = """
@@ -279,10 +277,6 @@ count_message_by_user_query = """
zerver_message.pub_date < %%(time_end)s
GROUP BY zerver_userprofile.id %(group_by_clause)s
"""
zerver_count_message_by_user_is_bot = DataCollector(UserCount, count_message_by_user_query,
(UserProfile, 'is_bot'))
zerver_count_message_by_user_client = DataCollector(UserCount, count_message_by_user_query,
(Message, 'sending_client_id'))
# Currently unused and untested
count_stream_by_realm_query = """
@@ -300,7 +294,6 @@ count_stream_by_realm_query = """
zerver_stream.date_created < %%(time_end)s
GROUP BY zerver_realm.id %(group_by_clause)s
"""
zerver_count_stream_by_realm = DataCollector(RealmCount, count_stream_by_realm_query, None)
# This query violates the count_X_by_Y_query conventions in several ways. One,
# the X table is not specified by the query name; MessageType is not a zerver
@@ -339,7 +332,6 @@ count_message_type_by_user_query = """
) AS subquery
GROUP BY realm_id, id, message_type
"""
zerver_count_message_type_by_user = DataCollector(UserCount, count_message_type_by_user_query, None)
# Note that this query also joins to the UserProfile table, since all
# current queries that use this also subgroup on UserProfile.is_bot. If in
@@ -367,8 +359,6 @@ count_message_by_stream_query = """
zerver_message.pub_date < %%(time_end)s
GROUP BY zerver_stream.id %(group_by_clause)s
"""
zerver_count_message_by_stream = DataCollector(StreamCount, count_message_by_stream_query,
(UserProfile, 'is_bot'))
check_useractivityinterval_by_user_query = """
INSERT INTO analytics_usercount
@@ -384,8 +374,6 @@ check_useractivityinterval_by_user_query = """
zerver_useractivityinterval.start < %%(time_end)s
GROUP BY zerver_userprofile.id %(group_by_clause)s
"""
zerver_check_useractivityinterval_by_user = DataCollector(
UserCount, check_useractivityinterval_by_user_query, None)
# Currently hardcodes the query needed for active_users_audit:is_bot:day.
# Assumes that a user cannot have two RealmAuditLog entries with the same event_time and
@@ -414,8 +402,6 @@ check_realmauditlog_by_user_query = """
WHERE
ral1.event_type in ('user_created', 'user_activated', 'user_reactivated')
"""
zerver_check_realmauditlog_by_user = DataCollector(UserCount, check_realmauditlog_by_user_query,
(UserProfile, 'is_bot'))
def do_pull_minutes_active(stat, start_time, end_time):
# type: (CountStat, datetime, datetime) -> None
@@ -442,27 +428,38 @@ def do_pull_minutes_active(stat, start_time, end_time):
(stat.property, (time.time()-timer_start)*1000, len(rows)))
count_stats_ = [
CountStat('messages_sent:is_bot:hour', zerver_count_message_by_user_is_bot, CountStat.HOUR),
CountStat('messages_sent:message_type:day', zerver_count_message_type_by_user, CountStat.DAY),
CountStat('messages_sent:client:day', zerver_count_message_by_user_client, CountStat.DAY),
CountStat('messages_in_stream:is_bot:day', zerver_count_message_by_stream, CountStat.DAY),
CountStat('messages_sent:is_bot:hour',
DataCollector(UserCount, count_message_by_user_query, (UserProfile, 'is_bot')),
CountStat.HOUR),
CountStat('messages_sent:message_type:day',
DataCollector(UserCount, count_message_type_by_user_query, None), CountStat.DAY),
CountStat('messages_sent:client:day',
DataCollector(UserCount, count_message_by_user_query, (Message, 'sending_client_id')),
CountStat.DAY),
CountStat('messages_in_stream:is_bot:day',
DataCollector(StreamCount, count_message_by_stream_query, (UserProfile, 'is_bot')),
CountStat.DAY),
# Sanity check on the bottom two stats. Is only an approximation,
# e.g. if a user is deactivated between the end of the day and when this
# stat is run, they won't be counted.
CountStat('active_users:is_bot:day', zerver_count_user_by_realm,
CountStat('active_users:is_bot:day',
DataCollector(RealmCount, count_user_by_realm_query, (UserProfile, 'is_bot')),
CountStat.DAY, interval=TIMEDELTA_MAX),
# In RealmCount, 'active_humans_audit::day' should be the partial sum sequence
# of 'active_users_log:is_bot:day', for any realm that started after the
# latter stat was introduced.
# 'active_users_audit:is_bot:day' is the canonical record of which users were
# active on which days (in the UserProfile.is_active sense).
CountStat('active_users_audit:is_bot:day', zerver_check_realmauditlog_by_user, CountStat.DAY),
CountStat('active_users_audit:is_bot:day',
DataCollector(UserCount, check_realmauditlog_by_user_query, (UserProfile, 'is_bot')),
CountStat.DAY),
LoggingCountStat('active_users_log:is_bot:day', RealmCount, CountStat.DAY),
# The minutes=15 part is due to the 15 minutes added in
# zerver.lib.actions.do_update_user_activity_interval.
CountStat('15day_actives::day', zerver_check_useractivityinterval_by_user,
CountStat('15day_actives::day',
DataCollector(UserCount, check_useractivityinterval_by_user_query, None),
CountStat.DAY, interval=timedelta(days=15)-timedelta(minutes=15)),
CustomPullCountStat('minutes_active::day', UserCount, CountStat.DAY, do_pull_minutes_active)
]