feat(workflow): example stats page with embedded Chart.js, inline form validation errors

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-24 01:59:27 +02:00
co-authored by Claude Sonnet 5.5
parent 16a0bb0de4
commit 00af65613d
36 changed files with 1274 additions and 138 deletions
+108
View File
@@ -0,0 +1,108 @@
#pragma once
// Request and connection statistics behind /stats. One mutex, fixed-size
// storage, static caps: 90 ten-second buckets (15 min of history), 256
// distinct routes, a 1024-sample latency ring. Everything past a cap is
// counted under "(other)" or dropped oldest-first, never grown.
// Gap: real export (Prometheus, OTel) lives in 02-observability.md.
#include <array>
#include <chrono>
#include <cstddef>
#include <map>
#include <mutex>
#include <string>
#include <vector>
#include "Http.hpp"
namespace molo::example::fw {
namespace stats_caps {
inline constexpr std::size_t kBuckets = 90;
inline constexpr long kBucketSec = 10;
inline constexpr std::size_t kMaxRoutes = 256;
inline constexpr std::size_t kLatencyRing = 1024;
} // namespace stats_caps
struct RequestSample {
Method method = Method::Get;
std::string route; // matched pattern, empty when no route matched
int status = 200;
std::size_t bytes_in = 0;
std::size_t bytes_out = 0;
long micros = 0;
};
enum class ConnEnd { Closed, Timeout, Malformed };
class Stats {
public:
Stats();
void record_request(const RequestSample& sample);
void connection_opened();
void connection_closed(ConnEnd how, std::size_t bytes_in,
std::size_t bytes_out);
void connection_rejected();
void set_connection_cap(long cap);
// Test hook: current time in seconds; defaults to the wall clock.
void set_clock(long (*now_sec)());
// JSON document consumed by the /stats page: totals, by_method, by_status,
// by_route, latency histogram + percentiles, connections, time series.
std::string to_json() const;
long total_requests() const;
long total_connections() const;
long active_connections() const;
private:
struct Bucket {
long slot = -1; // epoch seconds / kBucketSec; -1 = unused
long requests = 0;
long errors = 0;
long bytes_in = 0;
long bytes_out = 0;
long micros = 0;
long connections = 0;
};
struct RouteStat {
long count = 0;
long errors = 0;
long micros = 0;
long max_micros = 0;
long bytes_out = 0;
};
long now() const;
Bucket& bucket_for(long slot);
mutable std::mutex mu_;
long (*clock_)() = nullptr;
long started_ = 0;
long requests_ = 0;
long errors_ = 0;
long bytes_in_ = 0;
long bytes_out_ = 0;
long micros_ = 0;
std::map<std::string, long> by_method_;
std::map<int, long> by_status_;
std::map<std::string, RouteStat> by_route_;
std::array<long, 8> histogram_{};
std::vector<long> ring_;
std::size_t ring_next_ = 0;
std::array<Bucket, stats_caps::kBuckets> buckets_{};
long conn_total_ = 0;
long conn_active_ = 0;
long conn_peak_ = 0;
long conn_timeout_ = 0;
long conn_malformed_ = 0;
long conn_rejected_ = 0;
long conn_cap_ = 0;
long conn_bytes_in_ = 0;
long conn_bytes_out_ = 0;
};
} // namespace molo::example::fw