2016-10-08 02:27:50 +02:00
|
|
|
from django.db import models
|
2016-07-29 21:52:45 +02:00
|
|
|
from django.test import TestCase
|
2016-10-08 02:27:50 +02:00
|
|
|
from django.utils import timezone
|
2016-07-29 21:52:45 +02:00
|
|
|
|
2016-10-11 02:23:42 +02:00
|
|
|
from analytics.lib.counts import CountStat, COUNT_STATS, process_count_stat, \
|
2016-07-29 21:52:45 +02:00
|
|
|
zerver_count_user_by_realm, zerver_count_message_by_user, \
|
2016-10-12 23:40:48 +02:00
|
|
|
zerver_count_message_by_stream, zerver_count_stream_by_realm, \
|
|
|
|
do_fill_count_stat_at_hour, ZerverCountQuery
|
2016-10-08 02:27:50 +02:00
|
|
|
from analytics.models import BaseCount, InstallationCount, RealmCount, \
|
2017-01-07 09:19:37 +01:00
|
|
|
UserCount, StreamCount, FillState, installation_epoch
|
2016-10-08 02:27:50 +02:00
|
|
|
from zerver.models import Realm, UserProfile, Message, Stream, Recipient, \
|
2017-01-12 21:04:55 +01:00
|
|
|
Huddle, get_user_profile_by_email, get_client
|
2016-07-29 21:52:45 +02:00
|
|
|
|
2016-10-08 02:27:50 +02:00
|
|
|
from datetime import datetime, timedelta
|
2016-07-29 21:52:45 +02:00
|
|
|
|
2017-01-12 21:04:55 +01:00
|
|
|
from typing import Any, Type, Optional, Text, Tuple
|
2016-07-29 21:52:45 +02:00
|
|
|
|
2016-10-07 01:29:57 +02:00
|
|
|
class AnalyticsTestCase(TestCase):
|
|
|
|
MINUTE = timedelta(seconds = 60)
|
|
|
|
HOUR = MINUTE * 60
|
|
|
|
DAY = HOUR * 24
|
2016-12-18 18:54:20 +01:00
|
|
|
TIME_ZERO = datetime(1988, 3, 14).replace(tzinfo=timezone.utc)
|
2016-10-07 01:29:57 +02:00
|
|
|
TIME_LAST_HOUR = TIME_ZERO - HOUR
|
|
|
|
|
2016-10-07 02:47:05 +02:00
|
|
|
def setUp(self):
|
|
|
|
# type: () -> None
|
2016-10-27 03:05:21 +02:00
|
|
|
self.default_realm = Realm.objects.create(
|
2016-10-28 07:21:53 +02:00
|
|
|
string_id='realmtest', name='Realm Test',
|
2016-12-17 03:26:39 +01:00
|
|
|
domain='test.analytics', date_created=self.TIME_ZERO - 2*self.DAY)
|
2017-01-13 08:12:39 +01:00
|
|
|
# used to generate unique names in self.create_*
|
2017-01-13 07:23:47 +01:00
|
|
|
self.name_counter = 100
|
2017-01-13 08:12:39 +01:00
|
|
|
# used as defaults in self.assertCountEquals
|
|
|
|
self.current_property = None # type: Optional[str]
|
|
|
|
self.current_interval = None # type: Optional[str]
|
2016-10-07 02:47:05 +02:00
|
|
|
|
|
|
|
# Lightweight creation of users, streams, and messages
|
2017-01-13 07:23:47 +01:00
|
|
|
def create_user(self, **kwargs):
|
|
|
|
# type: (**Any) -> UserProfile
|
|
|
|
self.name_counter += 1
|
2016-10-07 02:47:05 +02:00
|
|
|
defaults = {
|
2017-01-13 07:23:47 +01:00
|
|
|
'email': 'user%s@domain.tld' % (self.name_counter,),
|
2016-10-07 02:47:05 +02:00
|
|
|
'date_joined': self.TIME_LAST_HOUR,
|
|
|
|
'full_name': 'full_name',
|
|
|
|
'short_name': 'short_name',
|
|
|
|
'pointer': -1,
|
|
|
|
'last_pointer_updater': 'seems unused?',
|
|
|
|
'realm': self.default_realm,
|
|
|
|
'api_key': '42'}
|
2016-07-29 21:52:45 +02:00
|
|
|
for key, value in defaults.items():
|
|
|
|
kwargs[key] = kwargs.get(key, value)
|
2017-01-13 07:23:47 +01:00
|
|
|
return UserProfile.objects.create(**kwargs)
|
2016-07-29 21:52:45 +02:00
|
|
|
|
2017-01-12 21:04:55 +01:00
|
|
|
def create_stream_with_recipient(self, **kwargs):
|
|
|
|
# type: (**Any) -> Tuple[Stream, Recipient]
|
2017-01-13 07:23:47 +01:00
|
|
|
self.name_counter += 1
|
|
|
|
defaults = {'name': 'stream name %s' % (self.name_counter,),
|
2016-10-07 02:47:05 +02:00
|
|
|
'realm': self.default_realm,
|
|
|
|
'date_created': self.TIME_LAST_HOUR}
|
2016-07-29 21:52:45 +02:00
|
|
|
for key, value in defaults.items():
|
|
|
|
kwargs[key] = kwargs.get(key, value)
|
2017-01-12 21:04:55 +01:00
|
|
|
stream = Stream.objects.create(**kwargs)
|
|
|
|
recipient = Recipient.objects.create(type_id=stream.id, type=Recipient.STREAM)
|
|
|
|
return stream, recipient
|
|
|
|
|
2017-01-13 07:23:47 +01:00
|
|
|
def create_huddle_with_recipient(self, **kwargs):
|
|
|
|
# type: (**Any) -> Tuple[Huddle, Recipient]
|
|
|
|
self.name_counter += 1
|
|
|
|
defaults = {'huddle_hash': 'hash%s' % (self.name_counter,)}
|
2017-01-12 21:04:55 +01:00
|
|
|
for key, value in defaults.items():
|
|
|
|
kwargs[key] = kwargs.get(key, value)
|
|
|
|
huddle = Huddle.objects.create(**kwargs)
|
|
|
|
recipient = Recipient.objects.create(type_id=huddle.id, type=Recipient.HUDDLE)
|
|
|
|
return huddle, recipient
|
2016-07-29 21:52:45 +02:00
|
|
|
|
|
|
|
def create_message(self, sender, recipient, **kwargs):
|
|
|
|
# type: (UserProfile, Recipient, **Any) -> Message
|
|
|
|
defaults = {
|
|
|
|
'sender': sender,
|
|
|
|
'recipient': recipient,
|
2016-10-07 02:47:05 +02:00
|
|
|
'subject': 'subject',
|
|
|
|
'content': 'hi',
|
|
|
|
'pub_date': self.TIME_LAST_HOUR,
|
|
|
|
'sending_client': get_client("website")}
|
2016-07-29 21:52:45 +02:00
|
|
|
for key, value in defaults.items():
|
|
|
|
kwargs[key] = kwargs.get(key, value)
|
2016-10-07 02:47:05 +02:00
|
|
|
return Message.objects.create(**kwargs)
|
2016-07-29 21:52:45 +02:00
|
|
|
|
2016-10-08 02:27:50 +02:00
|
|
|
# kwargs should only ever be a UserProfile or Stream.
|
2017-01-13 08:12:39 +01:00
|
|
|
def assertCountEquals(self, table, value, property=None, subgroup=None,
|
|
|
|
end_time=TIME_ZERO, interval=None, realm=None, **kwargs):
|
|
|
|
# type: (Type[BaseCount], int, Optional[Text], Optional[Text], datetime, Optional[str], Optional[Realm], **models.Model) -> None
|
|
|
|
if property is None:
|
|
|
|
property = self.current_property
|
|
|
|
if interval is None:
|
|
|
|
interval = self.current_interval
|
2016-12-17 02:25:01 +01:00
|
|
|
queryset = table.objects.filter(property=property, interval=interval, end_time=end_time) \
|
|
|
|
.filter(**kwargs)
|
|
|
|
if table is not InstallationCount:
|
|
|
|
if realm is None:
|
|
|
|
realm = self.default_realm
|
|
|
|
queryset = queryset.filter(realm=realm)
|
2016-12-17 02:26:46 +01:00
|
|
|
if subgroup is not None:
|
|
|
|
queryset = queryset.filter(subgroup=subgroup)
|
2016-12-17 02:25:01 +01:00
|
|
|
self.assertEqual(queryset.values_list('value', flat=True)[0], value)
|
2016-07-29 21:52:45 +02:00
|
|
|
|
2016-10-08 03:23:24 +02:00
|
|
|
class TestProcessCountStat(AnalyticsTestCase):
|
2016-11-03 08:27:32 +01:00
|
|
|
def make_dummy_count_stat(self, current_time):
|
|
|
|
# type: (datetime) -> CountStat
|
|
|
|
dummy_query = """INSERT INTO analytics_realmcount (realm_id, property, end_time, interval, value)
|
2016-11-14 09:15:35 +01:00
|
|
|
VALUES (1, 'test stat', '%(end_time)s','hour', 22)""" % {'end_time': current_time}
|
2016-11-03 08:27:32 +01:00
|
|
|
count_stat = CountStat('test stat', ZerverCountQuery(Recipient, UserCount, dummy_query),
|
|
|
|
{}, None, CountStat.HOUR, False)
|
|
|
|
return count_stat
|
|
|
|
|
2016-10-12 23:40:48 +02:00
|
|
|
def assertFillStateEquals(self, end_time, state = FillState.DONE, property = None):
|
2016-12-08 05:06:51 +01:00
|
|
|
# type: (datetime, int, Optional[Text]) -> None
|
2016-11-03 08:27:32 +01:00
|
|
|
count_stat = self.make_dummy_count_stat(end_time)
|
2016-10-12 23:40:48 +02:00
|
|
|
if property is None:
|
2016-11-03 08:27:32 +01:00
|
|
|
property = count_stat.property
|
2017-01-07 09:19:37 +01:00
|
|
|
fill_state = FillState.objects.filter(property=property).first()
|
|
|
|
self.assertEqual(fill_state.end_time, end_time)
|
|
|
|
self.assertEqual(fill_state.state, state)
|
2016-10-12 23:40:48 +02:00
|
|
|
|
|
|
|
def test_process_stat(self):
|
|
|
|
# type: () -> None
|
|
|
|
# process new stat
|
|
|
|
current_time = installation_epoch() + self.HOUR
|
2016-11-03 08:27:32 +01:00
|
|
|
count_stat = self.make_dummy_count_stat(current_time)
|
|
|
|
process_count_stat(count_stat, current_time)
|
2016-10-12 23:40:48 +02:00
|
|
|
self.assertFillStateEquals(current_time)
|
2016-11-03 08:27:32 +01:00
|
|
|
self.assertEqual(InstallationCount.objects.filter(property = count_stat.property,
|
analytics: Simplify frequency and measurement interval options.
Change the CountStat object to take an is_gauge variable instead of a
smallest_interval variable. Previously, (smallest_interval, frequency)
could be any of (hour, hour), (hour, day), (hour, gauge), (day, hour),
(day, day), or (day, gauge).
The current change is equivalent to excluding (hour, day) and (day, hour)
from the list above.
This change, along with other recent changes, allows us to simplify how we
handle time intervals. This commit also removes the TimeInterval object.
2016-10-14 00:15:46 +02:00
|
|
|
interval = CountStat.HOUR).count(), 1)
|
2016-10-12 23:40:48 +02:00
|
|
|
|
|
|
|
# dirty stat
|
2016-11-03 08:27:32 +01:00
|
|
|
FillState.objects.filter(property=count_stat.property).update(state=FillState.STARTED)
|
|
|
|
process_count_stat(count_stat, current_time)
|
2016-10-12 23:40:48 +02:00
|
|
|
self.assertFillStateEquals(current_time)
|
2016-11-03 08:27:32 +01:00
|
|
|
self.assertEqual(InstallationCount.objects.filter(property = count_stat.property,
|
analytics: Simplify frequency and measurement interval options.
Change the CountStat object to take an is_gauge variable instead of a
smallest_interval variable. Previously, (smallest_interval, frequency)
could be any of (hour, hour), (hour, day), (hour, gauge), (day, hour),
(day, day), or (day, gauge).
The current change is equivalent to excluding (hour, day) and (day, hour)
from the list above.
This change, along with other recent changes, allows us to simplify how we
handle time intervals. This commit also removes the TimeInterval object.
2016-10-14 00:15:46 +02:00
|
|
|
interval = CountStat.HOUR).count(), 1)
|
2016-10-12 23:40:48 +02:00
|
|
|
|
|
|
|
# clean stat, no update
|
2016-11-03 08:27:32 +01:00
|
|
|
process_count_stat(count_stat, current_time)
|
2016-10-12 23:40:48 +02:00
|
|
|
self.assertFillStateEquals(current_time)
|
2016-11-03 08:27:32 +01:00
|
|
|
self.assertEqual(InstallationCount.objects.filter(property = count_stat.property,
|
analytics: Simplify frequency and measurement interval options.
Change the CountStat object to take an is_gauge variable instead of a
smallest_interval variable. Previously, (smallest_interval, frequency)
could be any of (hour, hour), (hour, day), (hour, gauge), (day, hour),
(day, day), or (day, gauge).
The current change is equivalent to excluding (hour, day) and (day, hour)
from the list above.
This change, along with other recent changes, allows us to simplify how we
handle time intervals. This commit also removes the TimeInterval object.
2016-10-14 00:15:46 +02:00
|
|
|
interval = CountStat.HOUR).count(), 1)
|
2016-10-12 23:40:48 +02:00
|
|
|
|
|
|
|
# clean stat, with update
|
|
|
|
current_time = current_time + self.HOUR
|
2016-11-03 08:27:32 +01:00
|
|
|
count_stat = self.make_dummy_count_stat(current_time)
|
|
|
|
process_count_stat(count_stat, current_time)
|
2016-10-12 23:40:48 +02:00
|
|
|
self.assertFillStateEquals(current_time)
|
2016-11-03 08:27:32 +01:00
|
|
|
self.assertEqual(InstallationCount.objects.filter(property = count_stat.property,
|
analytics: Simplify frequency and measurement interval options.
Change the CountStat object to take an is_gauge variable instead of a
smallest_interval variable. Previously, (smallest_interval, frequency)
could be any of (hour, hour), (hour, day), (hour, gauge), (day, hour),
(day, day), or (day, gauge).
The current change is equivalent to excluding (hour, day) and (day, hour)
from the list above.
This change, along with other recent changes, allows us to simplify how we
handle time intervals. This commit also removes the TimeInterval object.
2016-10-14 00:15:46 +02:00
|
|
|
interval = CountStat.HOUR).count(), 2)
|
2016-10-12 23:40:48 +02:00
|
|
|
|
2016-10-08 03:23:24 +02:00
|
|
|
class TestCountStats(AnalyticsTestCase):
|
2016-12-17 03:26:39 +01:00
|
|
|
def setUp(self):
|
|
|
|
# type: () -> None
|
|
|
|
super(TestCountStats, self).setUp()
|
2017-01-13 01:55:46 +01:00
|
|
|
# This tests two things for each of the queries/CountStats: Handling
|
|
|
|
# more than 1 realm, and the time bounds (time_start and time_end in
|
|
|
|
# the queries).
|
2016-12-17 03:26:39 +01:00
|
|
|
self.second_realm = Realm.objects.create(
|
|
|
|
string_id='second-realm', name='Second Realm',
|
|
|
|
domain='second.analytics', date_created=self.TIME_ZERO-2*self.DAY)
|
2017-01-13 01:55:46 +01:00
|
|
|
for minutes_ago in [0, 1, 61, 60*24+1]:
|
|
|
|
creation_time = self.TIME_ZERO - minutes_ago*self.MINUTE
|
|
|
|
user = self.create_user(email='user%s@domain.tld' % (minutes_ago,),
|
|
|
|
realm=self.second_realm, date_joined=creation_time)
|
|
|
|
recipient = self.create_stream_with_recipient(
|
|
|
|
name='stream %s' % (minutes_ago,), realm=self.second_realm,
|
|
|
|
date_created=creation_time)[1]
|
|
|
|
self.create_message(user, recipient, pub_date=creation_time)
|
|
|
|
|
|
|
|
# This realm should not show up in the *Count tables for any of the
|
|
|
|
# messages_* CountStats
|
|
|
|
self.no_message_realm = Realm.objects.create(
|
|
|
|
string_id='no-message-realm', name='No Message Realm',
|
|
|
|
domain='no.message', date_created=self.TIME_ZERO-2*self.DAY)
|
|
|
|
self.create_user(realm=self.no_message_realm)
|
|
|
|
self.create_stream_with_recipient(realm=self.no_message_realm)
|
|
|
|
# This huddle should not show up anywhere
|
|
|
|
self.create_huddle_with_recipient()
|
2016-12-17 03:26:39 +01:00
|
|
|
|
|
|
|
def test_active_users_by_is_bot(self):
|
2016-10-08 03:23:24 +02:00
|
|
|
# type: () -> None
|
2017-01-13 08:12:39 +01:00
|
|
|
stat = COUNT_STATS['active_users:is_bot']
|
|
|
|
self.current_property = stat.property
|
|
|
|
self.current_interval = stat.interval
|
2016-10-08 03:23:24 +02:00
|
|
|
|
2016-12-17 03:26:39 +01:00
|
|
|
# To be included
|
2017-01-13 07:23:47 +01:00
|
|
|
self.create_user(is_bot=True)
|
|
|
|
self.create_user(is_bot=True, date_joined=self.TIME_ZERO-25*self.HOUR)
|
|
|
|
self.create_user(is_bot=False)
|
2016-10-08 03:23:24 +02:00
|
|
|
|
2016-12-17 03:26:39 +01:00
|
|
|
# To be excluded
|
2017-01-13 07:23:47 +01:00
|
|
|
self.create_user(is_active=False)
|
2016-12-17 03:26:39 +01:00
|
|
|
|
|
|
|
do_fill_count_stat_at_hour(stat, self.TIME_ZERO)
|
2016-10-08 03:23:24 +02:00
|
|
|
|
2017-01-13 08:12:39 +01:00
|
|
|
self.assertCountEquals(RealmCount, 2, subgroup='true')
|
|
|
|
self.assertCountEquals(RealmCount, 1, subgroup='false')
|
|
|
|
self.assertCountEquals(RealmCount, 3, subgroup='false', realm=self.second_realm)
|
|
|
|
self.assertCountEquals(RealmCount, 1, subgroup='false', realm=self.no_message_realm)
|
2017-01-13 01:55:46 +01:00
|
|
|
self.assertEqual(RealmCount.objects.count(), 4)
|
2017-01-13 08:12:39 +01:00
|
|
|
self.assertCountEquals(InstallationCount, 2, subgroup='true')
|
|
|
|
self.assertCountEquals(InstallationCount, 5, subgroup='false')
|
2016-12-19 23:45:53 +01:00
|
|
|
self.assertEqual(InstallationCount.objects.count(), 2)
|
2016-12-17 03:26:39 +01:00
|
|
|
self.assertFalse(UserCount.objects.exists())
|
|
|
|
self.assertFalse(StreamCount.objects.exists())
|
2017-01-11 02:37:43 +01:00
|
|
|
|
|
|
|
def test_messages_sent_by_message_type(self):
|
|
|
|
# type: () -> None
|
|
|
|
stat = COUNT_STATS['messages_sent:message_type']
|
|
|
|
self.current_property = stat.property
|
|
|
|
self.current_interval = stat.interval
|
|
|
|
|
|
|
|
# Nothing currently in this stat that is bot related, but so many of
|
|
|
|
# the rest of our stats make the human/bot distinction that one can
|
|
|
|
# imagine a later refactoring that will intentionally or
|
|
|
|
# unintentionally change this. So make one of our users a bot.
|
|
|
|
user1 = self.create_user(is_bot=True)
|
|
|
|
user2 = self.create_user()
|
|
|
|
user3 = self.create_user()
|
|
|
|
|
|
|
|
# private streams
|
|
|
|
recipient_stream1 = self.create_stream_with_recipient(invite_only=True)[1]
|
|
|
|
recipient_stream2 = self.create_stream_with_recipient(invite_only=True)[1]
|
|
|
|
self.create_message(user1, recipient_stream1)
|
|
|
|
self.create_message(user2, recipient_stream1)
|
|
|
|
self.create_message(user2, recipient_stream2)
|
|
|
|
|
|
|
|
# public streams
|
|
|
|
recipient_stream3 = self.create_stream_with_recipient()[1]
|
|
|
|
recipient_stream4 = self.create_stream_with_recipient()[1]
|
|
|
|
self.create_message(user1, recipient_stream3)
|
|
|
|
self.create_message(user1, recipient_stream4)
|
|
|
|
self.create_message(user2, recipient_stream3)
|
|
|
|
|
|
|
|
# huddles
|
|
|
|
recipient_huddle1 = self.create_huddle_with_recipient()[1]
|
|
|
|
recipient_huddle2 = self.create_huddle_with_recipient()[1]
|
|
|
|
self.create_message(user1, recipient_huddle1)
|
|
|
|
self.create_message(user2, recipient_huddle2)
|
|
|
|
|
|
|
|
# private messages
|
|
|
|
recipient_user1 = Recipient.objects.create(type_id=user1.id, type=Recipient.PERSONAL)
|
|
|
|
recipient_user2 = Recipient.objects.create(type_id=user2.id, type=Recipient.PERSONAL)
|
|
|
|
recipient_user3 = Recipient.objects.create(type_id=user3.id, type=Recipient.PERSONAL)
|
|
|
|
self.create_message(user1, recipient_user2)
|
|
|
|
self.create_message(user2, recipient_user1)
|
|
|
|
self.create_message(user3, recipient_user3)
|
|
|
|
|
|
|
|
do_fill_count_stat_at_hour(stat, self.TIME_ZERO)
|
|
|
|
|
|
|
|
self.assertCountEquals(UserCount, 1, subgroup='private_stream', user=user1)
|
|
|
|
self.assertCountEquals(UserCount, 2, subgroup='private_stream', user=user2)
|
|
|
|
self.assertCountEquals(UserCount, 2, subgroup='public_stream', user=user1)
|
|
|
|
self.assertCountEquals(UserCount, 1, subgroup='public_stream', user=user2)
|
|
|
|
self.assertCountEquals(UserCount, 2, subgroup='private_message', user=user1)
|
|
|
|
self.assertCountEquals(UserCount, 2, subgroup='private_message', user=user2)
|
|
|
|
self.assertCountEquals(UserCount, 1, subgroup='private_message', user=user3)
|
|
|
|
self.assertCountEquals(UserCount, 1, subgroup='public_stream', realm=self.second_realm,
|
|
|
|
user=UserProfile.objects.get(email='user1@domain.tld'))
|
|
|
|
self.assertCountEquals(UserCount, 1, subgroup='public_stream', realm=self.second_realm,
|
|
|
|
user=UserProfile.objects.get(email='user61@domain.tld'))
|
|
|
|
self.assertEqual(UserCount.objects.count(), 9)
|
|
|
|
|
|
|
|
self.assertCountEquals(RealmCount, 3, subgroup='private_stream')
|
|
|
|
self.assertCountEquals(RealmCount, 3, subgroup='public_stream')
|
|
|
|
self.assertCountEquals(RealmCount, 5, subgroup='private_message')
|
|
|
|
self.assertCountEquals(RealmCount, 2, subgroup='public_stream', realm=self.second_realm)
|
|
|
|
self.assertEqual(RealmCount.objects.count(), 4)
|
|
|
|
|
|
|
|
self.assertCountEquals(InstallationCount, 3, subgroup='private_stream')
|
|
|
|
self.assertCountEquals(InstallationCount, 5, subgroup='public_stream')
|
|
|
|
self.assertCountEquals(InstallationCount, 5, subgroup='private_message')
|
|
|
|
self.assertEqual(InstallationCount.objects.count(), 3)
|
|
|
|
|
|
|
|
self.assertFalse(StreamCount.objects.exists())
|
|
|
|
|
|
|
|
def test_messages_sent_to_recipients_with_same_id(self):
|
|
|
|
# type: () -> None
|
|
|
|
stat = COUNT_STATS['messages_sent:message_type']
|
|
|
|
self.current_property = stat.property
|
|
|
|
self.current_interval = stat.interval
|
|
|
|
|
|
|
|
user = self.create_user(id=1000)
|
|
|
|
user_recipient = Recipient.objects.create(type_id=user.id, type=Recipient.PERSONAL)
|
|
|
|
stream_recipient = self.create_stream_with_recipient(id=1000)[1]
|
|
|
|
huddle_recipient = self.create_huddle_with_recipient(id=1000)[1]
|
|
|
|
|
|
|
|
self.create_message(user, user_recipient)
|
|
|
|
self.create_message(user, stream_recipient)
|
|
|
|
self.create_message(user, huddle_recipient)
|
|
|
|
|
|
|
|
do_fill_count_stat_at_hour(stat, self.TIME_ZERO)
|
|
|
|
|
|
|
|
self.assertCountEquals(UserCount, 2, subgroup='private_message')
|
|
|
|
self.assertCountEquals(UserCount, 1, subgroup='public_stream')
|
2017-01-13 06:03:20 +01:00
|
|
|
|
|
|
|
def test_messages_sent_to_stream_by_is_bot(self):
|
|
|
|
# type: () -> None
|
|
|
|
stat = COUNT_STATS['messages_sent_to_stream:is_bot']
|
|
|
|
self.current_property = stat.property
|
|
|
|
self.current_interval = stat.interval
|
|
|
|
|
|
|
|
bot = self.create_user(is_bot=True)
|
|
|
|
human1 = self.create_user()
|
|
|
|
human2 = self.create_user()
|
|
|
|
recipient_human1 = Recipient.objects.create(type_id=human1.id, type=Recipient.PERSONAL)
|
|
|
|
|
|
|
|
stream1, recipient_stream1 = self.create_stream_with_recipient()
|
|
|
|
stream2, recipient_stream2 = self.create_stream_with_recipient()
|
|
|
|
|
|
|
|
# To be included
|
|
|
|
self.create_message(human1, recipient_stream1)
|
|
|
|
self.create_message(human2, recipient_stream1)
|
|
|
|
self.create_message(human1, recipient_stream2)
|
|
|
|
self.create_message(bot, recipient_stream2)
|
|
|
|
self.create_message(bot, recipient_stream2)
|
|
|
|
|
|
|
|
# To be excluded
|
|
|
|
self.create_message(human2, recipient_human1)
|
|
|
|
self.create_message(bot, recipient_human1)
|
|
|
|
recipient_huddle = self.create_huddle_with_recipient()[1]
|
|
|
|
self.create_message(human1, recipient_huddle)
|
|
|
|
|
|
|
|
do_fill_count_stat_at_hour(stat, self.TIME_ZERO)
|
|
|
|
|
|
|
|
self.assertCountEquals(StreamCount, 2, subgroup='false', stream=stream1)
|
|
|
|
self.assertCountEquals(StreamCount, 1, subgroup='false', stream=stream2)
|
|
|
|
self.assertCountEquals(StreamCount, 2, subgroup='true', stream=stream2)
|
|
|
|
self.assertCountEquals(StreamCount, 1, subgroup='false', realm=self.second_realm)
|
|
|
|
self.assertEqual(StreamCount.objects.count(), 4)
|
|
|
|
|
|
|
|
self.assertCountEquals(RealmCount, 3, subgroup='false')
|
|
|
|
self.assertCountEquals(RealmCount, 2, subgroup='true')
|
|
|
|
self.assertCountEquals(RealmCount, 1, subgroup='false', realm=self.second_realm)
|
|
|
|
self.assertEqual(RealmCount.objects.count(), 3)
|
|
|
|
|
|
|
|
self.assertCountEquals(InstallationCount, 4, subgroup='false')
|
|
|
|
self.assertCountEquals(InstallationCount, 2, subgroup='true')
|
|
|
|
self.assertEqual(InstallationCount.objects.count(), 2)
|
|
|
|
|
|
|
|
self.assertFalse(UserCount.objects.exists())
|