VC: Fix error on clone page for legacy-ID events
[cds-indico.git] / indico / MaKaC / statistics.py
blobcb6a1e29d22c3c74d3c518a84c82e7e9c8d5f73b
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):
23 """
24 """
25 pass
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).
33 self._target = 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()
40 return categStats
41 return None
43 @classmethod
44 def updateStatistics(cls, cat, logger=None):
45 dbi = DBMgr.getInstance()
47 cls._updateStatistics(cat, dbi, 0, logger)
48 if logger:
49 logger.info("Statistics calculation finished")
51 dbi.commit()
53 @classmethod
54 def _processEvent(cls, dbi, event, statistics):
55 nevents = 0
56 year = event.getStartDate().year
57 if year in statistics["events"]:
58 statistics["events"][year] += 1
59 else:
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
67 else:
68 statistics["contributions"][year] = 1
69 if len(cont.getSubContributionList()) > 0:
70 for scont in cont.getSubContributionList():
71 l = scont.getAllMaterialList()
72 for m in l:
73 statistics["resources"] += m.getNbResources()
74 l = cont.getAllMaterialList()
75 for m in l:
76 statistics["resources"] += m.getNbResources()
77 if len(event.getSessionList()) > 0:
78 for sess in event.getSessionList():
79 l = sess.getAllMaterialList()
80 for m in l:
81 statistics["resources"] += m.getNbResources()
82 l = event.getAllMaterialList()
83 for m in l:
84 statistics["resources"] += m.getNbResources()
86 # commit every 1000 events
87 if nevents % 1000 == 999:
88 dbi.commit()
90 @classmethod
91 def _updateStatistics(cls, cat, dbi, level=0, logger=None):
93 stats = cat.getStatistics()
94 stats["events"] = {}
95 stats["contributions"] = {}
96 stats["resources"] = 0
98 if len(cat.getSubCategoryList()) > 0:
99 for scat in cat.getSubCategoryList():
100 # only at top level
101 if level == 0 and logger:
102 logger.info("Processing '%s' (%s)" % (scat.getTitle(),
103 scat.getId()))
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]
110 else:
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]
115 else:
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
125 cat._p_changed = 1
127 dbi.commit()
129 if level == 1:
130 logger.info("%s : %s" % (cat.getId(), cat._statistics))
132 return stats