blob: 8d8c77d4d05df27e7c7e99fabe8126c6a358f1c6 [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 +02004def execute_device_update_history_query(cursor, params):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +02005 if (
6 settings.DATABASES["default"]["ENGINE"]
7 == "django.db.backends.postgresql_psycopg2"
8 ):
Dirk Vogt0e565a72017-10-09 15:14:21 +02009 return psql_execute_device_update_history_query(cursor, params)
10 else:
11 return sqlite_execute_device_update_history_query(cursor, params)
12
13
14def execute_device_report_history(cursor, params):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020015 if (
16 settings.DATABASES["default"]["ENGINE"]
17 == "django.db.backends.postgresql_psycopg2"
18 ):
Dirk Vogt0e565a72017-10-09 15:14:21 +020019 return psql_execute_device_report_history(cursor, params)
20 else:
21 return sqlite_execute_device_report_history(cursor, params)
22
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020023
Dirk Vogt0e565a72017-10-09 15:14:21 +020024def sqlite_execute_device_update_history_query(cursor, params):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020025 query = """
Dirk Vogta10db9d2017-05-05 09:08:45 +020026 SELECT
Dirk Vogt62ff7f22017-05-04 16:07:21 +020027 min(crashreports_heartbeat.date) as update_date,
Dirk Vogta10db9d2017-05-05 09:08:45 +020028 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 Vogt38f606c2017-12-05 15:30:27 +010036 and crashreports_crashreport.build_fingerprint == crashreports_heartbeat.build_fingerprint ) as smpl,
Dirk Vogta10db9d2017-05-05 09:08:45 +020037 ( 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 Vogt62ff7f22017-05-04 16:07:21 +020043 crashreports_device
Dirk Vogta10db9d2017-05-05 09:08:45 +020044 JOIN
45 crashreports_heartbeat
46 ON
47 crashreports_device.id == crashreports_heartbeat.device_id
48 where
Dirk Vogt62ff7f22017-05-04 16:07:21 +020049 crashreports_device.uuid=%s
50 group by build_fingerprint;
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020051 """
52 uuid = params.get("uuid", "18f530d7-e9c3-4dcf-adba-3dddcd7d3155")
Dirk Vogt62ff7f22017-05-04 16:07:21 +020053 param_array = [uuid]
54 cursor.execute(query, param_array)
55
56
Dirk Vogt0e565a72017-10-09 15:14:21 +020057def psql_execute_device_update_history_query(cursor, params):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020058 query = """
Dirk Vogt0e565a72017-10-09 15:14:21 +020059 SELECT
60 min(crashreports_heartbeat.date) as update_date,
61 build_fingerprint,
Dirk Vogt38f606c2017-12-05 15:30:27 +010062 max(crashreports_device.id),
Dirk Vogt0e565a72017-10-09 15:14:21 +020063 ( select count(crashreports_crashreport.id) from crashreports_crashreport
64 where boot_reason in ('UNKNOWN', 'keyboard power on')
Dirk Vogt38f606c2017-12-05 15:30:27 +010065 and max(crashreports_device.id) = crashreports_crashreport.device_id
Dirk Vogt0e565a72017-10-09 15:14:21 +020066 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 Vogt38f606c2017-12-05 15:30:27 +010069 and max(crashreports_device.id) = crashreports_crashreport.device_id
70 and crashreports_crashreport.build_fingerprint = crashreports_heartbeat.build_fingerprint ) as smpl,
Dirk Vogt0e565a72017-10-09 15:14:21 +020071 ( select count(crashreports_crashreport.id) from crashreports_crashreport
72 where boot_reason not in ('UNKNOWN', 'keyboard power on', 'RTC alarm')
Dirk Vogt38f606c2017-12-05 15:30:27 +010073 and max(crashreports_device.id) = crashreports_crashreport.device_id
Dirk Vogt0e565a72017-10-09 15:14:21 +020074 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 Nikolauscb50f2c2018-08-24 13:54:48 +020085 """
86 uuid = params.get("uuid", "18f530d7-e9c3-4dcf-adba-3dddcd7d3155")
Dirk Vogt0e565a72017-10-09 15:14:21 +020087 param_array = [uuid]
88 cursor.execute(query, param_array)
89
90
91def sqlite_execute_device_report_history(cursor, params):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020092 query = """
Dirk Vogt62ff7f22017-05-04 16:07:21 +020093 SELECT
Dirk Vogta10db9d2017-05-05 09:08:45 +020094 strftime("%%Y-%%m-%%d",crashreports_heartbeat.date) as date,
Dirk Vogt62ff7f22017-05-04 16:07:21 +020095 count(crashreports_heartbeat.id) as heartbeats,
96 (
Dirk Vogta10db9d2017-05-05 09:08:45 +020097 select count(id) from crashreports_crashreport
98 where
99 boot_reason in ("RTC alarm")
Dirk Vogt62ff7f22017-05-04 16:07:21 +0200100 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 Vogta10db9d2017-05-05 09:08:45 +0200104 select count(id) from crashreports_crashreport
105 where
106 boot_reason in ("UNKNOWN", "keyboard power on")
Dirk Vogt62ff7f22017-05-04 16:07:21 +0200107 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 Vogta10db9d2017-05-05 09:08:45 +0200111 select count(id) from crashreports_crashreport
112 where
113 boot_reason not in ("RTC alarm", "UNKNOWN", "keyboard power on")
Dirk Vogt62ff7f22017-05-04 16:07:21 +0200114 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 Vogta10db9d2017-05-05 09:08:45 +0200117 from crashreports_device
118 join
119 crashreports_heartbeat on crashreports_device.id == crashreports_heartbeat.device_id
120 where
Dirk Vogt62ff7f22017-05-04 16:07:21 +0200121 crashreports_device.uuid = %s
122 group by date;
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200123 """
124 uuid = params.get("uuid", "18f530d7-e9c3-4dcf-adba-3dddcd7d3155")
Dirk Vogt62ff7f22017-05-04 16:07:21 +0200125 param_array = [uuid]
126 cursor.execute(query, param_array)
Dirk Vogt0e565a72017-10-09 15:14:21 +0200127
128
129def psql_execute_device_report_history(cursor, params):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200130 query = """
Dirk Vogt0e565a72017-10-09 15:14:21 +0200131 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 Vogt1e8eb122017-12-06 10:56:37 +0100141 crashreports_device.uuid = %s group by crashreports_heartbeat.date, crashreports_device.id;
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200142 """
143 uuid = params.get("uuid", "18f530d7-e9c3-4dcf-adba-3dddcd7d3155")
Dirk Vogt0e565a72017-10-09 15:14:21 +0200144 param_array = [uuid]
145 cursor.execute(query, param_array)