Parent: [c2a33d] (diff)

Child: [b68415] (diff)

Download this file

main.py    327 lines (281 with data), 13.3 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
325
326
#-*- python -*-
import logging
from datetime import datetime, timedelta
import urllib2
# Non-stdlib imports
import pkg_resources
import pymongo
from tg import expose, validate, redirect, flash
from tg.decorators import with_trailing_slash, without_trailing_slash
from pylons import g, c, request, response
from formencode import validators
from webob import exc
import MySQLdb
import json
# Pyforge-specific imports
from allura.app import Application, ConfigOption, SitemapEntry
from allura.app import DefaultAdminController
from allura.lib import helpers as h
from allura.lib.search import search
from allura.lib.decorators import require_post
from allura.lib.security import has_access, require_access
from allura.lib import widgets as w
from allura.lib.widgets.subscriptions import SubscribeForm
from allura.lib.widgets import form_fields as ffw
from allura import model as M
from allura.controllers import BaseController, AppDiscussionController
# Local imports
from bitergiametrics import model as BM
from bitergiametrics import version
from bitergiametrics import widgets
from bitergiametrics import tasks
from forgetracker import model as TM
log = logging.getLogger(__name__)
class W:
thread=w.Thread(
page=None, limit=None, page_size=None, count=None,
style='linear')
# pager = widgets.BlogPager()
# new_post_form = widgets.NewPostForm()
# edit_post_form = widgets.EditPostForm()
view_metrics_form = widgets.ViewMetricsForm()
# label_edit = ffw.LabelEdit()
# subscribe_form = SubscribeForm()
class BitergiaMetricsApp(Application):
__version__ = version.__version__
tool_label='Metrics'
default_mount_label='Metrics'
default_mount_point='metrics'
permissions = ['configure', 'read', 'write', 'admin']
ordinal=14
installable=True
config_options = Application.config_options
icons={
24:'images/blog_24.png',
32:'images/blog_32.png',
48:'images/blog_48.png'
}
def __init__(self, project, config):
Application.__init__(self, project, config)
self.root = RootController()
self.admin = MetricsAdminController(self)
@property
@h.exceptionless([], log)
def sitemap(self):
menu_id = self.config.options.mount_label.title()
with h.push_config(c, app=self):
return [
SitemapEntry(menu_id, '.')[self.sidebar_menu()] ]
@h.exceptionless([], log)
def sidebar_menu(self):
base = c.app.url
links = [
SitemapEntry('Summary', base),
SitemapEntry('Git', base),
SitemapEntry(' Commits & committers', self.config.url() + 'commits_commiters'),
SitemapEntry(' Ratio commits/committer', self.config.url() + 'ratio_commits_commiters'),
# SitemapEntry(' Lines added/removed', self.config.url() + 'lines_added_removed', ui_icon=g.icons['stats']),
SitemapEntry(' Lines added/removed', self.config.url() + 'lines_added_removed'),
SitemapEntry('Tickets', base),
SitemapEntry(' Open & closed', self.config.url() + 'open_closed'),
SitemapEntry('Tools', self.config.url() + 'open_closed'),
SitemapEntry(' Bicho', self.config.url() + 'bicho'),
SitemapEntry(' CVSAnaly', self.config.url() + 'cvsanaly'),
]
return links
def admin_menu(self):
return super(BitergiaMetricsApp, self).admin_menu(force_options=True)
def install(self, project):
'Set up any default permissions and roles here'
super(BitergiaMetricsApp, self).install(project)
# Setup permissions
role_admin = M.ProjectRole.by_name('Admin')._id
role_developer = M.ProjectRole.by_name('Developer')._id
role_auth = M.ProjectRole.by_name('*authenticated')._id
role_anon = M.ProjectRole.by_name('*anonymous')._id
self.config.acl = [
M.ACE.allow(role_anon, 'read'),
M.ACE.allow(role_developer, 'write'),
M.ACE.allow(role_admin, 'configure'),
M.ACE.allow(role_admin, 'admin'),
]
def uninstall(self, project):
"Remove all the tool's artifacts from the database"
# BM.Attachment.query.remove(dict(app_config_id=c.app.config._id))
# BM.BlogPost.query.remove(dict(app_config_id=c.app.config._id))
# BM.BlogPostSnapshot.query.remove(dict(app_config_id=c.app.config._id))
super(BitergiaMetricsApp, self).uninstall(project)
class RootController(BaseController):
def __init__(self):
# setattr(self, 'feed.atom', self.feed)
# setattr(self, 'feed.rss', self.feed)
# self._discuss = AppDiscussionController()
pass
@expose('jinja:bitergiametrics:templates/metrics/bar.html')
@with_trailing_slash
def bar(self, page=0, limit=10, **kw):
return dict()
@expose('jinja:bitergiametrics:templates/metrics/tickets_swscopio.html')
@with_trailing_slash
def tickets_swscopio(self, page=0, limit=10, **kw):
# FIXME: SQL query to get the results
tickets_per_month = [
# ['23989','1999','1','Jan 1999','10','2','2','3'],
# ['23990','1999','2','Feb 1999','15','2','2','3'],
# ['23991','1999','3','Mar 1999','20','4','9','3'],
['24135','2011','3','Mar 2011','0.56','9','23','12'],
['24136','2011','4','Apr 2011','0.89','2','30','15'],
['24137','2011','5','May 2011','0.210','5','12','50'],
['24138','2011','6','Jun 2011','0.300','6','3','33'],
['24139','2011','7','Jul 2011','0.215','2','50','45'],
['24140','2011','8','Aug 2011','0.120','3','88','15'],
['24141','2011','9','Sep 2011','0.100','8','93','84'],
['24142','2011','10','Oct 2011','0.20','4','87','29'],
['24143','2011','11','Nov 2011','0.18','4','90','88'],
['24144','2011','12','Dec 2011','0.45','2','15','90'],
['24145','2012','1','Jan 2012','0.40','3','23','56'],
['24146','2012','2','Feb 2012','0.37','4','80','15'],
['24147','2012','3','Mar 2012','0.89','7','14','9'],
['24148','2012','4','Apr 2012','0.121','5','78','33'],
]
tickets_per_month_json = {}
tickets_per_month_json['id'] = []
tickets_per_month_json['year'] = []
tickets_per_month_json['month'] = []
tickets_per_month_json['date'] = []
tickets_per_month_json['live'] = []
tickets_per_month_json['people'] = []
tickets_per_month_json['open'] = []
tickets_per_month_json['close'] = []
for ticket in tickets_per_month:
tickets_per_month_json['id'].append(ticket[0])
tickets_per_month_json['year'].append(ticket[1])
tickets_per_month_json['month'].append(ticket[2])
tickets_per_month_json['date'].append(ticket[3])
tickets_per_month_json['live'].append(ticket[4])
tickets_per_month_json['people'].append(ticket[5])
tickets_per_month_json['open'].append(ticket[6])
tickets_per_month_json['close'].append(ticket[7])
# FIXME: Find better way to find the path to the file
f = open ("../AlluraBitergiaMetrics/bitergiametrics/nf/metrics/tickets_per_month_swscopio.json", 'w')
# f.write(tickets_per_month)
f.write(json.dumps(tickets_per_month_json))
f.close()
return dict()
@expose('jinja:bitergiametrics:templates/metrics/pie.html')
@with_trailing_slash
def pie(self, page=0, limit=10, **kw):
return dict()
# Metrics
@expose('jinja:bitergiametrics:templates/metrics/commits_commiters.html')
@with_trailing_slash
def commits_commiters(self, page=0, limit=10, **kw):
return dict()
@expose('jinja:bitergiametrics:templates/metrics/ratio_commits_commiters.html')
@with_trailing_slash
def ratio_commits_commiters(self, page=0, limit=10, **kw):
return dict()
@expose('jinja:bitergiametrics:templates/metrics/lines_added_removed.html')
@with_trailing_slash
def lines_added_removed(self, page=0, limit=10, **kw):
return dict()
@expose('jinja:bitergiametrics:templates/metrics/open_closed.html')
@with_trailing_slash
def open_closed(self, page=0, limit=10, **kw):
return dict()
# Tools management
@expose('jinja:bitergiametrics:templates/metrics/bicho.html')
@with_trailing_slash
@validate(dict(when=validators.UnicodeString(if_empty=None)))
def bicho(self, when=None, **kw):
if not when:
when = ''
else:
log.info("Executing bicho")
tasks.launch_bicho.post("PROJECT")
return dict(when=when)
@expose('jinja:bitergiametrics:templates/metrics/cvsanaly.html')
@with_trailing_slash
def cvsanaly(self, page=0, limit=10, **kw):
return dict()
# FIXME: Clean testing code and direct SQL to MySQL
@expose('jinja:bitergiametrics:templates/metrics/index.html')
@with_trailing_slash
def index(self, page=0, limit=10, **kw):
query_filter = dict(app_config_id=c.app.config._id)
if not has_access(c.app, 'write')():
query_filter['state'] = 'published'
# q = BM.BlogPost.query.find(query_filter)
# post_count = q.count()
# limit, page = h.paging_sanitizer(limit, page, post_count)
# posts = q.sort('timestamp', pymongo.DESCENDING) \
# .skip(page * limit).limit(limit)
c.form = W.view_metrics_form
# c.pager = W.pager
# return dict(posts=posts, page=page, limit=limit, count=post_count)
# TODO: error and config management. Share db connection
bichodb = None
tickets_per_month = None
try:
bichodb = MySQLdb.connect(user="root", db="bicho_allura")
cursor = bichodb.cursor()
tickets_month_sql = "SELECT id, DATE_FORMAT(submitted_on, '%Y') as year, "
tickets_month_sql += "DATE_FORMAT(submitted_on, '%m') as month, "
tickets_month_sql += "DATE_FORMAT(submitted_on, '%Y %m') AS yearmonth, "
tickets_month_sql += "COUNT(*) AS nissues FROM issues "
tickets_month_sql += "GROUP BY yearmonth order by yearmonth"
# cursor.execute("SELECT DATE_FORMAT(submitted_on, '%Y%V') AS yearweek, COUNT(*) AS nissues FROM issues GROUP BY yearweek")
cursor.execute(tickets_month_sql)
tickets_per_month = cursor.fetchall()
tickets_per_month_json = {}
tickets_per_month_json['id'] = []
tickets_per_month_json['year'] = []
tickets_per_month_json['month'] = []
tickets_per_month_json['date'] = []
tickets_per_month_json['tickets'] = []
for ticket in tickets_per_month:
tickets_per_month_json['id'].append(ticket[0])
tickets_per_month_json['year'].append(ticket[1])
tickets_per_month_json['month'].append(ticket[2])
tickets_per_month_json['date'].append(ticket[3])
tickets_per_month_json['tickets'].append(ticket[4])
# FIXME: Find better way to find the path to the file
f = open ("../AlluraBitergiaMetrics/bitergiametrics/nf/metrics/tickets_per_month.json", 'w')
# f.write(tickets_per_month)
f.write(json.dumps(tickets_per_month_json))
f.close()
except MySQLdb.Error, e:
log.error("Error accessing Bicho %d: %s" % (e.args[0], e.args[1]))
finally:
if bichodb:
bichodb.close()
total = TM.Ticket.query.find().count()
tickets = TM.Ticket.query.find()
now = datetime.utcnow()
week = timedelta(weeks=1)
week_ago = now - week
week_tickets = self.tickets_since(week_ago)
return dict(week_ago=str(week_ago),week_tickets=week_tickets,
total=total, tickets = tickets, tickets_per_month = tickets_per_month)
def tickets_since(self, when=None):
count = 0
if when:
count = TM.Ticket.query.find(dict(created_date={'$gte':when})).count()
else:
count = TM.Ticket.query.find(dict(app_config_id=c.app.config._id)).count()
return count
class MetricsAdminController(DefaultAdminController):
def __init__(self, app):
self.app = app
@without_trailing_slash
@expose('jinja:bitergiametrics:templates/metrics/admin_options.html')
def options(self):
return dict(app=self.app,
allow_config=has_access(self.app, 'configure')())
@without_trailing_slash
@expose()
@require_post()
def set_options(self, show_discussion=False):
self.app.config.options['show_discussion'] = show_discussion and True or False
flash('Metrics options updated')
redirect(h.really_unicode(c.project.url()+'admin/tools').encode('utf-8'))