1 # This file is part of Indico.
2 # Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
4 # Indico is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3 of the
7 # License, or (at your option) any later version.
9 # Indico is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Indico; if not, see <http://www.gnu.org/licenses/>.
17 from indico
.core
.db
import DBMgr
19 from indico
.util
.date_time
import nowutc
22 class Statistics(object):
28 class CategoryStatistics(Statistics
):
30 def __init__(self
, target
):
31 # The category for which we want the statistics is attached to the
32 # instance at initialization time (target).
35 def getStatistics(self
):
36 # The returned statistics are composed of a dictionary containing other
37 # dictionaries (one per statistic).
38 if self
._target
.getNumConferences() != 0:
39 categStats
= self
._target
.getStatistics()
44 def updateStatistics(cls
, cat
, logger
=None):
45 dbi
= DBMgr
.getInstance()
47 cls
._updateStatistics
(cat
, dbi
, 0, logger
)
49 logger
.info("Statistics calculation finished")
54 def _processEvent(cls
, dbi
, event
, statistics
):
56 year
= event
.getStartDate().year
57 if year
in statistics
["events"]:
58 statistics
["events"][year
] += 1
60 statistics
["events"][year
] = 1
61 if len(event
.getContributionList()) > 0:
62 for cont
in event
.getContributionList():
63 if cont
.getStartDate() != None:
64 year
= cont
.getStartDate().year
65 if year
in statistics
["contributions"]:
66 statistics
["contributions"][year
] += 1
68 statistics
["contributions"][year
] = 1
69 if len(cont
.getSubContributionList()) > 0:
70 for scont
in cont
.getSubContributionList():
71 l
= scont
.getAllMaterialList()
73 statistics
["resources"] += m
.getNbResources()
74 l
= cont
.getAllMaterialList()
76 statistics
["resources"] += m
.getNbResources()
77 if len(event
.getSessionList()) > 0:
78 for sess
in event
.getSessionList():
79 l
= sess
.getAllMaterialList()
81 statistics
["resources"] += m
.getNbResources()
82 l
= event
.getAllMaterialList()
84 statistics
["resources"] += m
.getNbResources()
86 # commit every 1000 events
87 if nevents
% 1000 == 999:
91 def _updateStatistics(cls
, cat
, dbi
, level
=0, logger
=None):
93 stats
= cat
.getStatistics()
95 stats
["contributions"] = {}
96 stats
["resources"] = 0
98 if len(cat
.getSubCategoryList()) > 0:
99 for scat
in cat
.getSubCategoryList():
101 if level
== 0 and logger
:
102 logger
.info("Processing '%s' (%s)" % (scat
.getTitle(),
105 cls
._updateStatistics
(scat
, dbi
, level
+ 1, logger
)
107 for year
in scat
._statistics
["events"]:
108 if year
in stats
["events"]:
109 stats
["events"][year
] += scat
._statistics
["events"][year
]
111 stats
["events"][year
] = scat
._statistics
["events"][year
]
112 for year
in scat
._statistics
["contributions"]:
113 if year
in stats
["contributions"]:
114 stats
["contributions"][year
] += scat
._statistics
["contributions"][year
]
116 stats
["contributions"][year
] = scat
._statistics
["contributions"][year
]
117 stats
["resources"] += scat
._statistics
["resources"]
119 elif cat
.conferences
:
120 for event
in cat
.conferences
:
121 cls
._processEvent
(dbi
, event
, stats
)
123 stats
["updated"] = nowutc()
124 cat
._statistics
= stats
130 logger
.info("%s : %s" % (cat
.getId(), cat
._statistics
))