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( |
| 74 | "crashreport_uploads", |
| 75 | str(instance.crashreport.device.uuid)[0:2], |
| 76 | str(instance.crashreport.device.uuid)[2:4], |
| 77 | str(instance.crashreport.device.uuid)[4:], |
| 78 | str(instance.crashreport.id), |
| 79 | filename, |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 80 | ) |
Dirk Vogt | f130c75 | 2016-08-23 14:45:01 +0200 | [diff] [blame] | 81 | |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 82 | |
Dirk Vogt | f130c75 | 2016-08-23 14:45:01 +0200 | [diff] [blame] | 83 | class Crashreport(models.Model): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 84 | """A crashreport that was sent by a device.""" |
| 85 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 86 | BOOT_REASON_UNKOWN = "UNKNOWN" |
| 87 | BOOT_REASON_KEYBOARD_POWER_ON = "keyboard power on" |
| 88 | BOOT_REASON_RTC_ALARM = "RTC alarm" |
| 89 | CRASH_BOOT_REASONS = [BOOT_REASON_UNKOWN, BOOT_REASON_KEYBOARD_POWER_ON] |
| 90 | SMPL_BOOT_REASONS = [BOOT_REASON_RTC_ALARM] |
Franz-Xaver Geiger | 0b3a48e | 2018-04-16 15:00:14 +0200 | [diff] [blame] | 91 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 92 | device = models.ForeignKey( |
| 93 | Device, |
| 94 | db_index=True, |
| 95 | related_name="crashreports", |
| 96 | on_delete=models.CASCADE, |
| 97 | ) |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 98 | is_fake_report = models.BooleanField(default=False) |
| 99 | app_version = models.IntegerField() |
| 100 | uptime = models.CharField(max_length=200) |
Dirk Vogt | 83107df | 2017-05-02 12:04:19 +0200 | [diff] [blame] | 101 | build_fingerprint = models.CharField(db_index=True, max_length=200) |
Borjan Tchakaloff | 6f239a6 | 2018-02-19 09:05:50 +0100 | [diff] [blame] | 102 | radio_version = models.CharField(db_index=True, max_length=200, null=True) |
Dirk Vogt | 83107df | 2017-05-02 12:04:19 +0200 | [diff] [blame] | 103 | boot_reason = models.CharField(db_index=True, max_length=200) |
| 104 | power_on_reason = models.CharField(db_index=True, max_length=200) |
| 105 | power_off_reason = models.CharField(db_index=True, max_length=200) |
| 106 | date = models.DateTimeField(db_index=True) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 107 | tags = TaggableManager(blank=True) |
Dirk Vogt | 67eb148 | 2016-10-13 12:42:56 +0200 | [diff] [blame] | 108 | device_local_id = models.PositiveIntegerField(blank=True) |
Dirk Vogt | 3663569 | 2016-10-17 12:19:10 +0200 | [diff] [blame] | 109 | next_logfile_key = models.PositiveIntegerField(default=1) |
Dirk Vogt | eda80d3 | 2016-11-21 11:45:50 +0100 | [diff] [blame] | 110 | created_at = models.DateTimeField(auto_now_add=True) |
Dirk Vogt | 67eb148 | 2016-10-13 12:42:56 +0200 | [diff] [blame] | 111 | |
| 112 | @transaction.atomic |
| 113 | def get_logfile_key(self): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 114 | """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] | 115 | ret = self.next_logfile_key |
| 116 | self.next_logfile_key = self.next_logfile_key + 1 |
| 117 | self.save() |
| 118 | return ret |
| 119 | |
Mitja Nikolaus | 773d0cd | 2018-08-31 10:55:43 +0200 | [diff] [blame] | 120 | def save( |
| 121 | self, |
| 122 | force_insert=False, |
| 123 | force_update=False, |
| 124 | using=None, |
| 125 | update_fields=None, |
| 126 | ): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 127 | """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] | 128 | if not self.device_local_id: |
| 129 | self.device_local_id = self.device.get_crashreport_key() |
Mitja Nikolaus | 773d0cd | 2018-08-31 10:55:43 +0200 | [diff] [blame] | 130 | super(Crashreport, self).save( |
| 131 | force_insert, force_update, using, update_fields |
| 132 | ) |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 133 | |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 134 | def _get_uuid(self): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 135 | """Return the device UUID.""" |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 136 | return self.device.uuid |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 137 | |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 138 | uuid = property(_get_uuid) |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 139 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 140 | |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 141 | class LogFile(models.Model): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 142 | """A log file that was sent along with a crashreport.""" |
| 143 | |
Dirk Vogt | 7160b5e | 2016-10-12 17:04:40 +0200 | [diff] [blame] | 144 | logfile_type = models.TextField(max_length=36, default="last_kmsg") |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 145 | crashreport = models.ForeignKey( |
| 146 | Crashreport, related_name="logfiles", on_delete=models.CASCADE |
| 147 | ) |
Dirk Vogt | eda80d3 | 2016-11-21 11:45:50 +0100 | [diff] [blame] | 148 | logfile = models.FileField(upload_to=crashreport_file_name, max_length=500) |
Dirk Vogt | 67eb148 | 2016-10-13 12:42:56 +0200 | [diff] [blame] | 149 | crashreport_local_id = models.PositiveIntegerField(blank=True) |
Dirk Vogt | eda80d3 | 2016-11-21 11:45:50 +0100 | [diff] [blame] | 150 | created_at = models.DateTimeField(auto_now_add=True) |
Dirk Vogt | 67eb148 | 2016-10-13 12:42:56 +0200 | [diff] [blame] | 151 | |
Mitja Nikolaus | 773d0cd | 2018-08-31 10:55:43 +0200 | [diff] [blame] | 152 | def save( |
| 153 | self, |
| 154 | force_insert=False, |
| 155 | force_update=False, |
| 156 | using=None, |
| 157 | update_fields=None, |
| 158 | ): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 159 | """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] | 160 | if not self.crashreport_local_id: |
| 161 | self.crashreport_local_id = self.crashreport.get_logfile_key() |
Mitja Nikolaus | 773d0cd | 2018-08-31 10:55:43 +0200 | [diff] [blame] | 162 | super(LogFile, self).save( |
| 163 | force_insert, force_update, using, update_fields |
| 164 | ) |
Dirk Vogt | f2a3342 | 2016-10-11 17:17:26 +0200 | [diff] [blame] | 165 | |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 166 | |
| 167 | class HeartBeat(models.Model): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 168 | """A heartbeat that was sent by a device.""" |
| 169 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 170 | device = models.ForeignKey( |
| 171 | Device, |
Dirk Vogt | 83107df | 2017-05-02 12:04:19 +0200 | [diff] [blame] | 172 | db_index=True, |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 173 | related_name="heartbeats", |
| 174 | on_delete=models.CASCADE, |
| 175 | ) |
Dirk Vogt | 1433f7c | 2016-09-20 15:30:56 +0200 | [diff] [blame] | 176 | app_version = models.IntegerField() |
Dirk Vogt | f130c75 | 2016-08-23 14:45:01 +0200 | [diff] [blame] | 177 | uptime = models.CharField(max_length=200) |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 178 | build_fingerprint = models.CharField(db_index=True, max_length=200) |
Borjan Tchakaloff | 6f239a6 | 2018-02-19 09:05:50 +0100 | [diff] [blame] | 179 | radio_version = models.CharField(db_index=True, max_length=200, null=True) |
Dirk Vogt | 83107df | 2017-05-02 12:04:19 +0200 | [diff] [blame] | 180 | date = models.DateTimeField(db_index=True) |
Dirk Vogt | 67eb148 | 2016-10-13 12:42:56 +0200 | [diff] [blame] | 181 | device_local_id = models.PositiveIntegerField(blank=True) |
Dirk Vogt | eda80d3 | 2016-11-21 11:45:50 +0100 | [diff] [blame] | 182 | created_at = models.DateTimeField(auto_now_add=True) |
Dirk Vogt | 67eb148 | 2016-10-13 12:42:56 +0200 | [diff] [blame] | 183 | |
Mitja Nikolaus | 773d0cd | 2018-08-31 10:55:43 +0200 | [diff] [blame] | 184 | def save( |
| 185 | self, |
| 186 | force_insert=False, |
| 187 | force_update=False, |
| 188 | using=None, |
| 189 | update_fields=None, |
| 190 | ): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 191 | """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] | 192 | if not self.device_local_id: |
| 193 | self.device_local_id = self.device.get_heartbeat_key() |
Mitja Nikolaus | 773d0cd | 2018-08-31 10:55:43 +0200 | [diff] [blame] | 194 | super(HeartBeat, self).save( |
| 195 | force_insert, force_update, using, update_fields |
| 196 | ) |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 197 | |
| 198 | def _get_uuid(self): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 199 | """Return the device UUID.""" |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 200 | return self.device.uuid |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 201 | |
Dirk Vogt | c9e10ab | 2016-10-12 13:58:15 +0200 | [diff] [blame] | 202 | uuid = property(_get_uuid) |