Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 2 | """Models for devices, heartbeats, crashreports and log files.""" |
| 3 | |
Mitja Nikolaus | b6cf697 | 2018-10-04 15:03:31 +0200 | [diff] [blame] | 4 | import os |
Mitja Nikolaus | bcaf502 | 2018-08-30 16:40:38 +0200 | [diff] [blame] | 5 | import uuid |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 6 | |
Mitja Nikolaus | bcaf502 | 2018-08-30 16:40:38 +0200 | [diff] [blame] | 7 | from django.db import models, transaction |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 8 | from django.contrib.auth.models import User |
| 9 | from taggit.managers import TaggableManager |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 10 | |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 11 | |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 12 | class Device(models.Model): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 13 | """A device representing a phone that has been registered on Hiccup.""" |
| 14 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 15 | def __str__(self): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 16 | """Return the UUID as string representation of a device.""" |
Dirk Vogt | 83107df | 2017-05-02 12:04:19 +0200 | [diff] [blame] | 17 | return self.uuid |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 18 | |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 19 | # for every device there is a django user |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 20 | uuid = models.CharField( |
| 21 | db_index=True, |
| 22 | max_length=64, |
| 23 | unique=True, |
| 24 | default=uuid.uuid4, |
| 25 | editable=False, |
| 26 | ) |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 27 | user = models.OneToOneField( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 28 | User, |
| 29 | related_name="Hiccup_Device", |
| 30 | on_delete=models.CASCADE, |
| 31 | unique=True, |
| 32 | ) |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 33 | imei = models.CharField(max_length=32, null=True, blank=True) |
| 34 | board_date = models.DateTimeField(null=True, blank=True) |
| 35 | chipset = models.CharField(max_length=200, null=True, blank=True) |
| 36 | tags = TaggableManager(blank=True) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 37 | last_heartbeat = models.DateTimeField(null=True, blank=True) |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 38 | token = models.CharField(max_length=200, null=True, blank=True) |
Dirk Vogt | 67eb148 | 2016-10-13 12:42:56 +0200 | [diff] [blame] | 39 | next_per_crashreport_key = models.PositiveIntegerField(default=1) |
| 40 | next_per_heartbeat_key = models.PositiveIntegerField(default=1) |
| 41 | |
| 42 | @transaction.atomic |
| 43 | def get_crashreport_key(self): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 44 | """Get the next key for a crashreport and update the ID-counter.""" |
Dirk Vogt | 67eb148 | 2016-10-13 12:42:56 +0200 | [diff] [blame] | 45 | ret = self.next_per_crashreport_key |
| 46 | self.next_per_crashreport_key = self.next_per_crashreport_key + 1 |
| 47 | self.save() |
| 48 | return ret |
| 49 | |
| 50 | @transaction.atomic |
| 51 | def get_heartbeat_key(self): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 52 | """Get the next key for a heartbeat and update the ID-counter.""" |
Dirk Vogt | 67eb148 | 2016-10-13 12:42:56 +0200 | [diff] [blame] | 53 | ret = self.next_per_heartbeat_key |
Dirk Vogt | 0d9d5d2 | 2016-10-13 16:17:57 +0200 | [diff] [blame] | 54 | self.next_per_heartbeat_key = self.next_per_heartbeat_key + 1 |
Dirk Vogt | 67eb148 | 2016-10-13 12:42:56 +0200 | [diff] [blame] | 55 | self.save() |
| 56 | return ret |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 57 | |
Dirk Vogt | f130c75 | 2016-08-23 14:45:01 +0200 | [diff] [blame] | 58 | |
| 59 | def crashreport_file_name(instance, filename): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 60 | """Generate the full path for new uploaded log files. |
| 61 | |
Mitja Nikolaus | b6cf697 | 2018-10-04 15:03:31 +0200 | [diff] [blame] | 62 | The path is created by splitting up the device UUID into 3 parts: The |
| 63 | first 2 characters, the second 2 characters and the rest. This way the |
| 64 | number of directories in each subdirectory does not get too big. |
| 65 | |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 66 | Args: |
| 67 | instance: The log file instance. |
| 68 | filename: The name of the actual log file. |
| 69 | |
| 70 | Returns: The generated path including the file name. |
| 71 | |
| 72 | """ |
Mitja Nikolaus | b6cf697 | 2018-10-04 15:03:31 +0200 | [diff] [blame] | 73 | return os.path.join( |
Mitja Nikolaus | b6cf697 | 2018-10-04 15:03:31 +0200 | [diff] [blame] | 74 | str(instance.crashreport.device.uuid)[0:2], |
| 75 | str(instance.crashreport.device.uuid)[2:4], |
| 76 | str(instance.crashreport.device.uuid)[4:], |
| 77 | str(instance.crashreport.id), |
| 78 | filename, |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 79 | ) |
Dirk Vogt | f130c75 | 2016-08-23 14:45:01 +0200 | [diff] [blame] | 80 | |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 81 | |
Dirk Vogt | f130c75 | 2016-08-23 14:45:01 +0200 | [diff] [blame] | 82 | class Crashreport(models.Model): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 83 | """A crashreport that was sent by a device.""" |
| 84 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 85 | BOOT_REASON_UNKOWN = "UNKNOWN" |
| 86 | BOOT_REASON_KEYBOARD_POWER_ON = "keyboard power on" |
| 87 | BOOT_REASON_RTC_ALARM = "RTC alarm" |
| 88 | CRASH_BOOT_REASONS = [BOOT_REASON_UNKOWN, BOOT_REASON_KEYBOARD_POWER_ON] |
| 89 | SMPL_BOOT_REASONS = [BOOT_REASON_RTC_ALARM] |
Franz-Xaver Geiger | 0b3a48e | 2018-04-16 15:00:14 +0200 | [diff] [blame] | 90 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 91 | device = models.ForeignKey( |
| 92 | Device, |
| 93 | db_index=True, |
| 94 | related_name="crashreports", |
| 95 | on_delete=models.CASCADE, |
| 96 | ) |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 97 | is_fake_report = models.BooleanField(default=False) |
| 98 | app_version = models.IntegerField() |
| 99 | uptime = models.CharField(max_length=200) |
Dirk Vogt | 83107df | 2017-05-02 12:04:19 +0200 | [diff] [blame] | 100 | build_fingerprint = models.CharField(db_index=True, max_length=200) |
Borjan Tchakaloff | 6f239a6 | 2018-02-19 09:05:50 +0100 | [diff] [blame] | 101 | radio_version = models.CharField(db_index=True, max_length=200, null=True) |
Dirk Vogt | 83107df | 2017-05-02 12:04:19 +0200 | [diff] [blame] | 102 | boot_reason = models.CharField(db_index=True, max_length=200) |
| 103 | power_on_reason = models.CharField(db_index=True, max_length=200) |
| 104 | power_off_reason = models.CharField(db_index=True, max_length=200) |
| 105 | date = models.DateTimeField(db_index=True) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 106 | tags = TaggableManager(blank=True) |
Dirk Vogt | 67eb148 | 2016-10-13 12:42:56 +0200 | [diff] [blame] | 107 | device_local_id = models.PositiveIntegerField(blank=True) |
Dirk Vogt | 3663569 | 2016-10-17 12:19:10 +0200 | [diff] [blame] | 108 | next_logfile_key = models.PositiveIntegerField(default=1) |
Dirk Vogt | eda80d3 | 2016-11-21 11:45:50 +0100 | [diff] [blame] | 109 | created_at = models.DateTimeField(auto_now_add=True) |
Dirk Vogt | 67eb148 | 2016-10-13 12:42:56 +0200 | [diff] [blame] | 110 | |
| 111 | @transaction.atomic |
| 112 | def get_logfile_key(self): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 113 | """Get the next key for a log file and update the ID-counter.""" |
Dirk Vogt | 67eb148 | 2016-10-13 12:42:56 +0200 | [diff] [blame] | 114 | ret = self.next_logfile_key |
| 115 | self.next_logfile_key = self.next_logfile_key + 1 |
| 116 | self.save() |
| 117 | return ret |
| 118 | |
Mitja Nikolaus | 773d0cd | 2018-08-31 10:55:43 +0200 | [diff] [blame] | 119 | def save( |
| 120 | self, |
| 121 | force_insert=False, |
| 122 | force_update=False, |
| 123 | using=None, |
| 124 | update_fields=None, |
| 125 | ): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 126 | """Save the crashreport and set its local ID if it was not set.""" |
Dirk Vogt | 67eb148 | 2016-10-13 12:42:56 +0200 | [diff] [blame] | 127 | if not self.device_local_id: |
| 128 | self.device_local_id = self.device.get_crashreport_key() |
Mitja Nikolaus | 773d0cd | 2018-08-31 10:55:43 +0200 | [diff] [blame] | 129 | super(Crashreport, self).save( |
| 130 | force_insert, force_update, using, update_fields |
| 131 | ) |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 132 | |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 133 | def _get_uuid(self): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 134 | """Return the device UUID.""" |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 135 | return self.device.uuid |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 136 | |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 137 | uuid = property(_get_uuid) |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 138 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 139 | |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 140 | class LogFile(models.Model): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 141 | """A log file that was sent along with a crashreport.""" |
| 142 | |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 143 | logfile_type = models.TextField(max_length=36, default="last_kmsg") |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 144 | crashreport = models.ForeignKey( |
| 145 | Crashreport, related_name="logfiles", on_delete=models.CASCADE |
| 146 | ) |
Dirk Vogt | eda80d3 | 2016-11-21 11:45:50 +0100 | [diff] [blame] | 147 | logfile = models.FileField(upload_to=crashreport_file_name, max_length=500) |
Dirk Vogt | 67eb148 | 2016-10-13 12:42:56 +0200 | [diff] [blame] | 148 | crashreport_local_id = models.PositiveIntegerField(blank=True) |
Dirk Vogt | eda80d3 | 2016-11-21 11:45:50 +0100 | [diff] [blame] | 149 | created_at = models.DateTimeField(auto_now_add=True) |
Dirk Vogt | 67eb148 | 2016-10-13 12:42:56 +0200 | [diff] [blame] | 150 | |
Mitja Nikolaus | 773d0cd | 2018-08-31 10:55:43 +0200 | [diff] [blame] | 151 | def save( |
| 152 | self, |
| 153 | force_insert=False, |
| 154 | force_update=False, |
| 155 | using=None, |
| 156 | update_fields=None, |
| 157 | ): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 158 | """Save the log file and set its local ID if it was not set.""" |
Dirk Vogt | 3663569 | 2016-10-17 12:19:10 +0200 | [diff] [blame] | 159 | if not self.crashreport_local_id: |
| 160 | self.crashreport_local_id = self.crashreport.get_logfile_key() |
Mitja Nikolaus | 773d0cd | 2018-08-31 10:55:43 +0200 | [diff] [blame] | 161 | super(LogFile, self).save( |
| 162 | force_insert, force_update, using, update_fields |
| 163 | ) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 164 | |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 165 | |
| 166 | class HeartBeat(models.Model): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 167 | """A heartbeat that was sent by a device.""" |
| 168 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 169 | device = models.ForeignKey( |
| 170 | Device, |
Dirk Vogt | 83107df | 2017-05-02 12:04:19 +0200 | [diff] [blame] | 171 | db_index=True, |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 172 | related_name="heartbeats", |
| 173 | on_delete=models.CASCADE, |
| 174 | ) |
Dirk Vogt | 1433f7c | 2016-09-20 15:30:56 +0200 | [diff] [blame] | 175 | app_version = models.IntegerField() |
Dirk Vogt | f130c75 | 2016-08-23 14:45:01 +0200 | [diff] [blame] | 176 | uptime = models.CharField(max_length=200) |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 177 | build_fingerprint = models.CharField(db_index=True, max_length=200) |
Borjan Tchakaloff | 6f239a6 | 2018-02-19 09:05:50 +0100 | [diff] [blame] | 178 | radio_version = models.CharField(db_index=True, max_length=200, null=True) |
Dirk Vogt | 83107df | 2017-05-02 12:04:19 +0200 | [diff] [blame] | 179 | date = models.DateTimeField(db_index=True) |
Dirk Vogt | 67eb148 | 2016-10-13 12:42:56 +0200 | [diff] [blame] | 180 | device_local_id = models.PositiveIntegerField(blank=True) |
Dirk Vogt | eda80d3 | 2016-11-21 11:45:50 +0100 | [diff] [blame] | 181 | created_at = models.DateTimeField(auto_now_add=True) |
Dirk Vogt | 67eb148 | 2016-10-13 12:42:56 +0200 | [diff] [blame] | 182 | |
Mitja Nikolaus | 773d0cd | 2018-08-31 10:55:43 +0200 | [diff] [blame] | 183 | def save( |
| 184 | self, |
| 185 | force_insert=False, |
| 186 | force_update=False, |
| 187 | using=None, |
| 188 | update_fields=None, |
| 189 | ): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 190 | """Save the heartbeat and set its local ID if it was not set.""" |
Dirk Vogt | 67eb148 | 2016-10-13 12:42:56 +0200 | [diff] [blame] | 191 | if not self.device_local_id: |
| 192 | self.device_local_id = self.device.get_heartbeat_key() |
Mitja Nikolaus | 773d0cd | 2018-08-31 10:55:43 +0200 | [diff] [blame] | 193 | super(HeartBeat, self).save( |
| 194 | force_insert, force_update, using, update_fields |
| 195 | ) |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 196 | |
| 197 | def _get_uuid(self): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 198 | """Return the device UUID.""" |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 199 | return self.device.uuid |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 200 | |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 201 | uuid = property(_get_uuid) |