blob: f76c89475b298b719b4f242f1a2a18de8aa2c62f [file] [log] [blame]
Dirk Vogtc9e10ab2016-10-12 13:58:15 +02001# -*- coding: utf-8 -*-
Mitja Nikolaus6a679132018-08-30 14:35:29 +02002"""Models for devices, heartbeats, crashreports and log files."""
Mitja Nikolausfd452f82018-11-07 11:53:59 +01003import logging
Mitja Nikolausb6cf6972018-10-04 15:03:31 +02004import os
Mitja Nikolausbcaf5022018-08-30 16:40:38 +02005import uuid
Dirk Vogtc9e10ab2016-10-12 13:58:15 +02006
Mitja Nikolausfd452f82018-11-07 11:53:59 +01007from django.db import models, transaction, IntegrityError
Dirk Vogtf2a33422016-10-11 17:17:26 +02008from django.contrib.auth.models import User
Mitja Nikolauscc90d572018-11-22 16:40:15 +01009from django.dispatch import receiver
Mitja Nikolausfd452f82018-11-07 11:53:59 +010010from django.forms import model_to_dict
Dirk Vogtf2a33422016-10-11 17:17:26 +020011from taggit.managers import TaggableManager
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020012
Mitja Nikolausfd452f82018-11-07 11:53:59 +010013LOGGER = logging.getLogger(__name__)
14
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020015
Dirk Vogtf2a33422016-10-11 17:17:26 +020016class Device(models.Model):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020017 """A device representing a phone that has been registered on Hiccup."""
18
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020019 def __str__(self):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020020 """Return the UUID as string representation of a device."""
Dirk Vogt83107df2017-05-02 12:04:19 +020021 return self.uuid
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020022
Dirk Vogtf2a33422016-10-11 17:17:26 +020023 # for every device there is a django user
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020024 uuid = models.CharField(
25 db_index=True,
26 max_length=64,
27 unique=True,
28 default=uuid.uuid4,
29 editable=False,
30 )
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020031 user = models.OneToOneField(
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020032 User,
33 related_name="Hiccup_Device",
34 on_delete=models.CASCADE,
35 unique=True,
36 )
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020037 imei = models.CharField(max_length=32, null=True, blank=True)
38 board_date = models.DateTimeField(null=True, blank=True)
39 chipset = models.CharField(max_length=200, null=True, blank=True)
40 tags = TaggableManager(blank=True)
Dirk Vogtf2a33422016-10-11 17:17:26 +020041 last_heartbeat = models.DateTimeField(null=True, blank=True)
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020042 token = models.CharField(max_length=200, null=True, blank=True)
Dirk Vogt67eb1482016-10-13 12:42:56 +020043 next_per_crashreport_key = models.PositiveIntegerField(default=1)
44 next_per_heartbeat_key = models.PositiveIntegerField(default=1)
45
46 @transaction.atomic
47 def get_crashreport_key(self):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020048 """Get the next key for a crashreport and update the ID-counter."""
Dirk Vogt67eb1482016-10-13 12:42:56 +020049 ret = self.next_per_crashreport_key
50 self.next_per_crashreport_key = self.next_per_crashreport_key + 1
51 self.save()
52 return ret
53
54 @transaction.atomic
55 def get_heartbeat_key(self):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020056 """Get the next key for a heartbeat and update the ID-counter."""
Dirk Vogt67eb1482016-10-13 12:42:56 +020057 ret = self.next_per_heartbeat_key
Dirk Vogt0d9d5d22016-10-13 16:17:57 +020058 self.next_per_heartbeat_key = self.next_per_heartbeat_key + 1
Dirk Vogt67eb1482016-10-13 12:42:56 +020059 self.save()
60 return ret
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020061
Dirk Vogtf130c752016-08-23 14:45:01 +020062
63def crashreport_file_name(instance, filename):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020064 """Generate the full path for new uploaded log files.
65
Mitja Nikolausb6cf6972018-10-04 15:03:31 +020066 The path is created by splitting up the device UUID into 3 parts: The
67 first 2 characters, the second 2 characters and the rest. This way the
68 number of directories in each subdirectory does not get too big.
69
Mitja Nikolaus6a679132018-08-30 14:35:29 +020070 Args:
71 instance: The log file instance.
72 filename: The name of the actual log file.
73
74 Returns: The generated path including the file name.
75
76 """
Mitja Nikolausb6cf6972018-10-04 15:03:31 +020077 return os.path.join(
Mitja Nikolausb6cf6972018-10-04 15:03:31 +020078 str(instance.crashreport.device.uuid)[0:2],
79 str(instance.crashreport.device.uuid)[2:4],
80 str(instance.crashreport.device.uuid)[4:],
81 str(instance.crashreport.id),
82 filename,
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020083 )
Dirk Vogtf130c752016-08-23 14:45:01 +020084
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020085
Dirk Vogtf130c752016-08-23 14:45:01 +020086class Crashreport(models.Model):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020087 """A crashreport that was sent by a device."""
88
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020089 BOOT_REASON_UNKOWN = "UNKNOWN"
90 BOOT_REASON_KEYBOARD_POWER_ON = "keyboard power on"
91 BOOT_REASON_RTC_ALARM = "RTC alarm"
92 CRASH_BOOT_REASONS = [BOOT_REASON_UNKOWN, BOOT_REASON_KEYBOARD_POWER_ON]
93 SMPL_BOOT_REASONS = [BOOT_REASON_RTC_ALARM]
Franz-Xaver Geiger0b3a48e2018-04-16 15:00:14 +020094
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020095 device = models.ForeignKey(
96 Device,
97 db_index=True,
98 related_name="crashreports",
99 on_delete=models.CASCADE,
100 )
Dirk Vogtc9e10ab2016-10-12 13:58:15 +0200101 is_fake_report = models.BooleanField(default=False)
102 app_version = models.IntegerField()
103 uptime = models.CharField(max_length=200)
Dirk Vogt83107df2017-05-02 12:04:19 +0200104 build_fingerprint = models.CharField(db_index=True, max_length=200)
Borjan Tchakaloff6f239a62018-02-19 09:05:50 +0100105 radio_version = models.CharField(db_index=True, max_length=200, null=True)
Dirk Vogt83107df2017-05-02 12:04:19 +0200106 boot_reason = models.CharField(db_index=True, max_length=200)
107 power_on_reason = models.CharField(db_index=True, max_length=200)
108 power_off_reason = models.CharField(db_index=True, max_length=200)
109 date = models.DateTimeField(db_index=True)
Dirk Vogtf2a33422016-10-11 17:17:26 +0200110 tags = TaggableManager(blank=True)
Dirk Vogt67eb1482016-10-13 12:42:56 +0200111 device_local_id = models.PositiveIntegerField(blank=True)
Dirk Vogt36635692016-10-17 12:19:10 +0200112 next_logfile_key = models.PositiveIntegerField(default=1)
Dirk Vogteda80d32016-11-21 11:45:50 +0100113 created_at = models.DateTimeField(auto_now_add=True)
Dirk Vogt67eb1482016-10-13 12:42:56 +0200114
Mitja Nikolausfd452f82018-11-07 11:53:59 +0100115 class Meta: # noqa: D106
116 unique_together = ("device", "date")
117
Dirk Vogt67eb1482016-10-13 12:42:56 +0200118 @transaction.atomic
119 def get_logfile_key(self):
Mitja Nikolaus6a679132018-08-30 14:35:29 +0200120 """Get the next key for a log file and update the ID-counter."""
Dirk Vogt67eb1482016-10-13 12:42:56 +0200121 ret = self.next_logfile_key
122 self.next_logfile_key = self.next_logfile_key + 1
123 self.save()
124 return ret
125
Mitja Nikolaus773d0cd2018-08-31 10:55:43 +0200126 def save(
127 self,
128 force_insert=False,
129 force_update=False,
130 using=None,
131 update_fields=None,
132 ):
Mitja Nikolaus6a679132018-08-30 14:35:29 +0200133 """Save the crashreport and set its local ID if it was not set."""
Mitja Nikolausfd452f82018-11-07 11:53:59 +0100134 try:
135 with transaction.atomic():
136 if not self.device_local_id:
137 self.device_local_id = self.device.get_crashreport_key()
138 super(Crashreport, self).save(
139 force_insert, force_update, using, update_fields
140 )
141 except IntegrityError:
142 # If there is a duplicate entry, log its values and return
143 # without throwing an exception to keep idempotency of the
144 # interface.
145 LOGGER.debug(
146 "Duplicate Crashreport received and dropped: %s",
147 model_to_dict(self),
148 )
Dirk Vogtc9e10ab2016-10-12 13:58:15 +0200149
Dirk Vogtf2a33422016-10-11 17:17:26 +0200150 def _get_uuid(self):
Mitja Nikolaus6a679132018-08-30 14:35:29 +0200151 """Return the device UUID."""
Dirk Vogtf2a33422016-10-11 17:17:26 +0200152 return self.device.uuid
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200153
Dirk Vogtf2a33422016-10-11 17:17:26 +0200154 uuid = property(_get_uuid)
Dirk Vogtc9e10ab2016-10-12 13:58:15 +0200155
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200156
Dirk Vogtf2a33422016-10-11 17:17:26 +0200157class LogFile(models.Model):
Mitja Nikolaus6a679132018-08-30 14:35:29 +0200158 """A log file that was sent along with a crashreport."""
159
Dirk Vogt7160b5e2016-10-12 17:04:40 +0200160 logfile_type = models.TextField(max_length=36, default="last_kmsg")
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200161 crashreport = models.ForeignKey(
162 Crashreport, related_name="logfiles", on_delete=models.CASCADE
163 )
Dirk Vogteda80d32016-11-21 11:45:50 +0100164 logfile = models.FileField(upload_to=crashreport_file_name, max_length=500)
Dirk Vogt67eb1482016-10-13 12:42:56 +0200165 crashreport_local_id = models.PositiveIntegerField(blank=True)
Dirk Vogteda80d32016-11-21 11:45:50 +0100166 created_at = models.DateTimeField(auto_now_add=True)
Dirk Vogt67eb1482016-10-13 12:42:56 +0200167
Mitja Nikolaus773d0cd2018-08-31 10:55:43 +0200168 def save(
169 self,
170 force_insert=False,
171 force_update=False,
172 using=None,
173 update_fields=None,
174 ):
Mitja Nikolaus6a679132018-08-30 14:35:29 +0200175 """Save the log file and set its local ID if it was not set."""
Dirk Vogt36635692016-10-17 12:19:10 +0200176 if not self.crashreport_local_id:
177 self.crashreport_local_id = self.crashreport.get_logfile_key()
Mitja Nikolaus773d0cd2018-08-31 10:55:43 +0200178 super(LogFile, self).save(
179 force_insert, force_update, using, update_fields
180 )
Dirk Vogtf2a33422016-10-11 17:17:26 +0200181
Dirk Vogtc9e10ab2016-10-12 13:58:15 +0200182
Mitja Nikolauscc90d572018-11-22 16:40:15 +0100183@receiver(models.signals.post_delete, sender=LogFile)
184def auto_delete_file_on_delete(sender, instance, **kwargs):
185 """Delete the file from the filesystem on deletion of the db instance."""
186 # pylint: disable=unused-argument
187
188 if instance.logfile:
189 if os.path.isfile(instance.logfile.path):
190 instance.logfile.delete(save=False)
191
192
Dirk Vogtc9e10ab2016-10-12 13:58:15 +0200193class HeartBeat(models.Model):
Mitja Nikolaus6a679132018-08-30 14:35:29 +0200194 """A heartbeat that was sent by a device."""
195
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200196 device = models.ForeignKey(
197 Device,
Dirk Vogt83107df2017-05-02 12:04:19 +0200198 db_index=True,
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200199 related_name="heartbeats",
200 on_delete=models.CASCADE,
201 )
Dirk Vogt1433f7c2016-09-20 15:30:56 +0200202 app_version = models.IntegerField()
Dirk Vogtf130c752016-08-23 14:45:01 +0200203 uptime = models.CharField(max_length=200)
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200204 build_fingerprint = models.CharField(db_index=True, max_length=200)
Borjan Tchakaloff6f239a62018-02-19 09:05:50 +0100205 radio_version = models.CharField(db_index=True, max_length=200, null=True)
Mitja Nikolausfd452f82018-11-07 11:53:59 +0100206 date = models.DateField(db_index=True)
Dirk Vogt67eb1482016-10-13 12:42:56 +0200207 device_local_id = models.PositiveIntegerField(blank=True)
Dirk Vogteda80d32016-11-21 11:45:50 +0100208 created_at = models.DateTimeField(auto_now_add=True)
Dirk Vogt67eb1482016-10-13 12:42:56 +0200209
Mitja Nikolausfd452f82018-11-07 11:53:59 +0100210 class Meta: # noqa: D106
211 unique_together = ("device", "date")
212
Mitja Nikolaus773d0cd2018-08-31 10:55:43 +0200213 def save(
214 self,
215 force_insert=False,
216 force_update=False,
217 using=None,
218 update_fields=None,
219 ):
Mitja Nikolaus6a679132018-08-30 14:35:29 +0200220 """Save the heartbeat and set its local ID if it was not set."""
Mitja Nikolausfd452f82018-11-07 11:53:59 +0100221 try:
222 with transaction.atomic():
223 if not self.device_local_id:
224 self.device_local_id = self.device.get_heartbeat_key()
225 super(HeartBeat, self).save(
226 force_insert, force_update, using, update_fields
227 )
228 except IntegrityError:
229 # If there is a duplicate entry, log its values and return
230 # without throwing an exception to keep idempotency of the
231 # interface.
232 LOGGER.debug(
233 "Duplicate HeartBeat received and dropped: %s",
234 model_to_dict(self),
235 )
Dirk Vogtc9e10ab2016-10-12 13:58:15 +0200236
237 def _get_uuid(self):
Mitja Nikolaus6a679132018-08-30 14:35:29 +0200238 """Return the device UUID."""
Dirk Vogtc9e10ab2016-10-12 13:58:15 +0200239 return self.device.uuid
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200240
Dirk Vogtc9e10ab2016-10-12 13:58:15 +0200241 uuid = property(_get_uuid)