Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1277 from selenamarie/bug826564-exploitability-sql
bug 826564 Add exploitability to reports_clean
- Loading branch information
Showing
6 changed files
with
199 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
"""bug 826564 - add exploitability to reports_clean | ||
Revision ID: 9798b1cc04 | ||
Revises: 3805e9f77df4 | ||
Create Date: 2013-06-05 13:20:25.116074 | ||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '9798b1cc04' | ||
down_revision = '3805e9f77df4' | ||
|
||
import os | ||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.dialects import postgresql | ||
from sqlalchemy import types | ||
from sqlalchemy.sql import table, column | ||
from sqlalchemy.dialects import postgresql | ||
|
||
class CITEXT(types.UserDefinedType): | ||
name = 'citext' | ||
|
||
def get_col_spec(self): | ||
return 'CITEXT' | ||
|
||
def bind_processor(self, dialect): | ||
def process(value): | ||
return value | ||
return process | ||
|
||
def result_processor(self, dialect, coltype): | ||
def process(value): | ||
return value | ||
return process | ||
|
||
def __repr__(self): | ||
return "citext" | ||
|
||
class JSON(types.UserDefinedType): | ||
name = 'json' | ||
|
||
def get_col_spec(self): | ||
return 'JSON' | ||
|
||
def bind_processor(self, dialect): | ||
def process(value): | ||
return value | ||
return process | ||
|
||
def result_processor(self, dialect, coltype): | ||
def process(value): | ||
return value | ||
return process | ||
|
||
def __repr__(self): | ||
return "json" | ||
|
||
def upgrade(): | ||
op.create_table(u'exploitability_reports', | ||
sa.Column(u'signature_id', sa.INTEGER(), nullable=False), | ||
sa.Column(u'report_date', sa.DATE(), nullable=False), | ||
sa.Column(u'null_count', sa.INTEGER(), server_default='0', nullable=False), | ||
sa.Column(u'none_count', sa.INTEGER(), server_default='0', nullable=False), | ||
sa.Column(u'low_count', sa.INTEGER(), server_default='0', nullable=False), | ||
sa.Column(u'medium_count', sa.INTEGER(), server_default='0', nullable=False), | ||
sa.Column(u'high_count', sa.INTEGER(), server_default='0', nullable=False), | ||
sa.ForeignKeyConstraint(['signature_id'], [u'signatures.signature_id'], ), | ||
sa.PrimaryKeyConstraint() | ||
) | ||
# We can probably get away with just applying this to the parent table | ||
# If there are performance problems on stage, break this out and apply to all | ||
# child partitions first, then reports_clean last. | ||
op.add_column(u'reports_clean', sa.Column(u'exploitability', sa.TEXT(), nullable=True)) | ||
app_path=os.getcwd() | ||
procs = [ | ||
'001_update_reports_clean.sql' | ||
, 'update_exploitability.sql' | ||
, 'backfill_exploitability.sql' | ||
] | ||
for myfile in [app_path + '/socorro/external/postgresql/raw_sql/procs/' + line for line in procs]: | ||
proc = open(myfile, 'r').read() | ||
op.execute(proc) | ||
|
||
def downgrade(): | ||
op.drop_column(u'reports_clean', u'exploitability') | ||
op.drop_table(u'exploitability_reports') | ||
# Not rolling back 001_update_reports_clean.sql... | ||
# if rolling back, need to pull out the old version and apply manually |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
socorro/external/postgresql/raw_sql/procs/backfill_exploitability.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
CREATE OR REPLACE FUNCTION backfill_exploitability(updateday date) RETURNS boolean | ||
LANGUAGE plpgsql | ||
AS $$ | ||
BEGIN | ||
-- stored procudure to delete and replace one day of | ||
-- product_adu, optionally only for a specific product | ||
-- intended to be called by backfill_matviews | ||
|
||
DELETE FROM exploitability_reports | ||
WHERE report_date = updateday; | ||
|
||
PERFORM update_exploitability(updateday, false); | ||
|
||
RETURN TRUE; | ||
END; $$; |
60 changes: 60 additions & 0 deletions
60
socorro/external/postgresql/raw_sql/procs/update_exploitability.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
CREATE OR REPLACE FUNCTION update_exploitability(updateday date, checkdata boolean DEFAULT true, check_period interval DEFAULT '01:00:00'::interval) RETURNS boolean | ||
LANGUAGE plpgsql | ||
SET client_min_messages TO 'ERROR' | ||
AS $$ | ||
BEGIN | ||
-- Populate a daily matview which reports on the exploitability of Top Crashers each day | ||
-- depends on tcbs and signatures | ||
|
||
-- check if we've been run | ||
IF checkdata THEN | ||
PERFORM 1 FROM exploitability_reports WHERE report_date = updateday LIMIT 1; | ||
IF FOUND THEN | ||
RAISE INFO 'exploitability_reports has already been run for %.',updateday; | ||
END IF; | ||
END IF; | ||
|
||
-- check if reports_clean is complete | ||
IF NOT reports_clean_done(updateday, check_period) THEN | ||
IF checkdata THEN | ||
RAISE NOTICE 'Reports_clean has not been updated to the end of %',updateday; | ||
RETURN FALSE; | ||
ELSE | ||
RETURN FALSE; | ||
END IF; | ||
END IF; | ||
|
||
-- check if tcbs is updated | ||
PERFORM 1 | ||
FROM tcbs | ||
WHERE tcbs.report_date = updateday | ||
LIMIT 1; | ||
|
||
IF NOT FOUND THEN | ||
IF checkdata THEN | ||
RAISE NOTICE 'TCBS has not been updated to the end of %',updateday; | ||
RETURN FALSE; | ||
ELSE | ||
RAISE NOTICE 'TCBS has not been updated, skipping.'; | ||
RETURN TRUE; | ||
END IF; | ||
END IF; | ||
|
||
|
||
INSERT INTO exploitability_reports ( | ||
signature_id, report_date, null_count, none_count, low_count, medium_count, high_count) | ||
select s.signature_id, | ||
r.date_processed::date, | ||
count(CASE WHEN r.exploitability is NULL THEN 1 ELSE 0 END) as count_null, | ||
count(CASE WHEN r.exploitability = 'none' THEN 1 ELSE 0 END) as count_none, | ||
count(CASE WHEN r.exploitability = 'low' THEN 1 ELSE 0 END) as count_low, | ||
count(CASE WHEN r.exploitability = 'medium' THEN 1 ELSE 0 END) as count_medium, | ||
count(CASE WHEN r.exploitability = 'high' THEN 1 ELSE 0 END) as count_high | ||
FROM | ||
signatures s JOIN reports_clean r ON r.signature_id = s.signature_id | ||
WHERE s.signature_id IN (select signature_id from tcbs where utc_day_is(tcbs.report_date, updateday)) | ||
AND utc_day_is(r.date_processed, updateday) | ||
GROUP BY s.signature_id, r.date_processed::date; | ||
|
||
RETURN TRUE; | ||
END; $$; |