aboutsummaryrefslogtreecommitdiffstats
path: root/pkgs/populate/src/record.rs
blob: dd52bda60e6d1466f96f46733fad93aa4b5f34f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//! The journal of runs, in the sentinel, and the record of the last run
//! that finished, which sums it up, for consumers of the target directory.
//!
//! The journal has a line of JSON for each entry: {"run": STARTED} when a
//! run starts, {"begin": NAME} before delivering the source NAME, or
//! changing it later, {"end": NAME, "changed": CHANGED, "delivered":
//! DELIVERED} after that, and {"pruned": true} after pruning removed
//! anything.  A run that doesn't finish leaves it there, and the next run
//! adds to it, so the record of the next run that finishes tells what
//! changed since the last record, also what runs that didn't finish
//! changed.

use crate::spec::Spec;
use serde_json::{json, Map, Value};
use std::collections::{BTreeMap, BTreeSet};
use std::time::{SystemTime, UNIX_EPOCH};

/// The version of the record's format.
const VERSION: u64 = 0;

/// The time now, in UTC, like 2026-11-20T12:34:56Z.
pub fn now() -> String {
    let seconds = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or_default();
    timestamp(seconds)
}

/// seconds since the epoch, in UTC, like 2026-11-20T12:34:56Z.
fn timestamp(seconds: u64) -> String {
    let (days, rest) = (seconds / 86400, seconds % 86400);
    // The proleptic Gregorian calendar's date of a day since the epoch,
    // after Howard Hinnant's civil_from_days, with eras of 400 years
    // starting on March 1st.
    let z = days as i64 + 719468;
    let era = z.div_euclid(146097);
    let day_of_era = z.rem_euclid(146097);
    let year_of_era =
        (day_of_era - day_of_era / 1460 + day_of_era / 36524 - day_of_era / 146096) / 365;
    let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
    let month_index = (5 * day_of_year + 2) / 153;
    let day = day_of_year - (153 * month_index + 2) / 5 + 1;
    let month = if month_index < 10 {
        month_index + 3
    } else {
        month_index - 9
    };
    let year = year_of_era + era * 400 + i64::from(month <= 2);
    format!(
        "{year:04}-{month:02}-{day:02}T{:02}:{:02}:{:02}Z",
        rest / 3600,
        rest % 3600 / 60,
        rest % 60
    )
}

/// The record of a run that started at started, delivering spec, with the
/// lines of the journal that runs before it left, and its own entries.
pub fn record(spec: &Spec, started: &str, previous: &[String], entries: &[Value]) -> Value {
    let mut open = BTreeSet::new();
    let mut changed = BTreeSet::new();
    let mut pruned = false;
    let mut delivered: BTreeMap<&str, Map<String, Value>> = BTreeMap::new();
    let mut unreadable = false;
    let previous: Vec<Value> = previous
        .iter()
        .filter(|line| !line.is_empty())
        .filter_map(|line| {
            let entry = serde_json::from_str(line).ok();
            unreadable |= entry.is_none();
            entry
        })
        .collect();
    let current = previous.len();
    for (i, entry) in previous.iter().chain(entries).enumerate() {
        if entry.get("run").is_some() {
            // What a run that didn't finish began may have changed.
            changed.append(&mut open);
        } else if let Some(name) = entry.get("begin").and_then(Value::as_str) {
            open.insert(name.to_string());
        } else if let Some(name) = entry.get("end").and_then(Value::as_str) {
            open.remove(name);
            if entry.get("changed").and_then(Value::as_bool) != Some(false) {
                changed.insert(name.to_string());
            }
            if i >= current {
                if let Some(Value::Object(fields)) = entry.get("delivered") {
                    let fields = fields.clone().into_iter();
                    delivered.entry(name).or_default().extend(fields);
                }
            }
        } else if entry.get("pruned").is_some() {
            pruned = true;
        }
    }
    changed.append(&mut open);
    let sources: Map<String, Value> = spec
        .source
        .iter()
        .map(|(name, source)| {
            let mut fields = Map::new();
            fields.insert("type".to_string(), json!(source.kind));
            fields.insert(
                "changed".to_string(),
                json!(unreadable || changed.contains(name)),
            );
            fields.extend(delivered.remove(name.as_str()).unwrap_or_default());
            (name.clone(), Value::Object(fields))
        })
        .collect();
    json!({
        "version": VERSION,
        "populate": env!("CARGO_PKG_VERSION"),
        "started": started,
        "finished": now(),
        "changed": unreadable || pruned || !changed.is_empty(),
        "sources": sources,
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn timestamps() {
        assert_eq!(timestamp(0), "1970-01-01T00:00:00Z");
        assert_eq!(timestamp(951_782_400), "2000-02-29T00:00:00Z");
        assert_eq!(timestamp(1_795_178_096), "2026-11-20T12:34:56Z");
    }
}