1# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import common
6from autotest_lib.frontend import setup_django_readonly_environment
7
8# Django and the models are only setup after
9# the setup_django_readonly_environment module is imported.
10from autotest_lib.frontend.tko import models as tko_models
11from django.db import models as django_models
12
13_TEST_ERROR_STATUS = 'ERROR'
14_TEST_ABORT_STATUS = 'ABORT'
15_TEST_FAIL_STATUS = 'FAIL'
16_TEST_WARN_STATUS = 'WARN'
17_TEST_PASS_STATUS = 'GOOD'
18_TEST_ALERT_STATUS = 'ALERT'
19
20
21def get_last_pass_times():
22    """
23    Get all the tests that have passed and the time they last passed.
24
25    @return the dict of test_name:last_finish_time pairs for tests that have
26            passed.
27
28    """
29    results = tko_models.Test.objects.values('test').filter(
30        status__word=_TEST_PASS_STATUS).annotate(
31        last_pass=django_models.Max('started_time'))
32    return {result['test']: result['last_pass'] for result in results}
33
34
35def get_last_fail_times():
36    """
37    Get all the tests that have failed and the time they last failed.
38
39    @return the dict of test_name:last_finish_time pairs for tests that have
40            failed.
41
42    """
43
44    failure_clauses = (django_models.Q(status__word=_TEST_FAIL_STATUS) |
45                       django_models.Q(status__word=_TEST_ERROR_STATUS) |
46                       django_models.Q(status__word=_TEST_ABORT_STATUS) |
47                       django_models.Q(status__word=_TEST_WARN_STATUS) |
48                       django_models.Q(status__word=_TEST_ALERT_STATUS))
49
50    results = tko_models.Test.objects.values('test').filter(
51        failure_clauses).annotate(
52        last_pass=django_models.Max('started_time'))
53
54    return {result['test']: result['last_pass'] for result in results}
55