blob: a2111f743f58834892806bd33d6aa3720e425252 [file] [log] [blame]
Dirk Vogt1accb672017-05-10 14:07:42 +02001from django.db import migrations, models
2import django.db.models.deletion
3
4from django.db import connection
5from datetime import date, timedelta
6
7from . import models as myModels
8
9from django.db import transaction
10
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020011
Dirk Vogt1accb672017-05-10 14:07:42 +020012def dictfetchall(cursor):
13 "Returns all rows from a cursor as a dict"
14 desc = cursor.description
15 return [
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020016 dict(zip([col[0] for col in desc], row)) for row in cursor.fetchall()
Dirk Vogt1accb672017-05-10 14:07:42 +020017 ]
18
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020019
Dirk Vogt1accb672017-05-10 14:07:42 +020020@transaction.atomic
21def fill_version_data():
22 myModels.Version.objects.all().delete()
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020023 query = """
Dirk Vogt1accb672017-05-10 14:07:42 +020024 SELECT fingerprint as build_fingerprint,
25 ( select count(id) from crashreports_crashreport where boot_reason in ("RTC alarm") and crashreports_crashreport.build_fingerprint = fingerprint) as SMPL,
26 ( select count(id) from crashreports_crashreport where boot_reason in ("UNKNOWN", "keyboard power on") and crashreports_crashreport.build_fingerprint = fingerprint) as prob_crashes,
27 ( select count(id) from crashreports_crashreport where boot_reason not in ("RTC alarm", "UNKNOWN", "keyboard power on") and crashreports_crashreport.build_fingerprint = fingerprint) as other,
28 ( select count(id) from crashreports_heartbeat where crashreports_heartbeat.build_fingerprint = fingerprint) as heartbeats,
29 ( select min(crashreports_heartbeat.created_at) from crashreports_heartbeat where crashreports_heartbeat.build_fingerprint = fingerprint) as first_seen
30 from (select distinct(build_fingerprint) as fingerprint
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020031 from crashreports_heartbeat) group by fingerprint order by heartbeats;"""
Dirk Vogt1accb672017-05-10 14:07:42 +020032 cursor = connection.cursor()
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020033 cursor.execute(query, [])
Dirk Vogt1accb672017-05-10 14:07:42 +020034 desc = cursor.description
35 for row in cursor.fetchall():
36 i = dict(zip([col[0] for col in desc], row))
37 version = myModels.Version(
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020038 build_fingerprint=i["build_fingerprint"],
39 first_seen_on=i["first_seen"].split()[0],
40 released_on=i["first_seen"].split()[0],
41 heartbeats=i["heartbeats"],
42 prob_crashes=i["prob_crashes"],
43 smpl=i["SMPL"],
44 other=i["other"],
Dirk Vogt1accb672017-05-10 14:07:42 +020045 )
46 version.save()
47
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020048
Dirk Vogt1accb672017-05-10 14:07:42 +020049@transaction.atomic
50def fill_version_daily_data():
51 myModels.VersionDaily.objects.all().delete()
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020052 query = """
Dirk Vogt1accb672017-05-10 14:07:42 +020053 SELECT build_fingerprint, count(id) as heartbeats,
54 strftime("%%Y-%%m-%%d",crashreports_heartbeat.date) as date,
55 ( select count(id) from crashreports_crashreport where boot_reason in ("RTC alarm") and crashreports_crashreport.build_fingerprint = crashreports_heartbeat.build_fingerprint and crashreports_crashreport.date >= %s and crashreports_crashreport.date < %s) as SMPL,
56 ( select count(id) from crashreports_crashreport where boot_reason in ("UNKNOWN", "keyboard power on") and crashreports_crashreport.build_fingerprint = crashreports_heartbeat.build_fingerprint and crashreports_crashreport.date >= %s and crashreports_crashreport.date < %s) as prob_crashes,
57 ( select count(id) from crashreports_crashreport where boot_reason not in ("RTC alarm", "UNKNOWN", "keyboard power on") and crashreports_crashreport.build_fingerprint = crashreports_heartbeat.build_fingerprint and crashreports_crashreport.date >= %s and crashreports_crashreport.date < %s) as other
58 from crashreports_heartbeat where crashreports_heartbeat.date >= %s and crashreports_heartbeat.date < %s
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020059 group by build_fingerprint"""
Dirk Vogt1accb672017-05-10 14:07:42 +020060 start = date(2016, 8, 1)
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020061 end = date.today() + timedelta(days=5)
Dirk Vogt1accb672017-05-10 14:07:42 +020062 delta = end - start
63 for d in range(delta.days + 1):
64 day = start + timedelta(days=d)
65 print("Getting Stats for " + str(day))
66 cursor = connection.cursor()
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020067 cursor.execute(query, [str(day), str(day + timedelta(days=1))] * 4)
Dirk Vogt1accb672017-05-10 14:07:42 +020068 desc = cursor.description
69 for row in cursor.fetchall():
70 i = dict(zip([col[0] for col in desc], row))
71 try:
72 version_daily = myModels.VersionDaily(
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020073 version=myModels.Version.objects.get(
74 build_fingerprint=i["build_fingerprint"]
75 ),
76 heartbeats=i["heartbeats"],
Dirk Vogt1accb672017-05-10 14:07:42 +020077 date=day,
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020078 prob_crashes=i["prob_crashes"],
79 smpl=i["SMPL"],
80 other=i["other"],
Dirk Vogt1accb672017-05-10 14:07:42 +020081 )
82 except:
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020083 print(
84 "Skipping entry for {} {}".format(
85 i["build_fingerprint"], day
86 )
87 )
Dirk Vogt1accb672017-05-10 14:07:42 +020088 version_daily.save()