influxdb: fix numpy scalar bool, uints, str

* use types from the numpy hierarchy because e.g. np.issubtype(np.uint64, int) == False
* streamline influxdb formatting
* tested with influxdb 0.12
This commit is contained in:
Robert Jördens 2016-04-24 15:15:16 +02:00
parent a0d2dab594
commit aee4859982
1 changed files with 11 additions and 20 deletions

View File

@ -64,24 +64,16 @@ def get_argparser():
return parser return parser
def influxdb_str(s):
return '"' + s.replace('"', '\\"') + '"'
def format_influxdb(v): def format_influxdb(v):
if isinstance(v, bool): if np.issubdtype(type(v), np.bool_):
if v: return "bool={}".format(v)
return "bool", "t" if np.issubdtype(type(v), np.integer):
else: return "int={}i".format(v)
return "bool", "f" if np.issubdtype(type(v), np.floating):
elif np.issubdtype(type(v), int): return "float={}".format(v)
return "int", "{}i".format(v) if np.issubdtype(type(v), np.str_):
elif np.issubdtype(type(v), float): return "str=\"{}\"".format(v.replace('"', '\\"'))
return "float", "{}".format(v) return "pyon=\"{}\"".format(pyon.encode(v).replace('"', '\\"'))
elif isinstance(v, str):
return "str", influxdb_str(v)
else:
return "pyon", influxdb_str(pyon.encode(v))
class DBWriter(TaskObject): class DBWriter(TaskObject):
@ -107,9 +99,8 @@ class DBWriter(TaskObject):
url = self.base_url + "/write" url = self.base_url + "/write"
params = {"u": self.user, "p": self.password, "db": self.database, params = {"u": self.user, "p": self.password, "db": self.database,
"precision": "ms"} "precision": "ms"}
fmt_ty, fmt_v = format_influxdb(v) data = "{},dataset={} {} {}".format(
data = "{},dataset={} {}={} {}".format( self.table, k, format_influxdb(v), round(t*1e3))
self.table, k, fmt_ty, fmt_v, round(t*1e3))
try: try:
response = await aiohttp.request( response = await aiohttp.request(
"POST", url, params=params, data=data) "POST", url, params=params, data=data)