2017-11-16 00:55:49 +01:00
|
|
|
import datetime
|
2016-07-30 00:33:29 +02:00
|
|
|
|
2017-11-16 00:55:49 +01:00
|
|
|
from django.db import models
|
|
|
|
|
2019-10-03 02:01:36 +02:00
|
|
|
from zerver.models import AbstractPushDeviceToken, AbstractRealmAuditLog
|
2019-01-31 00:39:02 +01:00
|
|
|
from analytics.models import BaseCount
|
2013-10-17 16:33:04 +02:00
|
|
|
|
2018-05-11 01:38:41 +02:00
|
|
|
def get_remote_server_by_uuid(uuid: str) -> 'RemoteZulipServer':
|
2016-10-27 23:55:31 +02:00
|
|
|
return RemoteZulipServer.objects.get(uuid=uuid)
|
|
|
|
|
|
|
|
class RemoteZulipServer(models.Model):
|
2018-05-04 01:40:46 +02:00
|
|
|
UUID_LENGTH = 36
|
|
|
|
API_KEY_LENGTH = 64
|
|
|
|
HOSTNAME_MAX_LENGTH = 128
|
2016-10-27 23:55:31 +02:00
|
|
|
|
2018-05-11 01:38:41 +02:00
|
|
|
uuid = models.CharField(max_length=UUID_LENGTH, unique=True) # type: str
|
|
|
|
api_key = models.CharField(max_length=API_KEY_LENGTH) # type: str
|
2018-05-04 01:40:46 +02:00
|
|
|
|
2018-05-11 01:38:41 +02:00
|
|
|
hostname = models.CharField(max_length=HOSTNAME_MAX_LENGTH) # type: str
|
|
|
|
contact_email = models.EmailField(blank=True, null=False) # type: str
|
2017-05-16 01:59:07 +02:00
|
|
|
last_updated = models.DateTimeField('last updated', auto_now=True) # type: datetime.datetime
|
2016-10-27 23:55:31 +02:00
|
|
|
|
2018-03-23 18:19:33 +01:00
|
|
|
def __str__(self) -> str:
|
|
|
|
return "<RemoteZulipServer %s %s>" % (self.hostname, self.uuid[0:12])
|
|
|
|
|
2016-10-27 23:55:31 +02:00
|
|
|
# Variant of PushDeviceToken for a remote server.
|
2018-01-13 19:37:13 +01:00
|
|
|
class RemotePushDeviceToken(AbstractPushDeviceToken):
|
2018-01-29 08:17:31 +01:00
|
|
|
server = models.ForeignKey(RemoteZulipServer, on_delete=models.CASCADE) # type: RemoteZulipServer
|
2016-10-27 23:55:31 +02:00
|
|
|
# The user id on the remote server for this device device this is
|
2017-10-19 06:21:27 +02:00
|
|
|
user_id = models.BigIntegerField(db_index=True) # type: int
|
|
|
|
|
2017-11-05 11:33:09 +01:00
|
|
|
class Meta:
|
2018-10-11 00:53:13 +02:00
|
|
|
unique_together = ("server", "user_id", "kind", "token")
|
2018-01-13 19:37:13 +01:00
|
|
|
|
2018-03-23 18:19:33 +01:00
|
|
|
def __str__(self) -> str:
|
2018-03-23 19:30:50 +01:00
|
|
|
return "<RemotePushDeviceToken %s %s>" % (self.server, self.user_id)
|
2019-01-31 00:39:02 +01:00
|
|
|
|
2019-10-03 02:01:36 +02:00
|
|
|
class RemoteRealmAuditLog(AbstractRealmAuditLog):
|
|
|
|
"""Synced audit data from a remote Zulip server, used primarily for
|
|
|
|
billing. See RealmAuditLog and AbstractRealmAuditLog for details.
|
|
|
|
"""
|
|
|
|
server = models.ForeignKey(RemoteZulipServer, on_delete=models.CASCADE) # type: RemoteZulipServer
|
|
|
|
realm_id = models.IntegerField(db_index=True) # type: int
|
|
|
|
# The remote_id field lets us deduplicate data from the remote server
|
|
|
|
remote_id = models.IntegerField(db_index=True) # type: int
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return "<RemoteRealmAuditLog: %s %s %s %s>" % (
|
|
|
|
self.server, self.event_type, self.event_time, self.id)
|
|
|
|
|
2019-01-31 00:39:02 +01:00
|
|
|
class RemoteInstallationCount(BaseCount):
|
|
|
|
server = models.ForeignKey(RemoteZulipServer, on_delete=models.CASCADE) # type: RemoteZulipServer
|
|
|
|
# The remote_id field lets us deduplicate data from the remote server
|
|
|
|
remote_id = models.IntegerField(db_index=True) # type: int
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ("server", "property", "subgroup", "end_time")
|
2019-04-23 22:19:16 +02:00
|
|
|
index_together = [
|
|
|
|
["server", "remote_id"],
|
|
|
|
]
|
2019-01-31 00:39:02 +01:00
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return "<InstallationCount: %s %s %s>" % (self.property, self.subgroup, self.value)
|
|
|
|
|
|
|
|
# We can't subclass RealmCount because we only have a realm_id here, not a foreign key.
|
|
|
|
class RemoteRealmCount(BaseCount):
|
|
|
|
server = models.ForeignKey(RemoteZulipServer, on_delete=models.CASCADE) # type: RemoteZulipServer
|
|
|
|
realm_id = models.IntegerField(db_index=True) # type: int
|
|
|
|
# The remote_id field lets us deduplicate data from the remote server
|
|
|
|
remote_id = models.IntegerField(db_index=True) # type: int
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ("server", "realm_id", "property", "subgroup", "end_time")
|
2019-04-23 22:19:16 +02:00
|
|
|
index_together = [
|
|
|
|
["property", "end_time"],
|
|
|
|
["server", "remote_id"],
|
|
|
|
]
|
2019-01-31 00:39:02 +01:00
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return "%s %s %s %s %s" % (self.server, self.realm_id, self.property, self.subgroup, self.value)
|