From 7f1800fb552fa2d746b9b1d7ecc6df2892c2707a Mon Sep 17 00:00:00 2001 From: Matthew Stidham Date: Wed, 14 Apr 2021 11:48:18 -0700 Subject: [PATCH] create csv_to_influx.py Signed-off-by: Matthew Stidham --- py-scripts/csv_to_influx.py | 133 ++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 py-scripts/csv_to_influx.py diff --git a/py-scripts/csv_to_influx.py b/py-scripts/csv_to_influx.py new file mode 100644 index 00000000..94d0bfce --- /dev/null +++ b/py-scripts/csv_to_influx.py @@ -0,0 +1,133 @@ +#!/usr/bin/env python3 + +#Copies the data from a CSV file from the KPI file generated from a Wifi Capacity test to an Influx database + +# The CSV requires three columns in order to work: Date, test details, and numeric-score. + +# Date is a unix timestamp, test details is the variable each datapoint is measuring, and numeric-score is the value for that timepoint and variable. + +import sys +import os +from pprint import pprint + +if sys.version_info[0] != 3: + print("This script requires Python 3") + exit(1) + +if 'py-json' not in sys.path: + sys.path.append(os.path.join(os.path.abspath('..'), 'py-json')) + +import argparse +from realm import Realm +import pandas as pd + + +class CSVtoInflux(Realm): + def __init__(self, + lfclient_host="localhost", + lfclient_port=8080, + debug=False, + _exit_on_error=False, + _exit_on_fail=False, + _proxy_str=None, + _capture_signal_list=[], + influxdb=None, + df=None): + super().__init__(lfclient_host=lfclient_host, + lfclient_port=lfclient_port, + debug_=debug, + _exit_on_error=_exit_on_error, + _exit_on_fail=_exit_on_fail, + _proxy_str=_proxy_str, + _capture_signal_list=_capture_signal_list) + self.influxdb = influxdb + self.df = df + + # Submit data to the influx db if configured to do so. + def post_to_influx(self, kpi): + dates = list(set(kpi['Date'])) + for date in dates: + kpi2 = self.df[self.df['Date'] == date][['Date', 'test details', 'numeric-score']] + metrics = list(set(kpi2['test details'])) + targets = dict() + for k in metrics: + targets[k] = [*kpi2[kpi2['test details'] == k]['numeric-score']][0] + targets[k.replace(' ', '-').lower()] = targets.pop(k) + targets + tags = dict() + tags['script'] = 'WiFi Capacity' + for k in targets.keys(): + self.influxdb.post_to_influx(k, targets[k], tags, date) + + +def main(): + lfjson_host = "localhost" + lfjson_port = 8080 + endp_types = "lf_udp" + debug = False + + parser = argparse.ArgumentParser( + prog='test_l3_longevity.py', + # formatter_class=argparse.RawDescriptionHelpFormatter, + formatter_class=argparse.RawTextHelpFormatter, + epilog=''' + ''', + + description='''\ +csv_to_influx.py: +-------------------- + +Summary : +---------- +Copies the data from a CSV file generated by a wifi capacity test to an influx database. + +Column names are designed for the KPI file generated by our Wifi Capacity Test. + +A user can of course change the column names to match these in order to input any csv file. + +The CSV file needs to have the following columns: + --date - which is a UNIX timestamp + --test details - which is the variable being measured by the test + --numeric-score - which is the value for that variable at that point in time. + +Generic command layout: +----------------------- +python .\\csv_to_influx.py + + +Command: +python3 csv_to_influx.py --influx_host localhost --influx_org Candela --influx_token random_token --influx_bucket lanforge + --target_csv kpi.csv + + + ''') + + parser.add_argument('--influx_host', help='Hostname for the Influx database') + parser.add_argument('--influx_port', help='IP Port for the Influx database', default=8086) + parser.add_argument('--influx_org', help='Organization for the Influx database') + parser.add_argument('--influx_token', help='Token for the Influx database') + parser.add_argument('--influx_bucket', help='Name of the Influx bucket') + parser.add_argument('--target_csv', help='CSV file to record to influx database', required=True) + + args = parser.parse_args() + + influxdb = None + if args.influx_bucket is not None: + from influx2 import RecordInflux + influxdb = RecordInflux(_lfjson_host=lfjson_host, + _lfjson_port=lfjson_port, + _influx_host=args.influx_host, + _influx_port=args.influx_port, + _influx_org=args.influx_org, + _influx_token=args.influx_token, + _influx_bucket=args.influx_bucket) + df = pd.read_csv(args.target_csv) + + csvtoinflux = CSVtoInflux(args=args, + influxdb=influxdb, + df=df) + csvtoinflux.record_kpi() + + +if __name__ == "__main__": + main()