#!/usr/bin/env python3
#
# Determine the duration of a specified test.
#

import datetime
import ipaddress
import math
import os

import pscheduler

from fpingreach_common import *

json = pscheduler.json_load(exit_on_error=True)['spec']

network = ipaddress.ip_network(json["network"])

hosts_to_scan = 1 if "gateway" in json else 0

limit = json.get("limit", network.num_addresses - 2)

# Add this to whatever the gateway did.
hosts_to_scan += min(limit, network.num_addresses-2)

timeout_iso = json.get("timeout", DEFAULT_TIMEOUT)
timeout = pscheduler.iso8601_as_timedelta(timeout_iso) + datetime.timedelta( seconds=0.5 )

# Any fraction means an additional run, so round up.
fping_runs = int(math.ceil(float(hosts_to_scan) / float(max_args_per_run(network))))

duration = (timeout + datetime.timedelta(seconds=FPING_RUN_SLOP)) * fping_runs
duration += datetime.timedelta(seconds=1)  # Internal slop

pscheduler.succeed_json({
    "duration": pscheduler.timedelta_as_iso8601( duration )
})
