Skip to content

Commit

Permalink
Add Tutorial 3 and add 3 and 4 to setup_db.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Lbromig committed Mar 30, 2021
1 parent 002e4a3 commit 1cfba44
Show file tree
Hide file tree
Showing 2 changed files with 256 additions and 143 deletions.
88 changes: 61 additions & 27 deletions examples/Tutorial_4_Incorporating_Databases.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,70 @@
"""
TUTORIAL 4: Incorporating Databases
---------------------------------------------
4.1 Import the InfluxDBClient and other packages you will need
"""
from influxdb import InfluxDBClient
from datetime import datetime
import numpy as np
import time

# Instantiate the database client.
influx_client = InfluxDBClient(host='localhost', port=8086, username='root', password='root', database='device_manager')

# Check connection
print(f'Checking connectivity. DB server version: {influx_client.ping()}')

for i in range(0, 100, 1):
# This is an example write operation
data_point = {
"measurement": "testMeasurement",
"tags": {
"experiment_name": "influxDB_test"
},
"time": datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ"),
"fields": {
"test_number": np.random.rand(1)
"""
4.2 Instantiate the InfluxDBClient with the connection details of the respective database and check the connection by
pinging the database server. Make sure to change the default connection details below to your database details!
"""


def run(services):
influx_client = InfluxDBClient(host='127.0.0.1', port=8086, username='root', password='root', database='SiLA_2_Manager')
print(f'Checking connectivity. DB server version: {influx_client.ping()}', flush=True)

"""
4.3 If you do not already have a database, creat a new one:
"""

influx_client.create_database(dbname='SiLA_2_Manager')

"""
4.4 Create a datapoint to write to the database. Add adequate tags so you can filter your data efficiently in
chronograf. If your script is running, you can check your data live in your browser, if your chronograf server
is running: <ip-of-the-chronograf/influxDB-server>:8888 .
"""

for i in range(0, 25, 1):
# This is an example write operation
random_number = np.random.rand()
data_point = {
"measurement": "testMeasurement",
"tags": {
"experiment_name": "influxDB_test",
"device": "experiment_docker_container"
},
"time": datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ"),
"fields": {
"test_number": random_number
}
}
}

try:
influx_client.write_points([data_point])
except:
print("This did not work...")
try:
influx_client.write_points([data_point])
print(f'A random number was written to the database: {random_number}', flush=True)
except:
print("This did not work...")

# This is an example query.
results = influx_client.query(
'SELECT test_number FROM "device_manager"."autogen"."testMeasurement" WHERE experiment_name = \'influxDB_test\' GROUP BY position'
'ORDER BY DESC LIMIT 1')
print(results)
"""
4.5 Query your data using the SQL-like Influx Query Language (InfluxQL). You can find information on the syntax at:
https://docs.influxdata.com/influxdb/v1.8/query_language/ . The following query will read the value that was
just written to the database.
Hint 4: Copy and paste the query below to visualize the data in chronograf. Change the LIMIT to display the
number of last data points. Remove the escape character (backslash) around the influxDB_test in the query.
You can leave out the last part of the query, starting at ORDER BY, to display all available measurements
of this type.
"""

time.sleep(10)
# This is an example query.
results = influx_client.query(
'SELECT test_number FROM "SiLA_2_Manager"."autogen"."testMeasurement" WHERE experiment_name = \'influxDB_test\' GROUP BY position ORDER BY DESC LIMIT 1')
print('The latest random number was queried from the database: ', flush=True)
print(results, flush=True)
time.sleep(5)
Loading

0 comments on commit 1cfba44

Please sign in to comment.