ppforest2 v0.1.0
Projection Pursuit Decision Trees and Random Forests
Loading...
Searching...
No Matches
ServeHandlers.hpp
Go to the documentation of this file.
1
8#pragma once
9
10#include "models/Model.hpp"
11#include "stats/Metrics.hpp"
12#include "utils/Types.hpp"
13
14#include <nlohmann/json.hpp>
15
16#include <cstddef>
17#include <deque>
18#include <map>
19#include <mutex>
20#include <optional>
21#include <string>
22#include <unordered_map>
23
33
35 struct Response {
36 int status = 200;
37 std::string body;
38 std::string content_type = "application/json";
39 std::map<std::string, std::string> headers;
40 };
41
47 types::FeatureMatrix proportions; // populated for classification only
48 std::optional<stats::Metrics> metrics; // present when the request carried a response column
49 };
50
60 public:
61 static constexpr std::size_t DEFAULT_CAPACITY = 32;
62
63 explicit PredictionStore(std::size_t capacity = DEFAULT_CAPACITY);
64
66 std::string put(PredictionResult result);
67
69 std::optional<PredictionResult> get(std::string const& id) const;
70
71 private:
72 mutable std::mutex mu;
73 std::size_t capacity;
74 std::deque<std::string> order;
75 std::unordered_map<std::string, PredictionResult> entries;
76 };
77
79 Response handle_health(std::string const& version);
80
86
92
103 Response handle_predict(LoadedModel const& loaded, PredictionStore& store, std::string const& csv_body);
104
111 Response handle_predict_html(LoadedModel const& loaded, PredictionStore& store, std::string const& csv_body);
112
118 Response handle_predict_view(LoadedModel const& loaded, PredictionStore const& store, std::string const& id_query);
119}
Mode-specific evaluation metric blocks.
std::shared_ptr< Model > Ptr
Definition Model.hpp:31
In-memory bounded store of recent prediction results, keyed by opaque IDs that survive a page refresh...
Definition ServeHandlers.hpp:59
std::string put(PredictionResult result)
Store result and return its newly-generated id.
std::optional< PredictionResult > get(std::string const &id) const
Look up id; returns nullopt if not in the store (evicted or never existed).
PredictionStore(std::size_t capacity=DEFAULT_CAPACITY)
static constexpr std::size_t DEFAULT_CAPACITY
Definition ServeHandlers.hpp:61
Definition ServeHandlers.hpp:24
Response handle_predict_view(LoadedModel const &loaded, PredictionStore const &store, std::string const &id_query)
GET /predict → 200 with the predict page (always showing the upload form). When id_query refers to an...
Response handle_predict_html(LoadedModel const &loaded, PredictionStore &store, std::string const &csv_body)
POST /predict (Accept: text/html) → 200 with the predict page rendered against the new result....
Response handle_summarize(LoadedModel const &loaded)
GET / (Accept: application/json) → 200 with the loaded model JSON, minus the heavy model field.
Response handle_summary_html(LoadedModel const &loaded)
GET / (Accept: text/html) → 200 with a self-contained HTML page rendering meta, training metrics,...
Response handle_health(std::string const &version)
GET /health → 200 with status + version.
Response handle_predict(LoadedModel const &loaded, PredictionStore &store, std::string const &csv_body)
POST /predict (Accept: application/json) → 200 with {predictions, [proportions], [metrics],...
std::vector< std::string > Names
Vector of name strings — used uniformly for class labels (group_names[i] is the label for GroupId == ...
Definition Types.hpp:49
Eigen::Matrix< Feature, Eigen::Dynamic, Eigen::Dynamic > FeatureMatrix
Dynamic-size matrix of feature values.
Definition Types.hpp:33
Eigen::Matrix< Outcome, Eigen::Dynamic, 1 > OutcomeVector
Dynamic-size column vector of predictions.
Definition Types.hpp:42
Mode
Training mode.
Definition Types.hpp:58
@ Classification
Definition Types.hpp:58
Immutable model snapshot captured once at server startup.
Definition ServeHandlers.hpp:26
Model::Ptr model
Definition ServeHandlers.hpp:27
nlohmann::json model_json
Definition ServeHandlers.hpp:31
types::Names feature_names
Definition ServeHandlers.hpp:29
types::Names group_names
Definition ServeHandlers.hpp:28
types::Mode mode
Definition ServeHandlers.hpp:30
Result of running a prediction request — kept around for the predictions store so the server can serv...
Definition ServeHandlers.hpp:45
types::OutcomeVector predictions
Definition ServeHandlers.hpp:46
types::FeatureMatrix proportions
Definition ServeHandlers.hpp:47
std::optional< stats::Metrics > metrics
Definition ServeHandlers.hpp:48
Status, body, MIME content type, and extra response headers.
Definition ServeHandlers.hpp:35
std::string body
Definition ServeHandlers.hpp:37
std::map< std::string, std::string > headers
Definition ServeHandlers.hpp:39
int status
Definition ServeHandlers.hpp:36
std::string content_type
Definition ServeHandlers.hpp:38