blob: 8523829976ad9338f4de9b237e43f07ecd5e55d7 [file] [log] [blame]
Mitja Nikolaus6a679132018-08-30 14:35:29 +02001"""SQL queries for getting device statistics."""
2
Dirk Vogt0e565a72017-10-09 15:14:21 +02003from django.conf import settings
4
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +02005
Dirk Vogt62ff7f22017-05-04 16:07:21 +02006def execute_device_update_history_query(cursor, params):
Mitja Nikolaus6a679132018-08-30 14:35:29 +02007 """Query the device update history."""
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +02008 if (
9 settings.DATABASES["default"]["ENGINE"]
10 == "django.db.backends.postgresql_psycopg2"
11 ):
Dirk Vogt0e565a72017-10-09 15:14:21 +020012 return psql_execute_device_update_history_query(cursor, params)
Mitja Nikolausb4e3bec2018-08-30 17:16:21 +020013 return sqlite_execute_device_update_history_query(cursor, params)
Dirk Vogt0e565a72017-10-09 15:14:21 +020014
15
16def execute_device_report_history(cursor, params):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020017 """Query the device report history."""
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020018 if (
19 settings.DATABASES["default"]["ENGINE"]
20 == "django.db.backends.postgresql_psycopg2"
21 ):
Dirk Vogt0e565a72017-10-09 15:14:21 +020022 return psql_execute_device_report_history(cursor, params)
Mitja Nikolausb4e3bec2018-08-30 17:16:21 +020023 return sqlite_execute_device_report_history(cursor, params)
Dirk Vogt0e565a72017-10-09 15:14:21 +020024
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020025
Dirk Vogt0e565a72017-10-09 15:14:21 +020026def sqlite_execute_device_update_history_query(cursor, params):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020027 """Execute SQLite query for getting the device update history."""
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020028 query = """
Dirk Vogta10db9d2017-05-05 09:08:45 +020029 SELECT
Dirk Vogt62ff7f22017-05-04 16:07:21 +020030 min(crashreports_heartbeat.date) as update_date,
Dirk Vogta10db9d2017-05-05 09:08:45 +020031 build_fingerprint,
32 ( select count(crashreports_crashreport.id) from crashreports_crashreport
33 where boot_reason in ("UNKNOWN", "keyboard power on")
34 and crashreports_device.id == crashreports_crashreport.device_id
35 and crashreports_crashreport.build_fingerprint == crashreports_heartbeat.build_fingerprint ) as prob_crashes,
36 ( select count(crashreports_crashreport.id) from crashreports_crashreport
37 where boot_reason in ("RTC alarm")
38 and crashreports_device.id == crashreports_crashreport.device_id
Dirk Vogt38f606c2017-12-05 15:30:27 +010039 and crashreports_crashreport.build_fingerprint == crashreports_heartbeat.build_fingerprint ) as smpl,
Dirk Vogta10db9d2017-05-05 09:08:45 +020040 ( select count(crashreports_crashreport.id) from crashreports_crashreport
41 where boot_reason not in ("UNKNOWN", "keyboard power on", "RTC alarm")
42 and crashreports_device.id == crashreports_crashreport.device_id
43 and crashreports_crashreport.build_fingerprint == crashreports_heartbeat.build_fingerprint ) as other,
44 count(crashreports_heartbeat.id) as heartbeats
45 FROM
Dirk Vogt62ff7f22017-05-04 16:07:21 +020046 crashreports_device
Dirk Vogta10db9d2017-05-05 09:08:45 +020047 JOIN
48 crashreports_heartbeat
49 ON
50 crashreports_device.id == crashreports_heartbeat.device_id
51 where
Dirk Vogt62ff7f22017-05-04 16:07:21 +020052 crashreports_device.uuid=%s
53 group by build_fingerprint;
Mitja Nikolaus21075cf2018-08-30 16:53:21 +020054 """ # noqa: E501
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020055 uuid = params.get("uuid", "18f530d7-e9c3-4dcf-adba-3dddcd7d3155")
Dirk Vogt62ff7f22017-05-04 16:07:21 +020056 param_array = [uuid]
57 cursor.execute(query, param_array)
58
59
Dirk Vogt0e565a72017-10-09 15:14:21 +020060def psql_execute_device_update_history_query(cursor, params):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020061 """Execute PostgreSQL query for getting the device update history."""
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020062 query = """
Dirk Vogt0e565a72017-10-09 15:14:21 +020063 SELECT
64 min(crashreports_heartbeat.date) as update_date,
65 build_fingerprint,
Dirk Vogt38f606c2017-12-05 15:30:27 +010066 max(crashreports_device.id),
Dirk Vogt0e565a72017-10-09 15:14:21 +020067 ( select count(crashreports_crashreport.id) from crashreports_crashreport
68 where boot_reason in ('UNKNOWN', 'keyboard power on')
Dirk Vogt38f606c2017-12-05 15:30:27 +010069 and max(crashreports_device.id) = crashreports_crashreport.device_id
Dirk Vogt0e565a72017-10-09 15:14:21 +020070 and crashreports_crashreport.build_fingerprint = crashreports_heartbeat.build_fingerprint ) as prob_crashes,
71 ( select count(crashreports_crashreport.id) from crashreports_crashreport
72 where boot_reason in ('RTC alarm')
Dirk Vogt38f606c2017-12-05 15:30:27 +010073 and max(crashreports_device.id) = crashreports_crashreport.device_id
74 and crashreports_crashreport.build_fingerprint = crashreports_heartbeat.build_fingerprint ) as smpl,
Dirk Vogt0e565a72017-10-09 15:14:21 +020075 ( select count(crashreports_crashreport.id) from crashreports_crashreport
76 where boot_reason not in ('UNKNOWN', 'keyboard power on', 'RTC alarm')
Dirk Vogt38f606c2017-12-05 15:30:27 +010077 and max(crashreports_device.id) = crashreports_crashreport.device_id
Dirk Vogt0e565a72017-10-09 15:14:21 +020078 and crashreports_crashreport.build_fingerprint = crashreports_heartbeat.build_fingerprint ) as other,
79 count(crashreports_heartbeat.id) as heartbeats
80 FROM
81 crashreports_device
82 JOIN
83 crashreports_heartbeat
84 ON
85 crashreports_device.id = crashreports_heartbeat.device_id
86 where
87 crashreports_device.uuid=%s
88 group by build_fingerprint;
Mitja Nikolaus21075cf2018-08-30 16:53:21 +020089 """ # noqa: E501
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020090 uuid = params.get("uuid", "18f530d7-e9c3-4dcf-adba-3dddcd7d3155")
Dirk Vogt0e565a72017-10-09 15:14:21 +020091 param_array = [uuid]
92 cursor.execute(query, param_array)
93
94
95def sqlite_execute_device_report_history(cursor, params):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020096 """Execute SQLite query for getting the device report history."""
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020097 query = """
Dirk Vogt62ff7f22017-05-04 16:07:21 +020098 SELECT
Dirk Vogta10db9d2017-05-05 09:08:45 +020099 strftime("%%Y-%%m-%%d",crashreports_heartbeat.date) as date,
Dirk Vogt62ff7f22017-05-04 16:07:21 +0200100 count(crashreports_heartbeat.id) as heartbeats,
101 (
Dirk Vogta10db9d2017-05-05 09:08:45 +0200102 select count(id) from crashreports_crashreport
103 where
104 boot_reason in ("RTC alarm")
Dirk Vogt62ff7f22017-05-04 16:07:21 +0200105 and strftime("%%Y-%%m-%%d",crashreports_crashreport.date) == strftime("%%Y-%%m-%%d",crashreports_heartbeat.date)
106 and crashreports_device.id == crashreports_crashreport.device_id
107 ) as smpl,
108 (
Dirk Vogta10db9d2017-05-05 09:08:45 +0200109 select count(id) from crashreports_crashreport
110 where
111 boot_reason in ("UNKNOWN", "keyboard power on")
Dirk Vogt62ff7f22017-05-04 16:07:21 +0200112 and strftime("%%Y-%%m-%%d",crashreports_crashreport.date) == strftime("%%Y-%%m-%%d",crashreports_heartbeat.date)
113 and crashreports_device.id == crashreports_crashreport.device_id
114 ) as prob_crashes,
115 (
Dirk Vogta10db9d2017-05-05 09:08:45 +0200116 select count(id) from crashreports_crashreport
117 where
118 boot_reason not in ("RTC alarm", "UNKNOWN", "keyboard power on")
Dirk Vogt62ff7f22017-05-04 16:07:21 +0200119 and strftime("%%Y-%%m-%%d",crashreports_crashreport.date) == strftime("%%Y-%%m-%%d",crashreports_heartbeat.date)
120 and crashreports_device.id == crashreports_crashreport.device_id
121 ) as other
Dirk Vogta10db9d2017-05-05 09:08:45 +0200122 from crashreports_device
123 join
124 crashreports_heartbeat on crashreports_device.id == crashreports_heartbeat.device_id
125 where
Dirk Vogt62ff7f22017-05-04 16:07:21 +0200126 crashreports_device.uuid = %s
127 group by date;
Mitja Nikolaus21075cf2018-08-30 16:53:21 +0200128 """ # noqa: E501
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200129 uuid = params.get("uuid", "18f530d7-e9c3-4dcf-adba-3dddcd7d3155")
Dirk Vogt62ff7f22017-05-04 16:07:21 +0200130 param_array = [uuid]
131 cursor.execute(query, param_array)
Dirk Vogt0e565a72017-10-09 15:14:21 +0200132
133
134def psql_execute_device_report_history(cursor, params):
Mitja Nikolaus6a679132018-08-30 14:35:29 +0200135 """Execute Postgresql query for getting the device report history."""
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200136 query = """
Dirk Vogt0e565a72017-10-09 15:14:21 +0200137 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 Vogt1e8eb122017-12-06 10:56:37 +0100147 crashreports_device.uuid = %s group by crashreports_heartbeat.date, crashreports_device.id;
Mitja Nikolaus21075cf2018-08-30 16:53:21 +0200148 """ # noqa: E501
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +0200149 uuid = params.get("uuid", "18f530d7-e9c3-4dcf-adba-3dddcd7d3155")
Dirk Vogt0e565a72017-10-09 15:14:21 +0200150 param_array = [uuid]
151 cursor.execute(query, param_array)