forked from gladhorn/influxdb-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutorial_sine_wave.py
78 lines (57 loc) · 1.94 KB
/
tutorial_sine_wave.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
"""Tutorial using all elements to define a sine wave."""
import argparse
import math
import datetime
import time
from influxdb import InfluxDBClient
USER = 'root'
PASSWORD = 'root'
DBNAME = 'tutorial'
def main(host='localhost', port=8086):
"""Define function to generate the sin wave."""
now = datetime.datetime.today()
points = []
for angle in range(0, 360):
y = 10 + math.sin(math.radians(angle)) * 10
point = {
"measurement": 'foobar',
"time": int(now.strftime('%s')) + angle,
"fields": {
"value": y
}
}
points.append(point)
client = InfluxDBClient(host, port, USER, PASSWORD, DBNAME)
print("Create database: " + DBNAME)
client.create_database(DBNAME)
client.switch_database(DBNAME)
# Write points
client.write_points(points)
time.sleep(3)
query = 'SELECT * FROM foobar'
print("Queying data: " + query)
result = client.query(query, database=DBNAME)
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 * from foobar
"""
print("Delete database: " + DBNAME)
client.drop_database(DBNAME)
def parse_args():
"""Parse the 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)