Switch to unified view

a/Allura/allura/model/stats.py b/Allura/allura/model/stats.py
1
import logging
1
import logging
2
from collections import defaultdict
3
from datetime import datetime, time
4
2
5
import ming
3
import ming
6
4
7
from .session import main_doc_session
5
from .session import main_doc_session
8
6
...
...
11
class Stats(ming.Document):
9
class Stats(ming.Document):
12
    class __mongometa__:
10
    class __mongometa__:
13
        session = main_doc_session
11
        session = main_doc_session
14
        name='stats'
12
        name='stats'
15
13
16
class CPA(ming.Document):
17
    class __mongometa__:
18
        session = main_doc_session
19
        name='content_production_activities'
20
        indexes = [
21
            'type', 'class_name', 'project_id', 'project_shortname', 'app_config_id', 'when', 'tool_name',
22
            ('tool_name', 'class_name')]
23
24
    @classmethod
25
    def post(cls, type, obj):
26
        d = dict(
27
                type=type,
28
                class_name='%s.%s' % (
29
                    obj.__class__.__module__,
30
                    obj.__class__.__name__),
31
                project_id=None,
32
                project_shortname='',
33
                app_config_id=obj.app_config_id,
34
                tool_name='',
35
                mount_point='',
36
                when=datetime.utcnow())
37
        if obj.app_config:
38
            d.update(
39
                project_id=obj.app_config.project_id,
40
                project_shortname=obj.app_config.project.shortname,
41
                tool_name=obj.app_config.tool_name,
42
                mount_point=obj.app_config.options.mount_point)
43
        doc = cls.make(d)
44
        doc.m.insert()
45
46
    @classmethod
47
    def stats(cls, since=None):
48
        result = defaultdict(lambda:dict(create=0, modify=0, delete=0))
49
        if since:
50
            q = dict(when={'$gt':datetime.combine(since, time.min)})
51
        else:
52
            q = {}
53
        for doc in cls.m.find(q).sort([('tool_name', 1), ('class_name', 1)]):
54
            result[doc.tool_name, doc.class_name][doc.type] += 1
55
        result = sorted(result.iteritems())
56
        return [ dict(v, tool_name=k[0], class_name=k[1])
57
                 for k,v in result]