"Data outside time range Zoom to Data"

  • What Grafana version and what operating system are you using?
    v11.1.0 (5b85c4c2fc)

  • What are you trying to achieve?

I would like to represent the server CPU utilization graph in real time through Time series graphs with Postgresql, python and Grafana.

  • How are you trying to achieve it?

1.create table

CREATE TABLE cpu_usage (
    id SERIAL PRIMARY KEY,
    server_name TEXT NOT NULL,
    timestamp TIMESTAMP NOT NULL,
    cpu_usage_percentage DECIMAL(5, 2) NOT NULL
);

2.query CPU usage and load data into Postgresql.

import psutil
import psycopg2
from datetime import datetime
import time

# Database connection
conn = psycopg2.connect(
    dbname="my_test_db",
    user="my_username",
    password="my_password",
    host="my_host",
    port="my_port"
)
cursor = conn.cursor()

# Function to insert CPU usage data
def log_cpu_usage(server_name):
    cpu_usage = psutil.cpu_percent(interval=1)  # Get CPU usage over 1 second
    timestamp = datetime.now()

    cursor.execute("""
        INSERT INTO cpu_usage (server_name, timestamp, cpu_usage_percentage)
        VALUES (%s, %s, %s)
    """, (server_name, timestamp, cpu_usage))
    conn.commit()

# Run the logging in an infinite loop every 10 seconds
try:
    while True:
        log_cpu_usage('my_server')
        time.sleep(9)  # Sleeps for 9 seconds, making the total time 10 seconds with psutil.cpu_percent's 1-second interval
except KeyboardInterrupt:
    print("Stopping CPU usage logging.")

# Close the connection
cursor.close()
conn.close()

Use Grafana to represent it

  • What happened?

Grafana doesn’t draw a utilization curve, it only shows the message “Data outside time range”, and when I click the Zoom to data button, it shows me the graph I want, as shown below:

For this method of displaying the graphs by clicking the Zoom to data button, when I click the “save” and “apply” buttons and set the panel auto-refresh time to 5 seconds, the graphs don’t refresh in real time as I expected, but I need to click the Zoom to data button again for the graphs to refresh.

  • What did you expect to happen?

I was expecting Grafana to refresh the graphs in real time, as I expected, instead of reporting “Data outside time range”.

  • Can you copy/paste the configuration(s) that you are having problems with?
    Yes, This is my configuration:

my sql script:

SELECT
  timestamp AS "time",
  cpu_usage_percentage AS "CPU Usage (%)"
FROM
  cpu_usage
WHERE
  server_name = 'my_server'
ORDER BY
  timestamp

Postgresql tables manipulated by this script

  • Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.

I only got this message: Data outside time range

  • Did you follow any online instructions? If so, what is the URL?

You query doesn’t have time condition in WHERE statement, so Grafana receives also data outside selected dashboard timerange. Add time condition to fix it.

Hello Sir, I change my sql script to this:

SELECT
  timestamp AS "time",
  cpu_usage_percentage AS "CPU Usage (%)"
FROM
  cpu_usage
WHERE
  server_name = 'my_server' AND "timestamp" > '2024-08-26 11:26:01.708' and "timestamp" < '2024-08-26 11:35:52.405'
ORDER BY
  timestamp

The problem still exists, the message ‘Data outside time range’ is still displayed.

Can you give me some more specific advice? Thank you for your advice.

Will you be editing that time condition every time, when you load dashboard to match selected dashboard time range?

I would say no, so use macros, which will fit your DB structure:

Some examples:

No, once I set this “6hour” time condition, I won’t change it arbitrarily. Only when I click on Zoom to data, it will change from 6hour to a more specific time period, as shown in the following picture.

Thanks! It works! I change my Python code from

......
timestamp = datetime.now()
......

to

timestamp = datetime.utcnow()

,
After taking your advice, Grafana was able to display the graph I wanted in real time instead of saying "Data outside time range ". Thank you. You’ve been very helpful!

1 Like