#!/usr/bin/python
#
# Determine the duration of the iperf3

import datetime
import sys

import pscheduler

from iperf3_defaults import *

logger = pscheduler.Log(prefix='tool-iperf3', quiet=True)

json = pscheduler.json_load(exit_on_error=True)

duration = json.get('duration', None)
if duration:
    delta = pscheduler.iso8601_as_timedelta(duration)
    duration = int(pscheduler.timedelta_as_seconds(delta))
else:
    duration = DEFAULT_DURATION

# factor in omit time as well
if json.get('omit'):
    omit_seconds = pscheduler.timedelta_as_seconds(pscheduler.iso8601_as_timedelta(json['omit'])) 
    logger.debug("Adding %s seconds because of omit flag" % omit_seconds)
    duration += omit_seconds

duration = duration + DEFAULT_FUDGE_FACTOR + DEFAULT_WAIT_SLEEP + DEFAULT_SERVER_SHUTDOWN

logger.debug("final duration = %ss" % duration)

pscheduler.succeed_json({
        "duration": 'PT%dS' % (duration)
})
