#import koji
#import koji.util as kojiutil
#from kojiweb import util
#from urllib.parse import quote
#import datetime

#def printChildren($taskID, $childMap)
  #set $iter = 0
  #set $children = $childMap[$str($taskID)]
  #if $children
  <ul>
    #for $child in $children
    #set $iter += 1
    #if $iter < $len($children)
    <li class="sibling">
    #else
    <li>
    #end if
      #set $childState = $util.taskState($child.state)
      <span class="treeBranch">
        <span class="treeLabel">
          <span class="task$childState">$util.imageTag($childState)</span>
          <a href="taskinfo?taskID=$child.id" class="task$childState" title="$childState">$util.escapeHTML($koji.taskLabel($child))</a>
        </span>
      </span>
    $printChildren($child.id, $childMap)
    </li>
    #end for
  </ul>
  #end if
#end def

#def printMap($vals, $prefix='')
  #for $key, $value in $vals.items()
  #if $key == 'properties'
  ${prefix}properties&nbsp;=&nbsp;$printProperties($value)<br/>
  #elif $key != '__starstar'
  $prefix$key&nbsp;=&nbsp;$printValue($key, $value)<br/>
  #end if
  #end for
#end def

#def printOpts($opts)
  #if $opts
  <strong>Options:</strong><br/>
  $printMap($opts, '&nbsp;&nbsp;')
  #end if
#end def

#def printValue($key, $value, $sep=', ')
  #if $value is None
None
  #elif $key == 'user'
<a href="userinfo?userID=$value['id']">$value['name']</a>
  #elif $key == 'task'
<a href="taskinfo?taskID=$value['id']">$value['id']</a>
  #elif $key == 'host'
<a href="hostinfo?hostID=$value['id']">$value['name']</a>
  #elif $key == 'build'
<a href="buildinfo?buildID=$value['id']">$value['nvr']</a>
  #elif $key == 'buildroot'
<a href="buildrootinfo?buildrootID=$value['id']">$value['id']</a>
  #elif $key in ('tag', 'destination_tag', 'build_tag')
<a href="taginfo?tagID=$value['id']">$value['name']</a>
  #elif $key in ('build_target', 'target_info')
<a href="buildtargetinfo?targetID=$value['id']">$value['name']</a>
  #elif $key in ('repo_info', 'oldrepo', 'repo')
    #if $key == 'repo' and isinstance($value, list)
$sep.join([$str($val) for $val in $value])
    #else
<a href="repoinfo?repoID=$value.id">$value.id</a> ($koji.formatTimeLong($value.create_ts))
    #end if
  #elif $key == 'task_list'
    #for $task in $params['task_list']
      <br/><strong>&nbsp;Task:</strong><br/>
      $printMap($task, '&nbsp;&nbsp;')
    #end for
  #elif $isinstance($value, list)
$sep.join([$str($val) for $val in $value])
  #elif $isinstance($value, dict)
$sep.join(['%s=%s' % (($n == '' and "''" or $n), $v) for $n, $v in $value.items()])
  #else
$value
  #end if
#end def

#def printProperties($props)
  #echo ', '.join([$v is not None and '%s=%s' % ($n, $v) or $str($n) for $n, $v in $props.items()])
#end def


#include "includes/header.chtml"

  <h4>Information for task <a href="taskinfo?taskID=$task.id">$util.escapeHTML($koji.taskLabel($task))</a></h4>

  <table>
    <tr>
      <th>ID</th><td>$task.id</td>
    </tr>
    <tr>
      <th>Method</th><td>$task.method</td>
    </tr>
    <tr>
      <th>Parameters</th>
      <td>
        #for key in $sorted($params)
          #if $params[$key] is not None
        <strong>$key.capitalize().replace('_', ' ')</strong>: $printValue($key, $params[$key])<br/>
          #end if
        #end for
        $printOpts($opts)
      </td>
    </tr>
    <tr>
      #set $state = $util.taskState($task.state)
      <th>State</th>
      <td class="task$state">$state
      #if $currentUser and ('admin' in $perms or $task.owner == $currentUser.id)
      #if $task.state in ($koji.TASK_STATES.FREE, $koji.TASK_STATES.OPEN, $koji.TASK_STATES.ASSIGNED)
      <span class="adminLink">(<a href="canceltask?taskID=$task.id$util.authToken($self)">cancel</a>)</span>
      #elif $task.state in ($koji.TASK_STATES.CANCELED, $koji.TASK_STATES.FAILED) and (not $parent)
      <span class="adminLink">(<a href="resubmittask?taskID=$task.id$util.authToken($self)">resubmit</a>)</span>
      #end if
      #end if
      #if $currentUser and 'admin' in $perms and $task.state in ($koji.TASK_STATES.OPEN, $koji.TASK_STATES.ASSIGNED)
      <span class="adminLink">(<a href="freetask?taskID=$task.id$util.authToken($self)">free</a>)</span>
      #end if
      </td>
    </tr>
    #if $taskBuilds
    #for $build in $taskBuilds
    <tr>
      <th>Build</th><td><a href="buildinfo?buildID=$build.build_id">$util.escapeHTML($koji.buildLabel($build))</a></td>
    </tr>
    #end for
    #end if
    <tr>
      <th>Created</th><td>$util.formatTimeLong($task.create_ts)</td>
    </tr>
    #if $task.start_time
    <tr>
      <th>Started</th><td>$util.formatTimeLong($task.start_ts)</td>
    #end if
    #set $end_ts = None
    #if $task.state == $koji.TASK_STATES.OPEN
    #if $estCompletion
    <tr>
      <th title="Estimation based on previous builds of same package">Est. Completion</th>
      <td>$util.formatTimeLong($estCompletion)</td>
    </tr>
    #end if
    #elif $task.completion_time
    <tr>
      <th>Completed</th><td>$util.formatTimeLong($task.completion_ts)</td>
    </tr>
    #set $end_ts = $task.completion_ts
    #end if
    #if not $end_ts
    #set $end_ts = $kojiutil.parseTime($kojiutil.encode_datetime(datetime.datetime.utcnow()))
    #end if
    <tr>
      <th title="From task's creation">Total time</th>
      <td>$util.formatTimestampDifference($task.create_ts, $end_ts)</td>
    </tr>
    #if $task.start_time
    <tr>
      <th title="From task's start">Task time</th>
      <td>$util.formatTimestampDifference($task.start_ts, $end_ts)</td>
    </tr>
    #end if
    <tr>
      <th>Owner</th>
      <td>
        #if $owner
          #if $owner.usertype == $koji.USERTYPES['HOST']
          <a href="hostinfo?userID=$owner.id">$util.escapeHTML($owner.name)</a>
          #else
          <a href="userinfo?userID=$owner.id">$util.escapeHTML($owner.name)</a>
          #end if
        #end if
      </td>
    </tr>
    <tr>
      <th>Channel</th>
      <td>
        #if $task.channel_id
        <a href="channelinfo?channelID=$task.channel_id">$util.escapeHTML($channelName)</a>
        #end if
      </td>
    </tr>
    <tr>
      <th>Host</th>
      <td>
        #if $task.host_id
        <a href="hostinfo?hostID=$task.host_id">$util.escapeHTML($hostName)</a>
        #end if
      </td>
    </tr>
    <tr>
      <th>Arch</th><td>$util.escapeHTML($task.arch)</td>
    </tr>
    #if $buildroots
    <tr>
      <th>Buildroot#if $len($buildroots) > 1 then 's' else ''#</th>
      <td>
        #for $buildroot in $buildroots
        <a href="buildrootinfo?buildrootID=$buildroot.id">#if $task.method == 'vmExec' then '' else '/var/lib/mock/'#$buildroot.tag_name-$buildroot.id-$buildroot.repo_id</a><br/>
        #end for
      </td>
    </tr>
    #end if
    <tr>
      <th>Parent</th>
        <td>
        #if $parent
        <a href="taskinfo?taskID=$parent.id" class="task$util.taskState($parent.state)">$util.escapeHTML($koji.taskLabel($parent))</a>
        #end if
      </td>
    </tr>
    <tr>
      <th>Descendants</th>
      <td class="tree">
        #if $len($descendents[$str($task.id)]) > 0
        #set $taskState = $util.taskState($task.state)
        <span class="task$taskState">$util.imageTag($taskState)</span>
        <span class="root">$task.method</span>
        #end if
        $printChildren($task.id, $descendents)
      </td>
    </tr>
    <tr>
      <th>Waiting?</th><td>#if $task.waiting then 'yes' else 'no'#</td>
    </tr>
    <tr>
      <th>Awaited?</th><td>#if $task.awaited then 'yes' else 'no'#</td>
    </tr>
    <tr>
      <th>Priority</th><td>$task.priority</td>
    </tr>
    <tr>
      <th>Weight</th><td>#echo '%.2f' % $task.weight#</td>
    </tr>
    <tr>
      <th>Result</th>
      <td>
      #if $abbr_result_text
        <div id="abbr-result">
        $abbr_result_text
        </div>
        <div id="full-result">
        $full_result_text
        </div>
        <a href="#" collapse" id="toggle-abbreviated-result" style="display: none;">Show abbreviated results</a>
        <a href="#" collapse" id="toggle-full-result" style="display: none;">Show complete results</a>
      #else
         <div id="result">
        $full_result_text
        </div>
      #end if
      </td>
    </tr>
    <tr>
      <th>Output</th>
      <td>
        #for $volume, $filename in $output
        <a href="$pathinfo.task($task.id, volume=$volume)/$quote($filename)">$filename</a>
           #if $filename.endswith('.log')
           (<a href="getfile?taskID=$task.id&volume=$volume&name=$quote($filename)&offset=-4000">tail</a>)
           #end if
        <br/>
        #end for
        #if $task.state not in ($koji.TASK_STATES.CLOSED, $koji.TASK_STATES.CANCELED, $koji.TASK_STATES.FAILED) and \
            $task.method in ('buildSRPMFromSCM', 'buildArch', 'createLiveMedia', 'buildMaven', 'wrapperRPM', 'vmExec', 'createrepo', 'runroot', 'createAppliance', 'createLiveCD')
        <br/>
        <a href="watchlogs?taskID=$task.id">Watch logs</a>
        #end if
      </td>
    </tr>
  </table>

#if $abbr_result_text
  <script type="text/javascript">
    (function() {
       var abbr = document.getElementById('abbr-result');
       var full = document.getElementById('full-result');
       var link_to_show_abbr = document.getElementById('toggle-abbreviated-result');
       var link_to_show_full = document.getElementById('toggle-full-result');
       full.style.display = 'none';
       abbr.style.display = 'block'
       link_to_show_full.style.display = 'inline'
       link_to_show_abbr.style.display = 'none'
       var trigger = function(e) {
         if (link_to_show_abbr.style.display == 'none') {
           link_to_show_abbr.style.display = 'inline'
           link_to_show_full.style.display = 'none'
           abbr.style.display = 'none';
           full.style.display = 'block'
         }
         else {
           link_to_show_abbr.style.display = 'none'
           link_to_show_full.style.display = 'inline'
           abbr.style.display = 'block';
           full.style.display = 'none'
         }
         return false;
       };

       link_to_show_full.onclick = trigger
       link_to_show_abbr.onclick = trigger
    })();
  </script>
#end if
#include "includes/footer.chtml"
