~launchpad-pqm/launchpad/devel

11435.5.1 by Tim Penhey
Extract the branch cloud into its own module.
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
"""The implementation of the branch cloud."""
5
6
__metaclass__ = type
7
__all__ = [
8
    'BranchCloud',
9
    ]
10
11
14550.1.1 by Steve Kowalik
Run format-imports over lib/lp and lib/canonical/launchpad
12
from datetime import (
13
    datetime,
14
    timedelta,
15
    )
11435.5.1 by Tim Penhey
Extract the branch cloud into its own module.
16
17
import pytz
14550.1.1 by Steve Kowalik
Run format-imports over lib/lp and lib/canonical/launchpad
18
from storm.expr import (
19
    Alias,
20
    Func,
21
    )
22
from storm.locals import (
23
    Count,
24
    Desc,
25
    Max,
26
    Not,
27
    )
11435.5.1 by Tim Penhey
Extract the branch cloud into its own module.
28
from zope.interface import classProvides
29
30
from lp.code.interfaces.branch import IBranchCloud
31
from lp.code.model.revision import RevisionCache
32
from lp.registry.model.product import Product
14612.2.1 by William Grant
format-imports on lib/. So many imports.
33
from lp.services.database.lpstorm import ISlaveStore
11435.5.1 by Tim Penhey
Extract the branch cloud into its own module.
34
35
36
class BranchCloud:
37
    """See `IBranchCloud`."""
38
39
    classProvides(IBranchCloud)
40
41
    @staticmethod
11435.5.4 by Tim Penhey
Always use the slave store, even in the tests.
42
    def getProductsWithInfo(num_products=None):
11435.5.1 by Tim Penhey
Extract the branch cloud into its own module.
43
        """See `IBranchCloud`."""
44
        distinct_revision_author = Func(
45
            "distinct", RevisionCache.revision_author_id)
46
        commits = Alias(Count(RevisionCache.revision_id))
47
        epoch = datetime.now(pytz.UTC) - timedelta(days=30)
11435.5.4 by Tim Penhey
Always use the slave store, even in the tests.
48
        # It doesn't matter if this query is even a whole day out of date, so
11435.5.10 by Tim Penhey
Tweak comment to not be a lie.
49
        # use the slave store.
11435.5.4 by Tim Penhey
Always use the slave store, even in the tests.
50
        result = ISlaveStore(RevisionCache).find(
11435.5.1 by Tim Penhey
Extract the branch cloud into its own module.
51
            (Product.name,
52
             commits,
53
             Count(distinct_revision_author),
54
             Max(RevisionCache.revision_date)),
55
            RevisionCache.product == Product.id,
56
            Not(RevisionCache.private),
57
            RevisionCache.revision_date >= epoch)
58
        result = result.group_by(Product.name)
59
        result = result.order_by(Desc(commits))
60
        if num_products:
61
            result.config(limit=num_products)
62
        return result