2016-07-29 21:52:45 +02:00
|
|
|
from django.db import models
|
2016-10-12 23:40:48 +02:00
|
|
|
from django.utils import timezone
|
2016-07-29 21:52:45 +02:00
|
|
|
|
|
|
|
from zerver.models import Realm, UserProfile, Stream, Recipient
|
|
|
|
from zerver.lib.str_utils import ModelReprMixin
|
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
|
|
|
from zerver.lib.timestamp import datetime_to_UTC, floor_to_day
|
|
|
|
|
2016-07-29 21:52:45 +02:00
|
|
|
import datetime
|
|
|
|
|
2016-12-08 05:06:51 +01:00
|
|
|
from typing import Optional, Tuple, Union, Dict, Any, Text
|
2016-07-29 21:52:45 +02:00
|
|
|
|
2016-10-12 23:40:48 +02:00
|
|
|
class FillState(ModelReprMixin, models.Model):
|
2016-12-08 05:06:51 +01:00
|
|
|
property = models.CharField(max_length=40, unique=True) # type: Text
|
2016-10-12 23:40:48 +02:00
|
|
|
end_time = models.DateTimeField() # type: datetime.datetime
|
|
|
|
|
|
|
|
# Valid states are {DONE, STARTED}
|
|
|
|
DONE = 1
|
|
|
|
STARTED = 2
|
|
|
|
state = models.PositiveSmallIntegerField() # type: int
|
|
|
|
|
|
|
|
last_modified = models.DateTimeField(auto_now=True) # type: datetime.datetime
|
|
|
|
|
|
|
|
def __unicode__(self):
|
2016-12-08 05:06:51 +01:00
|
|
|
# type: () -> Text
|
2016-10-12 23:40:48 +02:00
|
|
|
return u"<FillState: %s %s %s>" % (self.property, self.end_time, self.state)
|
|
|
|
|
|
|
|
# The earliest/starting end_time in FillState
|
|
|
|
# We assume there is at least one realm
|
|
|
|
def installation_epoch():
|
|
|
|
# type: () -> datetime.datetime
|
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
|
|
|
earliest_realm_creation = Realm.objects.aggregate(models.Min('date_created'))['date_created__min']
|
|
|
|
return floor_to_day(datetime_to_UTC(earliest_realm_creation))
|
2016-07-29 21:52:45 +02:00
|
|
|
|
2017-02-08 08:04:10 +01:00
|
|
|
def last_successful_fill(property):
|
|
|
|
# type: (str) -> Optional[datetime.datetime]
|
|
|
|
fillstate = FillState.objects.filter(property=property).first()
|
|
|
|
if fillstate is None:
|
|
|
|
return None
|
|
|
|
if fillstate.state == FillState.DONE:
|
|
|
|
return fillstate.end_time
|
|
|
|
return fillstate.end_time - datetime.timedelta(hours=1)
|
|
|
|
|
2016-07-29 21:52:45 +02:00
|
|
|
# would only ever make entries here by hand
|
|
|
|
class Anomaly(ModelReprMixin, models.Model):
|
2016-12-08 05:06:51 +01:00
|
|
|
info = models.CharField(max_length=1000) # type: Text
|
2016-07-29 21:52:45 +02:00
|
|
|
|
|
|
|
def __unicode__(self):
|
2016-12-08 05:06:51 +01:00
|
|
|
# type: () -> Text
|
2016-07-29 21:52:45 +02:00
|
|
|
return u"<Anomaly: %s... %s>" % (self.info, self.id)
|
|
|
|
|
|
|
|
class BaseCount(ModelReprMixin, models.Model):
|
2016-10-06 23:25:55 +02:00
|
|
|
# Note: When inheriting from BaseCount, you may want to rearrange
|
|
|
|
# the order of the columns in the migration to make sure they
|
|
|
|
# match how you'd like the table to be arranged.
|
2016-12-08 05:06:51 +01:00
|
|
|
property = models.CharField(max_length=32) # type: Text
|
|
|
|
subgroup = models.CharField(max_length=16, null=True) # type: Text
|
2016-07-29 21:52:45 +02:00
|
|
|
end_time = models.DateTimeField() # type: datetime.datetime
|
2016-10-06 23:25:55 +02:00
|
|
|
value = models.BigIntegerField() # type: int
|
2016-07-29 21:52:45 +02:00
|
|
|
anomaly = models.ForeignKey(Anomaly, null=True) # type: Optional[Anomaly]
|
|
|
|
|
|
|
|
class Meta(object):
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def extended_id():
|
|
|
|
# type: () -> Tuple[str, ...]
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def key_model():
|
|
|
|
# type: () -> models.Model
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
class InstallationCount(BaseCount):
|
|
|
|
|
|
|
|
class Meta(object):
|
2017-01-16 22:05:51 +01:00
|
|
|
unique_together = ("property", "subgroup", "end_time")
|
2016-07-29 21:52:45 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def extended_id():
|
|
|
|
# type: () -> Tuple[str, ...]
|
|
|
|
return ()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def key_model():
|
|
|
|
# type: () -> models.Model
|
|
|
|
return None
|
|
|
|
|
|
|
|
def __unicode__(self):
|
2016-12-08 05:06:51 +01:00
|
|
|
# type: () -> Text
|
2016-12-17 03:25:00 +01:00
|
|
|
return u"<InstallationCount: %s %s %s>" % (self.property, self.subgroup, self.value)
|
2016-07-29 21:52:45 +02:00
|
|
|
|
|
|
|
class RealmCount(BaseCount):
|
|
|
|
realm = models.ForeignKey(Realm)
|
|
|
|
|
|
|
|
class Meta(object):
|
2017-01-16 22:05:51 +01:00
|
|
|
unique_together = ("realm", "property", "subgroup", "end_time")
|
2017-02-01 23:29:55 +01:00
|
|
|
index_together = ["property", "end_time"]
|
2016-07-29 21:52:45 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def extended_id():
|
|
|
|
# type: () -> Tuple[str, ...]
|
|
|
|
return ('realm_id',)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def key_model():
|
|
|
|
# type: () -> models.Model
|
|
|
|
return Realm
|
|
|
|
|
|
|
|
def __unicode__(self):
|
2016-12-08 05:06:51 +01:00
|
|
|
# type: () -> Text
|
2016-12-17 03:25:00 +01:00
|
|
|
return u"<RealmCount: %s %s %s %s>" % (self.realm, self.property, self.subgroup, self.value)
|
2016-07-29 21:52:45 +02:00
|
|
|
|
|
|
|
class UserCount(BaseCount):
|
|
|
|
user = models.ForeignKey(UserProfile)
|
|
|
|
realm = models.ForeignKey(Realm)
|
|
|
|
|
|
|
|
class Meta(object):
|
2017-01-16 22:05:51 +01:00
|
|
|
unique_together = ("user", "property", "subgroup", "end_time")
|
2017-02-01 23:29:55 +01:00
|
|
|
# This index dramatically improves the performance of
|
|
|
|
# aggregating from users to realms
|
|
|
|
index_together = ["property", "realm", "end_time"]
|
2016-07-29 21:52:45 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def extended_id():
|
|
|
|
# type: () -> Tuple[str, ...]
|
|
|
|
return ('user_id', 'realm_id')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def key_model():
|
|
|
|
# type: () -> models.Model
|
|
|
|
return UserProfile
|
|
|
|
|
|
|
|
def __unicode__(self):
|
2016-12-08 05:06:51 +01:00
|
|
|
# type: () -> Text
|
2016-12-17 03:25:00 +01:00
|
|
|
return u"<UserCount: %s %s %s %s>" % (self.user, self.property, self.subgroup, self.value)
|
2016-07-29 21:52:45 +02:00
|
|
|
|
|
|
|
class StreamCount(BaseCount):
|
|
|
|
stream = models.ForeignKey(Stream)
|
|
|
|
realm = models.ForeignKey(Realm)
|
|
|
|
|
|
|
|
class Meta(object):
|
2017-01-16 22:05:51 +01:00
|
|
|
unique_together = ("stream", "property", "subgroup", "end_time")
|
2017-02-01 23:29:55 +01:00
|
|
|
# This index dramatically improves the performance of
|
|
|
|
# aggregating from streams to realms
|
|
|
|
index_together = ["property", "realm", "end_time"]
|
2016-07-29 21:52:45 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def extended_id():
|
|
|
|
# type: () -> Tuple[str, ...]
|
|
|
|
return ('stream_id', 'realm_id')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def key_model():
|
|
|
|
# type: () -> models.Model
|
|
|
|
return Stream
|
|
|
|
|
|
|
|
def __unicode__(self):
|
2016-12-08 05:06:51 +01:00
|
|
|
# type: () -> Text
|
2016-12-17 03:25:00 +01:00
|
|
|
return u"<StreamCount: %s %s %s %s %s>" % (self.stream, self.property, self.subgroup, self.value, self.id)
|