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:
@@ -0,0 +1,98 @@
|
||||
#include "App.hpp"
|
||||
|
||||
#include "../fw/Router.hpp"
|
||||
#include "../fw/Templates.hpp"
|
||||
|
||||
namespace molo::example::app {
|
||||
|
||||
namespace {
|
||||
|
||||
std::string num(std::size_t v) { return std::to_string(v); }
|
||||
|
||||
std::string quote(const std::string& s) {
|
||||
return "\"" + fw::json_escape(s) + "\"";
|
||||
}
|
||||
|
||||
// Counts of every service the example wires, for the "services" charts.
|
||||
std::string services_json(Services& s) {
|
||||
std::string out = "{";
|
||||
out += "\"stores\":{\"kv\":" + num(s.kv->size()) +
|
||||
",\"cache\":" + num(s.cache->size()) +
|
||||
",\"sessions\":" + num(s.sessions->size()) +
|
||||
",\"pubsub_topics\":" + num(s.pubsub->topics().size()) +
|
||||
",\"events\":" + num(s.events->delivered().size()) +
|
||||
",\"spans\":" + num(s.telemetry->spans().size()) +
|
||||
",\"uploads\":" + num(s.uploads->list().size()) +
|
||||
",\"webdav_files\":" + num(s.webdav->size()) +
|
||||
",\"replication_entries\":" + num(s.repl->latest()) +
|
||||
",\"openapi_routes\":" + num(s.openapi->size()) +
|
||||
",\"embedded_assets\":" + num(s.embedded.size()) + "}";
|
||||
out += ",\"cache\":{\"hits\":" + std::to_string(s.cache->hits()) +
|
||||
",\"misses\":" + std::to_string(s.cache->misses()) + "}";
|
||||
|
||||
std::size_t upload_bytes = 0;
|
||||
for (const auto& u : s.uploads->list()) upload_bytes += u.size;
|
||||
out += ",\"upload_bytes\":" + num(upload_bytes);
|
||||
|
||||
std::map<std::string, std::size_t> task_states;
|
||||
for (const auto& t : s.tasks->list()) ++task_states[t.state];
|
||||
out += ",\"tasks\":{";
|
||||
bool first = true;
|
||||
for (const auto& [k, v] : task_states) {
|
||||
out += (first ? "" : ",") + quote(k) + ":" + num(v);
|
||||
first = false;
|
||||
}
|
||||
out += "},\"workers\":[";
|
||||
first = true;
|
||||
for (const auto& w : s.supervisor->status()) {
|
||||
out += std::string(first ? "" : ",") + "{\"name\":" + quote(w.name) +
|
||||
",\"running\":" + (w.running ? "true" : "false") +
|
||||
",\"restarts\":" + std::to_string(w.restarts) + "}";
|
||||
first = false;
|
||||
}
|
||||
out += "],\"orm\":{";
|
||||
first = true;
|
||||
for (auto* db : {s.sqlite.get(), s.postgres.get()}) {
|
||||
std::size_t rows = 0;
|
||||
auto tables = db->tables();
|
||||
for (const auto& t : tables) rows += db->select_all(t).rows.size();
|
||||
out += std::string(first ? "" : ",") + quote(fw::backend_name(db->backend())) +
|
||||
":{\"tables\":" + num(tables.size()) + ",\"rows\":" + num(rows) + "}";
|
||||
first = false;
|
||||
}
|
||||
out += "},\"metrics\":{";
|
||||
first = true;
|
||||
for (const auto& [k, v] : s.metrics->snapshot()) {
|
||||
out += (first ? "" : ",") + quote(k) + ":" + std::to_string(v);
|
||||
first = false;
|
||||
}
|
||||
out += "}}";
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void register_stats_demo(fw::Router& router, Services& services) {
|
||||
Services* s = &services;
|
||||
fw::Environment* env = services.templates.get();
|
||||
|
||||
router.get("/stats", "stats-page", [env](fw::Request req) {
|
||||
(void)req;
|
||||
fw::TemplateCtx ctx;
|
||||
ctx["title"] = std::string("Statistics");
|
||||
return fw::Response(200, env->render("stats.html", ctx),
|
||||
"text/html; charset=utf-8");
|
||||
});
|
||||
|
||||
router.get("/stats/data", "stats-data", [s](fw::Request req) {
|
||||
(void)req;
|
||||
fw::Response res(200,
|
||||
"{\"http\":" + s->stats->to_json() +
|
||||
",\"services\":" + services_json(*s) + "}",
|
||||
"application/json");
|
||||
res.headers.set("Cache-Control", "no-store");
|
||||
return res;
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace molo::example::app
|
||||
Reference in New Issue
Block a user