blob: 512032288b21f7865ecbe7fe6fb5ca06772ab3d4 [file] [log] [blame]
Dirk Vogtc9e10ab2016-10-12 13:58:15 +02001# -*- coding: utf-8 -*-
Dirk Vogtf130c752016-08-23 14:45:01 +02002from django.db import models
Dirk Vogt67eb1482016-10-13 12:42:56 +02003from django.db import transaction
Dirk Vogtc9e10ab2016-10-12 13:58:15 +02004
Dirk Vogtf2a33422016-10-11 17:17:26 +02005from django.contrib.auth.models import User
6from taggit.managers import TaggableManager
Dirk Vogtc9e10ab2016-10-12 13:58:15 +02007
Dirk Vogtf2a33422016-10-11 17:17:26 +02008import uuid
9
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020010
Dirk Vogtf2a33422016-10-11 17:17:26 +020011class Device(models.Model):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020012 def __str__(self):
Dirk Vogt83107df2017-05-02 12:04:19 +020013 return self.uuid
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020014
Dirk Vogtf2a33422016-10-11 17:17:26 +020015 # for every device there is a django user
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020016 uuid = models.CharField(
17 db_index=True,
18 max_length=64,
19 unique=True,
20 default=uuid.uuid4,
21 editable=False,
22 )
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020023 user = models.OneToOneField(
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020024 User,
25 related_name="Hiccup_Device",
26 on_delete=models.CASCADE,
27 unique=True,
28 )
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020029 imei = models.CharField(max_length=32, null=True, blank=True)
30 board_date = models.DateTimeField(null=True, blank=True)
31 chipset = models.CharField(max_length=200, null=True, blank=True)
32 tags = TaggableManager(blank=True)
Dirk Vogtf2a33422016-10-11 17:17:26 +020033 last_heartbeat = models.DateTimeField(null=True, blank=True)
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020034 token = models.CharField(max_length=200, null=True, blank=True)
Dirk Vogt67eb1482016-10-13 12:42:56 +020035 next_per_crashreport_key = models.PositiveIntegerField(default=1)
36 next_per_heartbeat_key = models.PositiveIntegerField(default=1)
37
38 @transaction.atomic
39 def get_crashreport_key(self):
40 ret = self.next_per_crashreport_key
41 self.next_per_crashreport_key = self.next_per_crashreport_key + 1
42 self.save()
43 return ret
44
45 @transaction.atomic
46 def get_heartbeat_key(self):
47 ret = self.next_per_heartbeat_key
Dirk Vogt0d9d5d22016-10-13 16:17:57 +020048 self.next_per_heartbeat_key = self.next_per_heartbeat_key + 1
Dirk Vogt67eb1482016-10-13 12:42:56 +020049 self.save()
50 return ret
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020051
Dirk Vogtf130c752016-08-23 14:45:01 +020052
53def crashreport_file_name(instance, filename):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020054 return "/".join(
55 [
56 "crashreport_uploads",
57 instance.crashreport.device.uuid,
58 str(instance.crashreport.id),
59 str(instance.crashreport.date),
60 filename,
61 ]
62 )
Dirk Vogtf130c752016-08-23 14:45:01 +020063
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020064
Dirk Vogtf130c752016-08-23 14:45:01 +020065class Crashreport(models.Model):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020066 BOOT_REASON_UNKOWN = "UNKNOWN"
67 BOOT_REASON_KEYBOARD_POWER_ON = "keyboard power on"
68 BOOT_REASON_RTC_ALARM = "RTC alarm"
69 CRASH_BOOT_REASONS = [BOOT_REASON_UNKOWN, BOOT_REASON_KEYBOARD_POWER_ON]
70 SMPL_BOOT_REASONS = [BOOT_REASON_RTC_ALARM]
Franz-Xaver Geiger0b3a48e2018-04-16 15:00:14 +020071
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020072 device = models.ForeignKey(
73 Device,
74 db_index=True,
75 related_name="crashreports",
76 on_delete=models.CASCADE,
77 )
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020078 is_fake_report = models.BooleanField(default=False)
79 app_version = models.IntegerField()
80 uptime = models.CharField(max_length=200)
Dirk Vogt83107df2017-05-02 12:04:19 +020081 build_fingerprint = models.CharField(db_index=True, max_length=200)
Borjan Tchakaloff6f239a62018-02-19 09:05:50 +010082 radio_version = models.CharField(db_index=True, max_length=200, null=True)
Dirk Vogt83107df2017-05-02 12:04:19 +020083 boot_reason = models.CharField(db_index=True, max_length=200)
84 power_on_reason = models.CharField(db_index=True, max_length=200)
85 power_off_reason = models.CharField(db_index=True, max_length=200)
86 date = models.DateTimeField(db_index=True)
Dirk Vogtf2a33422016-10-11 17:17:26 +020087 tags = TaggableManager(blank=True)
Dirk Vogt67eb1482016-10-13 12:42:56 +020088 device_local_id = models.PositiveIntegerField(blank=True)
Dirk Vogt36635692016-10-17 12:19:10 +020089 next_logfile_key = models.PositiveIntegerField(default=1)
Dirk Vogteda80d32016-11-21 11:45:50 +010090 created_at = models.DateTimeField(auto_now_add=True)
Dirk Vogt67eb1482016-10-13 12:42:56 +020091
92 @transaction.atomic
93 def get_logfile_key(self):
94 ret = self.next_logfile_key
95 self.next_logfile_key = self.next_logfile_key + 1
96 self.save()
97 return ret
98
99 def save(self, *args, **kwargs):
100 if not self.device_local_id:
101 self.device_local_id = self.device.get_crashreport_key()
102 super(Crashreport, self).save(*args, **kwargs)
Dirk Vogtc9e10ab2016-10-12 13:58:15 +0200103
Dirk Vogtf2a33422016-10-11 17:17:26 +0200104 def _get_uuid(self):
105 "Returns the person's full name."
106 return self.device.uuid
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200107
Dirk Vogtf2a33422016-10-11 17:17:26 +0200108 uuid = property(_get_uuid)
Dirk Vogtc9e10ab2016-10-12 13:58:15 +0200109
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200110
111# TODO remove logfile_type or make it meaningful
Dirk Vogtf2a33422016-10-11 17:17:26 +0200112class LogFile(models.Model):
Dirk Vogt7160b5e2016-10-12 17:04:40 +0200113 logfile_type = models.TextField(max_length=36, default="last_kmsg")
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200114 crashreport = models.ForeignKey(
115 Crashreport, related_name="logfiles", on_delete=models.CASCADE
116 )
Dirk Vogteda80d32016-11-21 11:45:50 +0100117 logfile = models.FileField(upload_to=crashreport_file_name, max_length=500)
Dirk Vogt67eb1482016-10-13 12:42:56 +0200118 crashreport_local_id = models.PositiveIntegerField(blank=True)
Dirk Vogteda80d32016-11-21 11:45:50 +0100119 created_at = models.DateTimeField(auto_now_add=True)
Dirk Vogt67eb1482016-10-13 12:42:56 +0200120
121 def save(self, *args, **kwargs):
Dirk Vogt36635692016-10-17 12:19:10 +0200122 if not self.crashreport_local_id:
123 self.crashreport_local_id = self.crashreport.get_logfile_key()
Dirk Vogt67eb1482016-10-13 12:42:56 +0200124 super(LogFile, self).save(*args, **kwargs)
Dirk Vogtf2a33422016-10-11 17:17:26 +0200125
Dirk Vogtc9e10ab2016-10-12 13:58:15 +0200126
127class HeartBeat(models.Model):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200128 device = models.ForeignKey(
129 Device,
Dirk Vogt83107df2017-05-02 12:04:19 +0200130 db_index=True,
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200131 related_name="heartbeats",
132 on_delete=models.CASCADE,
133 )
Dirk Vogt1433f7c2016-09-20 15:30:56 +0200134 app_version = models.IntegerField()
Dirk Vogtf130c752016-08-23 14:45:01 +0200135 uptime = models.CharField(max_length=200)
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200136 build_fingerprint = models.CharField(db_index=True, max_length=200)
Borjan Tchakaloff6f239a62018-02-19 09:05:50 +0100137 radio_version = models.CharField(db_index=True, max_length=200, null=True)
Dirk Vogt83107df2017-05-02 12:04:19 +0200138 date = models.DateTimeField(db_index=True)
Dirk Vogt67eb1482016-10-13 12:42:56 +0200139 device_local_id = models.PositiveIntegerField(blank=True)
Dirk Vogteda80d32016-11-21 11:45:50 +0100140 created_at = models.DateTimeField(auto_now_add=True)
Dirk Vogt67eb1482016-10-13 12:42:56 +0200141
142 def save(self, *args, **kwargs):
143 if not self.device_local_id:
144 self.device_local_id = self.device.get_heartbeat_key()
145 super(HeartBeat, self).save(*args, **kwargs)
Dirk Vogtc9e10ab2016-10-12 13:58:15 +0200146
147 def _get_uuid(self):
148 "Returns the person's full name."
149 return self.device.uuid
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200150
Dirk Vogtc9e10ab2016-10-12 13:58:15 +0200151 uuid = property(_get_uuid)