-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ | |
dist | ||
build | ||
.pytest_cache | ||
.venv | ||
.venv | ||
.env |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from dotenv import load_dotenv | ||
import pandas as pd | ||
from gewv_timeseries_client import TimeseriesClient | ||
from influxdb_client import Point | ||
from datetime import datetime, timedelta | ||
from pytz import UTC | ||
from loguru import logger | ||
|
||
load_dotenv() | ||
|
||
# Use .env-file for the credentials and the address | ||
# INFLUXDB_V2_URL=<db_host> | ||
# INFLUXDB_V2_ORG=<org_id> | ||
# INFLUXDB_V2_TOKEN=<token> | ||
|
||
PROJECT = "EXPERIMENTAL" | ||
|
||
|
||
def read_and_write_data(): | ||
client = TimeseriesClient.from_env_properties() | ||
|
||
# write single points | ||
_start_points = datetime.now(UTC) | ||
_point1 = ( | ||
Point("test_single_point") | ||
.tag("location", "Prague") | ||
.field("temperature", 25.3) | ||
.time(_start_points) | ||
) | ||
_point2 = ( | ||
Point("test_single_point") | ||
.tag("location", "New York") | ||
.field("temperature", 24.3) | ||
.time(_start_points) | ||
) | ||
client.write_points(project=PROJECT, points=[_point1, _point2]) | ||
|
||
# write a dataframe | ||
_start_dataframe = datetime.now(UTC) | ||
_dataframe = pd.DataFrame( | ||
data=[["coyote_creek", 1.0], ["coyote_creek", 2.0]], | ||
index=[_start_dataframe, _start_dataframe + timedelta(hours=1)], | ||
columns=["location", "water_level"], | ||
) | ||
client.write_a_dataframe( | ||
project=PROJECT, | ||
measurement_name="test_dataframe", | ||
dataframe=_dataframe, | ||
tag_columns=["location"], | ||
) | ||
|
||
# read single points | ||
points = client.get_points( | ||
project=PROJECT, | ||
fields={"_measurement": "test_single_point"}, | ||
start_time=_start_points, | ||
) | ||
logger.info("Request Points:") | ||
results = [] | ||
for table in points: | ||
for record in table.records: | ||
results.append((record.get_value(), record.get_field())) | ||
|
||
print(results) | ||
|
||
# read a dataframe | ||
dataframe = client.get_dataframe( | ||
project=PROJECT, | ||
fields={"_measurement": "test_dataframe", "_field": "water_level"}, | ||
start_time=_start_dataframe, | ||
) | ||
logger.info("Requested Dataframe:") | ||
logger.info(dataframe) | ||
|
||
|
||
if __name__ == "__main__": | ||
read_and_write_data() |