blob: c5c7f61771d3f1d63e44ed883b2dc11f64f74f8f [file] [log] [blame]
David Garcia Quintas54687062017-08-03 19:45:45 -07001#!/usr/bin/env python
2# Copyright 2015 gRPC authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""Detect new flakes introduced in the last 24h hours with respect to the
17previous six days"""
18
David Garcia Quintas2f394862017-08-02 21:36:35 -070019from __future__ import absolute_import
20from __future__ import division
21from __future__ import print_function
22
David Garcia Quintasf277b172017-08-09 22:31:21 -070023import datetime
David Garcia Quintas2f394862017-08-02 21:36:35 -070024import os
25import sys
26import logging
27logging.basicConfig(format='%(asctime)s %(message)s')
28
29gcp_utils_dir = os.path.abspath(
30 os.path.join(os.path.dirname(__file__), '../gcp/utils'))
31sys.path.append(gcp_utils_dir)
32
33import big_query_utils
34
David Garcia Quintasf277b172017-08-09 22:31:21 -070035def print_table(table):
David Garcia Quintas38a83b32017-09-08 09:10:29 -070036 kokoro_base_url = 'https://kokoro.corp.google.com/job/'
37 for k, v in table.items():
David Garcia Quintasf277b172017-08-09 22:31:21 -070038 job_name = v[0]
39 build_id = v[1]
40 ts = int(float(v[2]))
41 # TODO(dgq): timezone handling is wrong. We need to determine the timezone
42 # of the computer running this script.
43 human_ts = datetime.datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S PDT')
David Garcia Quintas38a83b32017-09-08 09:10:29 -070044 job_path = '{}/{}'.format('/job/'.join(job_name.split('/')), build_id)
45 full_kokoro_url = kokoro_base_url + job_path
46 print("Test: {}, Timestamp: {}, url: {}\n".format(k, human_ts, full_kokoro_url))
David Garcia Quintasf277b172017-08-09 22:31:21 -070047
David Garcia Quintas2f394862017-08-02 21:36:35 -070048
David Garcia Quintas54687062017-08-03 19:45:45 -070049def get_flaky_tests(days_lower_bound, days_upper_bound, limit=None):
David Garcia Quintas2f394862017-08-02 21:36:35 -070050 """ period is one of "WEEK", "DAY", etc.
51 (see https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#date_add). """
52
53 bq = big_query_utils.create_big_query()
54 query = """
55SELECT
David Garcia Quintasf277b172017-08-09 22:31:21 -070056 REGEXP_REPLACE(test_name, r'/\d+', '') AS filtered_test_name,
57 job_name,
58 build_id,
59 timestamp
60FROM
61 [grpc-testing:jenkins_test_results.aggregate_results]
62WHERE
David Garcia Quintas4721fc02017-08-11 14:01:32 -070063 timestamp > DATE_ADD(CURRENT_DATE(), {days_lower_bound}, "DAY")
David Garcia Quintas54687062017-08-03 19:45:45 -070064 AND timestamp <= DATE_ADD(CURRENT_DATE(), {days_upper_bound}, "DAY")
David Garcia Quintasf277b172017-08-09 22:31:21 -070065 AND NOT REGEXP_MATCH(job_name, '.*portability.*')
66 AND result != 'PASSED' AND result != 'SKIPPED'
67ORDER BY timestamp desc
David Garcia Quintas54687062017-08-03 19:45:45 -070068""".format(days_lower_bound=days_lower_bound, days_upper_bound=days_upper_bound)
David Garcia Quintas2f394862017-08-02 21:36:35 -070069 if limit:
70 query += '\n LIMIT {}'.format(limit)
71 query_job = big_query_utils.sync_query_job(bq, 'grpc-testing', query)
72 page = bq.jobs().getQueryResults(
73 pageToken=None, **query_job['jobReference']).execute(num_retries=3)
David Garcia Quintasbfd9d802017-08-16 12:10:48 -070074 rows = page.get('rows')
75 if rows:
76 return {row['f'][0]['v']:
77 (row['f'][1]['v'], row['f'][2]['v'], row['f'][3]['v'])
78 for row in rows}
79 else:
80 return {}
David Garcia Quintas2f394862017-08-02 21:36:35 -070081
82
83def get_new_flakes():
David Garcia Quintasf277b172017-08-09 22:31:21 -070084 last_week_sans_yesterday = get_flaky_tests(-14, -1)
David Garcia Quintas4721fc02017-08-11 14:01:32 -070085 last_24 = get_flaky_tests(0, +1)
David Garcia Quintas54687062017-08-03 19:45:45 -070086 last_week_sans_yesterday_names = set(last_week_sans_yesterday.keys())
David Garcia Quintas2f394862017-08-02 21:36:35 -070087 last_24_names = set(last_24.keys())
David Garcia Quintas54687062017-08-03 19:45:45 -070088 logging.debug('|last_week_sans_yesterday| =', len(last_week_sans_yesterday_names))
David Garcia Quintas2f394862017-08-02 21:36:35 -070089 logging.debug('|last_24_names| =', len(last_24_names))
David Garcia Quintas54687062017-08-03 19:45:45 -070090 new_flakes = last_24_names - last_week_sans_yesterday_names
David Garcia Quintas2f394862017-08-02 21:36:35 -070091 logging.debug('|new_flakes| = ', len(new_flakes))
92 return {k: last_24[k] for k in new_flakes}
93
94
95def main():
David Garcia Quintas2f394862017-08-02 21:36:35 -070096 new_flakes = get_new_flakes()
97 if new_flakes:
David Garcia Quintas54687062017-08-03 19:45:45 -070098 print("Found {} new flakes:".format(len(new_flakes)))
David Garcia Quintasf277b172017-08-09 22:31:21 -070099 print_table(new_flakes)
David Garcia Quintasbfd9d802017-08-16 12:10:48 -0700100 else:
101 print("No new flakes found!")
David Garcia Quintas2f394862017-08-02 21:36:35 -0700102
103
104if __name__ == '__main__':
105 main()