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