Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 1 | from django.db import migrations, models |
| 2 | import django.db.models.deletion |
| 3 | |
| 4 | from django.db import connection |
| 5 | from datetime import date, timedelta |
| 6 | |
| 7 | from . import models as myModels |
| 8 | |
| 9 | from django.db import transaction |
| 10 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 11 | |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 12 | def dictfetchall(cursor): |
| 13 | "Returns all rows from a cursor as a dict" |
| 14 | desc = cursor.description |
| 15 | return [ |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 16 | dict(zip([col[0] for col in desc], row)) for row in cursor.fetchall() |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 17 | ] |
| 18 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 19 | |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 20 | @transaction.atomic |
| 21 | def fill_version_data(): |
| 22 | myModels.Version.objects.all().delete() |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 23 | query = """ |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 24 | 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 Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 31 | from crashreports_heartbeat) group by fingerprint order by heartbeats;""" |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 32 | cursor = connection.cursor() |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 33 | cursor.execute(query, []) |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 34 | 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 Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 38 | 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 Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 45 | ) |
| 46 | version.save() |
| 47 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 48 | |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 49 | @transaction.atomic |
| 50 | def fill_version_daily_data(): |
| 51 | myModels.VersionDaily.objects.all().delete() |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 52 | query = """ |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 53 | 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 Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 59 | group by build_fingerprint""" |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 60 | start = date(2016, 8, 1) |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 61 | end = date.today() + timedelta(days=5) |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 62 | 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 Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 67 | cursor.execute(query, [str(day), str(day + timedelta(days=1))] * 4) |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 68 | 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 Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 73 | version=myModels.Version.objects.get( |
| 74 | build_fingerprint=i["build_fingerprint"] |
| 75 | ), |
| 76 | heartbeats=i["heartbeats"], |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 77 | date=day, |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 78 | prob_crashes=i["prob_crashes"], |
| 79 | smpl=i["SMPL"], |
| 80 | other=i["other"], |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 81 | ) |
| 82 | except: |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame^] | 83 | print( |
| 84 | "Skipping entry for {} {}".format( |
| 85 | i["build_fingerprint"], day |
| 86 | ) |
| 87 | ) |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 88 | version_daily.save() |