2017-01-07 04:21:34 +01:00
|
|
|
|
2017-11-16 00:55:49 +01:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from typing import Any, Dict, List, Mapping, Optional, Text, Type, Union
|
|
|
|
|
2017-01-07 04:21:34 +01:00
|
|
|
from django.core.management.base import BaseCommand
|
2017-04-15 04:03:56 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2017-01-07 04:21:34 +01:00
|
|
|
|
2017-11-16 00:55:49 +01:00
|
|
|
from analytics.lib.counts import COUNT_STATS, \
|
|
|
|
CountStat, do_drop_all_analytics_tables
|
2017-01-16 20:25:06 +01:00
|
|
|
from analytics.lib.fixtures import generate_time_series_data
|
|
|
|
from analytics.lib.time_utils import time_range
|
2017-11-16 00:55:49 +01:00
|
|
|
from analytics.models import BaseCount, FillState, RealmCount, UserCount
|
2017-01-07 04:21:34 +01:00
|
|
|
from zerver.lib.timestamp import floor_to_day
|
2017-11-16 00:55:49 +01:00
|
|
|
from zerver.models import Client, Realm, RealmAuditLog, UserProfile
|
2017-01-07 04:21:34 +01:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = """Populates analytics tables with randomly generated data."""
|
|
|
|
|
2017-01-16 20:25:06 +01:00
|
|
|
DAYS_OF_DATA = 100
|
2017-01-17 22:55:16 +01:00
|
|
|
random_seed = 26
|
2017-01-16 20:25:06 +01:00
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def create_user(self, email: Text,
|
|
|
|
full_name: Text,
|
|
|
|
is_staff: bool,
|
|
|
|
date_joined: datetime,
|
|
|
|
realm: Realm) -> UserProfile:
|
2017-02-15 04:35:10 +01:00
|
|
|
user = UserProfile.objects.create(
|
2017-01-07 04:21:34 +01:00
|
|
|
email=email, full_name=full_name, is_staff=is_staff,
|
|
|
|
realm=realm, short_name=full_name, pointer=-1, last_pointer_updater='none',
|
2017-01-16 20:13:18 +01:00
|
|
|
api_key='42', date_joined=date_joined)
|
2017-02-15 04:35:10 +01:00
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=realm, modified_user=user, event_type='user_created',
|
|
|
|
event_time=user.date_joined)
|
|
|
|
return user
|
2017-01-07 04:21:34 +01:00
|
|
|
|
2017-01-16 20:25:06 +01:00
|
|
|
def generate_fixture_data(self, stat, business_hours_base, non_business_hours_base,
|
2017-04-25 23:54:30 +02:00
|
|
|
growth, autocorrelation, spikiness, holiday_rate=0,
|
|
|
|
partial_sum=False):
|
|
|
|
# type: (CountStat, float, float, float, float, float, float, bool) -> List[int]
|
2017-01-17 22:55:16 +01:00
|
|
|
self.random_seed += 1
|
2017-01-16 20:25:06 +01:00
|
|
|
return generate_time_series_data(
|
|
|
|
days=self.DAYS_OF_DATA, business_hours_base=business_hours_base,
|
|
|
|
non_business_hours_base=non_business_hours_base, growth=growth,
|
|
|
|
autocorrelation=autocorrelation, spikiness=spikiness, holiday_rate=holiday_rate,
|
2017-04-25 23:54:30 +02:00
|
|
|
frequency=stat.frequency, partial_sum=partial_sum, random_seed=self.random_seed)
|
2017-01-16 20:25:06 +01:00
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2017-01-16 20:13:18 +01:00
|
|
|
do_drop_all_analytics_tables()
|
|
|
|
# I believe this also deletes any objects with this realm as a foreign key
|
|
|
|
Realm.objects.filter(string_id='analytics').delete()
|
2017-01-07 04:21:34 +01:00
|
|
|
|
2017-04-15 04:03:56 +02:00
|
|
|
installation_time = timezone_now() - timedelta(days=self.DAYS_OF_DATA)
|
|
|
|
last_end_time = floor_to_day(timezone_now())
|
2017-01-16 20:13:18 +01:00
|
|
|
realm = Realm.objects.create(
|
2017-03-14 00:51:51 +01:00
|
|
|
string_id='analytics', name='Analytics', date_created=installation_time)
|
2017-01-16 20:25:06 +01:00
|
|
|
shylock = self.create_user('shylock@analytics.ds', 'Shylock', True, installation_time, realm)
|
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def insert_fixture_data(stat: CountStat,
|
|
|
|
fixture_data: Mapping[Optional[str], List[int]],
|
|
|
|
table: Type[BaseCount]) -> None:
|
2017-01-16 20:25:06 +01:00
|
|
|
end_times = time_range(last_end_time, last_end_time, stat.frequency,
|
|
|
|
len(list(fixture_data.values())[0]))
|
|
|
|
if table == RealmCount:
|
|
|
|
id_args = {'realm': realm}
|
|
|
|
if table == UserCount:
|
|
|
|
id_args = {'realm': realm, 'user': shylock}
|
|
|
|
for subgroup, values in fixture_data.items():
|
|
|
|
table.objects.bulk_create([
|
|
|
|
table(property=stat.property, subgroup=subgroup, end_time=end_time,
|
2017-01-16 22:05:51 +01:00
|
|
|
value=value, **id_args)
|
2017-01-16 20:25:06 +01:00
|
|
|
for end_time, value in zip(end_times, values) if value != 0])
|
2017-01-07 04:21:34 +01:00
|
|
|
|
2017-04-25 23:54:30 +02:00
|
|
|
stat = COUNT_STATS['realm_active_humans::day']
|
2017-01-16 20:59:00 +01:00
|
|
|
realm_data = {
|
2017-04-25 23:54:30 +02:00
|
|
|
None: self.generate_fixture_data(stat, .1, .03, 3, .5, 3, partial_sum=True),
|
2017-05-31 21:39:07 +02:00
|
|
|
} # type: Mapping[Optional[str], List[int]]
|
2017-01-16 20:25:06 +01:00
|
|
|
insert_fixture_data(stat, realm_data, RealmCount)
|
2017-02-08 08:04:10 +01:00
|
|
|
FillState.objects.create(property=stat.property, end_time=last_end_time,
|
|
|
|
state=FillState.DONE)
|
2017-01-16 20:59:00 +01:00
|
|
|
|
2017-01-16 22:39:03 +01:00
|
|
|
stat = COUNT_STATS['messages_sent:is_bot:hour']
|
2017-05-26 02:36:54 +02:00
|
|
|
user_data = {'false': self.generate_fixture_data(
|
|
|
|
stat, 2, 1, 1.5, .6, 8, holiday_rate=.1)} # type: Mapping[Optional[str], List[int]]
|
2017-01-16 20:59:00 +01:00
|
|
|
insert_fixture_data(stat, user_data, UserCount)
|
|
|
|
realm_data = {'false': self.generate_fixture_data(stat, 35, 15, 6, .6, 4),
|
|
|
|
'true': self.generate_fixture_data(stat, 15, 15, 3, .4, 2)}
|
|
|
|
insert_fixture_data(stat, realm_data, RealmCount)
|
2017-02-08 08:04:10 +01:00
|
|
|
FillState.objects.create(property=stat.property, end_time=last_end_time,
|
|
|
|
state=FillState.DONE)
|
2017-01-16 20:59:00 +01:00
|
|
|
|
2017-01-16 22:05:16 +01:00
|
|
|
stat = COUNT_STATS['messages_sent:message_type:day']
|
2017-01-16 20:59:00 +01:00
|
|
|
user_data = {
|
|
|
|
'public_stream': self.generate_fixture_data(stat, 1.5, 1, 3, .6, 8),
|
2017-03-19 00:11:07 +01:00
|
|
|
'private_message': self.generate_fixture_data(stat, .5, .3, 1, .6, 8),
|
|
|
|
'huddle_message': self.generate_fixture_data(stat, .2, .2, 2, .6, 8)}
|
2017-01-16 20:59:00 +01:00
|
|
|
insert_fixture_data(stat, user_data, UserCount)
|
|
|
|
realm_data = {
|
|
|
|
'public_stream': self.generate_fixture_data(stat, 30, 8, 5, .6, 4),
|
|
|
|
'private_stream': self.generate_fixture_data(stat, 7, 7, 5, .6, 4),
|
2017-03-19 00:11:07 +01:00
|
|
|
'private_message': self.generate_fixture_data(stat, 13, 5, 5, .6, 4),
|
|
|
|
'huddle_message': self.generate_fixture_data(stat, 6, 3, 3, .6, 4)}
|
2017-01-16 20:59:00 +01:00
|
|
|
insert_fixture_data(stat, realm_data, RealmCount)
|
2017-02-08 08:04:10 +01:00
|
|
|
FillState.objects.create(property=stat.property, end_time=last_end_time,
|
|
|
|
state=FillState.DONE)
|
2017-01-16 20:59:00 +01:00
|
|
|
|
2017-04-12 07:25:53 +02:00
|
|
|
website, created = Client.objects.get_or_create(name='website')
|
|
|
|
old_desktop, created = Client.objects.get_or_create(name='desktop app Linux 0.3.7')
|
|
|
|
android, created = Client.objects.get_or_create(name='ZulipAndroid')
|
|
|
|
iOS, created = Client.objects.get_or_create(name='ZulipiOS')
|
|
|
|
react_native, created = Client.objects.get_or_create(name='ZulipMobile')
|
|
|
|
API, created = Client.objects.get_or_create(name='API: Python')
|
|
|
|
zephyr_mirror, created = Client.objects.get_or_create(name='zephyr_mirror')
|
|
|
|
unused, created = Client.objects.get_or_create(name='unused')
|
|
|
|
long_webhook, created = Client.objects.get_or_create(name='ZulipLooooooooooongNameWebhook')
|
2017-01-16 20:59:00 +01:00
|
|
|
|
2017-01-16 22:05:16 +01:00
|
|
|
stat = COUNT_STATS['messages_sent:client:day']
|
2017-01-16 20:59:00 +01:00
|
|
|
user_data = {
|
2017-02-09 02:55:18 +01:00
|
|
|
website.id: self.generate_fixture_data(stat, 2, 1, 1.5, .6, 8),
|
2017-04-12 07:25:53 +02:00
|
|
|
zephyr_mirror.id: self.generate_fixture_data(stat, 0, .3, 1.5, .6, 8)}
|
2017-01-16 20:59:00 +01:00
|
|
|
insert_fixture_data(stat, user_data, UserCount)
|
|
|
|
realm_data = {
|
2017-02-09 02:55:18 +01:00
|
|
|
website.id: self.generate_fixture_data(stat, 30, 20, 5, .6, 3),
|
|
|
|
old_desktop.id: self.generate_fixture_data(stat, 5, 3, 8, .6, 3),
|
|
|
|
android.id: self.generate_fixture_data(stat, 5, 5, 2, .6, 3),
|
|
|
|
iOS.id: self.generate_fixture_data(stat, 5, 5, 2, .6, 3),
|
|
|
|
react_native.id: self.generate_fixture_data(stat, 5, 5, 10, .6, 3),
|
|
|
|
API.id: self.generate_fixture_data(stat, 5, 5, 5, .6, 3),
|
2017-04-12 07:25:53 +02:00
|
|
|
zephyr_mirror.id: self.generate_fixture_data(stat, 1, 1, 3, .6, 3),
|
2017-02-09 02:55:18 +01:00
|
|
|
unused.id: self.generate_fixture_data(stat, 0, 0, 0, 0, 0),
|
|
|
|
long_webhook.id: self.generate_fixture_data(stat, 5, 5, 2, .6, 3)}
|
2017-01-16 20:59:00 +01:00
|
|
|
insert_fixture_data(stat, realm_data, RealmCount)
|
2017-02-08 08:04:10 +01:00
|
|
|
FillState.objects.create(property=stat.property, end_time=last_end_time,
|
|
|
|
state=FillState.DONE)
|
2017-01-16 20:59:00 +01:00
|
|
|
|
|
|
|
# TODO: messages_sent_to_stream:is_bot
|