#!/usr/bin/python3
import rrdtool
import os
import time

print("===== Starting Python bindings test =====")

rrd_file = "python_test.rrd"
png_file = "python_test.png"

try:
    print("--> Creating RRD via Python bindings...")
    rrdtool.create(
        rrd_file,
        "--start", "now-10m",
        "--step", "60",
        "DS:test:GAUGE:120:0:100",
        "RRA:AVERAGE:0.5:1:10"
    )
    print("OK: RRD file created.")

    print("--> Updating RRD via Python bindings...")
    rrdtool.update(rrd_file, "N:73")
    print("OK: RRD file updated.")

    print("--> Fetching data via Python bindings...")
    start_time = int(time.time()) - 300
    fetch_result = rrdtool.fetch(rrd_file, "AVERAGE", "--start", str(start_time))
    print(fetch_result)
    print("OK: Data fetched.")

    print("--> Creating graph via Python bindings...")
    rrdtool.graph(png_file, "--start", "now-10m", f"DEF:mydata={rrd_file}:test:AVERAGE", "LINE1:mydata#0000FF:test data")
    print("OK: Graph created.")

    print("===== Python bindings test PASSED =====")

finally:
    if os.path.exists(rrd_file): os.remove(rrd_file)
    if os.path.exists(png_file): os.remove(png_file)
