% layout 'mojo-status';

% content_for head => begin
  % if (my $refresh = param 'refresh') {
    <meta http-equiv="refresh" content="<%= $refresh %>">
  % }
% end

% my $localtime = sub {
%   my ($s, $m, $h, $day, $month, $year) = localtime(shift);
%   return sprintf '%04d-%02d-%02d %02d:%02d:%02d', $year + 1900, $month + 1,
%     $day, $h, $m, $s;
% };

<div class="row">
  <div class="col-md-4">
    <table class="table borderless novertpad">
      <tbody>
        <tr>
          <th class="fit text-left noleftpad" scope="row">
            <i class="fas fa-cloud"></i>
          </th>
          <th class="fit text-left noleftpad" scope="row">Mojolicious:</th>
          % my $version  = $Mojolicious::VERSION;
          % my $codename = $Mojolicious::CODENAME;
          <td><%= "$version ($codename)" %></td>
        </tr>
        <tr>
          <th class="fit text-left noleftpad" scope="row">
            <i class="fas fa-microchip"></i>
          </th>
          <th class="fit text-left noleftpad" scope="row">Plugin:</th>
          <td>
            <%= $Mojolicious::Plugin::Status::VERSION %> (<%= "$usage/$size" %>)
          </td>
        </tr>
        <tr>
          <th class="fit text-left noleftpad" scope="row">
            <i class="fas fa-rocket"></i>
          </th>
          <th class="fit text-left noleftpad" scope="row">Uptime:</th>
          <td>
            % my $uptime = int(($now - $all->{started}) / 86400);
            <%= "$uptime " . ($uptime == 1 ? 'day' : 'days') %>
          </td>
        </tr>
        <tr>
          <th class="fit text-left noleftpad" scope="row">
            <i class="fas fa-clock"></i>
          </th>
          <th class="fit text-left noleftpad" scope="row">Started:</th>
          <td>
            <span class="badge badge-dark">
              <%= $localtime->($all->{started}) %>
            </span>
          </td>
        </tr>
        <tr>
          <th class="fit text-left noleftpad" scope="row">
            <i class="fas fa-stream"></i>
          </th>
          <th class="fit text-left noleftpad" scope="row">Requests:</th>
          % my $stats  = $all->{stats};
          % my $recent = $stats->{info} + $stats->{success} + $stats->{redirect}
          %  + $stats->{client_error} + $stats->{server_error};
          % my $rps = ($recent > 1 ? $recent : 0) / ($now - $stats->{started});
          <td><%= sprintf '%.3f', $rps %>/s</td>
        </tr>
        <tr>
          <th class="fit text-left noleftpad" scope="row">
            <i class="fas fa-wrench"></i>
          </th>
          <th class="fit text-left noleftpad" scope="row">Workers:</th>
          % my @workers = values %{$all->{workers}};
          % my $idle = grep { !keys %{$_->{connections}} } @workers;
          <td><%= keys(%{$all->{workers}}) . " ($idle idle)" %></td>
        </tr>
      </tbody>
    </table>
  </div>

  <div class="col-md-2">
    <div class="stats">
      <div class="stats-body">
        <%= $all->{stats}{info} + $all->{stats}{success} %>
      </div>
      <div class="stats-description">Success</div>
    </div>
  </div>

  <div class="col-md-2">
    <div class="stats">
      <div class="stats-body">
        <%= $all->{stats}{redirect} %>
      </div>
      <div class="stats-description">Redirect</div>
    </div>
  </div>

  <div class="col-md-2">
    <div class="stats">
      <div class="stats-body">
        <%= $all->{stats}{client_error} %>
      </div>
      <div class="stats-description">Client Error</div>
    </div>
  </div>

  <div class="col-md-2">
    <div class="stats">
      <div class="stats-body">
        <%= $all->{stats}{server_error} %>
      </div>
      <div class="stats-description">Server Error</div>
    </div>
  </div>
</div>

<h4>Activity</h4>

<div class="row">
  <div class="col-md-12">
    <table class="table">
      <thead>
        <tr>
          <th>Worker</th>
          <th>CPU</th>
          <th>RSS</th>
          <th>Client</th>
          <th>Read/Written</th>
          <th>Requests</th>
          <th>ID</th>
          <th></th>
          <th>Time</th>
          <th>Protocol</th>
          <th>Request</th>
        </tr>
      </thead>
      <tbody>
        % for my $row (@$activity) {
          <tr>
            <td><%= $row->[0] // '' %></td>
            <td><%= $row->[1] // '' %></td>
            <td><%= $row->[2] // '' %></td>
            <td><%= $row->[3] // '' %></td>
            <td><%= $row->[4] // '' %></td>
            <td><%= $row->[5] // '' %></td>
            <td><%= $row->[6] // '' %></td>
            <td>
              % if ($row->[7]) {
                <i class="fas fa-circle"></i>
              % }
              % elsif (defined $row->[7]) {
                <i class="fas fa-check-circle"></i>
              % }
            </td>
            <td><%= $row->[8] // '' %></td>
            <td><%= $row->[9] // '' %></td>
            <td class="wrappable"><code><%= $row->[10] // '' %></code></td>
          </tr> 
        % }
      </tbody>
    </table>
  </div>
</div>

% if (@$slowest) {

  <h4>Slowest</h4>

  <div class="row">
    <div class="col-md-12">
      <table class="table">
        <thead>
          <tr>
            <th>Time</th>
            <th>Request</th>
            <th>ID</th>
            <th></th>
            <th>Worker</th>
            <th>Client</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          % for my $row (@$slowest) {
            % my $time = $row->[0];
            <tr>
              <td><%= $time %></td>
              <td class="wrappable"><code><%= $row->[1] %></code></td>
              <td><%= $row->[2] %></td>
              <td>
                % if ($time < 5) {
                  <span style="color: green">
                    <i class="fas fa-smile"></i>
                  </span>
                % }
                % elsif ($time < 30) {
                  <span style="color: orange">
                    <i class="fas fa-meh"></i>
                  </span>
                % }
                % else {
                  <span style="color: red">
                    <i class="fas fa-frown"></i>
                  </span>
                % }
              </td>
              <td><%= $row->[3] %></td>
              <td><%= $row->[4] %></td>
              <td>
                <span class="badge badge-dark">
                  <%= $localtime->($row->[5]) %>
                </span>
              </td>
            </tr>
          % }
        </tbody>
      </table>
    </div>
  </div>

% }