blob: a71a346ac72a3b079beec4b7e9d80a921603ed65 [file] [log] [blame]
Dirk Vogt0e565a72017-10-09 15:14:21 +02001from django.conf import settings
2
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +02003
Dirk Vogt62ff7f22017-05-04 16:07:21 +02004class FormatDict(dict):
5 def __missing__(self, key):
6 return "{" + key + "}"
7
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +02008
Dirk Vogt62ff7f22017-05-04 16:07:21 +02009def fill_in_build_fingerprints(query, build_fingerprints):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020010 all_fingerprints_query = (
11 "select distinct build_fingerprint from crashreports_crashreport"
12 )
13 if len(build_fingerprints) > 0:
Dirk Vogt62ff7f22017-05-04 16:07:21 +020014 return query.format(
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020015 FormatDict(
16 fingerprint_placeholers=",".join(
17 ["%s"] * len(build_fingerprints)
18 )
19 )
20 )
Dirk Vogt62ff7f22017-05-04 16:07:21 +020021 else:
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020022 return query.format(
23 FormatDict(fingerprint_placeholers=all_fingerprints_query)
24 )
Dirk Vogt62ff7f22017-05-04 16:07:21 +020025
Dirk Vogt0e565a72017-10-09 15:14:21 +020026
Dirk Vogt62ff7f22017-05-04 16:07:21 +020027def execute_device_update_history_query(cursor, params):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020028 if (
29 settings.DATABASES["default"]["ENGINE"]
30 == "django.db.backends.postgresql_psycopg2"
31 ):
Dirk Vogt0e565a72017-10-09 15:14:21 +020032 return psql_execute_device_update_history_query(cursor, params)
33 else:
34 return sqlite_execute_device_update_history_query(cursor, params)
35
36
37def execute_device_report_history(cursor, params):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020038 if (
39 settings.DATABASES["default"]["ENGINE"]
40 == "django.db.backends.postgresql_psycopg2"
41 ):
Dirk Vogt0e565a72017-10-09 15:14:21 +020042 return psql_execute_device_report_history(cursor, params)
43 else:
44 return sqlite_execute_device_report_history(cursor, params)
45
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020046
Dirk Vogt0e565a72017-10-09 15:14:21 +020047def sqlite_execute_device_update_history_query(cursor, params):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020048 query = """
Dirk Vogta10db9d2017-05-05 09:08:45 +020049 SELECT
Dirk Vogt62ff7f22017-05-04 16:07:21 +020050 min(crashreports_heartbeat.date) as update_date,
Dirk Vogta10db9d2017-05-05 09:08:45 +020051 build_fingerprint,
52 ( select count(crashreports_crashreport.id) from crashreports_crashreport
53 where boot_reason in ("UNKNOWN", "keyboard power on")
54 and crashreports_device.id == crashreports_crashreport.device_id
55 and crashreports_crashreport.build_fingerprint == crashreports_heartbeat.build_fingerprint ) as prob_crashes,
56 ( select count(crashreports_crashreport.id) from crashreports_crashreport
57 where boot_reason in ("RTC alarm")
58 and crashreports_device.id == crashreports_crashreport.device_id
Dirk Vogt38f606c2017-12-05 15:30:27 +010059 and crashreports_crashreport.build_fingerprint == crashreports_heartbeat.build_fingerprint ) as smpl,
Dirk Vogta10db9d2017-05-05 09:08:45 +020060 ( select count(crashreports_crashreport.id) from crashreports_crashreport
61 where boot_reason not in ("UNKNOWN", "keyboard power on", "RTC alarm")
62 and crashreports_device.id == crashreports_crashreport.device_id
63 and crashreports_crashreport.build_fingerprint == crashreports_heartbeat.build_fingerprint ) as other,
64 count(crashreports_heartbeat.id) as heartbeats
65 FROM
Dirk Vogt62ff7f22017-05-04 16:07:21 +020066 crashreports_device
Dirk Vogta10db9d2017-05-05 09:08:45 +020067 JOIN
68 crashreports_heartbeat
69 ON
70 crashreports_device.id == crashreports_heartbeat.device_id
71 where
Dirk Vogt62ff7f22017-05-04 16:07:21 +020072 crashreports_device.uuid=%s
73 group by build_fingerprint;
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020074 """
75 uuid = params.get("uuid", "18f530d7-e9c3-4dcf-adba-3dddcd7d3155")
Dirk Vogt62ff7f22017-05-04 16:07:21 +020076 param_array = [uuid]
77 cursor.execute(query, param_array)
78
79
Dirk Vogt0e565a72017-10-09 15:14:21 +020080def psql_execute_device_update_history_query(cursor, params):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020081 query = """
Dirk Vogt0e565a72017-10-09 15:14:21 +020082 SELECT
83 min(crashreports_heartbeat.date) as update_date,
84 build_fingerprint,
Dirk Vogt38f606c2017-12-05 15:30:27 +010085 max(crashreports_device.id),
Dirk Vogt0e565a72017-10-09 15:14:21 +020086 ( select count(crashreports_crashreport.id) from crashreports_crashreport
87 where boot_reason in ('UNKNOWN', 'keyboard power on')
Dirk Vogt38f606c2017-12-05 15:30:27 +010088 and max(crashreports_device.id) = crashreports_crashreport.device_id
Dirk Vogt0e565a72017-10-09 15:14:21 +020089 and crashreports_crashreport.build_fingerprint = crashreports_heartbeat.build_fingerprint ) as prob_crashes,
90 ( select count(crashreports_crashreport.id) from crashreports_crashreport
91 where boot_reason in ('RTC alarm')
Dirk Vogt38f606c2017-12-05 15:30:27 +010092 and max(crashreports_device.id) = crashreports_crashreport.device_id
93 and crashreports_crashreport.build_fingerprint = crashreports_heartbeat.build_fingerprint ) as smpl,
Dirk Vogt0e565a72017-10-09 15:14:21 +020094 ( select count(crashreports_crashreport.id) from crashreports_crashreport
95 where boot_reason not in ('UNKNOWN', 'keyboard power on', 'RTC alarm')
Dirk Vogt38f606c2017-12-05 15:30:27 +010096 and max(crashreports_device.id) = crashreports_crashreport.device_id
Dirk Vogt0e565a72017-10-09 15:14:21 +020097 and crashreports_crashreport.build_fingerprint = crashreports_heartbeat.build_fingerprint ) as other,
98 count(crashreports_heartbeat.id) as heartbeats
99 FROM
100 crashreports_device
101 JOIN
102 crashreports_heartbeat
103 ON
104 crashreports_device.id = crashreports_heartbeat.device_id
105 where
106 crashreports_device.uuid=%s
107 group by build_fingerprint;
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200108 """
109 uuid = params.get("uuid", "18f530d7-e9c3-4dcf-adba-3dddcd7d3155")
Dirk Vogt0e565a72017-10-09 15:14:21 +0200110 param_array = [uuid]
111 cursor.execute(query, param_array)
112
113
114def sqlite_execute_device_report_history(cursor, params):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200115 query = """
Dirk Vogt62ff7f22017-05-04 16:07:21 +0200116 SELECT
Dirk Vogta10db9d2017-05-05 09:08:45 +0200117 strftime("%%Y-%%m-%%d",crashreports_heartbeat.date) as date,
Dirk Vogt62ff7f22017-05-04 16:07:21 +0200118 count(crashreports_heartbeat.id) as heartbeats,
119 (
Dirk Vogta10db9d2017-05-05 09:08:45 +0200120 select count(id) from crashreports_crashreport
121 where
122 boot_reason in ("RTC alarm")
Dirk Vogt62ff7f22017-05-04 16:07:21 +0200123 and strftime("%%Y-%%m-%%d",crashreports_crashreport.date) == strftime("%%Y-%%m-%%d",crashreports_heartbeat.date)
124 and crashreports_device.id == crashreports_crashreport.device_id
125 ) as smpl,
126 (
Dirk Vogta10db9d2017-05-05 09:08:45 +0200127 select count(id) from crashreports_crashreport
128 where
129 boot_reason in ("UNKNOWN", "keyboard power on")
Dirk Vogt62ff7f22017-05-04 16:07:21 +0200130 and strftime("%%Y-%%m-%%d",crashreports_crashreport.date) == strftime("%%Y-%%m-%%d",crashreports_heartbeat.date)
131 and crashreports_device.id == crashreports_crashreport.device_id
132 ) as prob_crashes,
133 (
Dirk Vogta10db9d2017-05-05 09:08:45 +0200134 select count(id) from crashreports_crashreport
135 where
136 boot_reason not in ("RTC alarm", "UNKNOWN", "keyboard power on")
Dirk Vogt62ff7f22017-05-04 16:07:21 +0200137 and strftime("%%Y-%%m-%%d",crashreports_crashreport.date) == strftime("%%Y-%%m-%%d",crashreports_heartbeat.date)
138 and crashreports_device.id == crashreports_crashreport.device_id
139 ) as other
Dirk Vogta10db9d2017-05-05 09:08:45 +0200140 from crashreports_device
141 join
142 crashreports_heartbeat on crashreports_device.id == crashreports_heartbeat.device_id
143 where
Dirk Vogt62ff7f22017-05-04 16:07:21 +0200144 crashreports_device.uuid = %s
145 group by date;
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200146 """
147 uuid = params.get("uuid", "18f530d7-e9c3-4dcf-adba-3dddcd7d3155")
Dirk Vogt62ff7f22017-05-04 16:07:21 +0200148 param_array = [uuid]
149 cursor.execute(query, param_array)
Dirk Vogt0e565a72017-10-09 15:14:21 +0200150
151
152def psql_execute_device_report_history(cursor, params):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200153 query = """
Dirk Vogt0e565a72017-10-09 15:14:21 +0200154 SELECT
155 crashreports_heartbeat.date::date as date,
156 count(crashreports_heartbeat.id) as heartbeats,
157 count(crashreports_crashreport.id) filter (where crashreports_crashreport.boot_reason in ('RTC alarm')) as SMPL,
158 count(crashreports_crashreport.id) filter (where crashreports_crashreport.boot_reason in ('UNKNOWN', 'keyboard power on')) as prob_crashes,
159 count(crashreports_crashreport.id) filter (where crashreports_crashreport.boot_reason not in ('RTC alarm', 'UNKNOWN', 'keyboard power on')) as other
160 from crashreports_device
161 join crashreports_heartbeat on crashreports_device.id = crashreports_heartbeat.device_id
162 left join crashreports_crashreport on crashreports_device.id = crashreports_crashreport.device_id and crashreports_heartbeat.date::date = crashreports_crashreport.date::date
163 where
Dirk Vogt1e8eb122017-12-06 10:56:37 +0100164 crashreports_device.uuid = %s group by crashreports_heartbeat.date, crashreports_device.id;
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200165 """
166 uuid = params.get("uuid", "18f530d7-e9c3-4dcf-adba-3dddcd7d3155")
Dirk Vogt0e565a72017-10-09 15:14:21 +0200167 param_array = [uuid]
168 cursor.execute(query, param_array)