import rrdtool
import time
import requests
import os


def create_graph(rrd_file: str, output_file: str):
    # Получаем текущее время
    now = int(time.time())
    start_time = now - 86400  # 24 часа назад
    start_date = time.strftime("%d-%m-%Y", time.localtime(start_time))
    end_date = time.strftime("%d-%m-%Y", time.localtime(now))

    try:
        rrdtool.graph(
            output_file,
            "--start", str(start_time),
            "--end", str(now),
            "--title", f"Temperature from {start_date} to {end_date}",
            "--vertical-label", "Temperature (°C)",
            "--width", "800",
            "--height", "400",
            "DEF:low_temp={}:temp_low:AVERAGE".format(rrd_file),
            "DEF:high_temp={}:temp_high:AVERAGE".format(rrd_file),
            "LINE1:low_temp#0000FF:Low Temperature",
            "LINE1:high_temp#FF0000:Top Temperature",
            r"GPRINT:low_temp:MIN:Min Low Temp\: %2.2lf°C",
            r"GPRINT:high_temp:MAX:Max Top Temp\: %2.2lf°C",
            r"GPRINT:low_temp:AVERAGE:Avg Low Temp\: %2.2lf°C",
            r"GPRINT:high_temp:AVERAGE:Avg Top Temp\: %2.2lf°C",
        )
        print(f"График успешно создан: {output_file}")
    except Exception as e:
        print(f"Ошибка создания графика: {e}")

    except Exception as e:
        print(f"Ошибка создания графика: {e}")
# Пример использования
rrd_file = "temperature_data.rrd"
output_file = "temperature_graph.png"
create_graph(rrd_file, output_file)
