##// END OF EJS Templates
application: add all() method to service
ergo -
Show More
@@ -1,193 +1,198 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2016 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # AppEnlight Enterprise Edition, including its added features, Support
19 19 # services, and proprietary license terms, please see
20 20 # https://rhodecode.com/licenses/
21 21
22 22 import logging
23 23 import urllib.parse
24 24
25 25 import sqlalchemy as sa
26 26 from pyramid.threadlocal import get_current_request
27 27
28 28 from appenlight.lib.cache_regions import get_region
29 29 from appenlight.lib.enums import ReportType
30 30 from appenlight.models import get_db_session
31 31 from appenlight.models.report_group import ReportGroup
32 32 from appenlight.models.application import Application
33 33 from appenlight.models.event import Event
34 34 from appenlight.models.services.base import BaseService
35 35 from appenlight.models.services.event import EventService
36 36
37 37 log = logging.getLogger(__name__)
38 38
39 39
40 # cache_memory_min_1 = get_region('memory_min_1')
41
42 40 class ApplicationService(BaseService):
41
42 @classmethod
43 def all(cls, db_session=None):
44 db_session = get_db_session(db_session)
45 q = db_session.query(Application)
46 return q
47
43 48 @classmethod
44 49 def by_api_key(cls, api_key, db_session=None):
45 50 db_session = get_db_session(db_session)
46 51 q = db_session.query(Application)
47 52 q = q.filter(Application.api_key == api_key)
48 53 q = q.options(sa.orm.eagerload(Application.owner))
49 54 return q.first()
50 55
51 56 @classmethod
52 57 def by_api_key_cached(cls, db_session=None):
53 58 db_session = get_db_session(db_session)
54 59 cache_region = get_region('redis_min_1')
55 60
56 61 @cache_region.cache_on_arguments('ApplicationService.by_api_key')
57 62 def cached(*args, **kwargs):
58 63 app = cls.by_api_key(*args, db_session=db_session, **kwargs)
59 64 if app:
60 65 db_session.expunge(app)
61 66 return app
62 67
63 68 return cached
64 69
65 70 @classmethod
66 71 def by_public_api_key(cls, api_key, db_session=None, from_cache=False,
67 72 request=None):
68 73 db_session = get_db_session(db_session)
69 74 cache_region = get_region('redis_min_1')
70 75
71 76 def uncached(api_key):
72 77 q = db_session.query(Application)
73 78 q = q.filter(Application.public_key == api_key)
74 79 q = q.options(sa.orm.eagerload(Application.owner))
75 80 return q.first()
76 81
77 82 if from_cache:
78 83 @cache_region.cache_on_arguments(
79 84 'ApplicationService.by_public_api_key')
80 85 def cached(api_key):
81 86 app = uncached(api_key)
82 87 if app:
83 88 db_session.expunge(app)
84 89 return app
85 90
86 91 app = cached(api_key)
87 92 else:
88 93 app = uncached(api_key)
89 94 return app
90 95
91 96 @classmethod
92 97 def by_id(cls, db_id, db_session=None):
93 98 db_session = get_db_session(db_session)
94 99 q = db_session.query(Application)
95 100 q = q.filter(Application.resource_id == db_id)
96 101 return q.first()
97 102
98 103 @classmethod
99 104 def by_id_cached(cls, db_session=None):
100 105 db_session = get_db_session(db_session)
101 106 cache_region = get_region('redis_min_1')
102 107
103 108 @cache_region.cache_on_arguments('ApplicationService.by_id')
104 109 def cached(*args, **kwargs):
105 110 app = cls.by_id(*args, db_session=db_session, **kwargs)
106 111 if app:
107 112 db_session.expunge(app)
108 113 return app
109 114
110 115 return cached
111 116
112 117 @classmethod
113 118 def by_ids(cls, db_ids, db_session=None):
114 119 db_session = get_db_session(db_session)
115 120 query = db_session.query(Application)
116 121 query = query.filter(Application.resource_id.in_(db_ids))
117 122 return query
118 123
119 124 @classmethod
120 125 def by_http_referer(cls, referer_string, db_session=None):
121 126 db_session = get_db_session(db_session)
122 127 domain = urllib.parse.urlsplit(
123 128 referer_string, allow_fragments=False).netloc
124 129 if domain:
125 130 if domain.startswith('www.'):
126 131 domain = domain[4:]
127 132 q = db_session.query(Application).filter(Application.domain == domain)
128 133 return q.first()
129 134
130 135 @classmethod
131 136 def last_updated(cls, since_when, exclude_status=None, db_session=None):
132 137 db_session = get_db_session(db_session)
133 138 q = db_session.query(Application)
134 139 q2 = ReportGroup.last_updated(
135 140 since_when, exclude_status=exclude_status, db_session=db_session)
136 141 q2 = q2.from_self(ReportGroup.resource_id)
137 142 q2 = q2.group_by(ReportGroup.resource_id)
138 143 q = q.filter(Application.resource_id.in_(q2))
139 144 return q
140 145
141 146 @classmethod
142 147 def check_for_groups_alert(cls, resource, event_type, *args, **kwargs):
143 148 """ Check for open alerts depending on group type.
144 149 Create new one if nothing is found and send alerts """
145 150 db_session = get_db_session(kwargs.get('db_session'))
146 151 request = get_current_request()
147 152 report_groups = kwargs['report_groups']
148 153 occurence_dict = kwargs['occurence_dict']
149 154
150 155 error_reports = 0
151 156 slow_reports = 0
152 157 for group in report_groups:
153 158 occurences = occurence_dict.get(group.id, 1)
154 159 if group.get_report().report_type == ReportType.error:
155 160 error_reports += occurences
156 161 elif group.get_report().report_type == ReportType.slow:
157 162 slow_reports += occurences
158 163
159 164 log_msg = 'LIMIT INFO: %s : %s error reports. %s slow_reports' % (
160 165 resource,
161 166 error_reports,
162 167 slow_reports)
163 168 logging.warning(log_msg)
164 169 threshold = 10
165 170 for event_type in ['error_report_alert', 'slow_report_alert']:
166 171 if (error_reports < resource.error_report_threshold and
167 172 event_type == 'error_report_alert'):
168 173 continue
169 174 elif (slow_reports <= resource.slow_report_threshold and
170 175 event_type == 'slow_report_alert'):
171 176 continue
172 177 if event_type == 'error_report_alert':
173 178 amount = error_reports
174 179 threshold = resource.error_report_threshold
175 180 elif event_type == 'slow_report_alert':
176 181 amount = slow_reports
177 182 threshold = resource.slow_report_threshold
178 183
179 184 event = EventService.for_resource([resource.resource_id],
180 185 event_type=Event.types[
181 186 event_type],
182 187 status=Event.statuses['active'])
183 188 if event.first():
184 189 log.info('ALERT: PROGRESS: %s %s' % (event_type, resource))
185 190 else:
186 191 log.warning('ALERT: OPEN: %s %s' % (event_type, resource))
187 192 new_event = Event(resource_id=resource.resource_id,
188 193 event_type=Event.types[event_type],
189 194 status=Event.statuses['active'],
190 195 values={'reports': amount,
191 196 'threshold': threshold})
192 197 db_session.add(new_event)
193 198 new_event.send_alerts(request=request, resource=resource)
General Comments 0
You need to be logged in to leave comments. Login now