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 | |
| 11 | def dictfetchall(cursor): |
| 12 | "Returns all rows from a cursor as a dict" |
| 13 | desc = cursor.description |
| 14 | return [ |
| 15 | dict(zip([col[0] for col in desc], row)) |
| 16 | for row in cursor.fetchall() |
| 17 | ] |
| 18 | |
| 19 | @transaction.atomic |
| 20 | def fill_version_data(): |
| 21 | myModels.Version.objects.all().delete() |
| 22 | query = ''' |
| 23 | SELECT fingerprint as build_fingerprint, |
| 24 | ( select count(id) from crashreports_crashreport where boot_reason in ("RTC alarm") and crashreports_crashreport.build_fingerprint = fingerprint) as SMPL, |
| 25 | ( select count(id) from crashreports_crashreport where boot_reason in ("UNKNOWN", "keyboard power on") and crashreports_crashreport.build_fingerprint = fingerprint) as prob_crashes, |
| 26 | ( 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, |
| 27 | ( select count(id) from crashreports_heartbeat where crashreports_heartbeat.build_fingerprint = fingerprint) as heartbeats, |
| 28 | ( select min(crashreports_heartbeat.created_at) from crashreports_heartbeat where crashreports_heartbeat.build_fingerprint = fingerprint) as first_seen |
| 29 | from (select distinct(build_fingerprint) as fingerprint |
| 30 | from crashreports_heartbeat) group by fingerprint order by heartbeats;''' |
| 31 | cursor = connection.cursor() |
| 32 | cursor.execute(query,[]) |
| 33 | desc = cursor.description |
| 34 | for row in cursor.fetchall(): |
| 35 | i = dict(zip([col[0] for col in desc], row)) |
| 36 | version = myModels.Version( |
| 37 | build_fingerprint = i['build_fingerprint'], |
| 38 | first_seen_on = i['first_seen'].split()[0], |
| 39 | released_on = i['first_seen'].split()[0], |
| 40 | heartbeats= i['heartbeats'], |
| 41 | prob_crashes = i['prob_crashes'], |
| 42 | smpl = i['SMPL'], |
| 43 | other = i['other'] |
| 44 | ) |
| 45 | version.save() |
| 46 | |
| 47 | @transaction.atomic |
| 48 | def fill_version_daily_data(): |
| 49 | myModels.VersionDaily.objects.all().delete() |
| 50 | query = ''' |
| 51 | SELECT build_fingerprint, count(id) as heartbeats, |
| 52 | strftime("%%Y-%%m-%%d",crashreports_heartbeat.date) as date, |
| 53 | ( 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, |
| 54 | ( 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, |
| 55 | ( 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 |
| 56 | from crashreports_heartbeat where crashreports_heartbeat.date >= %s and crashreports_heartbeat.date < %s |
| 57 | group by build_fingerprint''' |
| 58 | start = date(2016, 8, 1) |
| 59 | end = date.today() + timedelta(days=5) |
| 60 | delta = end - start |
| 61 | for d in range(delta.days + 1): |
| 62 | day = start + timedelta(days=d) |
| 63 | print("Getting Stats for " + str(day)) |
| 64 | cursor = connection.cursor() |
| 65 | cursor.execute(query,[str(day), str(day+timedelta(days=1))]*4) |
| 66 | desc = cursor.description |
| 67 | for row in cursor.fetchall(): |
| 68 | i = dict(zip([col[0] for col in desc], row)) |
| 69 | try: |
| 70 | version_daily = myModels.VersionDaily( |
| 71 | version = myModels.Version.objects.get(build_fingerprint=i['build_fingerprint']), |
| 72 | heartbeats= i['heartbeats'], |
| 73 | date=day, |
| 74 | prob_crashes = i['prob_crashes'], |
| 75 | smpl = i['SMPL'], |
| 76 | other = i['other'] |
| 77 | ) |
| 78 | except: |
| 79 | print("Skipping entry for {} {}".format(i['build_fingerprint'],day)) |
| 80 | version_daily.save() |