"""
Retrieves history data for a given numeric (either int or float) item_id
"""

import time
from datetime import datetime

from pyzabbix import ZabbixAPI

# The hostname at which the Zabbix web interface is available
ZABBIX_SERVER = "http://localhost/zabbix"

zapi = ZabbixAPI(ZABBIX_SERVER)

# Login to the Zabbix API
zapi.login("Admin", "zabbix")

item_id = "item_id"

# Create a time range
time_till = time.mktime(datetime.now().timetuple())
time_from = time_till - 60 * 60 * 4  # 4 hours

# Query item's history (integer) data
history = zapi.history.get(
    itemids=[item_id],
    time_from=time_from,
    time_till=time_till,
    output="extend",
    limit="5000",
)

# If nothing was found, try getting it from history (float) data
if not len(history):
    history = zapi.history.get(
        itemids=[item_id],
        time_from=time_from,
        time_till=time_till,
        output="extend",
        limit="5000",
        history=0,
    )

# Print out each datapoint
for point in history:
    print(
        "{}: {}".format(
            datetime.fromtimestamp(int(point["clock"])).strftime("%x %X"),
            point["value"],
        )
    )
