Show More
@@ -1,188 +1,187 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ |
|
2 | """ | |
3 | rhodecode.controllers.summary |
|
3 | rhodecode.controllers.summary | |
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
5 |
|
5 | |||
6 | Summary controller for Rhodecode |
|
6 | Summary controller for Rhodecode | |
7 |
|
7 | |||
8 | :created_on: Apr 18, 2010 |
|
8 | :created_on: Apr 18, 2010 | |
9 | :author: marcink |
|
9 | :author: marcink | |
10 | :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> |
|
10 | :copyright: (C) 2009-2011 Marcin Kuzminski <marcin@python-works.com> | |
11 | :license: GPLv3, see COPYING for more details. |
|
11 | :license: GPLv3, see COPYING for more details. | |
12 | """ |
|
12 | """ | |
13 | # This program is free software: you can redistribute it and/or modify |
|
13 | # This program is free software: you can redistribute it and/or modify | |
14 | # it under the terms of the GNU General Public License as published by |
|
14 | # it under the terms of the GNU General Public License as published by | |
15 | # the Free Software Foundation, either version 3 of the License, or |
|
15 | # the Free Software Foundation, either version 3 of the License, or | |
16 | # (at your option) any later version. |
|
16 | # (at your option) any later version. | |
17 | # |
|
17 | # | |
18 | # This program is distributed in the hope that it will be useful, |
|
18 | # This program is distributed in the hope that it will be useful, | |
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | # GNU General Public License for more details. |
|
21 | # GNU General Public License for more details. | |
22 | # |
|
22 | # | |
23 | # You should have received a copy of the GNU General Public License |
|
23 | # You should have received a copy of the GNU General Public License | |
24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
25 |
|
25 | |||
26 | import calendar |
|
26 | import calendar | |
27 | import logging |
|
27 | import logging | |
28 | from time import mktime |
|
28 | from time import mktime | |
29 | from datetime import datetime, timedelta, date |
|
29 | from datetime import datetime, timedelta, date | |
30 |
|
30 | |||
31 | from vcs.exceptions import ChangesetError |
|
31 | from vcs.exceptions import ChangesetError | |
32 |
|
32 | |||
33 | from pylons import tmpl_context as c, request, url |
|
33 | from pylons import tmpl_context as c, request, url | |
34 | from pylons.i18n.translation import _ |
|
34 | from pylons.i18n.translation import _ | |
35 |
|
35 | |||
36 | from rhodecode.model.db import Statistics, Repository |
|
36 | from rhodecode.model.db import Statistics, Repository | |
37 | from rhodecode.model.repo import RepoModel |
|
37 | from rhodecode.model.repo import RepoModel | |
38 |
|
38 | |||
39 | from rhodecode.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator |
|
39 | from rhodecode.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator | |
40 | from rhodecode.lib.base import BaseRepoController, render |
|
40 | from rhodecode.lib.base import BaseRepoController, render | |
41 | from rhodecode.lib.utils import OrderedDict, EmptyChangeset |
|
41 | from rhodecode.lib.utils import OrderedDict, EmptyChangeset | |
42 |
|
42 | |||
43 | from rhodecode.lib.celerylib import run_task |
|
43 | from rhodecode.lib.celerylib import run_task | |
44 | from rhodecode.lib.celerylib.tasks import get_commits_stats, \ |
|
44 | from rhodecode.lib.celerylib.tasks import get_commits_stats, \ | |
45 | LANGUAGES_EXTENSIONS_MAP |
|
45 | LANGUAGES_EXTENSIONS_MAP | |
46 | from rhodecode.lib.helpers import RepoPage |
|
46 | from rhodecode.lib.helpers import RepoPage | |
47 |
|
47 | |||
48 | try: |
|
48 | try: | |
49 | import json |
|
49 | import json | |
50 | except ImportError: |
|
50 | except ImportError: | |
51 | #python 2.5 compatibility |
|
51 | #python 2.5 compatibility | |
52 | import simplejson as json |
|
52 | import simplejson as json | |
53 | log = logging.getLogger(__name__) |
|
53 | log = logging.getLogger(__name__) | |
54 |
|
54 | |||
55 |
|
55 | |||
56 | class SummaryController(BaseRepoController): |
|
56 | class SummaryController(BaseRepoController): | |
57 |
|
57 | |||
58 | @LoginRequired() |
|
58 | @LoginRequired() | |
59 | @HasRepoPermissionAnyDecorator('repository.read', 'repository.write', |
|
59 | @HasRepoPermissionAnyDecorator('repository.read', 'repository.write', | |
60 | 'repository.admin') |
|
60 | 'repository.admin') | |
61 | def __before__(self): |
|
61 | def __before__(self): | |
62 | super(SummaryController, self).__before__() |
|
62 | super(SummaryController, self).__before__() | |
63 |
|
63 | |||
64 | def index(self, repo_name): |
|
64 | def index(self, repo_name): | |
65 |
|
65 | |||
66 | e = request.environ |
|
66 | e = request.environ | |
67 | c.dbrepo = dbrepo = Repository.by_repo_name(repo_name) |
|
67 | c.dbrepo = dbrepo = Repository.by_repo_name(repo_name) | |
68 |
|
68 | |||
69 | c.following = self.scm_model.is_following_repo(repo_name, |
|
69 | c.following = self.scm_model.is_following_repo(repo_name, | |
70 | self.rhodecode_user.user_id) |
|
70 | self.rhodecode_user.user_id) | |
71 |
|
71 | |||
72 | def url_generator(**kw): |
|
72 | def url_generator(**kw): | |
73 | return url('shortlog_home', repo_name=repo_name, size=10, **kw) |
|
73 | return url('shortlog_home', repo_name=repo_name, size=10, **kw) | |
74 |
|
74 | |||
75 | c.repo_changesets = RepoPage(c.rhodecode_repo, page=1, |
|
75 | c.repo_changesets = RepoPage(c.rhodecode_repo, page=1, | |
76 | items_per_page=10, url=url_generator) |
|
76 | items_per_page=10, url=url_generator) | |
77 |
|
77 | |||
78 | if self.rhodecode_user.username == 'default': |
|
78 | if self.rhodecode_user.username == 'default': | |
79 | #for default(anonymous) user we don't need to pass credentials |
|
79 | #for default(anonymous) user we don't need to pass credentials | |
80 | username = '' |
|
80 | username = '' | |
81 | password = '' |
|
81 | password = '' | |
82 | else: |
|
82 | else: | |
83 | username = str(self.rhodecode_user.username) |
|
83 | username = str(self.rhodecode_user.username) | |
84 | password = '@' |
|
84 | password = '@' | |
85 |
|
85 | |||
86 | if e.get('wsgi.url_scheme') == 'https': |
|
86 | if e.get('wsgi.url_scheme') == 'https': | |
87 | split_s = 'https://' |
|
87 | split_s = 'https://' | |
88 | else: |
|
88 | else: | |
89 | split_s = 'http://' |
|
89 | split_s = 'http://' | |
90 |
|
90 | |||
91 | qualified_uri = [split_s] + [url.current(qualified=True)\ |
|
91 | qualified_uri = [split_s] + [url.current(qualified=True)\ | |
92 | .split(split_s)[-1]] |
|
92 | .split(split_s)[-1]] | |
93 | uri = u'%(proto)s%(user)s%(pass)s%(rest)s' \ |
|
93 | uri = u'%(proto)s%(user)s%(pass)s%(rest)s' \ | |
94 | % {'user': username, |
|
94 | % {'user': username, | |
95 | 'pass': password, |
|
95 | 'pass': password, | |
96 | 'proto': qualified_uri[0], |
|
96 | 'proto': qualified_uri[0], | |
97 | 'rest': qualified_uri[1]} |
|
97 | 'rest': qualified_uri[1]} | |
98 | c.clone_repo_url = uri |
|
98 | c.clone_repo_url = uri | |
99 | c.repo_tags = OrderedDict() |
|
99 | c.repo_tags = OrderedDict() | |
100 | for name, hash in c.rhodecode_repo.tags.items()[:10]: |
|
100 | for name, hash in c.rhodecode_repo.tags.items()[:10]: | |
101 | try: |
|
101 | try: | |
102 | c.repo_tags[name] = c.rhodecode_repo.get_changeset(hash) |
|
102 | c.repo_tags[name] = c.rhodecode_repo.get_changeset(hash) | |
103 | except ChangesetError: |
|
103 | except ChangesetError: | |
104 | c.repo_tags[name] = EmptyChangeset(hash) |
|
104 | c.repo_tags[name] = EmptyChangeset(hash) | |
105 |
|
105 | |||
106 | c.repo_branches = OrderedDict() |
|
106 | c.repo_branches = OrderedDict() | |
107 | for name, hash in c.rhodecode_repo.branches.items()[:10]: |
|
107 | for name, hash in c.rhodecode_repo.branches.items()[:10]: | |
108 | try: |
|
108 | try: | |
109 | c.repo_branches[name] = c.rhodecode_repo.get_changeset(hash) |
|
109 | c.repo_branches[name] = c.rhodecode_repo.get_changeset(hash) | |
110 | except ChangesetError: |
|
110 | except ChangesetError: | |
111 | c.repo_branches[name] = EmptyChangeset(hash) |
|
111 | c.repo_branches[name] = EmptyChangeset(hash) | |
112 |
|
112 | |||
113 | td = date.today() + timedelta(days=1) |
|
113 | td = date.today() + timedelta(days=1) | |
114 | td_1m = td - timedelta(days=calendar.mdays[td.month]) |
|
114 | td_1m = td - timedelta(days=calendar.mdays[td.month]) | |
115 | td_1y = td - timedelta(days=365) |
|
115 | td_1y = td - timedelta(days=365) | |
116 |
|
116 | |||
117 | ts_min_m = mktime(td_1m.timetuple()) |
|
117 | ts_min_m = mktime(td_1m.timetuple()) | |
118 | ts_min_y = mktime(td_1y.timetuple()) |
|
118 | ts_min_y = mktime(td_1y.timetuple()) | |
119 | ts_max_y = mktime(td.timetuple()) |
|
119 | ts_max_y = mktime(td.timetuple()) | |
120 |
|
120 | |||
121 | if dbrepo.enable_statistics: |
|
121 | if dbrepo.enable_statistics: | |
122 | c.no_data_msg = _('No data loaded yet') |
|
122 | c.no_data_msg = _('No data loaded yet') | |
123 | run_task(get_commits_stats, c.dbrepo.repo_name, ts_min_y, ts_max_y) |
|
123 | run_task(get_commits_stats, c.dbrepo.repo_name, ts_min_y, ts_max_y) | |
124 | else: |
|
124 | else: | |
125 | c.no_data_msg = _('Statistics are disabled for this repository') |
|
125 | c.no_data_msg = _('Statistics are disabled for this repository') | |
126 | c.ts_min = ts_min_m |
|
126 | c.ts_min = ts_min_m | |
127 | c.ts_max = ts_max_y |
|
127 | c.ts_max = ts_max_y | |
128 |
|
128 | |||
129 | stats = self.sa.query(Statistics)\ |
|
129 | stats = self.sa.query(Statistics)\ | |
130 | .filter(Statistics.repository == dbrepo)\ |
|
130 | .filter(Statistics.repository == dbrepo)\ | |
131 | .scalar() |
|
131 | .scalar() | |
132 |
|
132 | |||
133 | c.stats_percentage = 0 |
|
133 | c.stats_percentage = 0 | |
134 |
|
134 | |||
135 | if stats and stats.languages: |
|
135 | if stats and stats.languages: | |
136 | c.no_data = False is dbrepo.enable_statistics |
|
136 | c.no_data = False is dbrepo.enable_statistics | |
137 | lang_stats = json.loads(stats.languages) |
|
137 | lang_stats = json.loads(stats.languages) | |
138 | c.commit_data = stats.commit_activity |
|
138 | c.commit_data = stats.commit_activity | |
139 | c.overview_data = stats.commit_activity_combined |
|
139 | c.overview_data = stats.commit_activity_combined | |
140 |
|
140 | |||
141 | lang_stats = [(x, {"count": y, |
|
141 | lang_stats = [(x, {"count": y, | |
142 | "desc": LANGUAGES_EXTENSIONS_MAP.get(x)}) |
|
142 | "desc": LANGUAGES_EXTENSIONS_MAP.get(x)}) | |
143 | for x, y in lang_stats.items()] |
|
143 | for x, y in lang_stats.items()] | |
144 | print lang_stats |
|
|||
145 |
|
144 | |||
146 | c.trending_languages = json.dumps(OrderedDict( |
|
145 | c.trending_languages = json.dumps(OrderedDict( | |
147 | sorted(lang_stats, reverse=True, |
|
146 | sorted(lang_stats, reverse=True, | |
148 | key=lambda k: k[1])[:10] |
|
147 | key=lambda k: k[1])[:10] | |
149 | ) |
|
148 | ) | |
150 | ) |
|
149 | ) | |
151 | last_rev = stats.stat_on_revision |
|
150 | last_rev = stats.stat_on_revision | |
152 | c.repo_last_rev = c.rhodecode_repo.count() - 1 \ |
|
151 | c.repo_last_rev = c.rhodecode_repo.count() - 1 \ | |
153 | if c.rhodecode_repo.revisions else 0 |
|
152 | if c.rhodecode_repo.revisions else 0 | |
154 | if last_rev == 0 or c.repo_last_rev == 0: |
|
153 | if last_rev == 0 or c.repo_last_rev == 0: | |
155 | pass |
|
154 | pass | |
156 | else: |
|
155 | else: | |
157 | c.stats_percentage = '%.2f' % ((float((last_rev)) / |
|
156 | c.stats_percentage = '%.2f' % ((float((last_rev)) / | |
158 | c.repo_last_rev) * 100) |
|
157 | c.repo_last_rev) * 100) | |
159 | else: |
|
158 | else: | |
160 | c.commit_data = json.dumps({}) |
|
159 | c.commit_data = json.dumps({}) | |
161 | c.overview_data = json.dumps([[ts_min_y, 0], [ts_max_y, 10]]) |
|
160 | c.overview_data = json.dumps([[ts_min_y, 0], [ts_max_y, 10]]) | |
162 | c.trending_languages = json.dumps({}) |
|
161 | c.trending_languages = json.dumps({}) | |
163 | c.no_data = True |
|
162 | c.no_data = True | |
164 |
|
163 | |||
165 | c.enable_downloads = dbrepo.enable_downloads |
|
164 | c.enable_downloads = dbrepo.enable_downloads | |
166 | if c.enable_downloads: |
|
165 | if c.enable_downloads: | |
167 | c.download_options = self._get_download_links(c.rhodecode_repo) |
|
166 | c.download_options = self._get_download_links(c.rhodecode_repo) | |
168 |
|
167 | |||
169 | return render('summary/summary.html') |
|
168 | return render('summary/summary.html') | |
170 |
|
169 | |||
171 | def _get_download_links(self, repo): |
|
170 | def _get_download_links(self, repo): | |
172 |
|
171 | |||
173 | download_l = [] |
|
172 | download_l = [] | |
174 |
|
173 | |||
175 | branches_group = ([], _("Branches")) |
|
174 | branches_group = ([], _("Branches")) | |
176 | tags_group = ([], _("Tags")) |
|
175 | tags_group = ([], _("Tags")) | |
177 |
|
176 | |||
178 | for name, chs in c.rhodecode_repo.branches.items(): |
|
177 | for name, chs in c.rhodecode_repo.branches.items(): | |
179 | #chs = chs.split(':')[-1] |
|
178 | #chs = chs.split(':')[-1] | |
180 | branches_group[0].append((chs, name),) |
|
179 | branches_group[0].append((chs, name),) | |
181 | download_l.append(branches_group) |
|
180 | download_l.append(branches_group) | |
182 |
|
181 | |||
183 | for name, chs in c.rhodecode_repo.tags.items(): |
|
182 | for name, chs in c.rhodecode_repo.tags.items(): | |
184 | #chs = chs.split(':')[-1] |
|
183 | #chs = chs.split(':')[-1] | |
185 | tags_group[0].append((chs, name),) |
|
184 | tags_group[0].append((chs, name),) | |
186 | download_l.append(tags_group) |
|
185 | download_l.append(tags_group) | |
187 |
|
186 | |||
188 | return download_l |
|
187 | return download_l |
General Comments 0
You need to be logged in to leave comments.
Login now