Show More
@@ -53,7 +53,7 b' from rhodecode.lib.utils2 import aslist ' | |||
|
53 | 53 | from rhodecode.lib.exc_tracking import store_exception |
|
54 | 54 | from rhodecode.subscribers import ( |
|
55 | 55 | scan_repositories_if_enabled, write_js_routes_if_enabled, |
|
56 | write_metadata_if_needed, inject_app_settings) | |
|
56 | write_metadata_if_needed, write_usage_data, inject_app_settings) | |
|
57 | 57 | |
|
58 | 58 | |
|
59 | 59 | log = logging.getLogger(__name__) |
@@ -316,6 +316,8 b' def includeme(config):' | |||
|
316 | 316 | pyramid.events.ApplicationCreated) |
|
317 | 317 | config.add_subscriber(write_metadata_if_needed, |
|
318 | 318 | pyramid.events.ApplicationCreated) |
|
319 | config.add_subscriber(write_usage_data, | |
|
320 | pyramid.events.ApplicationCreated) | |
|
319 | 321 | config.add_subscriber(write_js_routes_if_enabled, |
|
320 | 322 | pyramid.events.ApplicationCreated) |
|
321 | 323 |
@@ -18,6 +18,7 b'' | |||
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | import io |
|
21 | import math | |
|
21 | 22 | import re |
|
22 | 23 | import os |
|
23 | 24 | import datetime |
@@ -196,6 +197,72 b' def write_metadata_if_needed(event):' | |||
|
196 | 197 | pass |
|
197 | 198 | |
|
198 | 199 | |
|
200 | def write_usage_data(event): | |
|
201 | import rhodecode | |
|
202 | from rhodecode.lib import system_info | |
|
203 | from rhodecode.lib import ext_json | |
|
204 | ||
|
205 | settings = event.app.registry.settings | |
|
206 | instance_tag = settings.get('metadata.write_usage_tag') | |
|
207 | if not settings.get('metadata.write_usage'): | |
|
208 | return | |
|
209 | ||
|
210 | def get_update_age(dest_file): | |
|
211 | now = datetime.datetime.utcnow() | |
|
212 | ||
|
213 | with open(dest_file, 'rb') as f: | |
|
214 | data = ext_json.json.loads(f.read()) | |
|
215 | if 'created_on' in data: | |
|
216 | update_date = parse(data['created_on']) | |
|
217 | diff = now - update_date | |
|
218 | return math.ceil(diff.total_seconds() / 60.0) | |
|
219 | ||
|
220 | return 0 | |
|
221 | ||
|
222 | utc_date = datetime.datetime.utcnow() | |
|
223 | hour_quarter = int(math.ceil((utc_date.hour + utc_date.minute/60.0) / 6.)) | |
|
224 | fname = '.rc_usage_{date.year}{date.month:02d}{date.day:02d}_{hour}.json'.format( | |
|
225 | date=utc_date, hour=hour_quarter) | |
|
226 | ini_loc = os.path.dirname(rhodecode.CONFIG.get('__file__')) | |
|
227 | ||
|
228 | usage_dir = os.path.join(ini_loc, '.rcusage') | |
|
229 | if not os.path.isdir(usage_dir): | |
|
230 | os.makedirs(usage_dir) | |
|
231 | usage_metadata_destination = os.path.join(usage_dir, fname) | |
|
232 | ||
|
233 | try: | |
|
234 | age_in_min = get_update_age(usage_metadata_destination) | |
|
235 | except Exception: | |
|
236 | age_in_min = 0 | |
|
237 | ||
|
238 | # write every 6th hour | |
|
239 | if age_in_min and age_in_min < 60 * 6: | |
|
240 | log.debug('Usage file created %s minutes ago, skipping (threashold: %s)...', | |
|
241 | age_in_min, 60 * 6) | |
|
242 | return | |
|
243 | ||
|
244 | def write(dest_file): | |
|
245 | configuration = system_info.SysInfo(system_info.rhodecode_config)()['value'] | |
|
246 | license_token = configuration['config']['license_token'] | |
|
247 | ||
|
248 | metadata = dict( | |
|
249 | desc='Usage data', | |
|
250 | instance_tag=instance_tag, | |
|
251 | license_token=license_token, | |
|
252 | created_on=datetime.datetime.utcnow().isoformat(), | |
|
253 | usage=system_info.SysInfo(system_info.usage_info)()['value'], | |
|
254 | ) | |
|
255 | ||
|
256 | with open(dest_file, 'wb') as f: | |
|
257 | f.write(ext_json.json.dumps(metadata, indent=2, sort_keys=True)) | |
|
258 | ||
|
259 | try: | |
|
260 | log.debug('Writing usage file at: %s', usage_metadata_destination) | |
|
261 | write(usage_metadata_destination) | |
|
262 | except Exception: | |
|
263 | pass | |
|
264 | ||
|
265 | ||
|
199 | 266 | def write_js_routes_if_enabled(event): |
|
200 | 267 | registry = event.app.registry |
|
201 | 268 |
General Comments 0
You need to be logged in to leave comments.
Login now