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

import datetime
import ipaddress
import math

import pscheduler

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", 64)
parallel = json.get("parallel", limit);

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

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

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

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

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