I am sending data to an InfluxDB database using the Python client library. The example given uses the time
method from the Point
class, which is supposed to allow for writing data with a provided timestamp rather than the data received time at the server.
I am using the following imports:
from influxdb_client import InfluxDBClient, Point
from influxdb_client.client.write_api import SYNCHRONOUS
from influxdb_client.domain.write_precision import WritePrecision
And I have the following code segments for constructing the Point
and sending it using the write
method:
def get_shark_intervals(sharks, buffer):
for shark in sharks:
demand = Shark.decode_readings(shark.read([Shark.DEMAND]))
interval = Shark.decode_datetime(shark.read([Shark.INTERVAL]))
if demand is None or interval is None:
return
print(interval)
buffer.append(
[
int(datetime(
2000 + interval[0],
interval[1],
interval[2],
interval[3],
interval[4],
interval[5]
).timestamp()),
"L1",
shark.get_equipment(),
shark.get_description(),
shark.get_nom_voltage(),
"DEMAND",
int(demand[0][1])
]
)
print(buffer[-1][0])
return
def flush_buffer(influx, buffer):
len_buffer = len(buffer)
points = [0] * len_buffer
for i in range(0, len_buffer):
point = Point("consumption") \
.tag("location", buffer[i][1]) \
.tag("equipment", buffer[i][2]) \
.tag("description", buffer[i][3]) \
.tag("nominal_voltage", buffer[i][4]) \
.tag("type", buffer[i][5]) \
.field("W", buffer[i][6]) \
.time(buffer[i][0], write_precision = WritePrecision.S)
points[i] = point
influx.write(
bucket = BUCKET,
record = points,
write_precision = WritePrecision.S
)
buffer = []
return
For the two print
statements in my code, I get this output:
The list is formatted as [Year, Month, Day, Hour, Minute, Second], and the Unix timestamp lines up with the items in the list when converted; however, as seen below, InfluxDB appears to be using the server received timestamp when displaying a query:
This is also seen in the raw data where _time
doesn't match the provided timestamps: