def __init__(self, start, end): """ Initialise the object with the start and end times :param start: The start time :param end: The end time """ self._start = get_timedelta(start) self._end = get_timedelta(end) self._validate()
def from_request(cls, request, redis_client): """ Generator. HTTP query can create many KQueries. """ for metric in request.get('metrics', []): new = cls(redis_client) new.query = metric # This seemingly terrifying kludge makes it easy to merge chunks of MTS together. # For all aggregators, we remove align_sampling and add align_start_time. # Otherwise, partial windows (if aggregated) will appear on every seam in the data. Info: # https://kairosdb.github.io/docs/build/html/restapi/QueryMetrics.html#metric-properties for agg_ndx in xrange(len(new.query.get('aggregators', []))): # Derive a maximum window size so we can merge more intelligently later. sampling = new.query['aggregators'][agg_ndx].get('sampling') if sampling: window_size = get_timedelta(sampling) if not new.window_size: new.window_size = window_size elif window_size > new.window_size: new.window_size = window_size if 'align_sampling' in new.query['aggregators'][agg_ndx]: del new.query['aggregators'][agg_ndx]['align_sampling'] new.query['aggregators'][agg_ndx]['align_start_time'] = True yield new
def from_request(cls, request, redis_client): """ Generator. HTTP query can create many KQueries. """ for metric in request.get('metrics', []): new = cls(redis_client) new.query = metric # This seemingly terrifying kludge makes it easy to merge chunks of MTS together. # For all aggregators, we remove align_sampling and add align_start_time. # Otherwise, partial windows (if aggregated) will appear on every seam in the data. Info: # https://kairosdb.github.io/docs/build/html/restapi/QueryMetrics.html#metric-properties for agg_ndx in xrange(len(new.query.get('aggregators', []))): # Derive a maximum window size so we can merge more intelligently later. sampling = new.query['aggregators'][agg_ndx].get('sampling') if sampling: window_size = get_timedelta(sampling) if not new.window_size: new.window_size = window_size elif window_size > new.window_size: new.window_size = window_size if 'align_sampling' in new.query['aggregators'][agg_ndx]: del new.query['aggregators'][agg_ndx]['align_sampling'] new.query['aggregators'][agg_ndx][ 'align_start_time'] = True yield new
def filter_by_date( model, date_obj, start_date, end_date, duration): """Creates django's filters for given date or interval :param model: obj where filter will be applied :type model: str. :param date_obj: database table where the requested data is :type date_obj: str. :param start_date: string start_date :type start_date: str. :param end_date: string end_date :type end_date: str. :param duration: Time interval in days. :type duration: str. :returns: dict -- A dictionary with django's query filters """ dic_filter = {} obj = utils.get_field_instance(model, date_obj) if obj: DATE_PATTERN = "%m/%d/%Y %H:%M:%S" tz = tzlocal() start_date_filter = {} end_date_filter = {} # Apply default case, query from yesterday to now if not start_date: yesterday = timezone.now() - timezone.timedelta(1) start_date = yesterday.strftime(DATE_PATTERN) if not end_date: end_date = "now" start_date = parser.parse(start_date).replace(tzinfo=tz) if end_date.lower() == 'now': end_date = timezone.now() else: end_date = parser.parse(end_date).replace(tzinfo=tz) # handle duration case if duration: start_date -= utils.get_timedelta(duration) # handle different field instances: timestamp, datetime if isinstance(obj, DateTimeField): start_date = start_date end_date = end_date elif isinstance(obj, DecimalField): start_date = utils.date_totimestamp( start_date.astimezone(pytz.utc)) end_date = utils.date_totimestamp(end_date.astimezone(pytz.utc)) start_date_filter[date_obj + "__gte"] = start_date end_date_filter[date_obj + "__lte"] = end_date dic_filter.update(start_date_filter) dic_filter.update(end_date_filter) return dic_filter
def end(self, value): self._end = get_timedelta(value) self._validate()
def start(self, value): self._start = get_timedelta(value) self._validate()