From 73a9c2814acb124229f97fa2a52f89f8b523442f Mon Sep 17 00:00:00 2001 From: areski Date: Thu, 16 Oct 2014 16:13:20 +0200 Subject: [PATCH 1/3] add extra tutorial for timeseries --- examples/tutorial.py | 6 ++-- examples/tutorial_timeseries.py | 64 +++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 examples/tutorial_timeseries.py diff --git a/examples/tutorial.py b/examples/tutorial.py index 51abaee3..442d2242 100644 --- a/examples/tutorial.py +++ b/examples/tutorial.py @@ -60,8 +60,10 @@ def main(host='localhost', port=8086): def parse_args(): parser = argparse.ArgumentParser( description='example code to play with InfluxDB') - parser.add_argument('--host', type=str, required=True) - parser.add_argument('--port', type=int, required=True) + parser.add_argument('--host', type=str, required=False, default='localhost', + help='hostname of InfluxDB http API') + parser.add_argument('--port', type=int, required=False, default=8086, + help='port of InfluxDB http API') return parser.parse_args() diff --git a/examples/tutorial_timeseries.py b/examples/tutorial_timeseries.py new file mode 100644 index 00000000..2e9bba15 --- /dev/null +++ b/examples/tutorial_timeseries.py @@ -0,0 +1,64 @@ +import argparse + +from influxdb import InfluxDBClient +import datetime +import random + + +USER = 'root' +PASSWORD = 'root' +DBNAME = 'tutorial' + + +def main(host='localhost', port=8086, nb_day=15): + + nb_day = 15 # number of day to generate time series + timeinterval_min = 5 # create an event every x minutes + total_minutes = 1440 * nb_day + total_records = int(total_minutes / timeinterval_min) + now = datetime.datetime.today() + cpu_series = [{ + 'name': "server_data.cpu_idle", + 'columns': ["time", "value", "hostName"], + 'points': [] + }] + + for i in range(0, total_records): + past_date = now - datetime.timedelta(minutes=i * timeinterval_min) + value = random.randint(0, 200) + hostName = "server-%d" % random.randint(1, 5) + pointValues = [int(past_date.strftime('%s')), value, hostName] + cpu_series[0]['points'].append(pointValues) + + client = InfluxDBClient(host, port, USER, PASSWORD, DBNAME) + + print("Create database: " + DBNAME) + client.create_database(DBNAME) + + print("Write points #: {0}".format(total_records)) + client.write_points(cpu_series) + + query = 'SELECT MEAN(value) FROM server_data.cpu_idle GROUP BY time(30m) WHERE time > now() - 1d;' + print("Queying data: " + query) + result = client.query(query) + print("Result: {0}".format(result)) + + print("Delete database: " + DBNAME) + client.delete_database(DBNAME) + + +def parse_args(): + parser = argparse.ArgumentParser( + description='example code to play with InfluxDB') + parser.add_argument('--host', type=str, required=False, default='localhost', + help='hostname influxdb http API') + parser.add_argument('--port', type=int, required=False, default=8086, + help='port influxdb http API') + parser.add_argument('--nb_day', type=int, required=False, default=15, + help='number of days to generate time series data') + return parser.parse_args() + + +if __name__ == '__main__': + args = parse_args() + main(host=args.host, port=args.port, nb_day=args.nb_day) From a7e3c12be5e30376ecc097b52347a10826ebdb26 Mon Sep 17 00:00:00 2001 From: areski Date: Fri, 17 Oct 2014 19:31:31 +0200 Subject: [PATCH 2/3] Rename tutorial_timeseries to tutorial_server_data --- examples/{tutorial_timeseries.py => tutorial_server_data.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename examples/{tutorial_timeseries.py => tutorial_server_data.py} (100%) diff --git a/examples/tutorial_timeseries.py b/examples/tutorial_server_data.py similarity index 100% rename from examples/tutorial_timeseries.py rename to examples/tutorial_server_data.py From 5766981e8aaffe91826f786976b3d7ff7758681d Mon Sep 17 00:00:00 2001 From: areski Date: Fri, 17 Oct 2014 23:26:35 +0200 Subject: [PATCH 3/3] Add Sine Wave example --- examples/tutorial_sine_wave.py | 68 ++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 examples/tutorial_sine_wave.py diff --git a/examples/tutorial_sine_wave.py b/examples/tutorial_sine_wave.py new file mode 100644 index 00000000..ea8aee63 --- /dev/null +++ b/examples/tutorial_sine_wave.py @@ -0,0 +1,68 @@ +import argparse + +from influxdb import InfluxDBClient +import math +import datetime + + +USER = 'root' +PASSWORD = 'root' +DBNAME = 'tutorial' + + +def main(host='localhost', port=8086): + """ + main function to generate the sin wave + """ + now = datetime.datetime.today() + data = [{ + 'name': "foobar", + 'columns': ["time", "value"], + 'points': [] + }] + + for angle in range(0, 360): + y = 10 + math.sin(math.radians(angle)) * 10 + point = [int(now.strftime('%s')) + angle, y] + data[0]['points'].append(point) + + client = InfluxDBClient(host, port, USER, PASSWORD, DBNAME) + + print("Create database: " + DBNAME) + client.create_database(DBNAME) + + #Write points + client.write_points(data) + + query = 'SELECT time, value FROM foobar GROUP BY value, time(1s)' + print("Queying data: " + query) + result = client.query(query) + print("Result: {0}".format(result)) + + """ + You might want to comment the delete and plot the result on InfluxDB Interface + Connect on InfluxDB Interface at http://127.0.0.1:8083/ + Select the database tutorial -> Explore Data + + Then run the following query: + + SELECT time, value FROM foobar GROUP BY value, time(1s) + """ + + print("Delete database: " + DBNAME) + client.delete_database(DBNAME) + + +def parse_args(): + parser = argparse.ArgumentParser( + description='example code to play with InfluxDB') + parser.add_argument('--host', type=str, required=False, default='localhost', + help='hostname influxdb http API') + parser.add_argument('--port', type=int, required=False, default=8086, + help='port influxdb http API') + return parser.parse_args() + + +if __name__ == '__main__': + args = parse_args() + main(host=args.host, port=args.port)