-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinflux.py
60 lines (43 loc) · 1.36 KB
/
influx.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
from os import environ
from datetime import datetime, timezone, timedelta
from random import randint
import time
from influxdb import InfluxDBClient
from dotenv import load_dotenv
load_dotenv(dotenv_path="py_influx.env")
def get_influxdb_client():
host = environ.get("INFLUXDB_HOST", "localhost")
port = int(environ.get("INFLUXDB_PORT", 8086))
username = environ.get("INFLUXDB_USERNAME", "admin")
password = environ.get("INFLUXDB_PASSWORD", "admin123")
db = environ.get("INFLUXDB_DATABASE", "testdb")
return InfluxDBClient(host, port, username, password, db)
def get_time():
return datetime.now(tz=timezone(timedelta(hours=5, minutes=30))).isoformat()
def get_random_value():
return randint(400, 500)
def send(client):
value = get_random_value()
json_body = [
{
"measurement": "sample_measurement",
"tags": {
"some_tag": "some_value"
},
"time": get_time(),
"fields": {
"user_token": "5299f779-5cbe-4a28-b18c-cc2e5718bfa1",
"username": "arion",
"value": value,
}
}
]
client.write_points(json_body)
print(f"sending data...{value}")
def main():
client = get_influxdb_client()
while True:
send(client)
time.sleep(5)
if __name__ == '__main__':
main()