Dirk Vogt | 0e565a7 | 2017-10-09 15:14:21 +0200 | [diff] [blame] | 1 | from django.conf import settings |
| 2 | |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 3 | class FormatDict(dict): |
| 4 | def __missing__(self, key): |
| 5 | return "{" + key + "}" |
| 6 | |
| 7 | def fill_in_build_fingerprints(query, build_fingerprints): |
| 8 | all_fingerprints_query = 'select distinct build_fingerprint from crashreports_crashreport' |
| 9 | if len(build_fingerprints) > 0 : |
| 10 | return query.format( |
| 11 | FormatDict(fingerprint_placeholers= |
| 12 | ','.join(["%s"] * len(build_fingerprints)))) |
| 13 | else: |
| 14 | return query.format(FormatDict(fingerprint_placeholers = all_fingerprints_query)) |
| 15 | |
Dirk Vogt | 0e565a7 | 2017-10-09 15:14:21 +0200 | [diff] [blame] | 16 | |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 17 | def execute_device_update_history_query(cursor, params): |
Dirk Vogt | 0e565a7 | 2017-10-09 15:14:21 +0200 | [diff] [blame] | 18 | if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql_psycopg2': |
| 19 | return psql_execute_device_update_history_query(cursor, params) |
| 20 | else: |
| 21 | return sqlite_execute_device_update_history_query(cursor, params) |
| 22 | |
| 23 | |
| 24 | def execute_device_report_history(cursor, params): |
| 25 | if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.postgresql_psycopg2': |
| 26 | return psql_execute_device_report_history(cursor, params) |
| 27 | else: |
| 28 | return sqlite_execute_device_report_history(cursor, params) |
| 29 | |
| 30 | def sqlite_execute_device_update_history_query(cursor, params): |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 31 | query = ''' |
Dirk Vogt | a10db9d | 2017-05-05 09:08:45 +0200 | [diff] [blame] | 32 | SELECT |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 33 | min(crashreports_heartbeat.date) as update_date, |
Dirk Vogt | a10db9d | 2017-05-05 09:08:45 +0200 | [diff] [blame] | 34 | build_fingerprint, |
| 35 | ( select count(crashreports_crashreport.id) from crashreports_crashreport |
| 36 | where boot_reason in ("UNKNOWN", "keyboard power on") |
| 37 | and crashreports_device.id == crashreports_crashreport.device_id |
| 38 | and crashreports_crashreport.build_fingerprint == crashreports_heartbeat.build_fingerprint ) as prob_crashes, |
| 39 | ( select count(crashreports_crashreport.id) from crashreports_crashreport |
| 40 | where boot_reason in ("RTC alarm") |
| 41 | and crashreports_device.id == crashreports_crashreport.device_id |
Dirk Vogt | 38f606c | 2017-12-05 15:30:27 +0100 | [diff] [blame] | 42 | and crashreports_crashreport.build_fingerprint == crashreports_heartbeat.build_fingerprint ) as smpl, |
Dirk Vogt | a10db9d | 2017-05-05 09:08:45 +0200 | [diff] [blame] | 43 | ( select count(crashreports_crashreport.id) from crashreports_crashreport |
| 44 | where boot_reason not in ("UNKNOWN", "keyboard power on", "RTC alarm") |
| 45 | and crashreports_device.id == crashreports_crashreport.device_id |
| 46 | and crashreports_crashreport.build_fingerprint == crashreports_heartbeat.build_fingerprint ) as other, |
| 47 | count(crashreports_heartbeat.id) as heartbeats |
| 48 | FROM |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 49 | crashreports_device |
Dirk Vogt | a10db9d | 2017-05-05 09:08:45 +0200 | [diff] [blame] | 50 | JOIN |
| 51 | crashreports_heartbeat |
| 52 | ON |
| 53 | crashreports_device.id == crashreports_heartbeat.device_id |
| 54 | where |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 55 | crashreports_device.uuid=%s |
| 56 | group by build_fingerprint; |
| 57 | ''' |
| 58 | uuid = params.get('uuid', '18f530d7-e9c3-4dcf-adba-3dddcd7d3155') |
| 59 | param_array = [uuid] |
| 60 | cursor.execute(query, param_array) |
| 61 | |
| 62 | |
Dirk Vogt | 0e565a7 | 2017-10-09 15:14:21 +0200 | [diff] [blame] | 63 | def psql_execute_device_update_history_query(cursor, params): |
| 64 | query = ''' |
| 65 | SELECT |
| 66 | min(crashreports_heartbeat.date) as update_date, |
| 67 | build_fingerprint, |
Dirk Vogt | 38f606c | 2017-12-05 15:30:27 +0100 | [diff] [blame] | 68 | max(crashreports_device.id), |
Dirk Vogt | 0e565a7 | 2017-10-09 15:14:21 +0200 | [diff] [blame] | 69 | ( select count(crashreports_crashreport.id) from crashreports_crashreport |
| 70 | where boot_reason in ('UNKNOWN', 'keyboard power on') |
Dirk Vogt | 38f606c | 2017-12-05 15:30:27 +0100 | [diff] [blame] | 71 | and max(crashreports_device.id) = crashreports_crashreport.device_id |
Dirk Vogt | 0e565a7 | 2017-10-09 15:14:21 +0200 | [diff] [blame] | 72 | and crashreports_crashreport.build_fingerprint = crashreports_heartbeat.build_fingerprint ) as prob_crashes, |
| 73 | ( select count(crashreports_crashreport.id) from crashreports_crashreport |
| 74 | where boot_reason in ('RTC alarm') |
Dirk Vogt | 38f606c | 2017-12-05 15:30:27 +0100 | [diff] [blame] | 75 | and max(crashreports_device.id) = crashreports_crashreport.device_id |
| 76 | and crashreports_crashreport.build_fingerprint = crashreports_heartbeat.build_fingerprint ) as smpl, |
Dirk Vogt | 0e565a7 | 2017-10-09 15:14:21 +0200 | [diff] [blame] | 77 | ( select count(crashreports_crashreport.id) from crashreports_crashreport |
| 78 | where boot_reason not in ('UNKNOWN', 'keyboard power on', 'RTC alarm') |
Dirk Vogt | 38f606c | 2017-12-05 15:30:27 +0100 | [diff] [blame] | 79 | and max(crashreports_device.id) = crashreports_crashreport.device_id |
Dirk Vogt | 0e565a7 | 2017-10-09 15:14:21 +0200 | [diff] [blame] | 80 | and crashreports_crashreport.build_fingerprint = crashreports_heartbeat.build_fingerprint ) as other, |
| 81 | count(crashreports_heartbeat.id) as heartbeats |
| 82 | FROM |
| 83 | crashreports_device |
| 84 | JOIN |
| 85 | crashreports_heartbeat |
| 86 | ON |
| 87 | crashreports_device.id = crashreports_heartbeat.device_id |
| 88 | where |
| 89 | crashreports_device.uuid=%s |
| 90 | group by build_fingerprint; |
| 91 | ''' |
| 92 | uuid = params.get('uuid', '18f530d7-e9c3-4dcf-adba-3dddcd7d3155') |
| 93 | param_array = [uuid] |
| 94 | cursor.execute(query, param_array) |
| 95 | |
| 96 | |
| 97 | def sqlite_execute_device_report_history(cursor, params): |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 98 | query = ''' |
| 99 | SELECT |
Dirk Vogt | a10db9d | 2017-05-05 09:08:45 +0200 | [diff] [blame] | 100 | strftime("%%Y-%%m-%%d",crashreports_heartbeat.date) as date, |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 101 | count(crashreports_heartbeat.id) as heartbeats, |
| 102 | ( |
Dirk Vogt | a10db9d | 2017-05-05 09:08:45 +0200 | [diff] [blame] | 103 | select count(id) from crashreports_crashreport |
| 104 | where |
| 105 | boot_reason in ("RTC alarm") |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 106 | and strftime("%%Y-%%m-%%d",crashreports_crashreport.date) == strftime("%%Y-%%m-%%d",crashreports_heartbeat.date) |
| 107 | and crashreports_device.id == crashreports_crashreport.device_id |
| 108 | ) as smpl, |
| 109 | ( |
Dirk Vogt | a10db9d | 2017-05-05 09:08:45 +0200 | [diff] [blame] | 110 | select count(id) from crashreports_crashreport |
| 111 | where |
| 112 | boot_reason in ("UNKNOWN", "keyboard power on") |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 113 | and strftime("%%Y-%%m-%%d",crashreports_crashreport.date) == strftime("%%Y-%%m-%%d",crashreports_heartbeat.date) |
| 114 | and crashreports_device.id == crashreports_crashreport.device_id |
| 115 | ) as prob_crashes, |
| 116 | ( |
Dirk Vogt | a10db9d | 2017-05-05 09:08:45 +0200 | [diff] [blame] | 117 | select count(id) from crashreports_crashreport |
| 118 | where |
| 119 | boot_reason not in ("RTC alarm", "UNKNOWN", "keyboard power on") |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 120 | and strftime("%%Y-%%m-%%d",crashreports_crashreport.date) == strftime("%%Y-%%m-%%d",crashreports_heartbeat.date) |
| 121 | and crashreports_device.id == crashreports_crashreport.device_id |
| 122 | ) as other |
Dirk Vogt | a10db9d | 2017-05-05 09:08:45 +0200 | [diff] [blame] | 123 | from crashreports_device |
| 124 | join |
| 125 | crashreports_heartbeat on crashreports_device.id == crashreports_heartbeat.device_id |
| 126 | where |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 127 | crashreports_device.uuid = %s |
| 128 | group by date; |
| 129 | ''' |
| 130 | uuid = params.get('uuid', '18f530d7-e9c3-4dcf-adba-3dddcd7d3155') |
| 131 | param_array = [uuid] |
| 132 | cursor.execute(query, param_array) |
Dirk Vogt | 0e565a7 | 2017-10-09 15:14:21 +0200 | [diff] [blame] | 133 | |
| 134 | |
| 135 | def psql_execute_device_report_history(cursor, params): |
| 136 | query = ''' |
| 137 | SELECT |
| 138 | crashreports_heartbeat.date::date as date, |
| 139 | count(crashreports_heartbeat.id) as heartbeats, |
| 140 | count(crashreports_crashreport.id) filter (where crashreports_crashreport.boot_reason in ('RTC alarm')) as SMPL, |
| 141 | count(crashreports_crashreport.id) filter (where crashreports_crashreport.boot_reason in ('UNKNOWN', 'keyboard power on')) as prob_crashes, |
| 142 | count(crashreports_crashreport.id) filter (where crashreports_crashreport.boot_reason not in ('RTC alarm', 'UNKNOWN', 'keyboard power on')) as other |
| 143 | from crashreports_device |
| 144 | join crashreports_heartbeat on crashreports_device.id = crashreports_heartbeat.device_id |
| 145 | left join crashreports_crashreport on crashreports_device.id = crashreports_crashreport.device_id and crashreports_heartbeat.date::date = crashreports_crashreport.date::date |
| 146 | where |
Dirk Vogt | 1e8eb12 | 2017-12-06 10:56:37 +0100 | [diff] [blame] | 147 | crashreports_device.uuid = %s group by crashreports_heartbeat.date, crashreports_device.id; |
Dirk Vogt | 0e565a7 | 2017-10-09 15:14:21 +0200 | [diff] [blame] | 148 | ''' |
| 149 | uuid = params.get('uuid', '18f530d7-e9c3-4dcf-adba-3dddcd7d3155') |
| 150 | param_array = [uuid] |
| 151 | cursor.execute(query, param_array) |