From 57ac5c8bdaa7cd743e76cc432fde3db7e3b816d6 Mon Sep 17 00:00:00 2001 From: "Eugeniy E. Mikhailov" Date: Sun, 26 Dec 2021 15:22:28 -0500 Subject: cleaner database commit --- qolab/hardware/basic.py | 4 ++++ qolab/tsdb/__init__.py | 35 ++++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 13 deletions(-) (limited to 'qolab') diff --git a/qolab/hardware/basic.py b/qolab/hardware/basic.py index 0f67cf9..147e8dc 100644 --- a/qolab/hardware/basic.py +++ b/qolab/hardware/basic.py @@ -1,5 +1,6 @@ import yaml from qolab.file_utils import get_next_data_file +from qolab.tsdb import tsdb_append_metric_for_class_setter_or_getter class BasicInstrument: def __init__(self, tsdb_ingester=None): @@ -15,6 +16,9 @@ class BasicInstrument: # self.deviceProperties = ['SampleRate', 'TimePerDiv', 'TrigDelay', ]; self.deviceProperties = []; + def tsdb_append(f): + return tsdb_append_metric_for_class_setter_or_getter()(f) + def getConfig(self): config = self.config.copy() dconfig = {} diff --git a/qolab/tsdb/__init__.py b/qolab/tsdb/__init__.py index b75de0c..7b3200f 100644 --- a/qolab/tsdb/__init__.py +++ b/qolab/tsdb/__init__.py @@ -1,5 +1,6 @@ import logging import universal_tsdb as utsdb +import functools import time logging.basicConfig(format='%(asctime)s %(levelname)8s %(name)s: %(message)s', datefmt='%m/%d/%Y %H:%M:%S') @@ -21,11 +22,12 @@ class Ingester(utsdb.Ingester): qolab_measurement = '.'.join((self.measurement_prefix, measurement)) # space is illegal for measurements fields qolab_measurement=qolab_measurement.replace(' ', '-') - print(qolab_measurement) - return super().append(timestamp=None, tags=tags, measurement=qolab_measurement, **kwargs) + logger.info(f'{qolab_measurement=} {tags=}, {kwargs=}') + return super().append(timestamp=timestamp, tags=tags, measurement=qolab_measurement, **kwargs) def tsdb_append_metric_for_class_setter_or_getter(tsdb_logger=None): def wrap(f): + @functools.wraps(f) def wrapper(*args, **kwds): if f.__name__[0:3] != 'get' and f.__name__[0:3] != 'set': logger.warning(f'Do not know how to work with {f.__name__}, it is neither set... or get...') @@ -48,14 +50,14 @@ def tsdb_append_metric_for_class_setter_or_getter(tsdb_logger=None): ts = time.time() ret = f(*args, **kwds) - logger.info(f'function {f.__name__} {action} {var_name} = {val}') + logger.debug(f'function {f.__name__} {action} {var_name} = {val}') ts_ms = int(ts*1000) fields={var_name: val} try: - tsdb_logger.append(ts_ms, measurement=device_type, tags={'action': action}, **fields) + if cls.tsdb_ingester is not None: + cls.tsdb_ingester.append(ts_ms, measurement=device_type, tags={'action': action}, **fields) except ValueError as err: logger.error(f'{err=} in function {f.__name__}: {var_name} = {val}') - ts_ms = int(ts*1000) return ret return wrapper return wrap @@ -63,10 +65,10 @@ def tsdb_append_metric_for_class_setter_or_getter(tsdb_logger=None): if __name__ == '__main__': from qolab.hardware.basic import BasicInstrument tsdb_client = utsdb.Client('influx', 'http://localhost:8428', database='qolab') - tsdb_logger = Ingester(tsdb_client, batch=10, measurement_prefix='VAMPIRE.HighPower') + tsdb_ingester = Ingester(tsdb_client, batch=10, measurement_prefix='VAMPIRE.HighPower') class InstWithLog(BasicInstrument): - def __init__(self): - super().__init__() + def __init__(self, *args, **kwds): + super().__init__(*args, **kwds) self.config['Device type'] = 'TestTSDBLogger' self.config['Device model'] = 'v01' self.config['FnamePrefix'] = 'test_log' @@ -74,16 +76,23 @@ if __name__ == '__main__': self.deviceProperties = ['D']; self.d = 13.45 - @tsdb_append_metric_for_class_setter_or_getter(tsdb_logger) + + # @tsdb_append_metric_for_class_setter_or_getter(tsdb_logger) + # @tsdb_append("dummy") + @BasicInstrument.tsdb_append def setD(self, val): self.d=val - @tsdb_append_metric_for_class_setter_or_getter(tsdb_logger) + # @tsdb_append_metric_for_class_setter_or_getter(tsdb_logger) + # @decor + # @tsdb_append + @BasicInstrument.tsdb_append def getD(self): + """ get D variable """ return self.d - dev = InstWithLog() + dev = InstWithLog(tsdb_ingester=tsdb_ingester) dev.getD() - dev.setD(21) + dev.setD(3) - tsdb_logger.commit() + tsdb_ingester.commit() -- cgit v1.2.3