diff --git a/socorro/external/postgresql/signature_summary.py b/socorro/external/postgresql/signature_summary.py index c91f766995..7b4d531671 100644 --- a/socorro/external/postgresql/signature_summary.py +++ b/socorro/external/postgresql/signature_summary.py @@ -1,45 +1,46 @@ -import logging -import web - from socorro.external.postgresql.base import PostgreSQLBase import socorro.database.database as db from socorro.lib import external_common report_type_sql = { - 'uptime' : { - "first_col" : 'uptime_string', + 'uptime': { + "first_col": 'uptime_string', "first_col_format": 'category', - "extra_join" : ' JOIN uptime_levels ON reports_clean.uptime >= min_uptime AND reports_clean.uptime < max_uptime ', + "extra_join": """ JOIN uptime_levels ON + reports_clean.uptime >= min_uptime AND + reports_clean.uptime < max_uptime""", }, - 'os' : { - 'first_col' : 'os_version_string', + 'os': { + 'first_col': 'os_version_string', 'first_col_format': 'category', - 'extra_join' : ' JOIN os_versions USING ( os_version_id ) ', + 'extra_join': ' JOIN os_versions USING ( os_version_id ) ', }, - 'process_type' : { + 'process_type': { 'first_col': 'process_type', 'first_col_format': 'category', }, - 'architecture' : { + 'architecture': { 'first_col': 'architecture', 'first_col_format': 'category', }, - 'flash_version' : { + 'flash_version': { 'first_col': 'flash_version', - 'first_col_format': '''CASE WHEN category = '' THEN 'Unknown/No Flash' ELSE category END''', - 'extra_join': ''' LEFT OUTER JOIN flash_versions USING (flash_version_id) ''', + 'first_col_format': '''CASE WHEN category = '' + THEN 'Unknown/No Flash' ELSE category END''', + 'extra_join': ''' LEFT OUTER JOIN flash_versions + USING (flash_version_id) ''', }, } + class SignatureSummary(PostgreSQLBase): def __init__(self, *args, **kwargs): super(SignatureSummary, self).__init__(*args, **kwargs) - def get(self, **kwargs): filters = [ ("report_type", None, "str"), @@ -59,7 +60,7 @@ def get(self, **kwargs): if params['versions'] and params['report_type'] is not 'products': glue = ',' version_search = ' AND reports_clean.product_version_id IN (%s)' - version_search = version_search % glue.join(params['versions']) + version_search = version_search % glue.join(params['versions']) else: version_search = '' @@ -70,78 +71,83 @@ def get(self, **kwargs): product_list = '' query_params = report_type_sql.get(params['report_type'], {}) - if params['report_type'] != 'products' and 'first_col' not in query_params: + if (params['report_type'] != 'products' and + 'first_col' not in query_params): raise Exception('Invalid report type') self.connection = self.database.connection() cursor = self.connection.cursor() if params['report_type'] == 'products': - result_cols = ['product_name', 'version_string', + result_cols = ['product_name', 'version_string', 'report_count', 'percentage'] query_string = """WITH counts AS ( - SELECT product_version_id, product_name, version_string, - count(*) AS report_count - FROM reports_clean - JOIN product_versions USING (product_version_id) - WHERE - signature_id = (SELECT signature_id FROM signatures + SELECT product_version_id, product_name, version_string, + count(*) AS report_count + FROM reports_clean + JOIN product_versions USING (product_version_id) + WHERE + signature_id = (SELECT signature_id FROM signatures WHERE signature = %s) - AND date_processed >= %s - AND date_processed < %s - GROUP BY product_version_id, product_name, version_string - ), - totals as ( - SELECT product_version_id, product_name, version_string, - report_count, - sum(report_count) OVER () as total_count - FROM counts - ) - SELECT product_name, version_string, - report_count::INT, - round((report_count * 100::numeric)/total_count,3)::TEXT as percentage - FROM totals - ORDER BY report_count DESC""" - query_parameters = (params['signature'], params['start_date'], params['end_date']) + AND date_processed >= %s + AND date_processed < %s + GROUP BY product_version_id, product_name, version_string + ), + totals as ( + SELECT product_version_id, product_name, version_string, + report_count, + sum(report_count) OVER () as total_count + FROM counts + ) + SELECT product_name, version_string, + report_count::INT, + round((report_count * 100::numeric)/total_count,3)::TEXT + as percentage + FROM totals + ORDER BY report_count DESC""" + query_parameters = (params['signature'], + params['start_date'], + params['end_date']) else: result_cols = ['category', 'report_count', 'percentage'] query_string = ["""WITH counts AS ( SELECT """] query_string.append(query_params['first_col']) query_string.append(""" as category, count(*) AS report_count - FROM reports_clean - JOIN product_versions USING (product_version_id) + FROM reports_clean + JOIN product_versions USING (product_version_id) """) query_string.append(query_params.get('extra_join', '')) query_string.append(""" - WHERE - signature_id = (SELECT signature_id FROM signatures + WHERE + signature_id = (SELECT signature_id FROM signatures WHERE signature = %s) AND date_processed >= %s AND date_processed < %s - """) + """) query_string.append(product_list) query_string.append(version_search) query_string.append(""" GROUP BY """) query_string.append(query_params['first_col']) query_string.append("""), - totals as ( - SELECT category, report_count, - sum(report_count) OVER () as total_count - FROM counts - ) - SELECT """) + totals as ( + SELECT category, report_count, + sum(report_count) OVER () as total_count + FROM counts + ) + SELECT """) query_string.append(query_params['first_col_format']) - query_string.append(""", - report_count::INT, - round((report_count::numeric)/total_count,5)::TEXT as percentage - FROM totals - ORDER BY report_count DESC""") + query_string.append(""", + report_count::INT, + round((report_count::numeric)/total_count,5)::TEXT + as percentage + FROM totals + ORDER BY report_count DESC""") query_string = " ".join(query_string) - - query_parameters = [params['signature'], - params['start_date'], - params['end_date'], - ] + + query_parameters = [params['signature'], + params['start_date'], + params['end_date'], + ] if(product_list): # This MUST be a tuple otherwise it gets cast to an array. @@ -155,4 +161,3 @@ def get(self, **kwargs): results.append(newrow) return results - diff --git a/socorro/middleware/signature_summary_service.py b/socorro/middleware/signature_summary_service.py index d22e5f89e3..f7955df6d4 100644 --- a/socorro/middleware/signature_summary_service.py +++ b/socorro/middleware/signature_summary_service.py @@ -1,9 +1,7 @@ -import logging - from socorro.middleware.service import DataAPIService -class SignatureSummary(DataAPIService): +class SignatureSummary(DataAPIService): service_name = "signature_summary" uri = "/signaturesummary/(.*)" @@ -11,9 +9,8 @@ class SignatureSummary(DataAPIService): def __init__(self, config): super(SignatureSummary, self).__init__(config) - def get(self, *args): params = self.parse_query_string(args[0]) module = self.get_module(params) impl = module.SignatureSummary(config=self.context) - return impl.get(**params) + return impl.get(**params)