Influxdb now() - 1s is not working

Real time graph has to be updated every second. Some exe will generate data and push to temp.json . Telegraf will read this data using [tail] configuration and push to influxdb every second. When we query with "select * from measurement where now() > 1s " we are not getting any data. But data available in the database.
So there is always some 5s or more delay on the update. How can we reduce the delay and get exact real time data to the graph.
As of now the load is very less. one second one point is inserted to file. Each point has some 200 values(columns).

Hello @SuneethaSaka,

Where are you executing this query?

Please try this:

SELECT *  FROM measurement where time >= now() - 1s

Hi,
I am executing this query from python code. Can influxdb give data of now()?? or should we use some time gap for now() query. In our application we need to show real time graph as accurate as possible, without any time lag. Is it poosible through influxdb.

Thanks
Suneetha

Hi Ray, I tried that query too, but it not work. I am calling this query in a python file.

I’m not a python Pro, but the following is working flawlessly for me. Over here data is put into database every 10 seconds.
You need to install influxdb for python3 first:
python3 -m pip install influxdb [--user]

#!/usr/bin/env python3
"""
query testscript
"""
from influxdb import InfluxDBClient



SERVER = 'server'
DATABASE = 'collectd_db'

IFCLIENT = InfluxDBClient(host=SERVER, port=8086, database=DATABASE)


results = IFCLIENT.query("SELECT * FROM \"default\".rain where time >= now() - 10s" )
points = results.get_points()
for item in points:
    print(item)

It outputs a JSON formatted string like this:

{'time': '2019-07-16T05:54:15Z', 'host': 'weather', 'instance': 'rain', 'region': 'europe', 'unit': 'ml', 'unit_type': 'volume', 'value': 0}

Thanks Ray, Yes that works, but I want every second data, i.e now() data, We need to show real time graph, with max of 1 sec delay (now() -1s) , but now() -1s is not returning any data, but data is present at that point of time in that database. WE are getting data when we give now()- 5s

Please adjust the variables and query according to your setup and try the following code.
Please post 2 or 3 iteration of the loop output here:

#!/usr/bin/env python3
"""
query testscript
"""
import time
from influxdb import InfluxDBClient


SERVER = 'server'
DATABASE = 'collectd_db'
INTERVAL = 10


IFCLIENT = InfluxDBClient(host=SERVER, port=8086, database=DATABASE)


while True:
    time.sleep(INTERVAL)
    results = IFCLIENT.query("SELECT * FROM \"default\".rain where time >= now() - " + str(INTERVAL) + "s" )
    points = results.get_points()
    count = 0
    print ("Interval: %s" % INTERVAL)
    for item in points:
        count +=1
        print(item['time'])
    print("Number of elements: %d" % count)
    print("---separator---")

Okay Ray, I will try this and update the output.

Hi @SuneethaSaka ,

what are the values for the parameters interval , flush_interval and round_interval in your
telegraf configuration ?

You can also use milli seconds in your where clause …
does where time > now() -1200ms return values ?

best regards

Hi @MarcV,
interval = 1s
flush_interval = 1s
round_interval = true.

I tried with your query I am not getting any data.

Can you please try this:

#!/usr/bin/env python3
"""
query testscript
"""
import time
import datetime
from influxdb import InfluxDBClient


SERVER = 'server'
DATABASE = 'collectd_db'
INTERVAL = 1
TIMEMULTIPLY = 20

IFCLIENT = InfluxDBClient(host=SERVER, port=8086, database=DATABASE)


while True:
    currenttime = datetime.datetime.utcnow()
    results = IFCLIENT.query("SELECT * FROM \"default\".rain where time >= now() - " + str(INTERVAL*TIMEMULTIPLY) + "s" )
    points = results.get_points()
    count = 0
    print ("Interval: %s" % INTERVAL)
    print("Now: %s"% currenttime)
    for item in points:
        count +=1
        print("Itm: %s" % str(item['time']))
    print("Number of elements: %d" % count)
    print("---separator---")
    time.sleep(INTERVAL)

I’d like to see the difference between current date and dates written into the InfluxDB.

1 Like

Hi @Rayn0r,
Thank you for the support. I could solve the issue, Some issue with time format. After changing to unix_ms, now I could query data with now() -1s.

Thanks
Suneetha

You are welcome.
I’m glad you got it all working.

1 Like