def enable_precompute(panel): """Schedule a precompute task for `panel`""" use_metis = panel['data_source']['source_type'] == 'querybuilder' if use_metis: query = panel['data_source']['query'] else: query = "u'''%s'''" % panel['data_source']['code'] precompute = panel['data_source']['precompute'] timeframe = panel['data_source']['timeframe'] bucket_width = precompute['bucket_width']['value'] time_scale = precompute['bucket_width']['scale']['name'] bucket_width_seconds = get_seconds(bucket_width, time_scale) if timeframe['mode']['value'] == 'recent': untrusted_time = precompute['untrusted_time']['value'] untrusted_time_scale = precompute['untrusted_time']['scale']['name'] untrusted_time_seconds = get_seconds(untrusted_time, untrusted_time_scale) # Schedule the task with an interval equal to the bucket_width interval = bucket_width_seconds elif timeframe['mode']['value'] == 'range': untrusted_time_seconds = 0 # Schedule the task with an interval of 0 so it only runs once interval = 0 task_code = PRECOMPUTE_INITIALIZATION_CODE % (query, timeframe, bucket_width_seconds, untrusted_time_seconds, use_metis) result = scheduler_client.schedule(task_code, interval) if result['status'] != 'success': raise RuntimeError(result.get('reason')) return result['id']
def callsource(id=None): request_body = request.get_json() code = request_body.get('code') query = request_body.get('query') metis = request_body.get('source_type') == 'querybuilder' precompute = request_body.get('precompute') timeframe = request_body.get('timeframe') if precompute['enabled']: bucket_width = get_seconds(precompute['bucket_width']['value'], precompute['bucket_width']['scale']['name']) else: bucket_width = None if metis: code = query task = QueryCompute(code, timeframe, bucket_width=bucket_width, metis=metis) events = task.compute(use_cache=precompute['enabled']) response = {} response['events'] = events return response
def _get_timeframe_bounds(self, timeframe, bucket_width): """ Get a `bucket_width` aligned `start_time` and `end_time` from a `timeframe` dict """ if bucket_width: bucket_width_seconds = bucket_width bucket_width = epoch_time_to_kronos_time(bucket_width) # TODO(derek): Potential optimization by setting the end_time equal to the # untrusted_time if end_time > untrusted_time and the results are not being # output to the user (only for caching) if timeframe['mode']['value'] == 'recent': # Set end_time equal to now and align to bucket width end_time = datetime_to_kronos_time(datetime.datetime.now()) original_end_time = end_time duration = get_seconds(timeframe['value'], timeframe['scale']['name']) duration = epoch_time_to_kronos_time(duration) start_time = original_end_time - duration if bucket_width: # Align values to the bucket width # TODO(derek): Warn the user that the timeframe has been altered to fit # the bucket width if (end_time % bucket_width) != 0: end_time += bucket_width - (end_time % bucket_width) if (start_time % bucket_width) != 0: start_time -= (start_time % bucket_width) start = kronos_time_to_datetime(start_time) end = kronos_time_to_datetime(end_time) elif timeframe['mode']['value'] == 'range': end = datetime.datetime.strptime(timeframe['to'], DT_FORMAT) end_seconds = datetime_to_epoch_time(end) start = datetime.datetime.strptime(timeframe['from'], DT_FORMAT) start_seconds = datetime_to_epoch_time(start) if bucket_width: # Align values to the bucket width # TODO(derek): Warn the user that the timeframe has been altered to fit # the bucket width start_bump = start_seconds % bucket_width_seconds start -= datetime.timedelta(seconds=start_bump) if (end_seconds % bucket_width_seconds) != 0: end_bump = bucket_width_seconds - (end_seconds % bucket_width_seconds) end += datetime.timedelta(seconds=end_bump) else: raise ValueError("Timeframe mode must be 'recent' or 'range'") return start, end
def callsource(id=None): request_body = request.get_json() code = request_body.get('code') precompute = request_body.get('precompute') timeframe = request_body.get('timeframe') bucket_width = get_seconds(precompute['bucket_width']['value'], precompute['bucket_width']['scale']) task = QueryCompute(code, timeframe, bucket_width=bucket_width) events = task.compute(use_cache=precompute['enabled']) response = {} response['events'] = events return response