Parent: [c99abb] (diff)

Child: [b21778] (diff)

Download this file

userstats.py    326 lines (282 with data), 11.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from tg import expose, validate, redirect
from tg.decorators import with_trailing_slash
from datetime import datetime
from allura.controllers import BaseController
import allura.model as M
from allura.lib.graphics.graphic_methods import create_histogram, create_progress_bar
from forgeuserstats.model.stats import UserStats
from pylons import tmpl_context as c
from allura.lib.security import require_access
from forgeuserstats.widgets.forms import StatsPreferencesForm
from allura.lib.decorators import require_post
from allura.lib import validators as V
stats_preferences_form = StatsPreferencesForm()
class ForgeUserStatsCatController(BaseController):
@expose()
def _lookup(self, category, *remainder):
cat = M.TroveCategory.query.get(shortname=category)
return ForgeUserStatsCatController(category = cat), remainder
def __init__(self, category = None):
self.category = category
super(ForgeUserStatsCatController, self).__init__()
@expose('jinja:forgeuserstats:templates/index.html')
@with_trailing_slash
def index(self, **kw):
self.user = c.project.user_project_of
if not self.user:
return None
stats = self.user.stats
if (not stats.visible) and (c.user != self.user):
return dict(user=self.user)
cat_id = None
if self.category:
cat_id = self.category._id
ret_dict = _getDataForCategory(cat_id, stats)
ret_dict['user'] = self.user
ret_dict['registration_date'] = stats.registration_date
ret_dict['category'] = self.category
return ret_dict
class ForgeUserStatsController(BaseController):
category = ForgeUserStatsCatController()
@expose('jinja:forgeuserstats:templates/settings.html')
@with_trailing_slash
def settings(self, **kw):
require_access(c.project, 'admin')
self.user = c.project.user_project_of
if not self.user:
return dict(user=None)
if not self.user.stats:
UserStats.create(self.user)
return dict(
user = self.user,
form = StatsPreferencesForm(
action = c.project.url() + 'userstats/change_settings'))
@expose()
@require_post()
@validate(stats_preferences_form, error_handler=settings)
def change_settings(self, **kw):
require_access(c.project, 'admin')
self.user = c.project.user_project_of
if not self.user:
return dict(user=None)
if not self.user.stats:
UserStats.create(self.user)
visible = kw.get('visible')
self.user.stats.visible = visible
redirect(c.project.url() + 'userstats/settings')
@expose('jinja:forgeuserstats:templates/index.html')
@with_trailing_slash
def index(self, **kw):
self.user = c.project.user_project_of
if not self.user:
return dict(user=None)
if not self.user.stats:
UserStats.create(self.user)
stats = self.user.stats
if (not stats.visible) and (c.user != self.user):
return dict(user=self.user)
ret_dict = _getDataForCategory(None, stats)
ret_dict['user'] = self.user
ret_dict['registration_date'] = stats.registration_date
ret_dict['totlogins'] = stats.tot_logins_count
ret_dict['last_login'] = stats.last_login
if stats.last_login:
ret_dict['last_login_days'] = \
(datetime.utcnow()-stats.last_login).days
categories = {}
for p in self.user.my_projects():
for cat in p.trove_topic:
cat = M.TroveCategory.query.get(_id = cat)
if categories.get(cat):
categories[cat] += 1
else:
categories[cat] = 1
categories = sorted(categories.items(), key=lambda (x,y): y,reverse=True)
ret_dict['lastmonth_logins'] = stats.getLastMonthLogins()
ret_dict['categories'] = categories
days = ret_dict['days']
if days >= 30:
ret_dict['permonthlogins'] = \
round(stats.tot_logins_count*30.0/days,2)
else:
ret_dict['permonthlogins'] = 'n/a'
ret_dict['codepercentage'] = stats.codeRanking()
ret_dict['discussionpercentage'] = stats.discussionRanking()
ret_dict['ticketspercentage'] = stats.ticketsRanking()
ret_dict['codecontribution'] = stats.getCodeContribution()
ret_dict['discussioncontribution'] = stats.getDiscussionContribution()
ret_dict['ticketcontribution'] = stats.getTicketsContribution()
ret_dict['maxcodecontrib'], ret_dict['averagecodecontrib'] =\
stats.getMaxAndAverageCodeContribution()
ret_dict['maxdisccontrib'], ret_dict['averagedisccontrib'] =\
stats.getMaxAndAverageDiscussionContribution()
ret_dict['maxticketcontrib'], ret_dict['averageticketcontrib'] =\
stats.getMaxAndAverageTicketsSolvingPercentage()
return ret_dict
@expose('jinja:forgeuserstats:templates/commits.html')
@with_trailing_slash
def commits(self, **kw):
self.user = c.project.user_project_of
if not self.user:
return dict(user=None)
if not self.user.stats:
UserStats.create(self.user)
stats = self.user.stats
if (not stats.visible) and (c.user != self.user):
return dict(user=self.user)
commits = stats.getCommitsByCategory()
return dict(
user = self.user,
data = commits)
@expose('jinja:forgeuserstats:templates/artifacts.html')
@with_trailing_slash
def artifacts(self, **kw):
self.user = c.project.user_project_of
if not self.user:
return dict(user=None)
if not self.user.stats:
UserStats.create(self.user)
stats = self.user.stats
if (not stats.visible) and (c.user != self.user):
return dict(user=self.user)
stats = self.user.stats
artifacts = stats.getArtifactsByCategory(detailed=True)
return dict(
user = self.user,
data = artifacts)
@expose('jinja:forgeuserstats:templates/tickets.html')
@with_trailing_slash
def tickets(self, **kw):
self.user = c.project.user_project_of
if not self.user:
return dict(user=None)
if not self.user.stats:
UserStats.create(self.user)
stats = self.user.stats
if (not stats.visible) and (c.user != self.user):
return dict(user=self.user)
artifacts = self.user.stats.getTicketsByCategory()
return dict(
user=self.user,
data=artifacts)
@expose()
def categories_graph(self):
self.user = c.project.user_project_of
if not self.user:
return None
categories = {}
for p in self.user.my_projects():
for cat in p.trove_topic:
cat = M.TroveCategory.query.get(_id = cat)
if categories.get(cat):
categories[cat] += 1
else:
categories[cat] = 1
data = []
labels = []
i = 0
for cat in sorted(categories.keys(), key=lambda x:x.fullname):
n = categories[cat]
data = data + [i] * n
label = cat.fullname
if len(label) > 15:
label = label[:15] + "..."
labels.append(label)
i += 1
return create_histogram(data, labels,
'Number of projects', 'Projects by category')
@expose()
def code_ranking_bar(self):
self.user = c.project.user_project_of
if not self.user:
return None
stats = self.user.stats
return create_progress_bar(stats.codeRanking())
@expose()
def discussion_ranking_bar(self):
self.user = c.project.user_project_of
if not self.user:
return None
stats = self.user.stats
return create_progress_bar(stats.discussionRanking())
@expose()
def tickets_ranking_bar(self):
self.user = c.project.user_project_of
if not self.user:
return None
stats = self.user.stats
return create_progress_bar(stats.ticketsRanking())
def _getDataForCategory(category, stats):
totcommits = stats.getCommits(category)
tottickets = stats.getTickets(category)
averagetime = tottickets.get('averagesolvingtime')
artifacts_by_type = stats.getArtifactsByType(category)
totartifacts = artifacts_by_type.get(None)
if totartifacts:
del artifacts_by_type[None]
else:
totartifacts = dict(created=0, modified=0)
lmcommits = stats.getLastMonthCommits(category)
lm_artifacts_by_type = stats.getLastMonthArtifactsByType(category)
lm_totartifacts = stats.getLastMonthArtifacts(category)
lm_tickets = stats.getLastMonthTickets(category)
averagetime = lm_tickets.get('averagesolvingtime')
days = (datetime.utcnow() - stats.registration_date).days
if days >= 30:
pmartifacts = dict(
created = round(totartifacts['created']*30.0/days,2),
modified=round(totartifacts['modified']*30.0/days,2))
pmcommits = dict(
number=round(totcommits['number']*30.0/days,2),
lines=round(totcommits['lines']*30.0/days,2))
pmtickets = dict(
assigned=round(tottickets['assigned']*30.0/days,2),
revoked=round(tottickets['revoked']*30.0/days,2),
solved=round(tottickets['solved']*30.0/days,2),
averagesolvingtime='n/a')
for key in artifacts_by_type:
value = artifacts_by_type[key]
artifacts_by_type[key]['pmcreated'] = \
round(value['created']*30.0/days,2)
artifacts_by_type[key]['pmmodified']= \
round(value['modified']*30.0/days,2)
else:
pmartifacts = dict(created='n/a', modified='n/a')
pmcommits = dict(number='n/a', lines='n/a')
pmtickets = dict(
assigned='n/a',
revoked='n/a',
solved='n/a',
averagesolvingtime='n/a')
for key in artifacts_by_type:
artifacts_by_type[key]['pmcreated'] = 'n/a'
artifacts_by_type[key]['pmmodified']= 'n/a'
return dict(
days = days,
totcommits = totcommits,
lastmonthcommits = lmcommits,
lastmonthtickets = lm_tickets,
tottickets = tottickets,
permonthcommits = pmcommits,
totartifacts = totartifacts,
lastmonthartifacts = lm_totartifacts,
permonthartifacts = pmartifacts,
artifacts_by_type = artifacts_by_type,
lastmonth_artifacts_by_type = lm_artifacts_by_type,
permonthtickets = pmtickets)