0

On the Web UI, it only shows tags without fields. The data only appears later, after I shut down my PC. Below is my code. I scheduled the data to be written every 15 seconds, but it does not show any data on the web. Th code runs with no errors Please help

from datetime import datetime
import pandas as pd
import schedule
import time

from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS

# You can generate a Token from the "Tokens Tab" in the UI
token = "9o2rIXlQSgAUEzLmRl8wrEjDGKJMe9vyHw_sk1fBuJ1OJVCHJBdH_muJ6SiGsd7VpcU-LVTdNjhNcw7nR56dZA=="
org = "Filtrona"
bucket = "Machines3"

client = InfluxDBClient(url="http://localhost:8086", token=token)
write_api = client.write_api(write_options=SYNCHRONOUS)

# Configure the batch size for data points
batch_size = 25

def fetch_and_store_data():
    try:
        df = pd.read_csv("http://10.9.161.25/dataList.csv", skiprows = [1])
        df['Date'] = pd.to_datetime(df['Date'].str.replace(r'(\w+)\. (\d+)', r'\1 \2', regex=True), format="%Y-%b %d. %H:%M:%S")
        df.set_index(['Date'], inplace=True)

        # Batch data points and write in batches
        for chunk in range(0, len(df), batch_size):
            batch_data = df.iloc[chunk:chunk + batch_size]
            write_api.write(bucket, org, record=batch_data, data_frame_measurement_name="Machine",
                            data_frame_tag_columns=['ID'])
            print(f"Batch of {batch_size} data points fetched and stored in InfluxDB., Data Below:")
            print(df)

    except Exception as e:
        print("An error occurred:", e)

# Schedule the fetch_and_store_data function to run every 2 seconds
schedule.every(20).seconds.do(fetch_and_store_data)

while True:
    schedule.run_pending()
    time.sleep(19)

Am I doing something wrong?

Simon
  • 495
  • 1
  • 4
  • 18

0 Answers0