~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/code/model/branchcloud.py

  • Committer: Tim Penhey
  • Date: 2010-08-27 02:11:36 UTC
  • mto: This revision was merged to the branch mainline in revision 11475.
  • Revision ID: tim.penhey@canonical.com-20100827021136-x3glyl6vaqxqhy8d
Extract the branch cloud into its own module.

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
"""The implementation of the branch cloud."""
 
5
 
 
6
__metaclass__ = type
 
7
__all__ = [
 
8
    'BranchCloud',
 
9
    ]
 
10
 
 
11
 
 
12
from datetime import datetime, timedelta
 
13
 
 
14
import pytz
 
15
from storm.expr import (
 
16
    Alias,
 
17
    Func,
 
18
    )
 
19
from storm.locals import (
 
20
    Count,
 
21
    Desc,
 
22
    Max,
 
23
    Not,
 
24
    )
 
25
from zope.interface import classProvides
 
26
 
 
27
 
 
28
from canonical.launchpad.interfaces.lpstorm import ISlaveStore
 
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
 
33
 
 
34
 
 
35
class BranchCloud:
 
36
    """See `IBranchCloud`."""
 
37
 
 
38
    classProvides(IBranchCloud)
 
39
 
 
40
    @staticmethod
 
41
    def getProductsWithInfo(num_products=None, store=None):
 
42
        """See `IBranchCloud`."""
 
43
        # It doesn't matter if this query is even a whole day out of date, so
 
44
        # use the slave store by default.
 
45
        if store is None:
 
46
            store = ISlaveStore(RevisionCache)
 
47
        distinct_revision_author = Func(
 
48
            "distinct", RevisionCache.revision_author_id)
 
49
        commits = Alias(Count(RevisionCache.revision_id))
 
50
        epoch = datetime.now(pytz.UTC) - timedelta(days=30)
 
51
        result = store.find(
 
52
            (Product.name,
 
53
             commits,
 
54
             Count(distinct_revision_author),
 
55
             Max(RevisionCache.revision_date)),
 
56
            RevisionCache.product == Product.id,
 
57
            Not(RevisionCache.private),
 
58
            RevisionCache.revision_date >= epoch)
 
59
        result = result.group_by(Product.name)
 
60
        result = result.order_by(Desc(commits))
 
61
        if num_products:
 
62
            result.config(limit=num_products)
 
63
        return result