def do_fill_count_stat_at_hour(stat, end_time): # type: (CountStat, datetime) -> None if stat.frequency == CountStat.DAY and (end_time != floor_to_day(end_time)): return if stat.interval == CountStat.HOUR: start_time = end_time - timedelta(hours = 1) elif stat.interval == CountStat.DAY: start_time = end_time - timedelta(days = 1) else: # stat.interval == CountStat.GAUGE start_time = MIN_TIME do_pull_from_zerver(stat, start_time, end_time) do_aggregate_to_summary_table(stat, end_time)
def do_fill_count_stat_at_hour(stat, end_time): # type: (CountStat, datetime) -> None if stat.frequency == CountStat.DAY and (end_time != floor_to_day(end_time)): return start_time = end_time - stat.interval if not isinstance(stat, LoggingCountStat): timer = time.time() rows_added = stat.data_collector.pull_function(stat.property, start_time, end_time) logger.info("%s run pull_function (%dms/%sr)" % (stat.property, (time.time() - timer) * 1000, rows_added)) do_aggregate_to_summary_table(stat, end_time)
def time_range(start, end, frequency, min_length): # type: (datetime, datetime, str, Optional[int]) -> List[datetime] if frequency == CountStat.HOUR: end = floor_to_hour(end) step = timedelta(hours=1) elif frequency == CountStat.DAY: end = floor_to_day(end) step = timedelta(days=1) else: raise ValueError("Unknown frequency: %s" % (frequency, )) times = [] if min_length is not None: start = min(start, end - (min_length - 1) * step) current = end while current >= start: times.append(current) current -= step return list(reversed(times))
def time_range(start, end, frequency, min_length): # type: (datetime, datetime, str, Optional[int]) -> List[datetime] if frequency == CountStat.HOUR: end = floor_to_hour(end) step = timedelta(hours=1) elif frequency == CountStat.DAY: end = floor_to_day(end) step = timedelta(days=1) else: raise ValueError("Unknown frequency: %s" % (frequency,)) times = [] if min_length is not None: start = min(start, end - (min_length-1)*step) current = end while current >= start: times.append(current) current -= step return list(reversed(times))
def time_range(start: datetime, end: datetime, frequency: str, min_length: Optional[int]) -> List[datetime]: verify_UTC(start) verify_UTC(end) if frequency == CountStat.HOUR: end = floor_to_hour(end) step = timedelta(hours=1) elif frequency == CountStat.DAY: end = floor_to_day(end) step = timedelta(days=1) else: raise AssertionError(f"Unknown frequency: {frequency}") times = [] if min_length is not None: start = min(start, end - (min_length - 1) * step) current = end while current >= start: times.append(current) current -= step return list(reversed(times))
def installation_epoch() -> datetime.datetime: earliest_realm_creation = Realm.objects.aggregate( models.Min('date_created'))['date_created__min'] return floor_to_day(earliest_realm_creation)
def handle(self, *args, **options): # type: (*Any, **Any) -> None 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() Client.objects.filter(name__startswith='_').delete() installation_time = timezone.now() - timedelta(days=self.DAYS_OF_DATA) last_end_time = floor_to_day(timezone.now()) realm = Realm.objects.create( string_id='analytics', name='Analytics', domain='analytics.ds', date_created=installation_time) shylock = self.create_user('*****@*****.**', 'Shylock', True, installation_time, realm) def insert_fixture_data(stat, fixture_data, table): # type: (CountStat, Dict[Optional[str], List[int]], Type[BaseCount]) -> None 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, value=value, **id_args) for end_time, value in zip(end_times, values) if value != 0]) stat = COUNT_STATS['active_users:is_bot:day'] realm_data = { 'false': self.generate_fixture_data(stat, .1, .03, 3, .5, 3), 'true': self.generate_fixture_data(stat, .01, 0, 1, 0, 1) } # type: Dict[Optional[str], List[int]] insert_fixture_data(stat, realm_data, RealmCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS['messages_sent:is_bot:hour'] user_data = {'false': self.generate_fixture_data(stat, 2, 1, 1.5, .6, 8, holiday_rate=.1)} 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) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS['messages_sent:message_type:day'] user_data = { 'public_stream': self.generate_fixture_data(stat, 1.5, 1, 3, .6, 8), 'private_message': self.generate_fixture_data(stat, .5, .3, 1, .6, 8)} 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), 'private_message': self.generate_fixture_data(stat, 13, 5, 5, .6, 4)} insert_fixture_data(stat, realm_data, RealmCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) # We use client names that start with _ to make it easy to # drop just these data manually without affecting other data. website = Client.objects.create(name='_Website') old_desktop = Client.objects.create(name='_Old desktop app') android = Client.objects.create(name='_Android app') iOS = Client.objects.create(name='_Old iOS app') react_native = Client.objects.create(name='_New iOS app') API = Client.objects.create(name='_Python API') barnowl = Client.objects.create(name='_Barnowl') unused = Client.objects.create(name='_Unused webhook') long_webhook = Client.objects.create(name='_Webhook with loooooooong name') stat = COUNT_STATS['messages_sent:client:day'] user_data = { website.id: self.generate_fixture_data(stat, 2, 1, 1.5, .6, 8), barnowl.id: self.generate_fixture_data(stat, 0, .3, 1.5, .6, 8)} insert_fixture_data(stat, user_data, UserCount) realm_data = { 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), barnowl.id: self.generate_fixture_data(stat, 1, 1, 3, .6, 3), 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)} insert_fixture_data(stat, realm_data, RealmCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE)
def handle(self, *args: Any, **options: Any) -> None: 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() installation_time = timezone_now() - timedelta(days=self.DAYS_OF_DATA) last_end_time = floor_to_day(timezone_now()) realm = Realm.objects.create( string_id='analytics', name='Analytics', date_created=installation_time) shylock = self.create_user('*****@*****.**', 'Shylock', True, installation_time, realm) def insert_fixture_data(stat: CountStat, fixture_data: Mapping[Optional[str], List[int]], table: Type[BaseCount]) -> None: 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, value=value, **id_args) for end_time, value in zip(end_times, values) if value != 0]) stat = COUNT_STATS['realm_active_humans::day'] realm_data = { None: self.generate_fixture_data(stat, .1, .03, 3, .5, 3, partial_sum=True), } # type: Mapping[Optional[str], List[int]] insert_fixture_data(stat, realm_data, RealmCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS['messages_sent:is_bot:hour'] user_data = {'false': self.generate_fixture_data( stat, 2, 1, 1.5, .6, 8, holiday_rate=.1)} # type: Mapping[Optional[str], List[int]] 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) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS['messages_sent:message_type:day'] user_data = { 'public_stream': self.generate_fixture_data(stat, 1.5, 1, 3, .6, 8), 'private_message': self.generate_fixture_data(stat, .5, .3, 1, .6, 8), 'huddle_message': self.generate_fixture_data(stat, .2, .2, 2, .6, 8)} 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), 'private_message': self.generate_fixture_data(stat, 13, 5, 5, .6, 4), 'huddle_message': self.generate_fixture_data(stat, 6, 3, 3, .6, 4)} insert_fixture_data(stat, realm_data, RealmCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) 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') stat = COUNT_STATS['messages_sent:client:day'] user_data = { website.id: self.generate_fixture_data(stat, 2, 1, 1.5, .6, 8), zephyr_mirror.id: self.generate_fixture_data(stat, 0, .3, 1.5, .6, 8)} insert_fixture_data(stat, user_data, UserCount) realm_data = { 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), zephyr_mirror.id: self.generate_fixture_data(stat, 1, 1, 3, .6, 3), 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)} insert_fixture_data(stat, realm_data, RealmCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE)
def handle(self, *args: Any, **options: Any) -> None: # TODO: This should arguably only delete the objects # associated with the "analytics" realm. do_drop_all_analytics_tables() # This also deletes any objects with this realm as a foreign key Realm.objects.filter(string_id='analytics').delete() # Because we just deleted a bunch of objects in the database # directly (rather than deleting individual objects in Django, # in which case our post_save hooks would have flushed the # individual objects from memcached for us), we need to flush # memcached in order to ensure deleted objects aren't still # present in the memcached cache. from zerver.apps import flush_cache flush_cache(None) installation_time = timezone_now() - timedelta(days=self.DAYS_OF_DATA) last_end_time = floor_to_day(timezone_now()) realm = Realm.objects.create( string_id='analytics', name='Analytics', date_created=installation_time) with mock.patch("zerver.lib.create_user.timezone_now", return_value=installation_time): shylock = create_user('*****@*****.**', 'Shylock', realm, full_name='Shylock', short_name='shylock', is_realm_admin=True) do_change_user_role(shylock, UserProfile.ROLE_REALM_ADMINISTRATOR) stream = Stream.objects.create( name='all', realm=realm, date_created=installation_time) recipient = Recipient.objects.create(type_id=stream.id, type=Recipient.STREAM) stream.recipient = recipient stream.save(update_fields=["recipient"]) # Subscribe shylock to the stream to avoid invariant failures. # TODO: This should use subscribe_users_to_streams from populate_db. subs = [ Subscription(recipient=recipient, user_profile=shylock, color=STREAM_ASSIGNMENT_COLORS[0]), ] Subscription.objects.bulk_create(subs) def insert_fixture_data(stat: CountStat, fixture_data: Mapping[Optional[str], List[int]], table: Type[BaseCount]) -> None: end_times = time_range(last_end_time, last_end_time, stat.frequency, len(list(fixture_data.values())[0])) if table == InstallationCount: id_args: Dict[str, Any] = {} if table == RealmCount: id_args = {'realm': realm} if table == UserCount: id_args = {'realm': realm, 'user': shylock} if table == StreamCount: id_args = {'stream': stream, 'realm': realm} for subgroup, values in fixture_data.items(): table.objects.bulk_create([ table(property=stat.property, subgroup=subgroup, end_time=end_time, value=value, **id_args) for end_time, value in zip(end_times, values) if value != 0]) stat = COUNT_STATS['1day_actives::day'] realm_data: Mapping[Optional[str], List[int]] = { None: self.generate_fixture_data(stat, .08, .02, 3, .3, 6, partial_sum=True), } insert_fixture_data(stat, realm_data, RealmCount) installation_data: Mapping[Optional[str], List[int]] = { None: self.generate_fixture_data(stat, .8, .2, 4, .3, 6, partial_sum=True), } insert_fixture_data(stat, installation_data, InstallationCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS['realm_active_humans::day'] realm_data = { None: self.generate_fixture_data(stat, .1, .03, 3, .5, 3, partial_sum=True), } insert_fixture_data(stat, realm_data, RealmCount) installation_data = { None: self.generate_fixture_data(stat, 1, .3, 4, .5, 3, partial_sum=True), } insert_fixture_data(stat, installation_data, InstallationCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS['active_users_audit:is_bot:day'] realm_data = { 'false': self.generate_fixture_data(stat, .1, .03, 3.5, .8, 2, partial_sum=True), } insert_fixture_data(stat, realm_data, RealmCount) installation_data = { 'false': self.generate_fixture_data(stat, 1, .3, 6, .8, 2, partial_sum=True), } insert_fixture_data(stat, installation_data, InstallationCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS['messages_sent:is_bot:hour'] user_data: Mapping[Optional[str], List[int]] = { 'false': self.generate_fixture_data(stat, 2, 1, 1.5, .6, 8, holiday_rate=.1), } 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) installation_data = {'false': self.generate_fixture_data(stat, 350, 150, 6, .6, 4), 'true': self.generate_fixture_data(stat, 150, 150, 3, .4, 2)} insert_fixture_data(stat, installation_data, InstallationCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS['messages_sent:message_type:day'] user_data = { 'public_stream': self.generate_fixture_data(stat, 1.5, 1, 3, .6, 8), 'private_message': self.generate_fixture_data(stat, .5, .3, 1, .6, 8), 'huddle_message': self.generate_fixture_data(stat, .2, .2, 2, .6, 8)} 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), 'private_message': self.generate_fixture_data(stat, 13, 5, 5, .6, 4), 'huddle_message': self.generate_fixture_data(stat, 6, 3, 3, .6, 4)} insert_fixture_data(stat, realm_data, RealmCount) installation_data = { 'public_stream': self.generate_fixture_data(stat, 300, 80, 5, .6, 4), 'private_stream': self.generate_fixture_data(stat, 70, 70, 5, .6, 4), 'private_message': self.generate_fixture_data(stat, 130, 50, 5, .6, 4), 'huddle_message': self.generate_fixture_data(stat, 60, 30, 3, .6, 4)} insert_fixture_data(stat, installation_data, InstallationCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) 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') stat = COUNT_STATS['messages_sent:client:day'] user_data = { website.id: self.generate_fixture_data(stat, 2, 1, 1.5, .6, 8), zephyr_mirror.id: self.generate_fixture_data(stat, 0, .3, 1.5, .6, 8)} insert_fixture_data(stat, user_data, UserCount) realm_data = { 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), zephyr_mirror.id: self.generate_fixture_data(stat, 1, 1, 3, .6, 3), 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)} insert_fixture_data(stat, realm_data, RealmCount) installation_data = { website.id: self.generate_fixture_data(stat, 300, 200, 5, .6, 3), old_desktop.id: self.generate_fixture_data(stat, 50, 30, 8, .6, 3), android.id: self.generate_fixture_data(stat, 50, 50, 2, .6, 3), iOS.id: self.generate_fixture_data(stat, 50, 50, 2, .6, 3), react_native.id: self.generate_fixture_data(stat, 5, 5, 10, .6, 3), API.id: self.generate_fixture_data(stat, 50, 50, 5, .6, 3), zephyr_mirror.id: self.generate_fixture_data(stat, 10, 10, 3, .6, 3), unused.id: self.generate_fixture_data(stat, 0, 0, 0, 0, 0), long_webhook.id: self.generate_fixture_data(stat, 50, 50, 2, .6, 3)} insert_fixture_data(stat, installation_data, InstallationCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS['messages_in_stream:is_bot:day'] realm_data = {'false': self.generate_fixture_data(stat, 30, 5, 6, .6, 4), 'true': self.generate_fixture_data(stat, 20, 2, 3, .2, 3)} insert_fixture_data(stat, realm_data, RealmCount) stream_data: Mapping[Optional[str], List[int]] = { 'false': self.generate_fixture_data(stat, 10, 7, 5, .6, 4), 'true': self.generate_fixture_data(stat, 5, 3, 2, .4, 2), } insert_fixture_data(stat, stream_data, StreamCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE)
def handle(self, *args: Any, **options: Any) -> None: # TODO: This should arguably only delete the objects # associated with the "analytics" realm. do_drop_all_analytics_tables() # This also deletes any objects with this realm as a foreign key Realm.objects.filter(string_id='analytics').delete() # Because we just deleted a bunch of objects in the database # directly (rather than deleting individual objects in Django, # in which case our post_save hooks would have flushed the # individual objects from memcached for us), we need to flush # memcached in order to ensure deleted objects aren't still # present in the memcached cache. from zerver.apps import flush_cache flush_cache(None) installation_time = timezone_now() - timedelta(days=self.DAYS_OF_DATA) last_end_time = floor_to_day(timezone_now()) realm = Realm.objects.create( string_id='analytics', name='Analytics', date_created=installation_time) shylock = self.create_user('*****@*****.**', 'Shylock', True, installation_time, realm) do_change_is_admin(shylock, True) stream = Stream.objects.create( name='all', realm=realm, date_created=installation_time) Recipient.objects.create(type_id=stream.id, type=Recipient.STREAM) def insert_fixture_data(stat: CountStat, fixture_data: Mapping[Optional[str], List[int]], table: Type[BaseCount]) -> None: end_times = time_range(last_end_time, last_end_time, stat.frequency, len(list(fixture_data.values())[0])) if table == InstallationCount: id_args = {} # type: Dict[str, Any] if table == RealmCount: id_args = {'realm': realm} if table == UserCount: id_args = {'realm': realm, 'user': shylock} if table == StreamCount: id_args = {'stream': stream, 'realm': realm} for subgroup, values in fixture_data.items(): table.objects.bulk_create([ table(property=stat.property, subgroup=subgroup, end_time=end_time, value=value, **id_args) for end_time, value in zip(end_times, values) if value != 0]) stat = COUNT_STATS['1day_actives::day'] realm_data = { None: self.generate_fixture_data(stat, .08, .02, 3, .3, 6, partial_sum=True), } # type: Mapping[Optional[str], List[int]] insert_fixture_data(stat, realm_data, RealmCount) installation_data = { None: self.generate_fixture_data(stat, .8, .2, 4, .3, 6, partial_sum=True), } # type: Mapping[Optional[str], List[int]] insert_fixture_data(stat, installation_data, InstallationCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS['realm_active_humans::day'] realm_data = { None: self.generate_fixture_data(stat, .1, .03, 3, .5, 3, partial_sum=True), } insert_fixture_data(stat, realm_data, RealmCount) installation_data = { None: self.generate_fixture_data(stat, 1, .3, 4, .5, 3, partial_sum=True), } insert_fixture_data(stat, installation_data, InstallationCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS['active_users_audit:is_bot:day'] realm_data = { 'false': self.generate_fixture_data(stat, .1, .03, 3.5, .8, 2, partial_sum=True), } insert_fixture_data(stat, realm_data, RealmCount) installation_data = { 'false': self.generate_fixture_data(stat, 1, .3, 6, .8, 2, partial_sum=True), } insert_fixture_data(stat, installation_data, InstallationCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS['messages_sent:is_bot:hour'] user_data = {'false': self.generate_fixture_data( stat, 2, 1, 1.5, .6, 8, holiday_rate=.1)} # type: Mapping[Optional[str], List[int]] 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) installation_data = {'false': self.generate_fixture_data(stat, 350, 150, 6, .6, 4), 'true': self.generate_fixture_data(stat, 150, 150, 3, .4, 2)} insert_fixture_data(stat, installation_data, InstallationCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS['messages_sent:message_type:day'] user_data = { 'public_stream': self.generate_fixture_data(stat, 1.5, 1, 3, .6, 8), 'private_message': self.generate_fixture_data(stat, .5, .3, 1, .6, 8), 'huddle_message': self.generate_fixture_data(stat, .2, .2, 2, .6, 8)} 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), 'private_message': self.generate_fixture_data(stat, 13, 5, 5, .6, 4), 'huddle_message': self.generate_fixture_data(stat, 6, 3, 3, .6, 4)} insert_fixture_data(stat, realm_data, RealmCount) installation_data = { 'public_stream': self.generate_fixture_data(stat, 300, 80, 5, .6, 4), 'private_stream': self.generate_fixture_data(stat, 70, 70, 5, .6, 4), 'private_message': self.generate_fixture_data(stat, 130, 50, 5, .6, 4), 'huddle_message': self.generate_fixture_data(stat, 60, 30, 3, .6, 4)} insert_fixture_data(stat, installation_data, InstallationCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) 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') stat = COUNT_STATS['messages_sent:client:day'] user_data = { website.id: self.generate_fixture_data(stat, 2, 1, 1.5, .6, 8), zephyr_mirror.id: self.generate_fixture_data(stat, 0, .3, 1.5, .6, 8)} insert_fixture_data(stat, user_data, UserCount) realm_data = { 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), zephyr_mirror.id: self.generate_fixture_data(stat, 1, 1, 3, .6, 3), 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)} insert_fixture_data(stat, realm_data, RealmCount) installation_data = { website.id: self.generate_fixture_data(stat, 300, 200, 5, .6, 3), old_desktop.id: self.generate_fixture_data(stat, 50, 30, 8, .6, 3), android.id: self.generate_fixture_data(stat, 50, 50, 2, .6, 3), iOS.id: self.generate_fixture_data(stat, 50, 50, 2, .6, 3), react_native.id: self.generate_fixture_data(stat, 5, 5, 10, .6, 3), API.id: self.generate_fixture_data(stat, 50, 50, 5, .6, 3), zephyr_mirror.id: self.generate_fixture_data(stat, 10, 10, 3, .6, 3), unused.id: self.generate_fixture_data(stat, 0, 0, 0, 0, 0), long_webhook.id: self.generate_fixture_data(stat, 50, 50, 2, .6, 3)} insert_fixture_data(stat, installation_data, InstallationCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS['messages_in_stream:is_bot:day'] realm_data = {'false': self.generate_fixture_data(stat, 30, 5, 6, .6, 4), 'true': self.generate_fixture_data(stat, 20, 2, 3, .2, 3)} insert_fixture_data(stat, realm_data, RealmCount) stream_data = {'false': self.generate_fixture_data(stat, 10, 7, 5, .6, 4), 'true': self.generate_fixture_data(stat, 5, 3, 2, .4, 2)} # type: Mapping[Optional[str], List[int]] insert_fixture_data(stat, stream_data, StreamCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE)
def installation_epoch(): # type: () -> datetime.datetime earliest_realm_creation = Realm.objects.aggregate(models.Min('date_created'))['date_created__min'] return floor_to_day(earliest_realm_creation)
def handle(self, *args: Any, **options: Any) -> None: # TODO: This should arguably only delete the objects # associated with the "analytics" realm. do_drop_all_analytics_tables() # This also deletes any objects with this realm as a foreign key Realm.objects.filter(string_id="analytics").delete() # Because we just deleted a bunch of objects in the database # directly (rather than deleting individual objects in Django, # in which case our post_save hooks would have flushed the # individual objects from memcached for us), we need to flush # memcached in order to ensure deleted objects aren't still # present in the memcached cache. from zerver.apps import flush_cache flush_cache(None) installation_time = timezone_now() - timedelta(days=self.DAYS_OF_DATA) last_end_time = floor_to_day(timezone_now()) realm = do_create_realm(string_id="analytics", name="Analytics", date_created=installation_time) with mock.patch("zerver.lib.create_user.timezone_now", return_value=installation_time): shylock = create_user( "*****@*****.**", "Shylock", realm, full_name="Shylock", role=UserProfile.ROLE_REALM_OWNER, ) do_change_user_role(shylock, UserProfile.ROLE_REALM_OWNER, acting_user=None) stream = Stream.objects.create(name="all", realm=realm, date_created=installation_time) recipient = Recipient.objects.create(type_id=stream.id, type=Recipient.STREAM) stream.recipient = recipient stream.save(update_fields=["recipient"]) # Subscribe shylock to the stream to avoid invariant failures. # TODO: This should use subscribe_users_to_streams from populate_db. subs = [ Subscription( recipient=recipient, user_profile=shylock, is_user_active=shylock.is_active, color=STREAM_ASSIGNMENT_COLORS[0], ), ] Subscription.objects.bulk_create(subs) FixtureData = Mapping[Union[str, int, None], List[int]] def insert_fixture_data( stat: CountStat, fixture_data: FixtureData, table: Type[BaseCount], ) -> None: end_times = time_range(last_end_time, last_end_time, stat.frequency, len(list(fixture_data.values())[0])) if table == InstallationCount: id_args: Dict[str, Any] = {} if table == RealmCount: id_args = {"realm": realm} if table == UserCount: id_args = {"realm": realm, "user": shylock} if table == StreamCount: id_args = {"stream": stream, "realm": realm} for subgroup, values in fixture_data.items(): table.objects.bulk_create( table( property=stat.property, subgroup=subgroup, end_time=end_time, value=value, **id_args, ) for end_time, value in zip(end_times, values) if value != 0) stat = COUNT_STATS["1day_actives::day"] realm_data: FixtureData = { None: self.generate_fixture_data(stat, 0.08, 0.02, 3, 0.3, 6, partial_sum=True), } insert_fixture_data(stat, realm_data, RealmCount) installation_data: FixtureData = { None: self.generate_fixture_data(stat, 0.8, 0.2, 4, 0.3, 6, partial_sum=True), } insert_fixture_data(stat, installation_data, InstallationCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS["7day_actives::day"] realm_data = { None: self.generate_fixture_data(stat, 0.2, 0.07, 3, 0.3, 6, partial_sum=True), } insert_fixture_data(stat, realm_data, RealmCount) installation_data = { None: self.generate_fixture_data(stat, 2, 0.7, 4, 0.3, 6, partial_sum=True), } insert_fixture_data(stat, installation_data, InstallationCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS["realm_active_humans::day"] realm_data = { None: self.generate_fixture_data(stat, 0.8, 0.08, 3, 0.5, 3, partial_sum=True), } insert_fixture_data(stat, realm_data, RealmCount) installation_data = { None: self.generate_fixture_data(stat, 1, 0.3, 4, 0.5, 3, partial_sum=True), } insert_fixture_data(stat, installation_data, InstallationCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS["active_users_audit:is_bot:day"] realm_data = { "false": self.generate_fixture_data(stat, 1, 0.2, 3.5, 0.8, 2, partial_sum=True), "true": self.generate_fixture_data(stat, 0.3, 0.05, 3, 0.3, 2, partial_sum=True), } insert_fixture_data(stat, realm_data, RealmCount) installation_data = { "false": self.generate_fixture_data(stat, 3, 1, 4, 0.8, 2, partial_sum=True), "true": self.generate_fixture_data(stat, 1, 0.4, 4, 0.8, 2, partial_sum=True), } insert_fixture_data(stat, installation_data, InstallationCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS["messages_sent:is_bot:hour"] user_data: FixtureData = { "false": self.generate_fixture_data(stat, 2, 1, 1.5, 0.6, 8, holiday_rate=0.1), } insert_fixture_data(stat, user_data, UserCount) realm_data = { "false": self.generate_fixture_data(stat, 35, 15, 6, 0.6, 4), "true": self.generate_fixture_data(stat, 15, 15, 3, 0.4, 2), } insert_fixture_data(stat, realm_data, RealmCount) installation_data = { "false": self.generate_fixture_data(stat, 350, 150, 6, 0.6, 4), "true": self.generate_fixture_data(stat, 150, 150, 3, 0.4, 2), } insert_fixture_data(stat, installation_data, InstallationCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS["messages_sent:message_type:day"] user_data = { "public_stream": self.generate_fixture_data(stat, 1.5, 1, 3, 0.6, 8), "private_message": self.generate_fixture_data(stat, 0.5, 0.3, 1, 0.6, 8), "huddle_message": self.generate_fixture_data(stat, 0.2, 0.2, 2, 0.6, 8), } insert_fixture_data(stat, user_data, UserCount) realm_data = { "public_stream": self.generate_fixture_data(stat, 30, 8, 5, 0.6, 4), "private_stream": self.generate_fixture_data(stat, 7, 7, 5, 0.6, 4), "private_message": self.generate_fixture_data(stat, 13, 5, 5, 0.6, 4), "huddle_message": self.generate_fixture_data(stat, 6, 3, 3, 0.6, 4), } insert_fixture_data(stat, realm_data, RealmCount) installation_data = { "public_stream": self.generate_fixture_data(stat, 300, 80, 5, 0.6, 4), "private_stream": self.generate_fixture_data(stat, 70, 70, 5, 0.6, 4), "private_message": self.generate_fixture_data(stat, 130, 50, 5, 0.6, 4), "huddle_message": self.generate_fixture_data(stat, 60, 30, 3, 0.6, 4), } insert_fixture_data(stat, installation_data, InstallationCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) 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") stat = COUNT_STATS["messages_sent:client:day"] user_data = { website.id: self.generate_fixture_data(stat, 2, 1, 1.5, 0.6, 8), zephyr_mirror.id: self.generate_fixture_data(stat, 0, 0.3, 1.5, 0.6, 8), } insert_fixture_data(stat, user_data, UserCount) realm_data = { website.id: self.generate_fixture_data(stat, 30, 20, 5, 0.6, 3), old_desktop.id: self.generate_fixture_data(stat, 5, 3, 8, 0.6, 3), android.id: self.generate_fixture_data(stat, 5, 5, 2, 0.6, 3), iOS.id: self.generate_fixture_data(stat, 5, 5, 2, 0.6, 3), react_native.id: self.generate_fixture_data(stat, 5, 5, 10, 0.6, 3), API.id: self.generate_fixture_data(stat, 5, 5, 5, 0.6, 3), zephyr_mirror.id: self.generate_fixture_data(stat, 1, 1, 3, 0.6, 3), unused.id: self.generate_fixture_data(stat, 0, 0, 0, 0, 0), long_webhook.id: self.generate_fixture_data(stat, 5, 5, 2, 0.6, 3), } insert_fixture_data(stat, realm_data, RealmCount) installation_data = { website.id: self.generate_fixture_data(stat, 300, 200, 5, 0.6, 3), old_desktop.id: self.generate_fixture_data(stat, 50, 30, 8, 0.6, 3), android.id: self.generate_fixture_data(stat, 50, 50, 2, 0.6, 3), iOS.id: self.generate_fixture_data(stat, 50, 50, 2, 0.6, 3), react_native.id: self.generate_fixture_data(stat, 5, 5, 10, 0.6, 3), API.id: self.generate_fixture_data(stat, 50, 50, 5, 0.6, 3), zephyr_mirror.id: self.generate_fixture_data(stat, 10, 10, 3, 0.6, 3), unused.id: self.generate_fixture_data(stat, 0, 0, 0, 0, 0), long_webhook.id: self.generate_fixture_data(stat, 50, 50, 2, 0.6, 3), } insert_fixture_data(stat, installation_data, InstallationCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS["messages_in_stream:is_bot:day"] realm_data = { "false": self.generate_fixture_data(stat, 30, 5, 6, 0.6, 4), "true": self.generate_fixture_data(stat, 20, 2, 3, 0.2, 3), } insert_fixture_data(stat, realm_data, RealmCount) stream_data: Mapping[Union[int, str, None], List[int]] = { "false": self.generate_fixture_data(stat, 10, 7, 5, 0.6, 4), "true": self.generate_fixture_data(stat, 5, 3, 2, 0.4, 2), } insert_fixture_data(stat, stream_data, StreamCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE) stat = COUNT_STATS["messages_read::hour"] user_data = { None: self.generate_fixture_data(stat, 7, 3, 2, 0.6, 8, holiday_rate=0.1), } insert_fixture_data(stat, user_data, UserCount) realm_data = { None: self.generate_fixture_data(stat, 50, 35, 6, 0.6, 4) } insert_fixture_data(stat, realm_data, RealmCount) FillState.objects.create(property=stat.property, end_time=last_end_time, state=FillState.DONE)
def handle(self, *args, **options): # type: (*Any, **Any) -> None 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() Client.objects.filter(name__endswith='_').delete() installation_time = timezone.now() - timedelta(days=self.DAYS_OF_DATA) last_end_time = floor_to_day(timezone.now()) realm = Realm.objects.create( string_id='analytics', name='Analytics', domain='analytics.ds', date_created=installation_time) shylock = self.create_user('*****@*****.**', 'Shylock', True, installation_time, realm) def insert_fixture_data(stat, fixture_data, table): # type: (CountStat, Dict[Optional[str], List[int]], Type[BaseCount]) -> None 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, value=value, **id_args) for end_time, value in zip(end_times, values) if value != 0]) stat = COUNT_STATS['active_users:is_bot:day'] realm_data = { 'false': self.generate_fixture_data(stat, .1, .03, 3, .5, 3), 'true': self.generate_fixture_data(stat, .01, 0, 1, 0, 1) } # type: Dict[Optional[str], List[int]] insert_fixture_data(stat, realm_data, RealmCount) stat = COUNT_STATS['messages_sent:is_bot:hour'] user_data = {'false': self.generate_fixture_data(stat, 2, 1, 1.5, .6, 8, holiday_rate=.1)} 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) stat = COUNT_STATS['messages_sent:message_type:day'] user_data = { 'public_stream': self.generate_fixture_data(stat, 1.5, 1, 3, .6, 8), 'private_message': self.generate_fixture_data(stat, .5, .3, 1, .6, 8)} 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), 'private_message': self.generate_fixture_data(stat, 13, 5, 5, .6, 4)} insert_fixture_data(stat, realm_data, RealmCount) website_ = Client.objects.create(name='website_') API_ = Client.objects.create(name='API_') android_ = Client.objects.create(name='android_') iOS_ = Client.objects.create(name='iOS_') react_native_ = Client.objects.create(name='react_native_') electron_ = Client.objects.create(name='electron_') barnowl_ = Client.objects.create(name='barnowl_') plan9_ = Client.objects.create(name='plan9_') stat = COUNT_STATS['messages_sent:client:day'] user_data = { website_.id: self.generate_fixture_data(stat, 2, 1, 1.5, .6, 8), barnowl_.id: self.generate_fixture_data(stat, 0, .3, 1.5, .6, 8)} insert_fixture_data(stat, user_data, UserCount) realm_data = { website_.id: self.generate_fixture_data(stat, 30, 20, 5, .6, 3), API_.id: self.generate_fixture_data(stat, 5, 5, 5, .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), electron_.id: self.generate_fixture_data(stat, 5, 3, 8, .6, 3), barnowl_.id: self.generate_fixture_data(stat, 1, 1, 3, .6, 3), plan9_.id: self.generate_fixture_data(stat, 0, 0, 0, 0, 0, 0)} insert_fixture_data(stat, realm_data, RealmCount)