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