<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width,user-scalable=yes"/>
    <title>Workflow Errors</title>
    <%include file="rotation_tables.html"/>
    <link rel="stylesheet" type="text/css" href="/static/css/workflow.css">
    <script type="text/javascript">
      var shortlist = [
      % if len(workflowdata['reasonslist'].keys()) != 0:
        "${'","'.join(workflowdata['reasonslist'].keys())}"
      % endif
      ];
      var fullreasons = {
      % for short, long in workflowdata['reasonslist'].iteritems():
        "${short}": "${long}",
      % endfor
      };
      var sitelist = [
      % for site in sorted(drain_statuses.keys()):
        "${site}",
      % endfor
      ];
      var param_defaults = {
        "group": "production",
      };
      var task_list = [
      % for step, _ in workflowdata['steplist']:
        "${step}",
      % endfor
      ];
      var sites_for_task = {
      % for step, _ in workflowdata['steplist']:
        "${step}": [
        % for site in workflowinfo.site_to_run(step):
          "${site}",
        % endfor
        ],
      % endfor
      };
      var drain_statuses = {
      % for site, stat in drain_statuses.iteritems():
      "${site}": "${stat}",
      % endfor
      };

    </script>
    <script src="/static/js/addreason.js"></script>
    <script src="/static/js/makeparams.js"></script>
    <script src="/static/js/wfwebtool.js"></script>
    <style type="text/css">
      td { text-align: center; }
    </style>
  </head>
  <body onload="wfwebtool.workflowTable()">
    <%
       sendtotop = '<p><a href="#top">To top</a></p>'
       loading = '<img src="/static/img/loading.gif" alt="Loading...">'
    %>
    <div id="top">

      <center>
        <h1>${workflow}</h1>
        <span id="reset"><button onclick="reset('${workflow}')">Reset</button></span> <br>
        <a href="/globalerror">Global Errors</a> <br>
        <a href="http://cms-unified.web.cern.ch/cms-unified/showlog/?search=${workflow}">Workflow logs</a> --
        <a href="http://cms-unified.web.cern.ch/cms-unified/showlog/?search=${workflow}&module=error">errors</a> --
        <a href="https://its.cern.ch/jira/secure/QuickSearch.jspa?searchString=PREPID">JIRA tickets</a> --
        <a href="https://dmytro.web.cern.ch/dmytro/cmsprodmon/workflows.php?prep_id=PREPID">Production Monitor</a>
      </center>

    </div>

    <hr>

    <div style="float:left; width:50%" id="errorinfo">

      <span style="font-weight:bold;">Dominant Error Code:</span>
      <span id="maxerror">${loading}</span>
      <br>
      <span style="font-weight:bold;">Types Of Errors:</span> <br>
      <span id="errortypes">${loading}</span>
      <br>
      <span style="font-weight:bold;">Recommended Action:</span> <br>
      <span id="errorrecommend">${loading}</span>

    </div>

    <div>

      <span style="font-weight:bold;">Workflow Parameters:</span>
      <a href="https://cmsweb.cern.ch/reqmgr2/data/request?name=${workflow}" target="blank">more</a>
      <a href="https://cmsweb.cern.ch/reqmgr2/data/request?prep_id=PREPID&detail=true" target="blank">Prep ID</a> <br>
      <span id="wkfparams">${loading}</span>

    </div>

    <hr style="clear: both;">

    <div>

      <div>
        <span style="font-weight:bold;">Tasks with errors:</span> <br>
        % for step, _ in workflowdata['steplist']:
        <a href="#${step}">${step}</a> <br>
        % endfor
      </div>

    % if issuggested:
    </div>
    % else:
      <br style="clear:both;">
      <hr>

      <div>
        <div id="prediction"></div>
        <span style="font-weight:bold;">Actions:</span> <br>
        % if last_submitted is None:
        <form name="actionform" method="POST" action="/submitaction">
        % else:
        <form name="actionform" method="POST" action="/submitaction" onsubmit="return confirm('Confirm resubmission!\n\nLast submission at ${last_submitted.strftime('%d %B %Y, %I:%M %p')}')">
        % endif
          <input type="hidden" name="workflows" value="${workflow}">
          % for task, _ in workflowdata['steplist']:
            <%
               submitted_task = '/'.join(task.split('/')[2:])
            %>
            <input type="hidden" name="task_${loop.index}" value="${submitted_task}">
          % endfor
          <%
             actions = {
                 'clone' : 'Kill and Clone',
                 'acdc' : 'ACDC',
                 'recovery' : 'Recovery (not ACDC)',
                 'special' : 'Other action'
             }
          %>
          % for val, text in actions.iteritems():
          <span id="opt${val}"><input type="radio" name="action" onclick="makeParamTable(this);" value="${val}">${text}</span>
          % endfor
          <br>
          
          <div id="actionparams"></div>

          <button id="showmulti" type="button" style="display:none;">Apply to multiple?</button> <br>
          <div id="multiwfs" style="display:none;">
            <!-- 

             This is something we want to add, but we don't have the information yet!!!
             This way, we can look at groups based on filters if the clustering doesn't look that good.

            <button id="addfilter" type="button" onClick"addFilter()">Add Manual Filter</button> <br>
            <div id="filter" style="display:none;">
              <input type="text" name="wffilter">
              <select name="wffiltertype">
                <option value="none">Filter Type</option>
                <option value="campaign">Campaign</option>
                <option value="type">Type of Workflow</option>
                <option value="whitelise">Site Whitelist</option>
                <option value="agent">Agent</option>
                <option value="wfname">Workflow Name</option>
              </select> <br>
              <button type="button">Apply filter</button> <br>
            </div>
            -->

            <div id="wflist"></div>
          </div>

          <button id="addreason" type="button" onClick="addReason('reasons');">Add Reason</button> <br>
          <div id="reasons"></div>
          <br style="clear:both">
          <input type="submit" value="Submit">
        </form>
      </div>

    </div>

    % endif

    <br style="clear:both;">
    <hr>

    <center>
    % for step, table in workflowdata['steplist']:

    <h2 id="${step}">${step}</h2>
    ${sendtotop}

    <table border="3" style="border-collapse: collapse;">
      <tr>
        <th>
        </th>
        % for site, status in zip(workflowdata['allsites'], readiness):
        % if site not in workflowdata['skips'][step]['sites']:
        <th class="rotate ${status}"><div>${site}</div></th>
        % endif
        % endfor
      </tr>
      
      % for tablerow, error in table:
      % if sum(tablerow) != 0:
      <tr>
        <th><a href="/explainerror?errorcode=${error}&workflowstep=${step}">
            ${error}
        </a></th>
        % for index, entry in enumerate(tablerow):
        % if index not in workflowdata['skips'][step]['index']:
        % if entry == 0:
        <td>${entry}</td>
        % else:
        <td style="background-color:#ef4f4f;">${entry}</td>
        % endif
        % endif
        % endfor
      </tr>
      % endif
      % endfor
      
    </table>
    % endfor

    </center>

  </body>

  <script>
    $("#showmulti").click(function () {
      $("#multiwfs").toggle();
    })
  </script>

</html>
