Permalink
Browse files

Fixes bug 734120 - Moved crash fequency query to a new middleware ser…

…vice.
  • Loading branch information...
adngdb committed Apr 3, 2012
1 parent f8d30b3 commit 40f91bf03ba316b1640d98a655773cc282108ecf
View
@@ -11,10 +11,11 @@ API map
New-style, documented services
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-* crashes/
- * `crashes/comments/ <#crashes-comments>`_
- * `crashes/paireduuid/ <#crashes-paireduuid>`_
- * `crashes/signatures/ <#crashes-signatures>`_
+* /crashes/
+ * `/crashes/comments <#crashes-comments>`_
+ * `/crashes/frequency <#crashes-frequency>`_
+ * `/crashes/paireduuid <#crashes-paireduuid>`_
+ * `/crashes/signatures <#crashes-signatures>`_
* `extensions/ <#id7>`_
* `priorityjobs/ <#priorityjobs>`_
* products/
@@ -145,6 +146,111 @@ In normal cases, return something like this::
If no signature is passed as a parameter, return null.
+.. ############################################################################
+ Crashes Frequency API
+ ############################################################################
+
+Crashes Frequency
+-----------------
+
+Return the number and frequency of crashes on each OS.
+
+API specifications
+^^^^^^^^^^^^^^^^^^
+
++----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
+| HTTP method | GET |
++----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
+| URL schema | /crashes/frequency/(parameters) |
++----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
+| Full URL | /crashes/frequency/signature/(signature)/products/(products)/from/(from_date)/to/(to_date)/versions/(versions)/os/(os_name)/branches/(branches)/reasons/(crash_reason)/build_ids/(build_ids)/build_from/(build_from)/build_to/(build_to)/report_process/(report_process)/report_type/(report_type)/plugin_in/(plugin_in)/plugin_search_mode/(plugin_search_mode)/plugin_terms/(plugin_terms)/ |
++----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
+| Example | http://socorro-api/bpapi/crashes/frequency/signature/SocketSend/products/Firefox/versions/Firefox:4.0.1/from/2011-05-01/to/2011-05-05/os/Windows/ |
++----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
+
+Mandatory parameters
+^^^^^^^^^^^^^^^^^^^^
+
++----------------+------------------+---------------+-------------------------+
+| Name | Type of value | Default value | Description |
++================+==================+===============+=========================+
+| signature | String | None | Signature of crash |
+| | | | reports to get. |
++----------------+------------------+---------------+-------------------------+
+
+Optional parameters
+^^^^^^^^^^^^^^^^^^^
+
++------------------------+-------------------------------+----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
+| Name | Type of value | Default value | Description |
++========================+===============================+================+=========================================================================================================================================================+
+| products | String or list of strings | '`Firefox`' | The product we are interested in. (e.g. Firefox, Fennec, Thunderbird… ) |
++------------------------+-------------------------------+----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
+| from | Date | Now - 7 days | Search for crashes that happened after this date. Can use the following formats: '`yyyy-MM-dd`', '`yyyy-MM-dd HH:ii:ss`' or '`yyyy-MM-dd HH:ii:ss.S`'. |
++------------------------+-------------------------------+----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
+| to | Date | Now | Search for crashes that happened before this date. Can use the following formats: '`yyyy-MM-dd`', '`yyyy-MM-dd HH:ii:ss`' or '`yyyy-MM-dd HH:ii:ss.S`'. |
++------------------------+-------------------------------+----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
+| versions | String or list of strings | None | Restring to a specific version of the product. Several versions can be specified, separated by a + symbol. |
++------------------------+-------------------------------+----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
+| os | String or list of strings | None | Restrict to an Operating System. (e.g. Windows, Mac, Linux… ) Several versions can be specified, separated by a + symbol. |
++------------------------+-------------------------------+----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
+| branches | String or list of strings | None | Restrict to a branch of the product. Several branches can be specified, separated by a + symbol. |
++------------------------+-------------------------------+----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
+| reasons | String or list of strings | None | Restricts search to crashes caused by this reason. |
++------------------------+-------------------------------+----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
+| build\_ids | Integer or list of integers | None | Restricts search to crashes that happened on a product with this build ID. |
++------------------------+-------------------------------+----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
+| build\_from | Integer or list of integers | None | Restricts search to crashes with a build id greater than this. |
++------------------------+-------------------------------+----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
+| build\_to | Integer or list of integers | None | Restricts search to crashes with a build id lower than this. |
++------------------------+-------------------------------+----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
+| report\_process | String | '`any`' | Can be '`any`', '`browser`' or '`plugin`'. |
++------------------------+-------------------------------+----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
+| report\_type | String | '`any`' | Can be '`any`', '`crash`' or '`hang`'. |
++------------------------+-------------------------------+----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
+| plugin\_in | String or list of strings | '`name`' | Search for a plugin in this field. '`report\_process`' has to be set to '`plugin`'. |
++------------------------+-------------------------------+----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
+| plugin\_search\_mode | String | '`default`' | How to search for this plugin. report\_process has to be set to plugin. Can be either '`default`', '`is\_exactly`', '`contains`' or '`starts\_with`'. |
++------------------------+-------------------------------+----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
+| plugin\_terms | String or list of strings | None | Terms to search for. Several terms can be specified, separated by a + symbol. report\_process has to be set to plugin. |
++------------------------+-------------------------------+----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
+
+Return value
+^^^^^^^^^^^^
+
+In normal cases, return something like this::
+
+ {
+ "hits": [
+ {
+ "count": 167,
+ "build_date": "20120129064235",
+ "count_mac": 0,
+ "frequency_windows": 1,
+ "count_windows": 167,
+ "frequency": 1,
+ "count_linux": 0,
+ "total": 167,
+ "frequency_linux": 0,
+ "frequency_mac": 0
+ },
+ {
+ "count": 1,
+ "build_date": "20120129063944",
+ "count_mac": 1,
+ "frequency_windows": 0,
+ "count_windows": 0,
+ "frequency": 1,
+ "count_linux": 0,
+ "total": 1,
+ "frequency_linux": 0,
+ "frequency_mac": 1
+ }
+ ],
+ "total": 2
+ }
+
+
.. ############################################################################
Crashes Paireduuid API
############################################################################
@@ -134,6 +134,7 @@ import socorro.middleware.extensions_service as extensions
import socorro.middleware.crashes_paireduuid_service as crashes_paireduuid
import socorro.middleware.products_versions_service as products_versions
import socorro.middleware.priorityjobs_service as priorityjobs
+import socorro.middleware.crashes_frequency_service as crashes_frequency
servicesList = cm.Option()
@@ -164,6 +165,7 @@ servicesList.default = [
crashes_paireduuid.CrashesPaireduuid,
products_versions.ProductsVersions,
priorityjobs.Priorityjobs,
+ crashes_frequency.CrashesFrequency,
]
crashBaseUrl = cm.Option()
@@ -150,6 +150,90 @@ def get_comments(self, **kwargs):
return result
+ 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.
@@ -0,0 +1,35 @@
+import logging
+
+from socorro.middleware.service import DataAPIService
+
+logger = logging.getLogger("webapi")
+
+
+class CrashesFrequency(DataAPIService):
+ """Return the number and frequency of crashes on each OS.
+ """
+
+ service_name = "crashes"
+ uri = "/crashes/frequency/(.*)"
+
+ def __init__(self, config):
+ super(CrashesFrequency, self).__init__(config)
+ logger.debug('CrashesFrequency service __init__')
+
+ def get(self, *args):
+ """Called when a get HTTP request is executed to /crashes/frequency/.
+ """
+ params = self.parse_query_string(args[0])
+ params = self._bind_params(params)
+
+ module = self.get_module(params)
+ impl = module.Crashes(config=self.context)
+ return impl.get_frequency(**params)
+
+ def _bind_params(self, params):
+ """
+ Return parameters with names adaptated for the implementation API.
+ """
+ params["from_date"] = params.get("from")
+ params["to_date"] = params.get("to")
+ return params
Oops, something went wrong.

0 comments on commit 40f91bf

Please sign in to comment.