Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 1 | """SQL queries for getting device statistics.""" |
| 2 | |
Dirk Vogt | 0e565a7 | 2017-10-09 15:14:21 +0200 | [diff] [blame] | 3 | from django.conf import settings |
| 4 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 5 | |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 6 | def execute_device_update_history_query(cursor, params): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 7 | """Query the device update history.""" |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 8 | if ( |
| 9 | settings.DATABASES["default"]["ENGINE"] |
| 10 | == "django.db.backends.postgresql_psycopg2" |
| 11 | ): |
Dirk Vogt | 0e565a7 | 2017-10-09 15:14:21 +0200 | [diff] [blame] | 12 | return psql_execute_device_update_history_query(cursor, params) |
| 13 | else: |
| 14 | return sqlite_execute_device_update_history_query(cursor, params) |
| 15 | |
| 16 | |
| 17 | def execute_device_report_history(cursor, params): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 18 | """Query the device report history.""" |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 19 | if ( |
| 20 | settings.DATABASES["default"]["ENGINE"] |
| 21 | == "django.db.backends.postgresql_psycopg2" |
| 22 | ): |
Dirk Vogt | 0e565a7 | 2017-10-09 15:14:21 +0200 | [diff] [blame] | 23 | return psql_execute_device_report_history(cursor, params) |
| 24 | else: |
| 25 | return sqlite_execute_device_report_history(cursor, params) |
| 26 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 27 | |
Dirk Vogt | 0e565a7 | 2017-10-09 15:14:21 +0200 | [diff] [blame] | 28 | def sqlite_execute_device_update_history_query(cursor, params): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 29 | """Execute SQLite query for getting the device update history.""" |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 30 | query = """ |
Dirk Vogt | a10db9d | 2017-05-05 09:08:45 +0200 | [diff] [blame] | 31 | SELECT |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 32 | min(crashreports_heartbeat.date) as update_date, |
Dirk Vogt | a10db9d | 2017-05-05 09:08:45 +0200 | [diff] [blame] | 33 | build_fingerprint, |
| 34 | ( select count(crashreports_crashreport.id) from crashreports_crashreport |
| 35 | where boot_reason in ("UNKNOWN", "keyboard power on") |
| 36 | and crashreports_device.id == crashreports_crashreport.device_id |
| 37 | and crashreports_crashreport.build_fingerprint == crashreports_heartbeat.build_fingerprint ) as prob_crashes, |
| 38 | ( select count(crashreports_crashreport.id) from crashreports_crashreport |
| 39 | where boot_reason in ("RTC alarm") |
| 40 | and crashreports_device.id == crashreports_crashreport.device_id |
Dirk Vogt | 38f606c | 2017-12-05 15:30:27 +0100 | [diff] [blame] | 41 | and crashreports_crashreport.build_fingerprint == crashreports_heartbeat.build_fingerprint ) as smpl, |
Dirk Vogt | a10db9d | 2017-05-05 09:08:45 +0200 | [diff] [blame] | 42 | ( select count(crashreports_crashreport.id) from crashreports_crashreport |
| 43 | where boot_reason not in ("UNKNOWN", "keyboard power on", "RTC alarm") |
| 44 | and crashreports_device.id == crashreports_crashreport.device_id |
| 45 | and crashreports_crashreport.build_fingerprint == crashreports_heartbeat.build_fingerprint ) as other, |
| 46 | count(crashreports_heartbeat.id) as heartbeats |
| 47 | FROM |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 48 | crashreports_device |
Dirk Vogt | a10db9d | 2017-05-05 09:08:45 +0200 | [diff] [blame] | 49 | JOIN |
| 50 | crashreports_heartbeat |
| 51 | ON |
| 52 | crashreports_device.id == crashreports_heartbeat.device_id |
| 53 | where |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 54 | crashreports_device.uuid=%s |
| 55 | group by build_fingerprint; |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 56 | """ |
| 57 | uuid = params.get("uuid", "18f530d7-e9c3-4dcf-adba-3dddcd7d3155") |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 58 | param_array = [uuid] |
| 59 | cursor.execute(query, param_array) |
| 60 | |
| 61 | |
Dirk Vogt | 0e565a7 | 2017-10-09 15:14:21 +0200 | [diff] [blame] | 62 | def psql_execute_device_update_history_query(cursor, params): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 63 | """Execute PostgreSQL query for getting the device update history.""" |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 64 | query = """ |
Dirk Vogt | 0e565a7 | 2017-10-09 15:14:21 +0200 | [diff] [blame] | 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; |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 91 | """ |
| 92 | uuid = params.get("uuid", "18f530d7-e9c3-4dcf-adba-3dddcd7d3155") |
Dirk Vogt | 0e565a7 | 2017-10-09 15:14:21 +0200 | [diff] [blame] | 93 | param_array = [uuid] |
| 94 | cursor.execute(query, param_array) |
| 95 | |
| 96 | |
| 97 | def sqlite_execute_device_report_history(cursor, params): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 98 | """Execute SQLite query for getting the device report history.""" |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 99 | query = """ |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 100 | SELECT |
Dirk Vogt | a10db9d | 2017-05-05 09:08:45 +0200 | [diff] [blame] | 101 | strftime("%%Y-%%m-%%d",crashreports_heartbeat.date) as date, |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 102 | count(crashreports_heartbeat.id) as heartbeats, |
| 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 ("RTC alarm") |
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 smpl, |
| 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 in ("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 prob_crashes, |
| 117 | ( |
Dirk Vogt | a10db9d | 2017-05-05 09:08:45 +0200 | [diff] [blame] | 118 | select count(id) from crashreports_crashreport |
| 119 | where |
| 120 | boot_reason not in ("RTC alarm", "UNKNOWN", "keyboard power on") |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 121 | and strftime("%%Y-%%m-%%d",crashreports_crashreport.date) == strftime("%%Y-%%m-%%d",crashreports_heartbeat.date) |
| 122 | and crashreports_device.id == crashreports_crashreport.device_id |
| 123 | ) as other |
Dirk Vogt | a10db9d | 2017-05-05 09:08:45 +0200 | [diff] [blame] | 124 | from crashreports_device |
| 125 | join |
| 126 | crashreports_heartbeat on crashreports_device.id == crashreports_heartbeat.device_id |
| 127 | where |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 128 | crashreports_device.uuid = %s |
| 129 | group by date; |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 130 | """ |
| 131 | uuid = params.get("uuid", "18f530d7-e9c3-4dcf-adba-3dddcd7d3155") |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 132 | param_array = [uuid] |
| 133 | cursor.execute(query, param_array) |
Dirk Vogt | 0e565a7 | 2017-10-09 15:14:21 +0200 | [diff] [blame] | 134 | |
| 135 | |
| 136 | def psql_execute_device_report_history(cursor, params): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 137 | """Execute Postgresql query for getting the device report history.""" |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 138 | query = """ |
Dirk Vogt | 0e565a7 | 2017-10-09 15:14:21 +0200 | [diff] [blame] | 139 | SELECT |
| 140 | crashreports_heartbeat.date::date as date, |
| 141 | count(crashreports_heartbeat.id) as heartbeats, |
| 142 | count(crashreports_crashreport.id) filter (where crashreports_crashreport.boot_reason in ('RTC alarm')) as SMPL, |
| 143 | count(crashreports_crashreport.id) filter (where crashreports_crashreport.boot_reason in ('UNKNOWN', 'keyboard power on')) as prob_crashes, |
| 144 | count(crashreports_crashreport.id) filter (where crashreports_crashreport.boot_reason not in ('RTC alarm', 'UNKNOWN', 'keyboard power on')) as other |
| 145 | from crashreports_device |
| 146 | join crashreports_heartbeat on crashreports_device.id = crashreports_heartbeat.device_id |
| 147 | left join crashreports_crashreport on crashreports_device.id = crashreports_crashreport.device_id and crashreports_heartbeat.date::date = crashreports_crashreport.date::date |
| 148 | where |
Dirk Vogt | 1e8eb12 | 2017-12-06 10:56:37 +0100 | [diff] [blame] | 149 | crashreports_device.uuid = %s group by crashreports_heartbeat.date, crashreports_device.id; |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 150 | """ |
| 151 | uuid = params.get("uuid", "18f530d7-e9c3-4dcf-adba-3dddcd7d3155") |
Dirk Vogt | 0e565a7 | 2017-10-09 15:14:21 +0200 | [diff] [blame] | 152 | param_array = [uuid] |
| 153 | cursor.execute(query, param_array) |