def convert_date_range(self, from_date, to_date, period): """Convert str from_date and to_data objects to datetime object :param from_date: from date string :param to_date: to date string :return: tuple (from_date, to_date) """ from_date = timestamp_to_datetime(from_date, DATE_FILTER_FORMAT) if from_date else from_date to_date = timestamp_to_datetime(to_date, DATE_FILTER_FORMAT) if to_date else to_date return date_min(from_date, period), date_max(to_date, period)
def convert_date_range(self, from_date, to_date, period): """Convert str from_date and to_data objects to datetime object :param from_date: from date string :param to_date: to date string :return: tuple (from_date, to_date) """ from_date = timestamp_to_datetime( from_date, DATE_FILTER_FORMAT) if from_date else from_date to_date = timestamp_to_datetime( to_date, DATE_FILTER_FORMAT) if to_date else to_date return date_min(from_date, period), date_max(to_date, period)
def incr(self, project, name, timestamp, value=1, filters={}, callback=None, **kwargs): """Add value to metric counter :param project: project name :param name: metric name :param timestamp: timestamp name :param value: increment value :param filters: dict of filters :param \*\*kwargs: additional kwargs """ timestamp = timestamp_to_datetime(timestamp) if filters: filters = dict(filters) for period in self._application.config['PERIODS']: if filters: for fname, fvalue in filters.items(): self.save_value(project, name, period, get_by_period(date_min(timestamp, period), period), fname, fvalue, value) self.save_value(project, name, period, get_by_period(date_min(timestamp, period), period), None, None, value) self.save_metric_meta(project, name, filters) self.update_stats('incr') if callback: callback(True)
def incr(self, project, name, timestamp, value=1, filters={}, callback=None, **kwargs): """Make incr in redis hash :param project: project name :param name: metric name :param timestamp: timstamp object :param value: increment value :param filters: dict of filters """ pipe = self.client.pipeline(transactional=True) pipe.select(self.selected_db) timestamp = timestamp_to_datetime(timestamp) if filters: filters = dict(filters) for period in self._application.config['PERIODS']: if filters: for fname, fvalue in filters.iteritems(): if not isinstance(fvalue, (list, tuple)): # Make incr for all values fvalue = [fvalue] for fvalue_item in fvalue: pipe.hincrby(self.make_key( project, name, period, {fname: fvalue_item}), get_by_period(date_min(timestamp, period), period), value) pipe.hincrby(self.make_key(project, name, period), get_by_period(date_min(timestamp, period), period), value) self.save_metric_meta(pipe, project, name, filters) res = (yield gen.Task(pipe.execute)) self.update_stats('incr') if callback: callback(res)
def incr(self, project, name, timestamp, value=1, filters={}, callback=None, **kwargs): """Add value to metric counter :param project: project name :param name: metric name :param timestamp: timestamp name :param value: increment value :param filters: dict of filters :param \*\*kwargs: additional kwargs """ timestamp = timestamp_to_datetime(timestamp) if filters: filters = dict(filters) for period in self._application.config['PERIODS']: if filters: for fname, fvalue in list(filters.items()): self.save_value( project, name, period, get_by_period(date_min(timestamp, period), period), fname, fvalue, value) self.save_value(project, name, period, get_by_period(date_min(timestamp, period), period), None, None, value) self.save_metric_meta(project, name, filters) self.update_stats('incr') if callback: callback(True)
def test_timestamp_to_datetime(self): d = datetime(2012, 11, 1, 3, 4, 5) timestamp = d.strftime("%Y-%m-%dT%H:%M:%S") self.assertEquals(d, timestamp_to_datetime(timestamp))