/
crashes.py
487 lines (395 loc) · 17.5 KB
/
crashes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import datetime
import logging
import psycopg2
from socorro.external import MissingOrBadArgumentError
from socorro.external.postgresql import tcbs
from socorro.external.postgresql.base import PostgreSQLBase
from socorro.external.postgresql.util import Util
from socorro.lib import datetimeutil, external_common, search_common, util
import socorro.database.database as db
logger = logging.getLogger("webapi")
class Crashes(PostgreSQLBase):
"""Handle retrieval and creation of crash reports data with PostgreSQL.
"""
def prepare_search_params(self, **kwargs):
"""Return a dictionary of parameters for a search-like SQL query.
Uses socorro.lib.search_common.get_parameters() for arguments
filtering.
"""
params = search_common.get_parameters(kwargs)
if not params["signature"]:
raise MissingOrBadArgumentError(
"Mandatory parameter 'signature' is missing or empty")
params["terms"] = params["signature"]
params["search_mode"] = "is_exactly"
# Default mode falls back to starts_with for postgres
if params["plugin_search_mode"] == "default":
params["plugin_search_mode"] = "starts_with"
# Searching for terms in plugins
if params["report_process"] == "plugin" and params["plugin_terms"]:
params["plugin_terms"] = " ".join(params["plugin_terms"])
params["plugin_terms"] = Crashes.prepare_terms(
params["plugin_terms"],
params["plugin_search_mode"])
# Get information about the versions
util_service = Util(config=self.context)
params["versions_info"] = util_service.versions_info(**params)
# Parsing the versions
params["versions_string"] = params["versions"]
(params["versions"], params["products"]) = Crashes.parse_versions(
params["versions"],
params["products"])
# Changing the OS ids to OS names
for i, elem in enumerate(params["os"]):
for platform in self.context.platforms:
if platform["id"] == elem:
params["os"][i] = platform["name"]
return params
def get_comments(self, **kwargs):
"""Return a list of comments on crash reports, filtered by
signatures and other fields.
See socorro.lib.search_common.get_parameters() for all filters.
"""
# Creating the connection to the DB
self.connection = self.database.connection()
cur = self.connection.cursor()
params = self.prepare_search_params(**kwargs)
# Creating the parameters for the sql query
sql_params = {}
# Preparing the different parts of the sql query
# WARNING: sensitive data is returned here (email). When there is
# an authentication mecanism, a verification should be done here.
sql_select = """
SELECT
r.date_processed,
r.user_comments,
r.uuid,
CASE
WHEN r.email = '' THEN null
WHEN r.email IS NULL THEN null
ELSE r.email
END
"""
sql_from = self.build_reports_sql_from(params)
(sql_where, sql_params) = self.build_reports_sql_where(params,
sql_params,
self.context)
sql_where = "%s AND r.user_comments IS NOT NULL" % sql_where
sql_order = "ORDER BY email ASC, r.date_processed ASC"
# Assembling the query
sql_query = " ".join((
"/* external.postgresql.crashes.Crashes.get_comments */",
sql_select, sql_from, sql_where, sql_order))
# Query for counting the results
sql_count_query = " ".join((
"/* external.postgresql.crashes.Crashes.get_comments */",
"SELECT count(*)", sql_from, sql_where))
# Querying the DB
try:
total = db.singleValueSql(cur, sql_count_query, sql_params)
except db.SQLDidNotReturnSingleValue:
total = 0
util.reportExceptionAndContinue(logger)
results = []
# No need to call Postgres if we know there will be no results
if total != 0:
try:
results = db.execute(cur, sql_query, sql_params)
except psycopg2.Error:
util.reportExceptionAndContinue(logger)
result = {
"total": total,
"hits": []
}
# Transforming the results into what we want
for crash in results:
row = dict(zip((
"date_processed",
"user_comments",
"uuid",
"email"), crash))
for i in row:
if isinstance(row[i], datetime.datetime):
row[i] = str(row[i])
result["hits"].append(row)
self.connection.close()
return result
def get_daily(self, **kwargs):
"""Return crashes by active daily users. """
now = datetimeutil.utc_now().date()
lastweek = now - datetime.timedelta(weeks=1)
filters = [
("product", None, "str"),
("versions", None, ["list", "str"]),
("from_date", lastweek, "date"),
("to_date", now, "date"),
("os", None, ["list", "str"]),
("report_type", None, ["list", "str"]),
("separated_by", None, "str"),
("date_range_type", "date", "str"),
]
params = external_common.parse_arguments(filters, kwargs)
if not params.product:
raise MissingOrBadArgumentError(
"Mandatory parameter 'product' is missing or empty")
if not params.versions or not params.versions[0]:
raise MissingOrBadArgumentError(
"Mandatory parameter 'versions' is missing or empty")
params.versions = tuple(params.versions)
if ((not params.os or not params.os[0]) and
(not params.report_type or not params.report_type[0]) and
(not params.separated_by or not params.separated_by[0])):
if params.date_range_type == "build":
table_to_use = "home_page_graph_build_view"
date_range_field = "build_date"
else:
table_to_use = "home_page_graph_view"
date_range_field = "report_date"
db_fields = ("product_name", "version_string", date_range_field,
"report_count", "adu", "crash_hadu")
out_fields = ("product", "version", "date", "report_count", "adu",
"crash_hadu")
db_group = None
sql_where = None
else:
if params.date_range_type == "build":
table_to_use = "crashes_by_user_build_view"
date_range_field = "build_date"
else:
table_to_use = "crashes_by_user_view"
date_range_field = "report_date"
db_fields = [
"product_name",
"version_string",
date_range_field,
"sum(report_count)::bigint as report_count",
"sum(adu)::bigint as adu",
"""crash_hadu(sum(report_count)::bigint, sum(adu)::bigint,
avg(throttle)) as crash_hadu""",
"avg(throttle) as throttle"
]
out_fields = ["product", "version", "date", "report_count", "adu",
"crash_hadu", "throttle"]
db_group = ["product_name", "version_string", date_range_field]
if params.separated_by == "report_type":
db_fields += ["crash_type", "crash_type_short"]
db_group += ["crash_type", "crash_type_short"]
out_fields += ["report_type", "report_type_short"]
elif params.separated_by == "os":
db_fields += ["os_name", "os_short_name"]
db_group += ["os_name", "os_short_name"]
out_fields += ["os", "os_short"]
sql_where = []
if params.os and params.os[0]:
sql_where.append("os_short_name IN %(os)s")
params.os = tuple(x[0:3].lower() for x in params.os)
if params.report_type and params.report_type[0]:
sql_where.append("crash_type_short IN %(report_type)s")
params.report_type = tuple(params.report_type)
sql_where = " AND ".join(sql_where)
sql = """/* socorro.external.postgresql.crashes.Crashes.get_daily */
SELECT %(db_fields)s
FROM %(table_to_use)s
WHERE product_name=%%(product)s
AND version_string IN %%(versions)s
AND %(date_range_field)s BETWEEN %%(from_date)s AND %%(to_date)s
""" % {"db_fields": ", ".join(db_fields),
"date_range_field": date_range_field,
"table_to_use": table_to_use}
if sql_where:
sql = "%s AND %s" % (sql, sql_where)
if db_group:
sql = "%s GROUP BY %s" % (sql, ", ".join(db_group))
sql = str(" ".join(sql.split())) # better formatting of the sql string
try:
connection = self.database.connection()
cursor = connection.cursor()
# logger.debug(cursor.mogrify(sql, params))
cursor.execute(sql, params)
results = cursor.fetchall()
except psycopg2.Error:
logger.error("Failed retrieving daily crash data from PostgreSQL",
exc_info=True)
results = []
finally:
connection.close()
hits = {}
for row in results:
daily_data = dict(zip(out_fields, row))
if "throttle" in daily_data:
daily_data["throttle"] = float(daily_data["throttle"])
daily_data["crash_hadu"] = float(daily_data["crash_hadu"])
daily_data["date"] = daily_data["date"].isoformat()
key = "%s:%s" % (daily_data["product"],
daily_data["version"])
if params.separated_by == "os":
key = "%s:%s" % (key, daily_data["os_short"])
if params.separated_by == "report_type":
key = "%s:%s" % (key, daily_data["report_type_short"])
if "os_short" in daily_data:
del daily_data["os_short"]
if "report_type_short" in daily_data:
del daily_data["report_type_short"]
if key not in hits:
hits[key] = {}
hits[key][daily_data["date"]] = daily_data
return {"hits": hits}
def get_frequency(self, **kwargs):
"""Return the number and frequency of crashes on each OS.
See socorro.lib.search_common.get_parameters() for all filters.
"""
params = self.prepare_search_params(**kwargs)
# Creating the parameters for the sql query
sql_params = {
"signature": params.signature
}
# Preparing the different parts of the sql query
sql_select = ["""
SELECT
r.build AS build_date,
COUNT(CASE WHEN (r.signature = %(signature)s) THEN 1 END)
AS count,
CAST(COUNT(CASE WHEN (r.signature = %(signature)s) THEN 1 END)
AS FLOAT(10)) / count(r.id) AS frequency,
COUNT(r.id) AS total
"""]
## Adding count for each OS
for i in self.context.platforms:
sql_select.append("""
COUNT(CASE WHEN (r.signature = %%(signature)s
AND r.os_name = '%s') THEN 1 END) AS count_%s
""" % (i["name"], i["id"]))
sql_select.append("""
CASE WHEN (COUNT(CASE WHEN (r.os_name = '%s') THEN 1 END)
> 0) THEN (CAST(COUNT(CASE WHEN (r.signature = '%s'
AND r.os_name = '%s') THEN 1 END) AS FLOAT(10)) /
COUNT(CASE WHEN (r.os_name = '%s') THEN 1 END)) ELSE 0.0
END AS frequency_%s
""" % (i["name"], params.signature, i["name"], i["name"], i["id"]))
sql_select = ", ".join(sql_select)
sql_from = self.build_reports_sql_from(params)
(sql_where, sql_params) = self.build_reports_sql_where(params,
sql_params,
self.context)
sql_group = "GROUP BY r.build"
sql_order = "ORDER BY r.build DESC"
# Assembling the query
sql = " ".join((
"/* external.postgresql.crashes.Crashes.get_fequency */",
sql_select, sql_from, sql_where, sql_group, sql_order))
sql = str(" ".join(sql.split())) # better formatting of the sql string
result = {
"total": 0,
"hits": []
}
connection = None
try:
connection = self.database.connection()
cur = connection.cursor()
logger.debug(cur.mogrify(sql, sql_params))
results = db.execute(cur, sql, sql_params)
except psycopg2.Error:
logger.error("Failed retrieving extensions data from PostgreSQL",
exc_info=True)
else:
fields = ["build_date", "count", "frequency", "total"]
for i in self.context.platforms:
fields.append("count_%s" % i["id"])
fields.append("frequency_%s" % i["id"])
for crash in results:
row = dict(zip(fields, crash))
result["hits"].append(row)
result["total"] = len(result["hits"])
finally:
if connection:
connection.close()
return result
def get_paireduuid(self, **kwargs):
"""Return paired uuid given a uuid and an optional hangid.
If a hangid is passed, then return only one result. Otherwise, return
all found paired uuids.
"""
filters = [
("uuid", None, "str"),
("hangid", None, "str"),
]
params = external_common.parse_arguments(filters, kwargs)
if not params.uuid:
raise MissingOrBadArgumentError(
"Mandatory parameter 'uuid' is missing or empty")
crash_date = datetimeutil.uuid_to_date(params.uuid)
sql = """
/* socorro.external.postgresql.crashes.Crashes.get_paireduuid */
SELECT uuid
FROM reports r
WHERE r.uuid != %(uuid)s
AND r.date_processed BETWEEN
TIMESTAMP %(crash_date)s - CAST('1 day' AS INTERVAL) AND
TIMESTAMP %(crash_date)s + CAST('1 day' AS INTERVAL)
"""
sql_params = {
"uuid": params.uuid,
"crash_date": crash_date
}
if params.hangid is not None:
sql = """%s
AND r.hangid = %%(hangid)s
LIMIT 1
""" % sql
sql_params["hangid"] = params.hangid
else:
sql = """%s
AND r.hangid IN (
SELECT hangid
FROM reports r2
WHERE r2.date_processed BETWEEN
TIMESTAMP %%(crash_date)s - CAST('1 day' AS INTERVAL)
AND
TIMESTAMP %%(crash_date)s + CAST('1 day' AS INTERVAL)
AND r2.uuid = %%(uuid)s
)
""" % sql
result = {
"total": 0,
"hits": []
}
try:
connection = self.database.connection()
cur = connection.cursor()
results = db.execute(cur, sql, sql_params)
# Transforming the results into what we want
for report in results:
row = dict(zip(("uuid",), report))
result["hits"].append(row)
result["total"] = len(result["hits"])
except psycopg2.Error:
logger.error("Failed to retrieve paired uuids from database",
exc_info=True)
finally:
connection.close()
return result
def get_signatures(self, **kwargs):
"""Return top crashers by signatures.
See http://socorro.readthedocs.org/en/latest/middleware.html#tcbs
"""
filters = [
("product", None, "str"),
("version", None, "str"),
("crash_type", "all", "str"),
("to_date", datetimeutil.utc_now(), "datetime"),
("duration", datetime.timedelta(7), "timedelta"),
("os", None, "str"),
("limit", 100, "int"),
("date_range_type", None, "str")
]
params = external_common.parse_arguments(filters, kwargs)
params.logger = logger
try:
connection = self.database.connection()
cursor = connection.cursor()
return tcbs.twoPeriodTopCrasherComparison(cursor, params)
finally:
connection.close()