1use std::path::Path;
23use std::sync::OnceLock;
24use std::time::{Duration, SystemTime};
25
26use schemars::JsonSchema;
27use serde::{Deserialize, Serialize};
28
29use crate::model::{CheckReport, Finding, Severity};
30use crate::reader::{Dataset, PlaneIndex};
31
32pub const DEFAULT_WINDOW_S: u64 = 300;
34
35pub const WINDOW_ENV: &str = "OPENREADOUT_LIVE_WINDOW";
37
38const TAIL_SLACK: u64 = 1 << 20;
40
41const MAX_TAIL: u64 = 64 << 20;
43
44const MIN_TAIL: u64 = 16 << 20;
46
47static WINDOW: OnceLock<Duration> = OnceLock::new();
48
49pub fn set_window(d: Duration) {
52 let _ = WINDOW.set(d);
53}
54
55pub fn window() -> Duration {
57 *WINDOW.get_or_init(|| {
58 std::env::var(WINDOW_ENV)
59 .ok()
60 .and_then(|v| v.trim().parse::<f64>().ok())
61 .filter(|s| s.is_finite() && *s >= 0.0)
62 .map_or(
63 Duration::from_secs(DEFAULT_WINDOW_S),
64 Duration::from_secs_f64,
65 )
66 })
67}
68
69#[derive(Debug, Clone, Default)]
73#[non_exhaustive]
74pub struct WriteState {
75 pub missing: Vec<String>,
78 pub complete: Vec<(u32, PlaneIndex)>,
80 pub expected_planes: Option<u64>,
82 pub tail_bytes: u64,
84 pub unit_bytes: Option<u64>,
86 pub append_consistent: bool,
89 pub modified: Option<SystemTime>,
92 pub evidence: Vec<String>,
94}
95
96impl WriteState {
97 pub fn new() -> Self {
99 WriteState {
100 append_consistent: true,
101 ..Default::default()
102 }
103 }
104}
105
106#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
108#[serde(rename_all = "snake_case")]
109#[non_exhaustive]
110pub enum AcquisitionState {
111 InProgress,
113 Interrupted,
116}
117
118impl AcquisitionState {
119 pub fn as_str(self) -> &'static str {
121 match self {
122 AcquisitionState::InProgress => "in_progress",
123 AcquisitionState::Interrupted => "interrupted",
124 }
125 }
126}
127
128#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
131pub struct Acquisition {
132 pub state: AcquisitionState,
134 pub complete_planes: u64,
136 #[serde(default, skip_serializing_if = "Option::is_none")]
138 pub expected_planes: Option<u64>,
139 pub modified_ago_s: f64,
141 pub window_s: f64,
143 pub missing: Vec<String>,
145 pub tail_bytes: u64,
147 pub evidence: Vec<String>,
149}
150
151impl Acquisition {
152 pub fn in_progress(&self) -> bool {
154 self.state == AcquisitionState::InProgress
155 }
156}
157
158pub fn seconds_since(t: SystemTime) -> f64 {
160 match SystemTime::now().duration_since(t) {
161 Ok(d) => d.as_secs_f64(),
162 Err(e) => -e.duration().as_secs_f64(),
163 }
164}
165
166pub fn assess(path: &Path, ws: &WriteState) -> Option<Acquisition> {
170 assess_with(path, ws, window())
171}
172
173pub fn assess_with(path: &Path, ws: &WriteState, window: Duration) -> Option<Acquisition> {
175 if !ws.append_consistent {
176 return None;
177 }
178 let limit = ws.unit_bytes.map_or(MAX_TAIL, |u| {
179 u.saturating_mul(2).saturating_add(TAIL_SLACK).max(MIN_TAIL)
180 });
181 if ws.tail_bytes > limit {
182 return None;
183 }
184 let modified = ws
185 .modified
186 .or_else(|| std::fs::metadata(path).and_then(|m| m.modified()).ok())?;
187 let ago = seconds_since(modified);
188 let mut evidence = ws.evidence.clone();
189 let w = window.as_secs_f64();
190 let recent = w > 0.0 && ago <= w && ago >= -60.0;
192 let state = if recent {
193 evidence.push(format!(
194 "modified {ago:.1} s ago, within the {w:.0} s live window"
195 ));
196 AcquisitionState::InProgress
197 } else {
198 evidence.push(format!(
199 "unchanged for {ago:.0} s, longer than the {w:.0} s live window"
200 ));
201 AcquisitionState::Interrupted
202 };
203 Some(Acquisition {
204 state,
205 complete_planes: ws.complete.len() as u64,
206 expected_planes: ws.expected_planes,
207 modified_ago_s: (ago * 10.0).round() / 10.0,
208 window_s: w,
209 missing: ws.missing.clone(),
210 tail_bytes: ws.tail_bytes,
211 evidence,
212 })
213}
214
215pub fn assess_dataset(ds: &dyn Dataset, path: &Path) -> Option<Acquisition> {
218 ds.write_state().and_then(|ws| assess(path, &ws))
219}
220
221pub const IN_PROGRESS_CODE: &str = "acquisition_in_progress";
223
224pub fn apply_to_check(report: &mut CheckReport, acq: &Acquisition) {
229 report.acquisition = Some(acq.clone());
230 if !acq.in_progress() {
231 return;
232 }
233 for f in &mut report.findings {
234 if f.severity == Severity::Error {
235 f.severity = Severity::Warning;
236 f.message = format!("in progress: {}", f.message);
237 }
238 }
239 report.ok = !report
240 .findings
241 .iter()
242 .any(|f| f.severity == Severity::Error);
243 let expected = acq
244 .expected_planes
245 .map_or_else(String::new, |e| format!(" of {e}"));
246 report.findings.insert(
247 0,
248 Finding::warning(
249 IN_PROGRESS_CODE,
250 format!(
251 "the file is still being written (modified {:.1} s ago; missing: {}): {}{} planes complete; `ok` covers the complete part only, run `check` again when the acquisition ends",
252 acq.modified_ago_s,
253 if acq.missing.is_empty() {
254 "nothing yet".into()
255 } else {
256 acq.missing.join(", ")
257 },
258 acq.complete_planes,
259 expected
260 ),
261 ),
262 );
263}
264
265pub fn annotate_error(path: &Path, err: crate::Error) -> crate::Error {
268 let crate::Error::Corrupt {
269 format,
270 detail,
271 offset,
272 } = err
273 else {
274 return err;
275 };
276 let w = window().as_secs_f64();
277 let ago = std::fs::metadata(path)
278 .and_then(|m| m.modified())
279 .ok()
280 .map(seconds_since);
281 match ago {
282 Some(a) if w > 0.0 && a <= w && a >= -60.0 => crate::Error::Corrupt {
283 format,
284 detail: format!(
285 "{detail} [modified {a:.1} s ago: the file may still be being written; this reader cannot read it until the acquisition ends (`openreadout watch` reports dataset_complete)]"
286 ),
287 offset,
288 },
289 _ => crate::Error::Corrupt {
290 format,
291 detail,
292 offset,
293 },
294 }
295}
296
297pub fn complete_set(ws: &WriteState) -> std::collections::HashSet<(u32, PlaneIndex)> {
300 ws.complete.iter().copied().collect()
301}
302
303#[cfg(test)]
304mod tests {
305 use super::*;
306
307 fn ws(tail: u64) -> WriteState {
308 let mut w = WriteState::new();
309 w.missing.push("chunk map".into());
310 w.complete.push((0, PlaneIndex::default()));
311 w.tail_bytes = tail;
312 w.unit_bytes = Some(4096);
313 w.modified = Some(SystemTime::now());
314 w
315 }
316
317 #[test]
318 fn recent_and_consistent_is_in_progress() {
319 let a = assess_with(Path::new("x"), &ws(100), Duration::from_secs(60)).unwrap();
320 assert_eq!(a.state, AcquisitionState::InProgress);
321 assert_eq!(a.complete_planes, 1);
322 }
323
324 #[test]
325 fn old_is_interrupted() {
326 let mut w = ws(100);
327 w.modified = Some(SystemTime::now() - Duration::from_secs(3600));
328 let a = assess_with(Path::new("x"), &w, Duration::from_secs(60)).unwrap();
329 assert_eq!(a.state, AcquisitionState::Interrupted);
330 }
331
332 #[test]
333 fn damage_or_huge_tail_is_not_an_acquisition() {
334 let mut w = ws(100);
335 w.append_consistent = false;
336 assert!(assess_with(Path::new("x"), &w, Duration::from_secs(60)).is_none());
337 let w = ws(1 << 30);
338 assert!(assess_with(Path::new("x"), &w, Duration::from_secs(60)).is_none());
339 }
340
341 #[test]
342 fn zero_window_disables() {
343 let a = assess_with(Path::new("x"), &ws(0), Duration::ZERO).unwrap();
344 assert_eq!(a.state, AcquisitionState::Interrupted);
345 }
346
347 #[test]
348 fn check_findings_downgraded_only_in_progress() {
349 let mut r = CheckReport::new("x", "nd2");
350 r.push(Finding::error("truncated", "chunk map missing"));
351 let a = assess_with(Path::new("x"), &ws(0), Duration::from_secs(60)).unwrap();
352 apply_to_check(&mut r, &a);
353 assert!(r.ok);
354 assert_eq!(r.findings[0].code, IN_PROGRESS_CODE);
355 assert!(r.findings[1].message.starts_with("in progress:"));
356 }
357}