##// END OF EJS Templates
exc_store: add filter for display and deletion.
marcink -
r3669:d393e493 default
parent child Browse files
Show More
@@ -1,160 +1,174 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2018-2019 RhodeCode GmbH
3 # Copyright (C) 2018-2019 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 import os
20 import os
21 import logging
21 import logging
22
22
23 from pyramid.httpexceptions import HTTPFound
23 from pyramid.httpexceptions import HTTPFound
24 from pyramid.view import view_config
24 from pyramid.view import view_config
25
25
26 from rhodecode.apps._base import BaseAppView
26 from rhodecode.apps._base import BaseAppView
27 from rhodecode.apps._base.navigation import navigation_list
27 from rhodecode.apps._base.navigation import navigation_list
28 from rhodecode.lib import helpers as h
28 from rhodecode.lib import helpers as h
29 from rhodecode.lib.auth import (
29 from rhodecode.lib.auth import (
30 LoginRequired, HasPermissionAllDecorator, CSRFRequired)
30 LoginRequired, HasPermissionAllDecorator, CSRFRequired)
31 from rhodecode.lib.utils2 import time_to_utcdatetime, safe_int
31 from rhodecode.lib.utils2 import time_to_utcdatetime, safe_int
32 from rhodecode.lib import exc_tracking
32 from rhodecode.lib import exc_tracking
33
33
34 log = logging.getLogger(__name__)
34 log = logging.getLogger(__name__)
35
35
36
36
37 class ExceptionsTrackerView(BaseAppView):
37 class ExceptionsTrackerView(BaseAppView):
38 def load_default_context(self):
38 def load_default_context(self):
39 c = self._get_local_tmpl_context()
39 c = self._get_local_tmpl_context()
40 c.navlist = navigation_list(self.request)
40 c.navlist = navigation_list(self.request)
41 return c
41 return c
42
42
43 def count_all_exceptions(self):
43 def count_all_exceptions(self):
44 exc_store_path = exc_tracking.get_exc_store()
44 exc_store_path = exc_tracking.get_exc_store()
45 count = 0
45 count = 0
46 for fname in os.listdir(exc_store_path):
46 for fname in os.listdir(exc_store_path):
47 parts = fname.split('_', 2)
47 parts = fname.split('_', 2)
48 if not len(parts) == 3:
48 if not len(parts) == 3:
49 continue
49 continue
50 count +=1
50 count +=1
51 return count
51 return count
52
52
53 def get_all_exceptions(self, read_metadata=False, limit=None):
53 def get_all_exceptions(self, read_metadata=False, limit=None, type_filter=None):
54 exc_store_path = exc_tracking.get_exc_store()
54 exc_store_path = exc_tracking.get_exc_store()
55 exception_list = []
55 exception_list = []
56
56
57 def key_sorter(val):
57 def key_sorter(val):
58 try:
58 try:
59 return val.split('_')[-1]
59 return val.split('_')[-1]
60 except Exception:
60 except Exception:
61 return 0
61 return 0
62 count = 0
62
63 for fname in reversed(sorted(os.listdir(exc_store_path), key=key_sorter)):
63 for fname in reversed(sorted(os.listdir(exc_store_path), key=key_sorter)):
64
64
65 parts = fname.split('_', 2)
65 parts = fname.split('_', 2)
66 if not len(parts) == 3:
66 if not len(parts) == 3:
67 continue
67 continue
68
68
69 exc_id, app_type, exc_timestamp = parts
69 exc_id, app_type, exc_timestamp = parts
70
70
71 exc = {'exc_id': exc_id, 'app_type': app_type, 'exc_type': 'unknown',
71 exc = {'exc_id': exc_id, 'app_type': app_type, 'exc_type': 'unknown',
72 'exc_utc_date': '', 'exc_timestamp': exc_timestamp}
72 'exc_utc_date': '', 'exc_timestamp': exc_timestamp}
73
73
74 if read_metadata:
74 if read_metadata:
75 full_path = os.path.join(exc_store_path, fname)
75 full_path = os.path.join(exc_store_path, fname)
76 if not os.path.isfile(full_path):
76 if not os.path.isfile(full_path):
77 continue
77 continue
78 try:
78 try:
79 # we can read our metadata
79 # we can read our metadata
80 with open(full_path, 'rb') as f:
80 with open(full_path, 'rb') as f:
81 exc_metadata = exc_tracking.exc_unserialize(f.read())
81 exc_metadata = exc_tracking.exc_unserialize(f.read())
82 exc.update(exc_metadata)
82 exc.update(exc_metadata)
83 except Exception:
83 except Exception:
84 log.exception('Failed to read exc data from:{}'.format(full_path))
84 log.exception('Failed to read exc data from:{}'.format(full_path))
85 pass
85 pass
86
87 # convert our timestamp to a date obj, for nicer representation
86 # convert our timestamp to a date obj, for nicer representation
88 exc['exc_utc_date'] = time_to_utcdatetime(exc['exc_timestamp'])
87 exc['exc_utc_date'] = time_to_utcdatetime(exc['exc_timestamp'])
88
89 type_present = exc.get('exc_type')
90 if type_filter:
91 if type_present and type_present == type_filter:
92 exception_list.append(exc)
93 else:
89 exception_list.append(exc)
94 exception_list.append(exc)
90
95
91 count += 1
96 if limit and len(exception_list) >= limit:
92 if limit and count >= limit:
93 break
97 break
94 return exception_list
98 return exception_list
95
99
96 @LoginRequired()
100 @LoginRequired()
97 @HasPermissionAllDecorator('hg.admin')
101 @HasPermissionAllDecorator('hg.admin')
98 @view_config(
102 @view_config(
99 route_name='admin_settings_exception_tracker', request_method='GET',
103 route_name='admin_settings_exception_tracker', request_method='GET',
100 renderer='rhodecode:templates/admin/settings/settings.mako')
104 renderer='rhodecode:templates/admin/settings/settings.mako')
101 def browse_exceptions(self):
105 def browse_exceptions(self):
102 _ = self.request.translate
106 _ = self.request.translate
103 c = self.load_default_context()
107 c = self.load_default_context()
104 c.active = 'exceptions_browse'
108 c.active = 'exceptions_browse'
105 c.limit = safe_int(self.request.GET.get('limit')) or 50
109 c.limit = safe_int(self.request.GET.get('limit')) or 50
110 c.type_filter = self.request.GET.get('type_filter')
106 c.next_limit = c.limit + 50
111 c.next_limit = c.limit + 50
107 c.exception_list = self.get_all_exceptions(read_metadata=True, limit=c.limit)
112 c.exception_list = self.get_all_exceptions(
113 read_metadata=True, limit=c.limit, type_filter=c.type_filter)
108 c.exception_list_count = self.count_all_exceptions()
114 c.exception_list_count = self.count_all_exceptions()
109 c.exception_store_dir = exc_tracking.get_exc_store()
115 c.exception_store_dir = exc_tracking.get_exc_store()
110 return self._get_template_context(c)
116 return self._get_template_context(c)
111
117
112 @LoginRequired()
118 @LoginRequired()
113 @HasPermissionAllDecorator('hg.admin')
119 @HasPermissionAllDecorator('hg.admin')
114 @view_config(
120 @view_config(
115 route_name='admin_settings_exception_tracker_show', request_method='GET',
121 route_name='admin_settings_exception_tracker_show', request_method='GET',
116 renderer='rhodecode:templates/admin/settings/settings.mako')
122 renderer='rhodecode:templates/admin/settings/settings.mako')
117 def exception_show(self):
123 def exception_show(self):
118 _ = self.request.translate
124 _ = self.request.translate
119 c = self.load_default_context()
125 c = self.load_default_context()
120
126
121 c.active = 'exceptions'
127 c.active = 'exceptions'
122 c.exception_id = self.request.matchdict['exception_id']
128 c.exception_id = self.request.matchdict['exception_id']
123 c.traceback = exc_tracking.read_exception(c.exception_id, prefix=None)
129 c.traceback = exc_tracking.read_exception(c.exception_id, prefix=None)
124 return self._get_template_context(c)
130 return self._get_template_context(c)
125
131
126 @LoginRequired()
132 @LoginRequired()
127 @HasPermissionAllDecorator('hg.admin')
133 @HasPermissionAllDecorator('hg.admin')
128 @CSRFRequired()
134 @CSRFRequired()
129 @view_config(
135 @view_config(
130 route_name='admin_settings_exception_tracker_delete_all', request_method='POST',
136 route_name='admin_settings_exception_tracker_delete_all', request_method='POST',
131 renderer='rhodecode:templates/admin/settings/settings.mako')
137 renderer='rhodecode:templates/admin/settings/settings.mako')
132 def exception_delete_all(self):
138 def exception_delete_all(self):
133 _ = self.request.translate
139 _ = self.request.translate
134 c = self.load_default_context()
140 c = self.load_default_context()
141 type_filter = self.request.POST.get('type_filter')
135
142
136 c.active = 'exceptions'
143 c.active = 'exceptions'
137 all_exc = self.get_all_exceptions()
144 all_exc = self.get_all_exceptions(read_metadata=bool(type_filter), type_filter=type_filter)
138 exc_count = len(all_exc)
145 exc_count = 0
146
139 for exc in all_exc:
147 for exc in all_exc:
148 if type_filter:
149 if exc.get('exc_type') == type_filter:
140 exc_tracking.delete_exception(exc['exc_id'], prefix=None)
150 exc_tracking.delete_exception(exc['exc_id'], prefix=None)
151 exc_count += 1
152 else:
153 exc_tracking.delete_exception(exc['exc_id'], prefix=None)
154 exc_count += 1
141
155
142 h.flash(_('Removed {} Exceptions').format(exc_count), category='success')
156 h.flash(_('Removed {} Exceptions').format(exc_count), category='success')
143 raise HTTPFound(h.route_path('admin_settings_exception_tracker'))
157 raise HTTPFound(h.route_path('admin_settings_exception_tracker'))
144
158
145 @LoginRequired()
159 @LoginRequired()
146 @HasPermissionAllDecorator('hg.admin')
160 @HasPermissionAllDecorator('hg.admin')
147 @CSRFRequired()
161 @CSRFRequired()
148 @view_config(
162 @view_config(
149 route_name='admin_settings_exception_tracker_delete', request_method='POST',
163 route_name='admin_settings_exception_tracker_delete', request_method='POST',
150 renderer='rhodecode:templates/admin/settings/settings.mako')
164 renderer='rhodecode:templates/admin/settings/settings.mako')
151 def exception_delete(self):
165 def exception_delete(self):
152 _ = self.request.translate
166 _ = self.request.translate
153 c = self.load_default_context()
167 c = self.load_default_context()
154
168
155 c.active = 'exceptions'
169 c.active = 'exceptions'
156 c.exception_id = self.request.matchdict['exception_id']
170 c.exception_id = self.request.matchdict['exception_id']
157 exc_tracking.delete_exception(c.exception_id, prefix=None)
171 exc_tracking.delete_exception(c.exception_id, prefix=None)
158
172
159 h.flash(_('Removed Exception {}').format(c.exception_id), category='success')
173 h.flash(_('Removed Exception {}').format(c.exception_id), category='success')
160 raise HTTPFound(h.route_path('admin_settings_exception_tracker'))
174 raise HTTPFound(h.route_path('admin_settings_exception_tracker'))
@@ -1,57 +1,65 b''
1 <div class="panel panel-default">
1 <div class="panel panel-default">
2 <div class="panel-heading">
2 <div class="panel-heading">
3 <h3 class="panel-title">${_('Exceptions Tracker ')}</h3>
3 <h3 class="panel-title">${_('Exceptions Tracker ')}</h3>
4 </div>
4 </div>
5 <div class="panel-body">
5 <div class="panel-body">
6 % if c.exception_list_count == 1:
6 % if c.exception_list_count == 1:
7 ${_('There is {} stored exception.').format(c.exception_list_count)}
7 ${_('There is {} stored exception.').format(c.exception_list_count)}
8 % else:
8 % else:
9 ${_('There are {} stored exceptions.').format(c.exception_list_count)}
9 ${_('There are total {} stored exceptions.').format(c.exception_list_count)}
10 % endif
10 % endif
11 <br/>
11 ${_('Store directory')}: ${c.exception_store_dir}
12 ${_('Store directory')}: ${c.exception_store_dir}
12
13
13 ${h.secure_form(h.route_path('admin_settings_exception_tracker_delete_all'), request=request)}
14 ${h.secure_form(h.route_path('admin_settings_exception_tracker_delete_all'), request=request)}
14 <div style="margin: 0 0 20px 0" class="fake-space"></div>
15 <div style="margin: 0 0 20px 0" class="fake-space"></div>
15
16 <input type="hidden" name="type_filter", value="${c.type_filter}">
16 <div class="field">
17 <div class="field">
17 <button class="btn btn-small btn-danger" type="submit"
18 <button class="btn btn-small btn-danger" type="submit"
18 onclick="return confirm('${_('Confirm to delete all exceptions')}');">
19 onclick="return confirm('${_('Confirm to delete all exceptions')}');">
19 <i class="icon-remove-sign"></i>
20 <i class="icon-remove-sign"></i>
21 % if c.type_filter:
22 ${_('Delete All `{}`').format(c.type_filter)}
23 % else:
20 ${_('Delete All')}
24 ${_('Delete All')}
25 % endif
26
21 </button>
27 </button>
22 </div>
28 </div>
23
29
24 ${h.end_form()}
30 ${h.end_form()}
25
31
26 </div>
32 </div>
27 </div>
33 </div>
28
34
29
35
30 <div class="panel panel-default">
36 <div class="panel panel-default">
31 <div class="panel-heading">
37 <div class="panel-heading">
32 <h3 class="panel-title">${_('Exceptions Tracker - Showing the last {} Exceptions').format(c.limit)}.</h3>
38 <h3 class="panel-title">${_('Exceptions Tracker - Showing the last {} Exceptions').format(c.limit)}.</h3>
33 <a class="panel-edit" href="${h.current_route_path(request, limit=c.next_limit)}">${_('Show more')}</a>
39 <a class="panel-edit" href="${h.current_route_path(request, limit=c.next_limit)}">${_('Show more')}</a>
34 </div>
40 </div>
35 <div class="panel-body">
41 <div class="panel-body">
36 <table class="rctable">
42 <table class="rctable">
37 <tr>
43 <tr>
38 <th>#</th>
44 <th>#</th>
39 <th>Exception ID</th>
45 <th>Exception ID</th>
40 <th>Date</th>
46 <th>Date</th>
41 <th>App Type</th>
47 <th>App Type</th>
42 <th>Exc Type</th>
48 <th>Exc Type</th>
43 </tr>
49 </tr>
44 <% cnt = len(c.exception_list)%>
50 <% cnt = len(c.exception_list)%>
45 % for tb in c.exception_list:
51 % for tb in c.exception_list:
46 <tr>
52 <tr>
47 <td>${cnt}</td>
53 <td>${cnt}</td>
48 <td><a href="${h.route_path('admin_settings_exception_tracker_show', exception_id=tb['exc_id'])}"><code>${tb['exc_id']}</code></a></td>
54 <td><a href="${h.route_path('admin_settings_exception_tracker_show', exception_id=tb['exc_id'])}"><code>${tb['exc_id']}</code></a></td>
49 <td>${h.format_date(tb['exc_utc_date'])}</td>
55 <td>${h.format_date(tb['exc_utc_date'])}</td>
50 <td>${tb['app_type']}</td>
56 <td>${tb['app_type']}</td>
51 <td>${tb['exc_type']}</td>
57 <td>
58 <a href="${h.current_route_path(request, type_filter=tb['exc_type'])}">${tb['exc_type']}</a>
59 </td>
52 </tr>
60 </tr>
53 <% cnt -=1 %>
61 <% cnt -=1 %>
54 % endfor
62 % endfor
55 </table>
63 </table>
56 </div>
64 </div>
57 </div>
65 </div>
General Comments 0
You need to be logged in to leave comments. Login now