<?xml version="1.0" encoding="UTF-8"?>
<?python
  import template
?>
<html xmlns:py="http://purl.org/kid/ns#"
      py:layout="template">

<div py:def="body(_)">
<h2>${_('Stats')}</h2>

<table py:def="show_result(result, caption, first, second)"
       style="width: 50%">
<caption py:content="caption"></caption>
<tr>
<th py:content="first"/>
<th py:content="second"/>
</tr>
<tr py:for="row in result">
<td style="text-align: right">${row[0]}</td>
<td>${row[1]}</td>
</tr>
</table>

<?python
  if 'A' in PERMS:
    cond_cmd=""
    cond_string=""
    cond_array=[]
  else:
    cond_cmd="WHERE"
    cond_string="recipient=%s"
    cond_array=[REMOTE_USER]
  total=DB.query("SELECT count(1), virname FROM log "
                 + cond_cmd + " " + cond_string
                 + " GROUP BY virname ORDER BY 1 DESC LIMIT 100",
                 cond_array)
  by_ip=DB.query("SELECT count(1),ip FROM log "
                 + cond_cmd + " " + cond_string
                 + " GROUP BY ip ORDER BY 1 DESC LIMIT 10",
                 cond_array)
?>

${show_result(total,
              _('Top 100 by virus name'),
              _('Count'), _('Virus name'))}
<br/>
<span py:if="'A' in PERMS" py:strip="">
<?python
  recipients=DB.query("SELECT count(1),recipient FROM log "
                      "GROUP BY recipient ORDER BY 1 DESC LIMIT 100")
?>
${show_result(recipients,
              _('Top 100 by recipient'),
              _('Count'), _('Recipient'))}
<br/>
</span>

${show_result(by_ip,
              _('Top 10 by IP address'),
              _('Count'), _('Sender IP address'))}
<br/>

<div>
<?python
  first_entry=DB.query("SELECT datetime FROM log ORDER BY 1 LIMIT 1")
  last_entry=DB.query("SELECT datetime FROM log ORDER BY 1 DESC LIMIT 1")
?>
${_('First entry: %s') % first_entry[0]}<br/>
${_('Last entry: %s') % last_entry[0]}<br/>
</div>
</div>

</html>
