{% extends "template.html" %}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

{% block body %}
<div>
<h2>{{ _('Stats') }}</h2>

{% macro show_result(result, caption, first, second) %}
<table style="width: 50%">
<caption >{{ caption|e }}</caption>
<tr>
<th>{{ first|e }}</th>
<th>{{ second|e }}</th>
</tr>
{% for row in result %}
<tr>
<td style="text-align: right">{{ row[0]|e }}</td>
<td>{{ row[1]|e }}</td>
</tr>
{% endfor %}
</table>
{% endmacro %}

{% if 'A' in PERMS %}
  {% set cond_cmd = "" %}
  {% set cond_string = "" %}
  {% set cond_array = [] %}
{% else %}
  {% set cond_cmd = "WHERE" %}
  {% set cond_string = "recipient=%s" %}
  {% set cond_array = [REMOTE_USER] %}
{% endif %}
{% set
  total=DB.query("SELECT count(1), virname FROM log "
                 + cond_cmd + " " + cond_string
                 + " GROUP BY virname ORDER BY 1 DESC LIMIT 100",
                 cond_array)
%}
{% set
  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/>
{% if 'A' in PERMS %}
{% set 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/>
{% endif %}

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

<div>
{% set first_entry = DB.query("SELECT datetime FROM log ORDER BY 1 LIMIT 1") %}
{% set 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>
{% endblock %}

</html>
