blob: 383198ae5fbafe427f07214667ef32e1bfd99b92 [file] [log] [blame]
Keyar Hood30d94bd2013-07-23 10:41:12 -07001#!/usr/bin/python
2#
3# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7
Keyar Hood01e1d9e2013-08-13 15:29:30 -07008import datetime, sys
Keyar Hood30d94bd2013-07-23 10:41:12 -07009
10import common
11from autotest_lib.client.common_lib import mail
12from autotest_lib.frontend import setup_django_readonly_environment
13
14# Django and the models are only setup after
15# the setup_django_readonly_environment module is imported.
16from autotest_lib.frontend.afe import models as afe_models
17from autotest_lib.frontend.health import utils
18
19
20# Keep tests that have not failed for at least this many days.
21_MIN_DAYS_SINCE_FAILURE = 30
22# Ignore any tests that have not passed in this many days.
23_MAX_DAYS_SINCE_LAST_PASS = 30
24
25_MAIL_RESULTS_FROM = 'chromeos-test-health@google.com'
26_MAIL_RESULTS_TO = 'chromeos-lab-infrastructure@google.com'
27
28
Keyar Hood30d94bd2013-07-23 10:41:12 -070029def get_experimental_tests():
30 """
31 Get all the tests marked experimental from the afe_autotests table.
32
33 @return the set of experimental test names.
34
35 """
36 entries = afe_models.Test.objects.values('name').filter(experimental=True)
37 return {entry['name'] for entry in entries}
38
39
40def find_long_passing_tests(pass_times, fail_times, valid_names):
41 """
42 Determine the experimental tests that have been passsing for a long time.
43
44 @param pass_times: The dictionary of test_name:pass_time pairs.
45 @param fail_times: The dictionary of test_name:fail_time pairs.
46 @param valid_names: An iterable of experimental test names.
47
48 @return the list of experimental test names that have been passing for a
49 long time.
50
51 """
52 failure_cutoff_date = (datetime.datetime.today() -
53 datetime.timedelta(_MIN_DAYS_SINCE_FAILURE))
54 pass_cutoff_date = (datetime.datetime.today() -
55 datetime.timedelta(_MAX_DAYS_SINCE_LAST_PASS))
56
57 valid_passes = {test for test in valid_names if test in pass_times}
58 valid_failures = {test for test in valid_names if test in fail_times}
59
60 recent_passes = {test for test in valid_passes
61 if (pass_times[test] > pass_cutoff_date)}
62 recent_fails = {test for test in valid_failures
63 if (fail_times[test] > failure_cutoff_date)}
64
65 return recent_passes - recent_fails
66
67
68def main():
69 """
70 The script code.
71
72 Allows other python code to import and run this code. This will be more
73 important if a nice way to test this code can be determined.
74
75 """
Keyar Hood30d94bd2013-07-23 10:41:12 -070076 experimental_tests = get_experimental_tests()
77 pass_times = utils.get_last_pass_times()
78 fail_times = utils.get_last_fail_times()
79
80 long_passers = find_long_passing_tests(pass_times, fail_times,
81 experimental_tests)
82
83 if long_passers:
84 mail.send(_MAIL_RESULTS_FROM,
85 [_MAIL_RESULTS_TO],
86 [],
87 'Long Passing Experimental Tests',
88 'The following experimental tests have been passing for at '
89 'least %i days:\n\n%s'
90 % (_MIN_DAYS_SINCE_FAILURE, '\n'.join(sorted(long_passers))))
91
92 return 0
93
94
95if __name__ == '__main__':
96 sys.exit(main())