BREP_plm 0.18.0

The BREP PLM server: parts, revisions, lifecycle, checkout, BOMs and configurable workflows over an embedded SQLite store, with a browser UI and the document API the BREP CAD app stores through.
Documentation
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//! The audit log: who changed what, and when, for every change to the store.
//!
//! # How every change is caught
//!
//! Nothing calls "log this" at each of the store's many write paths — one
//! forgotten call would be a silent hole. Instead [`crate::db::Db::mutate`],
//! which every change already goes through, compares the state before and
//! after and turns the difference into events ([`diff`]). A write path added
//! later is logged without anyone remembering to. The few events that are not
//! a change to the store — a failed sign-in, a script file edited in the
//! browser, a test run — are recorded explicitly ([`crate::db::Db::record`]).
//!
//! An event is written in the SAME SQLite transaction as its change
//! ([`crate::sql::Sql::write`]): a change is never on disk without its events,
//! and a change that is refused or rolled back leaves none.
//!
//! # Who
//!
//! The request guard knows who a request is; the store does not, and most of
//! its methods are not told. So the guard runs each request inside a
//! task-local [`Actor`] scope ([`scope`]), the blocking pool carries it across
//! ([`crate::api::blocking`]), and `mutate` reads it ([`current_actor`]).
//! Anything outside a request — start-up, a test driving the store directly —
//! is the `system` actor unless it says otherwise ([`as_actor`]).
//!
//! # Where
//!
//! Behind [`AuditSink`], so the storage can change without the rest noticing.
//! The store's sink is the `audit` table of `plm.sqlite` ([`crate::sql::Sql`]),
//! with an index for each filter the API offers. Nothing is ever deleted from
//! it.
//!
//! # Actions
//!
//! `create`, `update`, `delete` for anything; and more specific ones where a
//! field says what happened:
//! * revisions: `release`, `submit` (to in review), `supersede`, `obsolete`,
//!   `return-to-draft`, `checkout`, `checkin`, `break-lock`, `document`,
//!   `uses`, `bake`;
//! * parts: `sourcing`;
//! * users: `password`, `groups`, `enable`, `disable`;
//! * tokens: `revoke` (a deleted token);
//! * sign-ins: `sign-in`, `session-ended`, `sign-in-failed`;
//! * scripts: `script-write`, `script-delete`, `script-run`.
//! * preferences (kind `preference`, entity `<user id>/<key>`): `update`,
//!   `delete` — written by [`crate::sql::Sql::set_preference`] in the write's
//!   own transaction, since preferences do not go through `mutate`; the
//!   `@recovery` mirror is not logged ([`crate::api::preferences`]).

use std::collections::{BTreeMap, BTreeSet};
use std::io;

use serde::Deserialize;
use serde_json::{Map, Value};

use crate::model::{Actor, AuditEvent, EntityRef, FieldChange, Timestamp};

// ===========================================================================
// Who is acting
// ===========================================================================

tokio::task_local! {
    static ACTOR: Actor;
}

/// Who the current request is, or the `system` actor outside one.
pub fn current_actor() -> Actor {
    ACTOR.try_with(Clone::clone).unwrap_or_else(|_| Actor::system())
}

/// Run `work` as `actor`, synchronously — the blocking pool, a test.
pub fn as_actor<R>(actor: Actor, work: impl FnOnce() -> R) -> R {
    ACTOR.sync_scope(actor, work)
}

/// Run a future as `actor` — the request guard.
pub async fn scope<F: std::future::Future>(actor: Actor, future: F) -> F::Output {
    ACTOR.scope(actor, future).await
}

// ===========================================================================
// Where events go
// ===========================================================================

/// What an event store must do. Appends assign the ids; nothing edits or
/// deletes an event.
pub trait AuditSink: Send + Sync {
    /// Append `events` in order, numbering each. Returns them numbered.
    fn append(&self, events: Vec<AuditEvent>) -> io::Result<Vec<AuditEvent>>;
    /// The events matching `filter`, newest first.
    fn query(&self, filter: &AuditFilter) -> io::Result<Vec<AuditEvent>>;
}

/// Narrowing a query. Every field is optional; empty means no constraint.
#[derive(Debug, Clone, Default, Deserialize)]
pub struct AuditFilter {
    /// The entity's kind (`part`, `revision`, `user`, …).
    #[serde(default)]
    pub kind: String,
    /// The entity's id.
    #[serde(default)]
    pub entity: String,
    /// A part's id: the part's own events AND its revisions'.
    #[serde(default)]
    pub part: String,
    /// A user id or username (the username ignoring ASCII case).
    #[serde(default)]
    pub user: String,
    #[serde(default)]
    pub action: String,
    /// Unix seconds, inclusive.
    #[serde(default)]
    pub since: Option<Timestamp>,
    #[serde(default)]
    pub until: Option<Timestamp>,
    /// Only events older than this id — the next page.
    #[serde(default)]
    pub before: Option<u64>,
    /// At most this many; the API caps it.
    #[serde(default)]
    pub limit: Option<usize>,
    /// Only these kinds (the API narrows a non-administrator to parts and
    /// revisions). Empty: any.
    #[serde(skip)]
    pub kinds: Vec<String>,
}

impl AuditFilter {
    pub fn matches(&self, event: &AuditEvent) -> bool {
        (self.kind.is_empty() || event.entity.kind == self.kind)
            && (self.kinds.is_empty() || self.kinds.iter().any(|k| *k == event.entity.kind))
            && (self.entity.is_empty() || event.entity.id == self.entity)
            && (self.part.is_empty()
                || event.entity.part_id == self.part
                || (event.entity.kind == "part" && event.entity.id == self.part))
            && (self.user.is_empty()
                || event.actor.user_id == self.user
                || event.actor.username.eq_ignore_ascii_case(&self.user))
            && (self.action.is_empty() || event.action == self.action)
            && self.since.is_none_or(|t| event.at >= t)
            && self.until.is_none_or(|t| event.at <= t)
            && self.before.is_none_or(|id| event.id < id)
    }
}

// ===========================================================================
// Turning a change into events
// ===========================================================================

/// Fields whose change alone is not worth an event: bookkeeping that moves
/// with every real change, or on every request.
const QUIET_FIELDS: [&str; 4] = ["modified_at", "last_seen", "last_used_at", "next"];

/// Fields whose values are secrets: the event says they changed, never what to.
const SECRET_FIELDS: [&str; 3] = ["password", "hash", "csrf"];

/// Store keys that are the store's own bookkeeping, not anyone's record.
const BOOKKEEPING: [&str; 3] = ["seq", "changes", "changes_from"];

/// Collections of records keyed by `id`: the store key, the entity kind, and
/// the field a person calls one by.
const COLLECTIONS: [(&str, &str, &str); 7] = [
    ("workspace", "workspace", "name"),
    ("users", "user", "username"),
    ("api_tokens", "token", "name"),
    ("part_types", "part-type", "name"),
    ("categories", "category", "name"),
    ("manufacturers", "manufacturer", "name"),
    ("suppliers", "supplier", "name"),
];

/// Every event that turns the store from `before` into `after`, by `actor`
/// at `at`, stamped with the new `seq`. Ids are left 0 for the sink.
pub fn diff(before: &Value, after: &Value, actor: &Actor, at: Timestamp, seq: u64) -> Vec<AuditEvent> {
    let mut out = Vec::new();
    let mut push = |action: &str, entity: EntityRef, changes: BTreeMap<String, FieldChange>| {
        out.push(AuditEvent {
            id: 0,
            at,
            actor: actor.clone(),
            action: action.to_string(),
            entity,
            changes,
            detail: String::new(),
            seq,
        });
    };
    let empty = Map::new();
    let before_map = before.as_object().unwrap_or(&empty);
    let after_map = after.as_object().unwrap_or(&empty);
    let keys: BTreeSet<&String> = before_map.keys().chain(after_map.keys()).collect();

    for key in keys {
        let old = before_map.get(key).unwrap_or(&Value::Null);
        let new = after_map.get(key).unwrap_or(&Value::Null);
        if old == new || BOOKKEEPING.contains(&key.as_str()) {
            continue;
        }
        match key.as_str() {
            "parts" => diff_parts(old, new, &mut push),
            "sessions" => diff_sessions(old, new, before_map, after_map, &mut push),
            "settings" => {
                let changes = field_changes(old, new, &[]);
                if !changes.is_empty() {
                    push("update", entity("settings", "settings", "Settings", ""), changes);
                }
            }
            name => {
                if let Some((_, kind, label)) = COLLECTIONS.iter().find(|(store, _, _)| *store == name) {
                    diff_collection(old, new, kind, label, &mut push);
                } else if keyed(old) && keyed(new) {
                    // A collection a later slice adds: logged by its store
                    // name, rather than silently not at all.
                    diff_collection(old, new, name, "name", &mut push);
                } else {
                    let mut changes = BTreeMap::new();
                    changes.insert(name.to_string(), FieldChange { before: old.clone(), after: new.clone() });
                    push("update", entity(name, name, name, ""), changes);
                }
            }
        }
    }
    out
}

fn entity(kind: &str, id: &str, label: &str, part_id: &str) -> EntityRef {
    EntityRef { kind: kind.into(), id: id.into(), label: label.into(), part_id: part_id.into() }
}

/// Whether `value` is a list of records with ids (or empty).
fn keyed(value: &Value) -> bool {
    match value {
        Value::Array(items) => items.iter().all(|item| item.get("id").and_then(Value::as_str).is_some()),
        Value::Null => true,
        _ => false,
    }
}

fn by_id(value: &Value) -> BTreeMap<String, &Value> {
    value
        .as_array()
        .map(|items| {
            items
                .iter()
                .filter_map(|item| Some((item.get("id")?.as_str()?.to_string(), item)))
                .collect()
        })
        .unwrap_or_default()
}

fn text<'a>(value: &'a Value, field: &str) -> &'a str {
    value.get(field).and_then(Value::as_str).unwrap_or("")
}

/// The fields that differ between two records, minus `skip` and the quiet
/// ones, with secrets redacted. A missing record is `null`: every field of
/// the other side appears.
fn field_changes(before: &Value, after: &Value, skip: &[&str]) -> BTreeMap<String, FieldChange> {
    let empty = Map::new();
    let old = before.as_object().unwrap_or(&empty);
    let new = after.as_object().unwrap_or(&empty);
    let mut changes = BTreeMap::new();
    for key in old.keys().chain(new.keys()).collect::<BTreeSet<_>>() {
        if skip.contains(&key.as_str()) || QUIET_FIELDS.contains(&key.as_str()) {
            continue;
        }
        let a = old.get(key).cloned().unwrap_or(Value::Null);
        let b = new.get(key).cloned().unwrap_or(Value::Null);
        if a == b {
            continue;
        }
        let change = if SECRET_FIELDS.contains(&key.as_str()) {
            let mark = |v: &Value| if v.is_null() { Value::Null } else { Value::from("(secret)") };
            FieldChange { before: mark(&a), after: mark(&b) }
        } else {
            FieldChange { before: a, after: b }
        };
        changes.insert(key.clone(), change);
    }
    changes
}

fn diff_collection(
    old: &Value,
    new: &Value,
    kind: &str,
    label_field: &str,
    push: &mut impl FnMut(&str, EntityRef, BTreeMap<String, FieldChange>),
) {
    let before = by_id(old);
    let after = by_id(new);
    for id in before.keys().chain(after.keys()).collect::<BTreeSet<_>>() {
        let (a, b) = (before.get(id).copied(), after.get(id).copied());
        let record = b.or(a).unwrap_or(&Value::Null);
        let who = entity(kind, id, text(record, label_field), "");
        let changes = field_changes(a.unwrap_or(&Value::Null), b.unwrap_or(&Value::Null), &[]);
        match (a, b) {
            (None, Some(_)) => push("create", who, changes),
            (Some(_), None) => push(if kind == "token" { "revoke" } else { "delete" }, who, changes),
            _ if changes.is_empty() => {}
            _ => {
                let action = match kind {
                    "user" => user_action(&changes),
                    "workspace" => workspace_action(&changes),
                    _ => "update",
                };
                push(action, who, changes);
            }
        }
    }
}

/// A workspace entry's change, named by what moved: a new file version
/// (a replace or a restore), a move to another folder, a rename, or a link
/// pinned or set to follow, or a new owner.
fn workspace_action(changes: &BTreeMap<String, FieldChange>) -> &'static str {
    let moved: Vec<&str> = changes.keys().map(String::as_str).filter(|k| !QUIET_FIELDS.contains(k)).collect();
    match moved.as_slice() {
        ["versions"] => "version",
        ["parent"] | ["name", "parent"] | ["owner", "parent"] | ["name", "owner", "parent"] => "move",
        ["name"] => "rename",
        ["link"] => "pin",
        ["owner"] => "owner",
        _ => "update",
    }
}

fn user_action(changes: &BTreeMap<String, FieldChange>) -> &'static str {
    let only = |field: &str| changes.len() == 1 && changes.contains_key(field);
    if only("password") {
        "password"
    } else if only("groups") {
        "groups"
    } else if only("active") {
        if changes["active"].after == Value::Bool(true) { "enable" } else { "disable" }
    } else {
        "update"
    }
}

fn diff_parts(old: &Value, new: &Value, push: &mut impl FnMut(&str, EntityRef, BTreeMap<String, FieldChange>)) {
    let before = by_id(old);
    let after = by_id(new);
    for id in before.keys().chain(after.keys()).collect::<BTreeSet<_>>() {
        let (a, b) = (before.get(id).copied(), after.get(id).copied());
        let part = b.or(a).unwrap_or(&Value::Null);
        let number = text(part, "number").to_string();
        let changes = field_changes(a.unwrap_or(&Value::Null), b.unwrap_or(&Value::Null), &["revisions"]);
        let who = entity("part", id, &number, "");
        match (a, b) {
            (None, Some(_)) => push("create", who, changes),
            (Some(_), None) => push("delete", who, changes),
            _ if changes.is_empty() => {}
            _ => {
                let action = if changes.len() == 1 && changes.contains_key("sourcing") { "sourcing" } else { "update" };
                push(action, who, changes);
            }
        }
        let revs_before = by_id(a.and_then(|p| p.get("revisions")).unwrap_or(&Value::Null));
        let revs_after = by_id(b.and_then(|p| p.get("revisions")).unwrap_or(&Value::Null));
        for rev in revs_before.keys().chain(revs_after.keys()).collect::<BTreeSet<_>>() {
            let (x, y) = (revs_before.get(rev).copied(), revs_after.get(rev).copied());
            let record = y.or(x).unwrap_or(&Value::Null);
            let label = format!("{number} rev {}", text(record, "label"));
            let who = entity("revision", rev, &label, id);
            let changes = field_changes(x.unwrap_or(&Value::Null), y.unwrap_or(&Value::Null), &[]);
            match (x, y) {
                (None, Some(_)) => push("create", who, changes),
                (Some(_), None) => push("delete", who, changes),
                _ if changes.is_empty() => {}
                _ => {
                    let action = revision_action(&changes);
                    push(action, who, changes);
                }
            }
        }
    }
}

/// The one word that best says what happened to a revision. Several fields
/// can change at once (a release also stamps who and when); the most
/// significant one names the event, and every field is still in `changes`.
fn revision_action(changes: &BTreeMap<String, FieldChange>) -> &'static str {
    // A reviewer's decision names the event even when it also moved the
    // lifecycle (a rejection sends the revision back to Draft).
    if let Some(change) = changes.get("reviews") {
        if let Some(verdict) = new_decision(&change.before, &change.after) {
            return verdict;
        }
        if let Some(status) = last_review_status(&change.after) {
            if status == "withdrawn" && last_review_status(&change.before) != Some("withdrawn") {
                return "withdraw";
            }
        }
    }
    if let Some(change) = changes.get("lifecycle") {
        return match change.after.as_str().unwrap_or("") {
            "released" => "release",
            "inreview" => "submit",
            "superseded" => "supersede",
            "obsolete" => "obsolete",
            "draft" => "return-to-draft",
            _ => "update",
        };
    }
    if let Some(change) = changes.get("lock") {
        return match (change.before.is_null(), change.after.is_null()) {
            (true, false) => "checkout",
            (false, true) => {
                // The holder checking in, or someone else breaking the lock:
                // the diff cannot tell who acted, so the caller's name is in
                // the actor and the holder's in `before`.
                "checkin"
            }
            _ => "checkout",
        };
    }
    if changes.contains_key("content_hash") {
        return "document";
    }
    if changes.contains_key("uses") {
        return "uses";
    }
    if changes.contains_key("bake") {
        return "bake";
    }
    if changes.contains_key("reviews") {
        return "review-update";
    }
    if changes.contains_key("comments") {
        return "comment";
    }
    "update"
}

/// The verdict of a decision `after` has that `before` did not, in the last
/// review round of a `reviews` list.
pub(crate) fn new_decision(before: &Value, after: &Value) -> Option<&'static str> {
    let decisions = |v: &Value| -> usize {
        v.as_array()
            .and_then(|rounds| rounds.last())
            .and_then(|r| r.get("decisions"))
            .and_then(Value::as_array)
            .map(Vec::len)
            .unwrap_or(0)
    };
    let same_round = |v: &Value| v.as_array().and_then(|r| r.last()).and_then(|r| r.get("id")).cloned();
    let grew = decisions(after) > decisions(before) || (same_round(before) != same_round(after) && decisions(after) > 0);
    if !grew {
        return None;
    }
    let verdict = after
        .as_array()?
        .last()?
        .get("decisions")?
        .as_array()?
        .last()?
        .get("verdict")?
        .as_str()?;
    Some(if verdict == "reject" { "reject" } else { "approve" })
}

/// The status of the last round in a `reviews` list.
pub(crate) fn last_review_status(value: &Value) -> Option<&str> {
    value.as_array()?.last()?.get("status")?.as_str()
}

fn diff_sessions(
    old: &Value,
    new: &Value,
    before: &Map<String, Value>,
    after: &Map<String, Value>,
    push: &mut impl FnMut(&str, EntityRef, BTreeMap<String, FieldChange>),
) {
    // Keyed by token, which never leaves this function: the event names the
    // user, not the session.
    let tokens = |value: &Value| -> BTreeMap<String, String> {
        value
            .as_array()
            .map(|items| {
                items
                    .iter()
                    .filter_map(|s| Some((s.get("token")?.as_str()?.to_string(), s.get("user_id")?.as_str()?.to_string())))
                    .collect()
            })
            .unwrap_or_default()
    };
    let username = |user_id: &str| -> String {
        for map in [after, before] {
            if let Some(users) = map.get("users").and_then(Value::as_array) {
                if let Some(user) = users.iter().find(|u| text(u, "id") == user_id) {
                    return text(user, "username").to_string();
                }
            }
        }
        String::new()
    };
    let (a, b) = (tokens(old), tokens(new));
    for (token, user_id) in &b {
        if !a.contains_key(token) {
            push("sign-in", entity("user", user_id, &username(user_id), ""), BTreeMap::new());
        }
    }
    // Several sessions of one user ending at once (sign out everywhere) are
    // one event, with the count.
    let mut ended: BTreeMap<&str, u64> = BTreeMap::new();
    for (token, user_id) in &a {
        if !b.contains_key(token) {
            *ended.entry(user_id).or_default() += 1;
        }
    }
    for (user_id, count) in ended {
        let mut changes = BTreeMap::new();
        changes.insert("sessions".to_string(), FieldChange { before: Value::from(count), after: Value::from(0) });
        push("session-ended", entity("user", user_id, &username(user_id), ""), changes);
    }
}

/// The lock change of a `checkin` event names whose lock it was; when that
/// is not the actor, the event is really a broken lock.
pub fn refine(mut events: Vec<AuditEvent>) -> Vec<AuditEvent> {
    for event in &mut events {
        if event.action == "checkin" {
            let holder = event
                .changes
                .get("lock")
                .and_then(|c| c.before.get("user_id"))
                .and_then(Value::as_str)
                .unwrap_or("");
            if !holder.is_empty() && holder != event.actor.user_id {
                event.action = "break-lock".into();
            }
        }
    }
    events
}

/// CSV of events, for the admin's export.
pub fn to_csv(events: &[AuditEvent]) -> String {
    let mut out = String::from("id,at,user,via,ip,action,kind,entity,label,part,changes,detail,seq\n");
    for e in events {
        let changes = if e.changes.is_empty() {
            String::new()
        } else {
            serde_json::to_string(&e.changes).unwrap_or_default()
        };
        let row = [
            e.id.to_string(),
            e.at.to_string(),
            e.actor.username.clone(),
            e.actor.via.clone(),
            e.actor.ip.clone(),
            e.action.clone(),
            e.entity.kind.clone(),
            e.entity.id.clone(),
            e.entity.label.clone(),
            e.entity.part_id.clone(),
            changes,
            e.detail.clone(),
            e.seq.to_string(),
        ];
        out.push_str(&row.iter().map(|f| crate::bom::csv_cell(f)).collect::<Vec<_>>().join(","));
        out.push('\n');
    }
    out
}