2017-01-07 04:21:34 +01:00
|
|
|
from __future__ import absolute_import, print_function
|
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
from analytics.models import InstallationCount, RealmCount, UserCount, StreamCount
|
|
|
|
from analytics.lib.counts import COUNT_STATS, CountStat, do_drop_all_analytics_tables
|
|
|
|
from analytics.lib.fixtures import generate_time_series_data, bulk_create_realmcount
|
|
|
|
from zerver.lib.timestamp import floor_to_day
|
|
|
|
from zerver.models import Realm, UserProfile, Stream, Message
|
|
|
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from typing import Any, Text
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = """Populates analytics tables with randomly generated data."""
|
|
|
|
|
|
|
|
def create_user(self, email, full_name, is_staff, date_joined, realm):
|
|
|
|
# type: (Text, Text, Text, bool, datetime, Realm) -> UserProfile
|
2017-01-16 20:13:18 +01:00
|
|
|
return 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-01-07 04:21:34 +01:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
# type: (*Any, **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
|
|
|
|
|
|
|
installation_time = timezone.now() - timedelta(days=100)
|
2017-01-16 20:13:18 +01:00
|
|
|
realm = Realm.objects.create(
|
2017-01-07 04:21:34 +01:00
|
|
|
string_id='analytics', name='Analytics', domain='analytics.ds',
|
2017-01-16 20:13:18 +01:00
|
|
|
date_created=installation_time)
|
2017-01-07 04:21:34 +01:00
|
|
|
self.create_user('shylock@analytics.ds', 'Shylock', True, installation_time, realm)
|
|
|
|
|
|
|
|
stat = COUNT_STATS['active_users:is_bot']
|
|
|
|
if not RealmCount.objects.filter(property=stat.property).exists():
|
|
|
|
last_end_time = floor_to_day(timezone.now())
|
2017-01-16 20:05:21 +01:00
|
|
|
human_data = generate_time_series_data(days=100, business_hours_base=30,
|
|
|
|
non_business_hours_base=10, growth=5, autocorrelation=.5,
|
2017-01-07 04:21:34 +01:00
|
|
|
spikiness=3, frequency=CountStat.DAY)
|
2017-01-16 20:05:21 +01:00
|
|
|
bot_data = generate_time_series_data(days=100, business_hours_base=20,
|
|
|
|
non_business_hours_base=20, growth=3, frequency=CountStat.DAY)
|
2017-01-07 04:21:34 +01:00
|
|
|
bulk_create_realmcount(stat.property, 'false', last_end_time,
|
|
|
|
stat.frequency, stat.interval, human_data, realm)
|
|
|
|
bulk_create_realmcount(stat.property, 'true', last_end_time,
|
|
|
|
stat.frequency, stat.interval, bot_data, realm)
|