annotate pylons_app/controllers/summary.py @ 493:2256c78afe53 celery

implemented basic autoupdating statistics fetched from database
author Marcin Kuzminski <marcin@python-works.com>
date Wed, 22 Sep 2010 04:30:36 +0200
parents 5c376ac2d4c9
children d5efb83590ef
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
252
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
1 #!/usr/bin/env python
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
2 # encoding: utf-8
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
3 # summary controller for pylons
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
4 # Copyright (C) 2009-2010 Marcin Kuzminski <marcin@python-works.com>
362
558eb7c5028f version bump to 0.8
Marcin Kuzminski <marcin@python-works.com>
parents: 318
diff changeset
5 #
252
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
6 # This program is free software; you can redistribute it and/or
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
7 # modify it under the terms of the GNU General Public License
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
8 # as published by the Free Software Foundation; version 2
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
9 # of the License or (at your opinion) any later version of the license.
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
10 #
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
11 # This program is distributed in the hope that it will be useful,
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
14 # GNU General Public License for more details.
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
15 #
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
16 # You should have received a copy of the GNU General Public License
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
17 # along with this program; if not, write to the Free Software
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
19 # MA 02110-1301, USA.
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
20 """
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
21 Created on April 18, 2010
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
22 summary controller for pylons
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
23 @author: marcink
3782a6d698af licensing updates, code cleanups
Marcin Kuzminski <marcin@python-works.com>
parents: 245
diff changeset
24 """
486
5c376ac2d4c9 rewrote graph plotting, added zooming and json dump insted of stupid string formating.
Marcin Kuzminski <marcin@python-works.com>
parents: 473
diff changeset
25 from pylons import tmpl_context as c, request, url
318
fdf9f6ee5217 Implemented permissions into hg app, secured admin controllers, templates and repository specific controllers
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
26 from pylons_app.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator
245
a83a1799480c Reimplemented way of caching repos list, hg model now get's repos objects right from cached dict, this way we skipp creating instances of MercurialRepository and gain performance. Some import cleanup
Marcin Kuzminski <marcin@python-works.com>
parents: 232
diff changeset
27 from pylons_app.lib.base import BaseController, render
362
558eb7c5028f version bump to 0.8
Marcin Kuzminski <marcin@python-works.com>
parents: 318
diff changeset
28 from pylons_app.lib.utils import OrderedDict
282
237470e64bb8 switched filters into webhelpers for easy of usage.
Marcin Kuzminski <marcin@python-works.com>
parents: 277
diff changeset
29 from pylons_app.model.hg_model import HgModel
493
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
30 from pylons_app.model.db import Statistics
282
237470e64bb8 switched filters into webhelpers for easy of usage.
Marcin Kuzminski <marcin@python-works.com>
parents: 277
diff changeset
31 from webhelpers.paginate import Page
467
3fc3ce53659b starting celery branch
Marcin Kuzminski <marcin@python-works.com>
parents: 439
diff changeset
32 from pylons_app.lib.celerylib import run_task
3fc3ce53659b starting celery branch
Marcin Kuzminski <marcin@python-works.com>
parents: 439
diff changeset
33 from pylons_app.lib.celerylib.tasks import get_commits_stats
493
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
34 from datetime import datetime, timedelta
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
35 from time import mktime
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
36 import calendar
191
b68b2246e5a6 Authenticated controller with LoginRequired decorator, and cleaned __before__ (used in baseController now). fixed User for clone url with logged in session user.
Marcin Kuzminski <marcin@python-works.com>
parents: 127
diff changeset
37 import logging
362
558eb7c5028f version bump to 0.8
Marcin Kuzminski <marcin@python-works.com>
parents: 318
diff changeset
38
82
670713507d03 Moved summary to seperate controller,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
39 log = logging.getLogger(__name__)
670713507d03 Moved summary to seperate controller,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
40
670713507d03 Moved summary to seperate controller,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
41 class SummaryController(BaseController):
191
b68b2246e5a6 Authenticated controller with LoginRequired decorator, and cleaned __before__ (used in baseController now). fixed User for clone url with logged in session user.
Marcin Kuzminski <marcin@python-works.com>
parents: 127
diff changeset
42
b68b2246e5a6 Authenticated controller with LoginRequired decorator, and cleaned __before__ (used in baseController now). fixed User for clone url with logged in session user.
Marcin Kuzminski <marcin@python-works.com>
parents: 127
diff changeset
43 @LoginRequired()
318
fdf9f6ee5217 Implemented permissions into hg app, secured admin controllers, templates and repository specific controllers
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
44 @HasRepoPermissionAnyDecorator('repository.read', 'repository.write',
fdf9f6ee5217 Implemented permissions into hg app, secured admin controllers, templates and repository specific controllers
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
45 'repository.admin')
82
670713507d03 Moved summary to seperate controller,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
46 def __before__(self):
191
b68b2246e5a6 Authenticated controller with LoginRequired decorator, and cleaned __before__ (used in baseController now). fixed User for clone url with logged in session user.
Marcin Kuzminski <marcin@python-works.com>
parents: 127
diff changeset
47 super(SummaryController, self).__before__()
318
fdf9f6ee5217 Implemented permissions into hg app, secured admin controllers, templates and repository specific controllers
Marcin Kuzminski <marcin@python-works.com>
parents: 282
diff changeset
48
82
670713507d03 Moved summary to seperate controller,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
49 def index(self):
670713507d03 Moved summary to seperate controller,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
50 hg_model = HgModel()
670713507d03 Moved summary to seperate controller,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
51 c.repo_info = hg_model.get_repo(c.repo_name)
282
237470e64bb8 switched filters into webhelpers for easy of usage.
Marcin Kuzminski <marcin@python-works.com>
parents: 277
diff changeset
52 c.repo_changesets = Page(list(c.repo_info[:10]), page=1, items_per_page=20)
82
670713507d03 Moved summary to seperate controller,
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
53 e = request.environ
191
b68b2246e5a6 Authenticated controller with LoginRequired decorator, and cleaned __before__ (used in baseController now). fixed User for clone url with logged in session user.
Marcin Kuzminski <marcin@python-works.com>
parents: 127
diff changeset
54 uri = u'%(protocol)s://%(user)s@%(host)s/%(repo_name)s' % {
232
37a832dc4a82 some beaker cache changes, and added repr to models
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
55 'protocol': e.get('wsgi.url_scheme'),
37a832dc4a82 some beaker cache changes, and added repr to models
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
56 'user':str(c.hg_app_user.username),
37a832dc4a82 some beaker cache changes, and added repr to models
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
57 'host':e.get('HTTP_HOST'),
37a832dc4a82 some beaker cache changes, and added repr to models
Marcin Kuzminski <marcin@python-works.com>
parents: 191
diff changeset
58 'repo_name':c.repo_name, }
191
b68b2246e5a6 Authenticated controller with LoginRequired decorator, and cleaned __before__ (used in baseController now). fixed User for clone url with logged in session user.
Marcin Kuzminski <marcin@python-works.com>
parents: 127
diff changeset
59 c.clone_repo_url = uri
389
174785aa5dc4 fixed sorting of tags and branches. Fix made in vcs.
Marcin Kuzminski <marcin@python-works.com>
parents: 365
diff changeset
60 c.repo_tags = OrderedDict()
277
7ec4463b6e53 fixed branches and tags, fetching for new vcs implementation
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
61 for name, hash in c.repo_info.tags.items()[:10]:
7ec4463b6e53 fixed branches and tags, fetching for new vcs implementation
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
62 c.repo_tags[name] = c.repo_info.get_changeset(hash)
7ec4463b6e53 fixed branches and tags, fetching for new vcs implementation
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
63
389
174785aa5dc4 fixed sorting of tags and branches. Fix made in vcs.
Marcin Kuzminski <marcin@python-works.com>
parents: 365
diff changeset
64 c.repo_branches = OrderedDict()
277
7ec4463b6e53 fixed branches and tags, fetching for new vcs implementation
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
65 for name, hash in c.repo_info.branches.items()[:10]:
7ec4463b6e53 fixed branches and tags, fetching for new vcs implementation
Marcin Kuzminski <marcin@python-works.com>
parents: 252
diff changeset
66 c.repo_branches[name] = c.repo_info.get_changeset(hash)
467
3fc3ce53659b starting celery branch
Marcin Kuzminski <marcin@python-works.com>
parents: 439
diff changeset
67
493
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
68 td = datetime.today() + timedelta(days=1)
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
69 y, m, d = td.year, td.month, td.day
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
70
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
71 ts_min_y = mktime((y - 1, (td - timedelta(days=calendar.mdays[m])).month,
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
72 d, 0, 0, 0, 0, 0, 0,))
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
73 ts_min_m = mktime((y, (td - timedelta(days=calendar.mdays[m])).month,
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
74 d, 0, 0, 0, 0, 0, 0,))
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
75
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
76 ts_max_y = mktime((y, m, d, 0, 0, 0, 0, 0, 0,))
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
77
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
78 run_task(get_commits_stats, c.repo_info.name, ts_min_y, ts_max_y)
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
79 c.ts_min = ts_min_m
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
80 c.ts_max = ts_max_y
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
81
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
82
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
83 stats = self.sa.query(Statistics)\
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
84 .filter(Statistics.repository == c.repo_info.dbrepo)\
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
85 .scalar()
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
86
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
87 if stats:
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
88 c.commit_data = stats.commit_activity
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
89 c.overview_data = stats.commit_activity_combined
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
90 else:
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
91 import json
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
92 c.commit_data = json.dumps({})
2256c78afe53 implemented basic autoupdating statistics fetched from database
Marcin Kuzminski <marcin@python-works.com>
parents: 486
diff changeset
93 c.overview_data = json.dumps([[ts_min_y, 0], [ts_max_y, 0] ])
362
558eb7c5028f version bump to 0.8
Marcin Kuzminski <marcin@python-works.com>
parents: 318
diff changeset
94
191
b68b2246e5a6 Authenticated controller with LoginRequired decorator, and cleaned __before__ (used in baseController now). fixed User for clone url with logged in session user.
Marcin Kuzminski <marcin@python-works.com>
parents: 127
diff changeset
95 return render('summary/summary.html')
362
558eb7c5028f version bump to 0.8
Marcin Kuzminski <marcin@python-works.com>
parents: 318
diff changeset
96