1use std::sync::Arc;
4
5use jiff::Timestamp;
6use schedr_db::Database;
7use schedr_model::SchedrVersion;
8
9use crate::auth::Auth;
10use crate::events::Feed;
11use crate::logs::{HttpLogSource, LogSource};
12use crate::metrics::ApiMetrics;
13
14#[cfg(test)]
15mod tests;
16
17pub use crate::auth::ANONYMOUS;
18
19#[derive(Debug, Clone)]
21pub struct ApiState {
22 pub db: Database,
24 pub version: SchedrVersion,
26 pub version_text: &'static str,
28 pub auth: Auth,
30 pub clock: fn() -> Timestamp,
32 pub logs: Arc<dyn LogSource>,
34 pub metrics: ApiMetrics,
36 pub connection_test_timeout: std::time::Duration,
38 pub heartbeat_interval: std::time::Duration,
41 pub feed: Feed,
43 pub max_event_size: usize,
45 pub node: Arc<crate::views::NodeView>,
48 pub usage: fn() -> Option<schedr_model::usage::NodeUsage>,
50 pub series: Arc<crate::series::cache::SeriesCache>,
52 pub flow: Arc<crate::task_flow::FlowCache>,
54 pub limits: ApiLimits,
56}
57
58impl ApiState {
59 #[must_use]
62 pub fn new(db: Database, identity_header: Option<String>) -> Self {
63 let feed = Feed::new(db.clone());
64 Self {
65 db,
66 version: SchedrVersion::current(),
67 version_text: env!("CARGO_PKG_VERSION"),
68 auth: Auth::identity(identity_header),
69 clock: Timestamp::now,
70 logs: Arc::new(HttpLogSource::new(None)),
71 metrics: ApiMetrics::default(),
72 connection_test_timeout: crate::routes::CONNECTION_TEST_TIMEOUT,
73 heartbeat_interval: std::time::Duration::from_secs(10),
75 feed,
76 max_event_size: crate::named_events::DEFAULT_MAX_EVENT_SIZE,
77 node: Arc::new(crate::views::NodeView {
78 process_id: String::new(),
79 host: String::new(),
80 schedr_version: env!("CARGO_PKG_VERSION").to_owned(),
81 config: serde_json::json!({}),
82 usage: None,
83 }),
84 usage: || None,
85 series: Arc::default(),
86 flow: Arc::default(),
87 limits: ApiLimits::default(),
88 }
89 }
90
91 #[must_use]
93 pub fn with_limits(self, limits: ApiLimits, cache: crate::series::cache::CacheLimits) -> Self {
94 Self {
95 limits,
96 series: Arc::new(crate::series::cache::SeriesCache::new(cache)),
97 ..self
98 }
99 }
100
101 #[must_use]
103 pub fn with_connection_test_timeout(self, timeout: std::time::Duration) -> Self {
104 Self {
105 connection_test_timeout: timeout,
106 ..self
107 }
108 }
109
110 #[must_use]
112 pub fn with_heartbeat_interval(self, interval: std::time::Duration) -> Self {
113 Self {
114 heartbeat_interval: interval,
115 ..self
116 }
117 }
118
119 #[must_use]
121 pub fn with_max_event_size(self, bytes: usize) -> Self {
122 Self {
123 max_event_size: bytes,
124 ..self
125 }
126 }
127
128 #[must_use]
130 pub fn with_usage(self, usage: fn() -> Option<schedr_model::usage::NodeUsage>) -> Self {
131 Self { usage, ..self }
132 }
133
134 #[must_use]
136 pub fn with_node(self, node: crate::views::NodeView) -> Self {
137 Self {
138 node: Arc::new(node),
139 ..self
140 }
141 }
142
143 #[must_use]
145 pub fn with_metrics(self, metrics: ApiMetrics) -> Self {
146 Self { metrics, ..self }
147 }
148
149 #[must_use]
152 pub fn with_refresh(self, interval: std::time::Duration) -> Self {
153 Self {
154 feed: self.feed.clone().with_refresh(interval),
155 ..self
156 }
157 }
158
159 #[must_use]
161 pub fn with_event_timings(self, timings: crate::events::EventTimings) -> Self {
162 Self {
163 feed: self.feed.clone().with_timings(timings),
164 ..self
165 }
166 }
167
168 #[must_use]
170 pub fn with_auth(self, auth: Auth) -> Self {
171 Self { auth, ..self }
172 }
173
174 #[must_use]
176 pub fn with_logs(self, logs: Arc<dyn LogSource>) -> Self {
177 Self { logs, ..self }
178 }
179}
180
181#[derive(Debug, Clone, Copy, PartialEq, Eq)]
184pub struct ApiLimits {
185 pub memory_peaks_window: jiff::SignedDuration,
187 pub memory_peaks_runs: u32,
189 pub event_future_tolerance: jiff::SignedDuration,
191 pub masters_heard_within: jiff::SignedDuration,
194 pub sampled_within: jiff::SignedDuration,
197}
198
199impl Default for ApiLimits {
200 fn default() -> Self {
201 Self {
202 memory_peaks_window: crate::attempt_memory::PEAKS_OVER,
203 memory_peaks_runs: schedr_db::task_memory::PEAKS_PER_TASK,
204 event_future_tolerance: crate::named_events::AT_TOLERANCE,
205 masters_heard_within: crate::routes::MASTERS_HEARD_WITHIN,
206 sampled_within: crate::routes::SAMPLED_WITHIN,
207 }
208 }
209}