<s> from __future__ import absolute_import , print_function <EOL> import logging <EOL> from datetime import datetime , timedelta <EOL> from changes . config import db <EOL> from changes . db . utils import try_create <EOL> from changes . lib . flaky_tests import get_flaky_tests <EOL> from changes . models . flakyteststat import FlakyTestStat <EOL> from changes . models . project import Project <EOL> from changes . models . test import TestCase <EOL> import urllib2 <EOL> def _log_metrics ( key , ** kws ) : <EOL> try : <EOL> urllib2 . urlopen ( <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" % key + <EOL> "<STR_LIT>" . join ( <EOL> "<STR_LIT>" % ( urllib2 . quote ( str ( k ) ) , urllib2 . quote ( str ( v ) ) ) <EOL> for ( k , v ) in kws . items ( ) <EOL> ) , <EOL> timeout = <NUM_LIT:10> <EOL> ) . read ( ) <EOL> except Exception : <EOL> logging . warning ( "<STR_LIT>" , exc_info = True ) <EOL> def aggregate_flaky_tests ( day = None , max_flaky_tests = <NUM_LIT:200> ) : <EOL> if day is None : <EOL> day = datetime . utcnow ( ) . date ( ) - timedelta ( days = <NUM_LIT:1> ) <EOL> try : <EOL> projects = Project . query . all ( ) <EOL> for project in projects : <EOL> tests = get_flaky_tests ( day , day + timedelta ( days = <NUM_LIT:1> ) , [ project ] , max_flaky_tests ) <EOL> for test in tests : <EOL> first_run = db . session . query ( <EOL> TestCase . date_created <EOL> ) . filter ( <EOL> TestCase . project_id == test [ '<STR_LIT>' ] , <EOL> TestCase . name_sha == test [ '<STR_LIT>' ] <EOL> ) . order_by ( <EOL> TestCase . date_created <EOL> ) . limit ( <NUM_LIT:1> ) . scalar ( ) <EOL> _log_metrics ( <EOL> "<STR_LIT>" , <EOL> flaky_test_reruns_name = test [ '<STR_LIT:name>' ] , <EOL> flaky_test_reruns_project_id = test [ '<STR_LIT>' ] , <EOL> flaky_test_reruns_flaky_runs = test [ '<STR_LIT>' ] , <EOL> flaky_test_reruns_passing_runs = test [ '<STR_LIT>' ] , <EOL> ) <EOL> try_create ( FlakyTestStat , { <EOL> '<STR_LIT:name>' : test [ '<STR_LIT:name>' ] , <EOL> '<STR_LIT>' : test [ '<STR_LIT>' ] , <EOL> '<STR_LIT:date>' : day , <EOL> '<STR_LIT>' : test [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : test [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : test [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : test [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : first_run <EOL> } ) <EOL> db . session . commit ( ) <EOL> db . session . commit ( ) <EOL> except Exception as err : <EOL> logging . exception ( unicode ( err ) ) </s>
