Created
October 14, 2020 02:25
-
-
Save carlos-jenkins/6835dc565f7376e4ee2d3147968e2a7b to your computer and use it in GitHub Desktop.
InfluxDB bootstrap script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Execute with: | |
# | |
# virtualenv venv | |
# source venv/bin/activate | |
# pip3 install influxdb | |
# python3 goinflux.py | |
# | |
from influxdb import InfluxDBClient | |
from datetime import timezone, timedelta, datetime | |
def sinewave(periods=10): | |
""" | |
Generate example data. | |
Query with: | |
SELECT "value" FROM "sinewave" WHERE time >= <XXXX>ms and time <= <YYYY>ms | |
""" | |
start = datetime.now(tz=timezone.utc) - timedelta(seconds=periods * 2 * 360) | |
points = [] | |
for angle in range(0, periods * 2 * 360 + 1): | |
y = sin(radians(angle)) * 100 | |
point = { | |
"measurement": 'sinewave', | |
"time": start + timedelta(seconds=angle), | |
"fields": { | |
"value": y, | |
}, | |
} | |
points.append(point) | |
return points | |
client = InfluxDBClient( | |
host='my-domain.com', # Domain, without protocol nor path | |
path='/path', # Path to the database, useful when in subpath in reverse proxy | |
port=443, # If using HTTPS (i.e reverse proxy doing HTTPS termination), 80 if not | |
database='sandbox', # Database name | |
username='root', # Username, here root | |
password='mypassword', # Root password | |
ssl=True, # If HTTPS, see port | |
verify_ssl=True, # If HTTPS, and the certificate is valid in the system (CA certificate installed and trusted) | |
# Or False if you don't care about that (internal certificate that you trust) | |
) | |
print('Connected to InfluxDB v{}'.format(client.ping())) | |
client.drop_measurement('sinewave') | |
points = sinewave() | |
client.write_points(points) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment