88#
99"""Send metric to influxdb
1010"""
11- import os
12- import dateutil .parser
1311import h5py
14- from datetime import datetime
15- from pprint import pprint
12+ from datetime import datetime , timedelta
1613from influxdb_client import InfluxDBClient , Point , WritePrecision , WriteOptions
1714from influxdb_client .client .write_api import SYNCHRONOUS
1815from influxdb_client .client .exceptions import InfluxDBError
@@ -30,30 +27,7 @@ def retry(self, conf: (str, str, str), data: str, exception: InfluxDBError):
3027 print (f"Retryable error occurs for batch: { conf } retry: { exception } - number of data points: { len (data )} " )
3128
3229
33- def get_last (ev , group ):
34- """Get last metric timestamp from DB
35- :param ev: Configuration options list
36- :param client: InfluxDB client
37- :param group: project
38- :return (datetime) last timestamp in seconds to epoc
39- """
40- # qry_str = 'SELECT last(vcpus) FROM cloud_acc WHERE project=$proj'
41- # bind_params = {'proj': group}
42- # last_ts = client.query(qry_str, bind_params=bind_params)
43- # if last_ts:
44- # time_stamp = last_ts.get_points()
45- # for t in time_stamp:
46- # a = dateutil.parser.parse(t["time"])
47- # ti = oaf.to_secepoc(a)
48-
49- # dt_ini = datetime(2026, 3, 30, 13, 0, 0)
50-
51- ti = ev ['secepoc_ini' ]
52- return ti
53-
54-
55- if __name__ == '__main__' :
56- ev = oaf .get_conf ()
30+ def write_influx (ev , lpts ):
5731 dbhost = ev ['dbhost' ]
5832 dbport = ev ['dbport' ]
5933 dbtoken = ev ['dbtoken' ]
@@ -69,9 +43,21 @@ def get_last(ev, group):
6943 max_retry_delay = 30_000 ,
7044 exponential_base = 2 )
7145
46+ with InfluxDBClient (url = baseurl , token = dbtoken , org = dborg , ssl = bssl , verify_ssl = bverify_ssl ) as client :
47+ bcb = BatchingCallback ()
48+ with client .write_api (success_callback = bcb .success , error_callback = bcb .error , retry_callback = bcb .retry ) as write_api :
49+ write_api .write (bucket = "openstack" , record = lpts , write_precision = WritePrecision .S )
50+
51+
52+ if __name__ == '__main__' :
53+ ev = oaf .get_conf ()
7254 filename = oaf .get_hdf_filename (ev )
73- print (80 * '=' )
74- print ('Filename:' , filename )
55+ # ti = ev['secepoc_ini']
56+ # dt_ini = datetime(2026, 3, 31, 3, 0, 0)
57+ dt_ini = datetime .today () - timedelta (hours = 2 )
58+ ti = oaf .to_secepoc (dt_ini )
59+ print (80 * '_' )
60+ print (f'Filename: { filename } - From date { dt_ini } - Seconds to epoch: { ti } ' )
7561 with h5py .File (filename , 'r' ) as f :
7662 tf = f .attrs ['LastRun' ]
7763 ts = f ['date' ][:]
@@ -83,7 +69,6 @@ def get_last(ev, group):
8369 if group == "date" :
8470 continue
8571
86- ti = get_last (ev , group )
8772 idx_start = oaf .time2index (ev , ti , ts )
8873 idx_end = oaf .time2index (ev , tf , ts )
8974 print (20 * '+' )
@@ -107,7 +92,4 @@ def get_last(ev, group):
10792
10893 lpoints .append (point )
10994
110- with InfluxDBClient (url = baseurl , token = dbtoken , org = dborg , ssl = bssl , verify_ssl = bverify_ssl ) as client :
111- bcb = BatchingCallback ()
112- with client .write_api (success_callback = bcb .success , error_callback = bcb .error , retry_callback = bcb .retry ) as write_api :
113- write_api .write (bucket = "openstack" , record = lpoints , write_precision = WritePrecision .S )
95+ write_influx (ev , lpoints )
0 commit comments