class OpenTSDB(BaseDB): """ OpenTSDB class herited from BaseDB Allow to handle an OpenTSDB database to save PowerReport. """ def __init__(self, report_type: Type[Report], host: str, port, metric_name: str): """ :param host: host of the OpenTSDB server :param port: port of the OpenTSDB server :param metric_name: mectric name to store :param report_type: type of report handled by this database """ BaseDB.__init__(self, report_type) self.host = host self.port = port self.metric_name = metric_name self.client = None def __iter__(self): raise NotImplementedError() def connect(self): """ Override from BaseDB. Create the connection to the openTSDB database with the current configuration (hostname/port), then check if the connection has been created without failure. """ # close connection if reload if self.client is not None: self.client.close() self.client.wait() self.client = TSDBClient(host=self.host, port=self.port) if not self.client.is_connected() and not self.client.is_alive(): raise CantConnectToOpenTSDBException('connexion error') def save(self, report: PowerReport): """ Override from BaseDB :param report: Report to save """ self.client.send(self.metric_name, report.power, timestamp=int(report.timestamp.timestamp()), host=report.target) def save_many(self, reports: List[Report]): """ Save a batch of data :param reports: Batch of data. """ for report in reports: self.save(report)
import time from opentsdb import TSDBClient import potsdb tsdb = TSDBClient('0.0.0.0') ''' tsdb.send('metric.test4', 'fwe', t1='v1') time.sleep(2) tsdb.send('metric.test4', 'fwe', t1='v1') time.sleep(2) tsdb.send('metric.test4', 'fwe', t1='v1') time.sleep(2) tsdb.send('metric.test4', 22, t1='v1') time.sleep(2) tsdb.send('metric.test4', 22, t1='v1') time.sleep(2) tsdb.send('metric.test4', 22, t1='v1') time.sleep(2) tsdb.send('metric.test4', 202, t1='v1') time.sleep(2) tsdb.send('metric.test4', 23, t1='v1') time.sleep(2) tsdb.send('metric.test4', 24, t1='v1') time.sleep(2) tsdb.send('metric.test4', 25, t1='v1') time.sleep(2) tsdb.send('metric.test4', 6, t1='v1') time.sleep(2) tsdb.send('metric.test4', 206, t1='v1') time.sleep(2) tsdb.send('metric.test4', 20, t1='v1')