def get_values(self, stats_key, start_timestamp=None, end_timestamp=None, date_strings=False): values = [] try: conditions = [MonitoringStats.stats_key == stats_key] if start_timestamp: utc_start_timestamp = convert_timestamp_to_utc(start_timestamp) conditions.append( MonitoringStats.timestamp >= utc_start_timestamp) if end_timestamp: utc_end_timestamp = convert_timestamp_to_utc(end_timestamp) conditions.append( MonitoringStats.timestamp <= utc_end_timestamp) for ms in MonitoringStats.query.filter(and_(*conditions)). \ order_by(MonitoringStats.timestamp.asc()): aware_timestamp = ms.timestamp.replace(tzinfo=tzutc()) values.append((aware_timestamp, ms.stats_value)) except Exception as exx: # pragma: no cover log.error(u"exception {0!r}".format(exx)) log.error(u"could not fetch list of keys") log.debug(u"{0!s}".format(traceback.format_exc())) self.session.rollback() finally: self.session.close() return values
def get_values(stats_key, start_timestamp=None, end_timestamp=None): """ Return a list of sets of (timestamp, value), ordered by timestamps in ascending order :param stats_key: The stats key to query :param start_timestamp: the start of the timespan, inclusive :type start_timestamp: timezone-aware datetime object :param end_timestamp: the end of the timespan, inclusive :type end_timestamp: timezone-aware datetime object :return: list of sets, with timestamps being timezone-aware UTC datetime objects """ values = [] conditions = [MonitoringStats.stats_key == stats_key] if start_timestamp: utc_start_timestamp = convert_timestamp_to_utc(start_timestamp) conditions.append(MonitoringStats.timestamp >= utc_start_timestamp) if end_timestamp: utc_end_timestamp = convert_timestamp_to_utc(end_timestamp) conditions.append(MonitoringStats.timestamp <= utc_end_timestamp) for ms in MonitoringStats.query.filter(and_(*conditions)).\ order_by(MonitoringStats.timestamp.asc()): aware_timestamp = ms.timestamp.replace(tzinfo=tzutc()) values.append((aware_timestamp, ms.stats_value)) return values
def get_values(stats_key, start_timestamp=None, end_timestamp=None, date_strings=False): """ Return a list of sets of (timestamp, value), ordered by timestamps in ascending order :param stats_key: The stats key to query :param start_timestamp: the start of the timespan, inclusive :type start_timestamp: timezone-aware datetime object :param end_timestamp: the end of the timespan, inclusive :type end_timestamp: timezone-aware datetime object :param date_strings: Return dates as strings formatted as AUTH_DATE_FORMAT :return: list of tuples, with timestamps being timezone-aware UTC datetime objects """ values = [] conditions = [MonitoringStats.stats_key == stats_key] if start_timestamp: utc_start_timestamp = convert_timestamp_to_utc(start_timestamp) conditions.append(MonitoringStats.timestamp >= utc_start_timestamp) if end_timestamp: utc_end_timestamp = convert_timestamp_to_utc(end_timestamp) conditions.append(MonitoringStats.timestamp <= utc_end_timestamp) for ms in MonitoringStats.query.filter(and_(*conditions)).\ order_by(MonitoringStats.timestamp.asc()): aware_timestamp = ms.timestamp.replace(tzinfo=tzutc()) if date_strings: aware_timestamp = aware_timestamp.strftime(AUTH_DATE_FORMAT) values.append((aware_timestamp, ms.stats_value)) return values
def get_values(self, stats_key, start_timestamp=None, end_timestamp=None, date_strings=False): values = [] try: conditions = [MonitoringStats.stats_key == stats_key] if start_timestamp: utc_start_timestamp = convert_timestamp_to_utc(start_timestamp) conditions.append(MonitoringStats.timestamp >= utc_start_timestamp) if end_timestamp: utc_end_timestamp = convert_timestamp_to_utc(end_timestamp) conditions.append(MonitoringStats.timestamp <= utc_end_timestamp) for ms in MonitoringStats.query.filter(and_(*conditions)). \ order_by(MonitoringStats.timestamp.asc()): aware_timestamp = ms.timestamp.replace(tzinfo=tzutc()) values.append((aware_timestamp, ms.stats_value)) except Exception as exx: # pragma: no cover log.error(u"exception {0!r}".format(exx)) log.error(u"could not fetch list of keys") log.debug(u"{0!s}".format(traceback.format_exc())) self.session.rollback() finally: self.session.close() return values
def delete_stats(stats_key, start_timestamp=None, end_timestamp=None): """ Delete statistics from a given key. Either delete all occurrences or only in a given time span. :param stats_key: The name of the key to delete :param start_timestamp: The start timestamp. :type start_timestamp: timezone-aware datetime object :param end_timestamp: The end timestamp. :type end_timestamp: timezone-aware datetime object :return: The number of deleted entries """ conditions = [MonitoringStats.stats_key == stats_key] if start_timestamp: utc_start_timestamp = convert_timestamp_to_utc(start_timestamp) conditions.append(MonitoringStats.timestamp >= utc_start_timestamp) if end_timestamp: utc_end_timestamp = convert_timestamp_to_utc(end_timestamp) conditions.append(MonitoringStats.timestamp <= utc_end_timestamp) r = MonitoringStats.query.filter(and_(*conditions)).delete() return r
def delete_stats(stats_key, start_timestamp=None, end_timestamp=None): """ Delete statistics from a given key. Either delete all occurrences or only in a given time span. :param stats_key: The name of the key to delete :param start_timestamp: The start timestamp. :type start_timestamp: timezone-aware datetime object :param end_timestamp: The end timestamp. :type end_timestamp: timezone-aware datetime object :return: The number of deleted entries """ conditions = [MonitoringStats.stats_key == stats_key] if start_timestamp: utc_start_timestamp = convert_timestamp_to_utc(start_timestamp) conditions.append(MonitoringStats.timestamp >= utc_start_timestamp) if end_timestamp: utc_end_timestamp = convert_timestamp_to_utc(end_timestamp) conditions.append(MonitoringStats.timestamp <= utc_end_timestamp) r = MonitoringStats.query.filter(and_(*conditions)).delete() return r
def delete(self, stats_key, start_timestamp, end_timestamp): r = None conditions = [MonitoringStats.stats_key == stats_key] if start_timestamp: utc_start_timestamp = convert_timestamp_to_utc(start_timestamp) conditions.append(MonitoringStats.timestamp >= utc_start_timestamp) if end_timestamp: utc_end_timestamp = convert_timestamp_to_utc(end_timestamp) conditions.append(MonitoringStats.timestamp <= utc_end_timestamp) try: r = self.session.query(MonitoringStats).filter(and_(*conditions)).delete() self.session.commit() except Exception as exx: # pragma: no cover log.error(u"exception {0!r}".format(exx)) log.error(u"could not delete statskeys {0!s}".format(stats_key)) log.debug(u"{0!s}".format(traceback.format_exc())) self.session.rollback() finally: self.session.close() return r
def delete(self, stats_key, start_timestamp, end_timestamp): r = None conditions = [MonitoringStats.stats_key == stats_key] if start_timestamp: utc_start_timestamp = convert_timestamp_to_utc(start_timestamp) conditions.append(MonitoringStats.timestamp >= utc_start_timestamp) if end_timestamp: utc_end_timestamp = convert_timestamp_to_utc(end_timestamp) conditions.append(MonitoringStats.timestamp <= utc_end_timestamp) try: r = self.session.query(MonitoringStats).filter( and_(*conditions)).delete() self.session.commit() except Exception as exx: # pragma: no cover log.error(u"exception {0!r}".format(exx)) log.error(u"could not delete statskeys {0!s}".format(stats_key)) log.debug(u"{0!s}".format(traceback.format_exc())) self.session.rollback() finally: self.session.close() return r
def add_value(self, stats_key, stats_value, timestamp, reset_values=False): utc_timestamp = convert_timestamp_to_utc(timestamp) try: ms = MonitoringStats(utc_timestamp, stats_key, stats_value) self.session.add(ms) self.session.commit() if reset_values: # Successfully saved the new stats entry, so remove old entries self.session.query(MonitoringStats).filter(and_(MonitoringStats.stats_key == stats_key, MonitoringStats.timestamp < utc_timestamp)).delete() self.session.commit() except Exception as exx: # pragma: no cover log.error(u"exception {0!r}".format(exx)) log.error(u"DATA: {0!s} -> {0!s}".format(stats_key, stats_value)) log.debug(u"{0!s}".format(traceback.format_exc())) self.session.rollback() finally: self.session.close()
def add_value(self, stats_key, stats_value, timestamp, reset_values=False): utc_timestamp = convert_timestamp_to_utc(timestamp) try: ms = MonitoringStats(utc_timestamp, stats_key, stats_value) self.session.add(ms) self.session.commit() if reset_values: # Successfully saved the new stats entry, so remove old entries self.session.query(MonitoringStats).filter( and_(MonitoringStats.stats_key == stats_key, MonitoringStats.timestamp < utc_timestamp)).delete() self.session.commit() except Exception as exx: # pragma: no cover log.error(u"exception {0!r}".format(exx)) log.error(u"DATA: {0!s} -> {0!s}".format(stats_key, stats_value)) log.debug(u"{0!s}".format(traceback.format_exc())) self.session.rollback() finally: self.session.close()
def write_stats(stats_key, stats_value, timestamp=None, reset_values=False): """ Write a new statistics value to the database :param stats_key: The key, that identifies the measurement point :type stats_key: basestring :param stats_value: The value to be measured :type stats_value: int :param timestamp: The time, when the value was measured :type timestamp: timezone-aware datetime object :param reset_values: Whether old entries should be deleted :return: id of the database entry """ timestamp = timestamp or datetime.datetime.now(tzlocal()) # Convert timestamp to UTC for database utc_timestamp = convert_timestamp_to_utc(timestamp) MonitoringStats(utc_timestamp, stats_key, stats_value) if reset_values: # Successfully saved the new stats entry, so remove old entries MonitoringStats.query.filter(and_(MonitoringStats.stats_key == stats_key, MonitoringStats.timestamp < utc_timestamp)).delete()
def write_stats(stats_key, stats_value, timestamp=None, reset_values=False): """ Write a new statistics value to the database :param stats_key: The key, that identifies the measurment point :type stats_key: basestring :param stats_value: The value to be measured :type stats_value: int :param timestamp: The time, when the value was measured :type timestamp: timezone-aware datetime object :param reset_values: Whether old entries should be deleted :return: id of the database entry """ timestamp = timestamp or datetime.datetime.now(tzlocal()) # Convert timestamp to UTC for database utc_timestamp = convert_timestamp_to_utc(timestamp) MonitoringStats(utc_timestamp, stats_key, stats_value) if reset_values: # Successfully saved the new stats entry, so remove old entries MonitoringStats.query.filter( and_(MonitoringStats.stats_key == stats_key, MonitoringStats.timestamp < utc_timestamp)).delete()
def test_14_convert_timestamp_to_utc(self): d = datetime.now(tz=gettz('America/New York')) d_utc = convert_timestamp_to_utc(d) self.assertGreater(d_utc, d.replace(tzinfo=None))
def test_14_convert_timestamp_to_utc(self): d = datetime.now(tz=gettz('America/New York')) d_utc = convert_timestamp_to_utc(d) self.assertGreater(d_utc, d.replace(tzinfo=None))