optimization attempt
* init client handler only once and not every time * don't try to create database on every run just on the first one
This commit is contained in:
parent
38dba3845f
commit
d0fb756e44
31
influx.py
31
influx.py
|
@ -9,9 +9,10 @@ from influxdb import InfluxDBClient
|
|||
from ejabberdrpc import EjabberdMetrics
|
||||
|
||||
|
||||
class Influx():
|
||||
def __init__(self, metrics):
|
||||
class Influx:
|
||||
def __init__(self, metrics, client):
|
||||
self._metrics = metrics
|
||||
self.client = client
|
||||
|
||||
@staticmethod
|
||||
def _timestamp():
|
||||
|
@ -71,27 +72,25 @@ class Influx():
|
|||
for k, v in ipv.items():
|
||||
data.append(self._parse("ejabberd_online_client_ipversion", k, v, cur_ts, {"vhost": vhost, "node": node, "ipversion": k, "client": cl}))
|
||||
|
||||
# output
|
||||
client = InfluxDBClient(host='localhost', port=8086)
|
||||
|
||||
# test code
|
||||
client.create_database('writetest')
|
||||
client_write_start_time = time.perf_counter()
|
||||
client.write_points(data, database='writetest', time_precision='ms', batch_size=10000, protocol='line')
|
||||
|
||||
# test code
|
||||
client_write_end_time = time.perf_counter()
|
||||
print("Client Library Write: {time}s".format(time=client_write_end_time - client_write_start_time))
|
||||
|
||||
# write output to database
|
||||
self.client.write_points(data, database='custom', time_precision='ms', batch_size=10000, protocol='line')
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("config.json", "r", encoding="utf-8") as f:
|
||||
login = json.load(f)
|
||||
|
||||
metrics = EjabberdMetrics("http://[::1]:4560", login)
|
||||
influx = Influx(metrics)
|
||||
# init global handler
|
||||
metrics = EjabberdMetrics("http://localhost:4560", login)
|
||||
client = InfluxDBClient(host='localhost', port=8086, database='custom')
|
||||
|
||||
# create database only once
|
||||
client.create_database('custom')
|
||||
|
||||
# init influx class
|
||||
influx = Influx(metrics, client)
|
||||
|
||||
while True:
|
||||
# TODO this will fail when the connection drops try except maybe?
|
||||
metrics.update()
|
||||
influx.writeMetrics()
|
||||
|
||||
|
|
Loading…
Reference in New Issue