#!/usr/bin/python
#
#NOTE: THIS NEEDS A LOT OF WORK. 
# Merge overal is not working quite right so lost of code that just prevents things from exploding
# Merge the participant results of a run by this tool into a
# test-standard result.
#


import pscheduler

logger = pscheduler.Log(quiet=True)

input = pscheduler.json_load(exit_on_error=True)

results = {'succeeded': False}

try:
    result_list = input['results']
except KeyError as e:
    logger.error("merged-result error %s" % e)
    pscheduler.fail("Missing required key in merged-result input: %s" % e)

if len(result_list) != 2:
    pscheduler.fail("Expected 2 results in merged-results, got %s" % len(result_list))

source_results = result_list[0]
dest_results   = result_list[1]

final_results = source_results

# if this was a reverse test, we want to look at the destination's
# output instead of the source's
if input['test']['spec'].get('reverse'):
    final_results = dest_results

# it's possible one could come back as null, ensure we have the right type
if not final_results:
    final_results = {"succeeded": False}

final_diag = ""

if source_results and source_results.get('diags'):
    final_diag += "Participant 0:\n%s\n" % source_results.get('diags')

if dest_results and dest_results.get('diags'):
    final_diag += "Participant 1:\n%s\n" % dest_results.get('diags')

final_results['diags'] = final_diag

pscheduler.succeed_json(final_results)
