<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>pywrkr Report &mdash; $title</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js@4/dist/chart.umd.min.js"></script>
<style>
  :root {
    --bg: #1a1a2e; --surface: #16213e; --card: #0f3460;
    --accent: #e94560; --green: #4caf50; --yellow: #ffc107;
    --text: #eee; --muted: #999; --border: #234;
  }
  * { margin:0; padding:0; box-sizing:border-box; }
  body { font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
         background: var(--bg); color: var(--text); padding: 0; }
  .header { background: linear-gradient(135deg, var(--surface), var(--card));
             padding: 24px 32px; border-bottom: 3px solid var(--accent); }
  .header h1 { font-size: 1.6em; font-weight: 600; }
  .header h1 span { color: var(--accent); }
  .header .meta { color: var(--muted); font-size: 0.85em; margin-top: 6px; }
  .container { max-width: 1200px; margin: 0 auto; padding: 24px; }
  .indicators { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
                 gap: 16px; margin-bottom: 28px; }
  .indicator { background: var(--surface); border-radius: 10px; padding: 18px 20px;
               border-left: 4px solid var(--accent); }
  .indicator .label { font-size: 0.75em; text-transform: uppercase; letter-spacing: 1px;
                       color: var(--muted); margin-bottom: 6px; }
  .indicator .value { font-size: 1.6em; font-weight: 700; }
  .indicator .value.green { color: var(--green); }
  .indicator .value.red { color: var(--accent); }
  .indicator .value.yellow { color: var(--yellow); }
  .charts { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 28px; }
  .chart-card { background: var(--surface); border-radius: 10px; padding: 20px; }
  .chart-card h3 { font-size: 0.95em; margin-bottom: 14px; color: var(--muted);
                    text-transform: uppercase; letter-spacing: 0.5px; }
  .chart-card.full { grid-column: 1 / -1; }
  .errors-table { width: 100%; border-collapse: collapse; margin-top: 10px; }
  .errors-table th, .errors-table td { padding: 8px 12px; text-align: left;
                                        border-bottom: 1px solid var(--border); }
  .errors-table th { color: var(--muted); font-size: 0.8em; text-transform: uppercase; }
  .footer { text-align: center; padding: 20px; color: var(--muted); font-size: 0.8em; }
  .footer a { color: var(--accent); text-decoration: none; }
  @media (max-width: 768px) { .charts { grid-template-columns: 1fr; } }
</style>
</head>
<body>
<div class="header">
  <h1><span>pywrkr</span> Benchmark Report</h1>
  <div class="meta">
    $method $url_display &bull; $mode &bull;
    $connections connections &bull; $timestamp
  </div>
</div>
<div class="container">

<!-- Indicators -->
<div class="indicators">
  <div class="indicator">
    <div class="label">Total Requests</div>
    <div class="value">$total_requests</div>
  </div>
  <div class="indicator">
    <div class="label">Duration</div>
    <div class="value">${duration_display}s</div>
  </div>
  <div class="indicator">
    <div class="label">Requests/sec</div>
    <div class="value green">$rps_display</div>
  </div>
  <div class="indicator">
    <div class="label">Errors</div>
    <div class="value $errors_class">$errors_display</div>
  </div>
  <div class="indicator">
    <div class="label">Mean Latency</div>
    <div class="value">$mean_latency</div>
  </div>
  <div class="indicator">
    <div class="label">p95 Latency</div>
    <div class="value $p95_class">$p95_latency</div>
  </div>
  <div class="indicator">
    <div class="label">p99 Latency</div>
    <div class="value $p99_class">$p99_latency</div>
  </div>
  <div class="indicator">
    <div class="label">Transfer</div>
    <div class="value">$transfer_rate/s</div>
  </div>
</div>

<!-- Charts -->
<div class="charts">
  <!-- Response Time Distribution -->
  <div class="chart-card">
    <h3>Response Time Distribution</h3>
    <canvas id="histChart"></canvas>
  </div>
  <!-- Percentiles -->
  <div class="chart-card">
    <h3>Response Time Percentiles</h3>
    <canvas id="pctChart"></canvas>
  </div>
  <!-- RPS Timeline -->
  <div class="chart-card full">
    <h3>Requests per Second Over Time</h3>
    <canvas id="rpsChart" height="80"></canvas>
  </div>
  <!-- Status Codes -->
  <div class="chart-card">
    <h3>Status Code Distribution</h3>
    <canvas id="scChart"></canvas>
  </div>
  <!-- Latency Breakdown -->
  <div class="chart-card" id="bdCard" style="$bd_card_display">
    <h3>Latency Breakdown (avg)</h3>
    <canvas id="bdChart"></canvas>
  </div>
</div>

<!-- Error Details -->
$error_table_html

</div>
<div class="footer">
  Generated by <a href="https://github.com/kurok/pywrkr">pywrkr</a>
  &mdash; Python HTTP benchmarking tool
</div>

<script>
const chartDefaults = {
  color: '#999',
  borderColor: 'rgba(255,255,255,0.1)',
  font: { family: "'Segoe UI', system-ui, sans-serif" }
};
Chart.defaults.color = chartDefaults.color;
Chart.defaults.borderColor = chartDefaults.borderColor;

// Response Time Distribution
new Chart(document.getElementById('histChart'), {
  type: 'bar',
  data: {
    labels: $hist_labels_json,
    datasets: [{
      label: 'Requests',
      data: $hist_counts_json,
      backgroundColor: $hist_colors_json,
      borderRadius: 3,
    }]
  },
  options: {
    responsive: true,
    plugins: {
      legend: { display: false },
      tooltip: { callbacks: { title: (items) => items[0].label + ' ms' } }
    },
    scales: {
      x: { title: { display: true, text: 'Response Time (ms)' },
            ticks: { maxRotation: 45, autoSkip: true, maxTicksLimit: 15 } },
      y: { title: { display: true, text: 'Count' }, beginAtZero: true }
    }
  }
});

// Percentile Chart
new Chart(document.getElementById('pctChart'), {
  type: 'line',
  data: {
    labels: $pct_labels_json,
    datasets: [{
      label: 'Latency (ms)',
      data: $pct_values_json,
      borderColor: '#e94560',
      backgroundColor: 'rgba(233,69,96,0.15)',
      fill: true,
      tension: 0.3,
      pointRadius: 5,
      pointBackgroundColor: '#e94560',
    }]
  },
  options: {
    responsive: true,
    plugins: { legend: { display: false } },
    scales: {
      y: { title: { display: true, text: 'Response Time (ms)' }, beginAtZero: true }
    }
  }
});

// RPS Timeline
new Chart(document.getElementById('rpsChart'), {
  type: 'line',
  data: {
    labels: $rps_labels_json,
    datasets: [{
      label: 'Req/s',
      data: $rps_values_json,
      borderColor: '#4caf50',
      backgroundColor: 'rgba(76,175,80,0.12)',
      fill: true,
      tension: 0.2,
      pointRadius: 2,
    }]
  },
  options: {
    responsive: true,
    plugins: { legend: { display: false } },
    scales: {
      x: { title: { display: true, text: 'Time' },
            ticks: { autoSkip: true, maxTicksLimit: 20 } },
      y: { title: { display: true, text: 'Requests/sec' }, beginAtZero: true }
    }
  }
});

// Status Codes
new Chart(document.getElementById('scChart'), {
  type: 'doughnut',
  data: {
    labels: $sc_labels_json,
    datasets: [{
      data: $sc_values_json,
      backgroundColor: $sc_colors_json,
      borderWidth: 0,
    }]
  },
  options: {
    responsive: true,
    plugins: {
      legend: { position: 'right', labels: { padding: 16 } }
    }
  }
});

// Latency Breakdown
if ($has_breakdown_json) {
  new Chart(document.getElementById('bdChart'), {
    type: 'bar',
    data: {
      labels: $bd_labels_json,
      datasets: [{
        label: 'Avg (ms)',
        data: $bd_values_json,
        backgroundColor: $bd_bar_colors_json,
        borderRadius: 4,
      }]
    },
    options: {
      indexAxis: 'y',
      responsive: true,
      plugins: { legend: { display: false } },
      scales: {
        x: { title: { display: true, text: 'Time (ms)' }, beginAtZero: true }
      }
    }
  });
}
</script>
</body>
</html>
