~cr3/launchpad-results/trunk

« back to all changes in this revision

Viewing changes to lib/lp/results/models/testperson.py

  • Committer: Marc Tardif
  • Date: 2010-12-07 01:16:17 UTC
  • Revision ID: marc.tardif@canonical.com-20101207011617-cqcv0240ca3cs5ba
Added TestStat for statistics about test runs and TestRank for ranking of projects.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
__metaclass__ = type
 
5
 
 
6
__all__ = [
 
7
    "TestPerson",
 
8
    "test_person_set",
 
9
    ]
 
10
 
 
11
from storm.expr import (
 
12
    Alias,
 
13
    LeftJoin,
 
14
    Sum,
 
15
    )
 
16
from storm.locals import Max
 
17
 
 
18
from zope.interface import implements
 
19
 
 
20
from lazr.delegates import delegates
 
21
 
 
22
from lp.results.database.search import search_to_expression
 
23
 
 
24
from lp.results.services.formatters import item_to_tuple
 
25
 
 
26
from lp.results.interfaces import (
 
27
    IPerson,
 
28
    ITestPerson,
 
29
    ITestPersonSet,
 
30
    TestPersonSearchParams,
 
31
    )
 
32
 
 
33
from lp.results.models.person import (
 
34
    Person,
 
35
    PersonSet,
 
36
    )
 
37
from lp.results.models.testrank import HasTestRankProjectsMixin
 
38
from lp.results.models.teststat import (
 
39
    HasTestStatPeopleMixin,
 
40
    TestStat,
 
41
    TestStatPersonMixin,
 
42
    )
 
43
 
 
44
 
 
45
class TestPerson(TestStatPersonMixin, HasTestRankProjectsMixin):
 
46
    """Users within the Django authentication system are represented by
 
47
    this model.
 
48
    """
 
49
    __slots__ = ("person", "test_run_count", "last_test_run_date",)
 
50
 
 
51
    implements(ITestPerson)
 
52
 
 
53
    delegates(IPerson, context="person")
 
54
 
 
55
    def __init__(self, person, test_run_count, last_test_run_date):
 
56
        self.person = person
 
57
        self.test_run_count = test_run_count
 
58
        self.last_test_run_date = last_test_run_date
 
59
 
 
60
    def __cmp__(self, other):
 
61
        if ITestPerson.providedBy(other):
 
62
            other = other.person
 
63
 
 
64
        return cmp(self.person, other)
 
65
 
 
66
 
 
67
class TestPersonSet(PersonSet, HasTestStatPeopleMixin):
 
68
    """See `ITestPersonSet`."""
 
69
    implements(ITestPersonSet)
 
70
 
 
71
    search_params_factory = TestPersonSearchParams
 
72
 
 
73
    def buildSearchExpressions(self, search_params):
 
74
        """See SearchSet."""
 
75
        expressions = super(TestPersonSet, self).buildSearchExpressions(
 
76
            search_params)
 
77
 
 
78
        if search_params.date_modified:
 
79
            expressions.append(
 
80
                search_to_expression(
 
81
                    TestStat.date_modified,
 
82
                    search_params.date_modified))
 
83
 
 
84
        if search_params.project:
 
85
            expressions.append(
 
86
                search_to_expression(
 
87
                    TestStat.project_id,
 
88
                    search_params.project,
 
89
                    modifier=lambda x: x.id))
 
90
 
 
91
        return expressions
 
92
 
 
93
    def buildSearchResult(self, search_params):
 
94
        """See SearchSet."""
 
95
        result = super(TestPersonSet, self).buildSearchResult(search_params)
 
96
 
 
97
        return result.group_by(Person)
 
98
 
 
99
    def buildSearchTables(self, search_params):
 
100
        """See SearchSet."""
 
101
        tables = super(TestPersonSet, self).buildSearchTables(search_params)
 
102
 
 
103
        tables.append(
 
104
            LeftJoin(TestStat,
 
105
                TestStat.person_id == Person.id))
 
106
 
 
107
        return tables
 
108
 
 
109
    def getSearchResultDecorator(self, result):
 
110
        """See SearchSet."""
 
111
        return TestPerson(*result)
 
112
 
 
113
    def getSearchResultTables(self):
 
114
        """See SearchSet."""
 
115
        result_tables = super(TestPersonSet, self).getSearchResultTables()
 
116
 
 
117
        return item_to_tuple(result_tables) + (
 
118
            Alias(Sum(TestStat.counter), name="test_run_count"),
 
119
            Alias(Max(TestStat.date_modified), name="last_test_run_date"),
 
120
            )
 
121
 
 
122
 
 
123
test_person_set = TestPersonSet()