Add option to emit booleans as numeric values

New command line option, -N or --numeric, that will cause all boolean values, including those in sequences/arrays, to be written as 1 or 0 instead of True or False.

Per request in issue 26.

WARNING: Use or non-use of this option with the database output scripts will change the schema of the data. sqlite doesn't care about that, because it stores booleans as integers, anyway, but InfluxDB will trip an error if you try to record data points with this option to a database that has data point recorded without it, or vice versa.
This commit is contained in:
sparky8512 2021-05-23 18:45:18 -07:00
parent 23b54c1344
commit d603272d90

View file

@ -50,6 +50,10 @@ def create_arg_parser(output_description, bulk_history=True):
help="host:port of dish to query, default is the standard IP address " help="host:port of dish to query, default is the standard IP address "
"and port (192.168.100.1:9200)") "and port (192.168.100.1:9200)")
group.add_argument("-h", "--help", action="help", help="Be helpful") group.add_argument("-h", "--help", action="help", help="Be helpful")
group.add_argument("-N",
"--numeric",
action="store_true",
help="Record boolean values as 1 and 0 instead of True and False")
group.add_argument("-t", group.add_argument("-t",
"--loop-interval", "--loop-interval",
type=float, type=float,
@ -201,7 +205,7 @@ def get_data(opts, gstate, add_item, add_sequence, add_bulk=None):
return rc return rc
def add_data(data, category, add_item, add_sequence): def add_data_normal(data, category, add_item, add_sequence):
for key, val in data.items(): for key, val in data.items():
name, start, seq = BRACKETS_RE.match(key).group(1, 4, 5) name, start, seq = BRACKETS_RE.match(key).group(1, 4, 5)
if seq is None: if seq is None:
@ -210,6 +214,18 @@ def add_data(data, category, add_item, add_sequence):
add_sequence(name, val, category, int(start) if start else 0) add_sequence(name, val, category, int(start) if start else 0)
def add_data_numeric(data, category, add_item, add_sequence):
for key, val in data.items():
name, start, seq = BRACKETS_RE.match(key).group(1, 4, 5)
if seq is None:
add_item(name, int(val) if isinstance(val, int) else val, category)
else:
add_sequence(name,
[int(subval) if isinstance(subval, int) else subval for subval in val],
category,
int(start) if start else 0)
def get_status_data(opts, gstate, add_item, add_sequence): def get_status_data(opts, gstate, add_item, add_sequence):
if opts.satus_mode: if opts.satus_mode:
try: try:
@ -229,6 +245,7 @@ def get_status_data(opts, gstate, add_item, add_sequence):
if opts.need_id: if opts.need_id:
gstate.dish_id = status_data["id"] gstate.dish_id = status_data["id"]
del status_data["id"] del status_data["id"]
add_data = add_data_numeric if opts.numeric else add_data_normal
if "status" in opts.mode: if "status" in opts.mode:
add_data(status_data, "status", add_item, add_sequence) add_data(status_data, "status", add_item, add_sequence)
if "obstruction_detail" in opts.mode: if "obstruction_detail" in opts.mode:
@ -281,6 +298,7 @@ def get_history_stats(opts, gstate, add_item, add_sequence):
verbose=opts.verbose, verbose=opts.verbose,
history=history) history=history)
general, ping, runlen, latency, loaded, usage = groups[0:6] general, ping, runlen, latency, loaded, usage = groups[0:6]
add_data = add_data_numeric if opts.numeric else add_data_normal
add_data(general, "ping_stats", add_item, add_sequence) add_data(general, "ping_stats", add_item, add_sequence)
if "ping_drop" in opts.mode: if "ping_drop" in opts.mode:
add_data(ping, "ping_stats", add_item, add_sequence) add_data(ping, "ping_stats", add_item, add_sequence)
@ -334,6 +352,13 @@ def get_bulk_data(opts, gstate, add_bulk):
new_counter, datetime.fromtimestamp(timestamp, tz=timezone.utc))) new_counter, datetime.fromtimestamp(timestamp, tz=timezone.utc)))
timestamp -= parsed_samples timestamp -= parsed_samples
if opts.numeric:
add_bulk(
{
k: [int(subv) if isinstance(subv, int) else subv for subv in v]
for k, v in bulk.items()
}, parsed_samples, timestamp, new_counter - parsed_samples)
else:
add_bulk(bulk, parsed_samples, timestamp, new_counter - parsed_samples) add_bulk(bulk, parsed_samples, timestamp, new_counter - parsed_samples)
gstate.counter = new_counter gstate.counter = new_counter