wasm4pm 26.6.13

High-performance process mining algorithms in WebAssembly for JavaScript/TypeScript
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
use crate::error::{codes, wasm_err};
use crate::models::{parse_timestamp_ms, OCEL};
use crate::state::{get_or_init_state, StoredObject};
use crate::utilities::to_js;
use crate::{Data, Median};
use rustc_hash::FxHashMap;
use serde::Serialize;
use serde_json::json;
/// Object-Centric Performance Analysis (Phase 2C).
///
/// For each object type in an OCEL log, builds a performance-annotated
/// directly-follows graph with per-edge timing statistics (mean, median, p95)
/// computed from event timestamps.
use wasm_bindgen::prelude::*; // Conditional import: statrs or hand_rolled_stats

// -------------------------------------------------------------------------
// Types
// -------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize)]
struct PerformanceNode {
    id: String,
    label: String,
    frequency: usize,
}

#[derive(Debug, Clone, Serialize)]
struct PerformanceEdge {
    from: String,
    to: String,
    count: usize,
    mean_ms: f64,
    median_ms: f64,
    p95_ms: f64,
}

#[derive(Debug, Clone, Serialize)]
struct PerformanceDFG {
    nodes: Vec<PerformanceNode>,
    edges: Vec<PerformanceEdge>,
    start_activities: FxHashMap<String, usize>,
    end_activities: FxHashMap<String, usize>,
}

// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------

fn get_ocel(handle: &str) -> Result<OCEL, JsValue> {
    get_or_init_state().with_object(handle, |obj| match obj {
        Some(StoredObject::OCEL(ocel)) => Ok(ocel.clone()),
        Some(_) => Err(wasm_err(codes::INVALID_INPUT, "Object is not an OCEL")),
        None => Err(wasm_err(
            codes::INVALID_HANDLE,
            format!("OCEL '{}' not found", handle),
        )),
    })
}

/// Compute mean / median / p95 from a slice of durations (ms).
/// NaN entries are filtered out.
fn compute_edge_stats(durs: &[f64]) -> (f64, f64, f64) {
    let valid: Vec<f64> = durs.iter().copied().filter(|v| v.is_finite()).collect();
    if valid.is_empty() {
        return (0.0, 0.0, 0.0);
    }
    let mean = valid.iter().sum::<f64>() / valid.len() as f64;
    let data = Data::new(valid.clone());
    let median = data.median();
    let mut sorted = valid;
    sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
    let p95_idx = ((sorted.len() as f64 - 1.0) * 0.95).round() as usize;
    let p95 = sorted[p95_idx.min(sorted.len() - 1)];
    (mean, median, p95)
}

/// Build per-type performance DFGs directly from the OCEL, without flattening.
///
/// Optimized to use single-pass aggregation: builds type → events index
/// in one pass instead of N+1 pattern (initialization + separate population).
fn build_performance_dfgs(ocel: &OCEL) -> FxHashMap<String, PerformanceDFG> {
    let mut result: FxHashMap<String, PerformanceDFG> = FxHashMap::default();

    // Pre-compute object_id → object_type lookup for O(1) access
    let obj_to_type: FxHashMap<String, &str> = ocel
        .objects
        .iter()
        .map(|obj| (obj.id.clone(), obj.object_type.as_str()))
        .collect();

    // Single-pass aggregation: build (obj_type, obj_id) → events index
    // This eliminates the N+1 pattern of initializing then populating separately
    let mut type_events: FxHashMap<&str, FxHashMap<String, Vec<(usize, &str, Option<i64>)>>> =
        FxHashMap::default();

    for (idx, event) in ocel.events.iter().enumerate() {
        let ts_ms = parse_timestamp_ms(&event.timestamp);
        for obj_id in event.all_object_ids() {
            // Get object type from pre-computed map
            if let Some(&obj_type) = obj_to_type.get(obj_id) {
                type_events
                    .entry(obj_type)
                    .or_default()
                    .entry(obj_id.to_string())
                    .or_default()
                    .push((idx, event.event_type.as_str(), ts_ms));
            }
        }
    }

    // Process each object type using the pre-built index
    for obj_type in &ocel.object_types {
        // Get the events for this type, removing from the index to allow mutation
        let mut events_by_object = type_events.remove(obj_type.as_str()).unwrap_or_default();

        // Sort by timestamp (ISO 8601 lexicographic sort)
        for events in events_by_object.values_mut() {
            events.sort_by_key(|(idx, _, _)| ocel.events[*idx].timestamp.clone());
        }

        // Activity frequencies scoped to this object type
        let mut activity_counts: FxHashMap<String, usize> = FxHashMap::default();
        for events in events_by_object.values() {
            for (_, event_type, _) in events {
                *activity_counts.entry(event_type.to_string()).or_insert(0) += 1;
            }
        }

        let nodes: Vec<PerformanceNode> = activity_counts
            .iter()
            .map(|(id, freq)| PerformanceNode {
                id: id.clone(),
                label: id.clone(),
                frequency: *freq,
            })
            .collect();

        // Edge durations + start/end
        let mut edge_times: FxHashMap<(String, String), Vec<f64>> = FxHashMap::default();
        let mut start_acts: FxHashMap<String, usize> = FxHashMap::default();
        let mut end_acts: FxHashMap<String, usize> = FxHashMap::default();

        for events in events_by_object.values() {
            if events.is_empty() {
                continue;
            }
            *start_acts.entry(events[0].1.to_string()).or_insert(0) += 1;
            *end_acts
                .entry(events[events.len() - 1].1.to_string())
                .or_insert(0) += 1;

            for pair in events.windows(2) {
                let from = pair[0].1;
                let to = pair[1].1;
                let dur = match (pair[0].2, pair[1].2) {
                    (Some(t1), Some(t2)) if t2 >= t1 => (t2 - t1) as f64,
                    _ => f64::NAN,
                };
                edge_times
                    .entry((from.to_string(), to.to_string()))
                    .or_default()
                    .push(dur);
            }
        }

        let edges: Vec<PerformanceEdge> = edge_times
            .into_iter()
            .map(|((from, to), durs)| {
                let count = durs.len();
                let (mean_ms, median_ms, p95_ms) = compute_edge_stats(&durs);
                PerformanceEdge {
                    from,
                    to,
                    count,
                    mean_ms,
                    median_ms,
                    p95_ms,
                }
            })
            .collect();

        result.insert(
            obj_type.clone(),
            PerformanceDFG {
                nodes,
                edges,
                start_activities: start_acts,
                end_activities: end_acts,
            },
        );
    }

    result
}

// -------------------------------------------------------------------------
// Public WASM API
// -------------------------------------------------------------------------

/// Analyze object-centric performance across all object types.
///
/// For each object type, builds a performance DFG with per-edge duration
/// statistics derived from event timestamps. The `timestamp_key` parameter
/// is accepted for API consistency but OCEL timestamps are always read from
/// the standard `time` / `timestamp` field of each event (ISO 8601).
///
/// Returns JSON keyed by object type:
/// ```json
/// {
///   "Order": {
///     "nodes": [{"id":"Create Order","label":"Create Order","frequency":50}],
///     "edges": [{"from":"Create Order","to":"Pay","count":45,
///                "mean_ms":86400000,"median_ms":82800000,"p95_ms":172800000}],
///     "start_activities": {"Create Order": 50},
///     "end_activities":   {"Close": 50}
///   },
///   "Item": { ... }
/// }
/// ```
#[cfg(feature = "ocel")]
#[wasm_bindgen]
pub fn analyze_oc_performance(ocel_handle: &str, _timestamp_key: &str) -> Result<JsValue, JsValue> {
    let ocel = get_ocel(ocel_handle)?;
    let result = build_performance_dfgs(&ocel);
    to_js(&result)
}

/// Compute per-object-type aggregate performance metrics from an OCEL.
///
/// Simpler than `analyze_oc_performance` — returns only min / max / mean /
/// median of all inter-event durations per object type.
///
/// Returns: JSON `{ "Order": { "min_ms": …, "max_ms": …, … }, "Item": { … } }`
#[cfg(feature = "ocel")]
pub fn oc_performance_analysis_inner(ocel: &OCEL) -> serde_json::Value {
    let mut result = serde_json::Map::new();

    let obj_to_type: FxHashMap<String, &str> = ocel
        .objects
        .iter()
        .map(|obj| (obj.id.clone(), obj.object_type.as_str()))
        .collect();

    let mut type_timestamps: FxHashMap<&str, FxHashMap<String, Vec<Option<i64>>>> =
        FxHashMap::default();

    for event in &ocel.events {
        let ts_ms = parse_timestamp_ms(&event.timestamp);
        for obj_id in event.all_object_ids() {
            if let Some(&obj_type) = obj_to_type.get(obj_id) {
                type_timestamps
                    .entry(obj_type)
                    .or_default()
                    .entry(obj_id.to_string())
                    .or_default()
                    .push(ts_ms);
            }
        }
    }

    for obj_type in &ocel.object_types {
        let events_by_object = type_timestamps
            .remove(obj_type.as_str())
            .unwrap_or_default();

        let mut durations: Vec<f64> = Vec::new();
        for timestamps in events_by_object.values() {
            let mut sorted_ts: Vec<i64> = timestamps.iter().filter_map(|t| *t).collect();
            sorted_ts.sort();
            for pair in sorted_ts.windows(2) {
                durations.push((pair[1] - pair[0]).abs() as f64);
            }
        }

        result.insert(obj_type.clone(), compute_duration_stats(&durations));
    }

    serde_json::Value::Object(result)
}

#[cfg(feature = "ocel")]
#[wasm_bindgen]
pub fn oc_performance_analysis(ocel_handle: &str) -> Result<JsValue, JsValue> {
    let ocel = get_ocel(ocel_handle)?;
    let result = oc_performance_analysis_inner(&ocel);
    to_js(&result)
}

/// Module info for capability registry.
#[cfg(feature = "ocel")]
#[wasm_bindgen]
pub fn oc_performance_info() -> JsValue {
    let info = json!({
        "module": "oc_performance",
        "description": "Object-Centric performance analysis from OCEL",
        "functions": [
            {
                "name": "analyze_oc_performance",
                "description": "Build per-type performance DFGs with edge timing stats (mean/median/p95)",
                "params": ["ocel_handle", "timestamp_key"],
                "returns": "JSON {object_type: {nodes, edges, start_activities, end_activities}}"
            },
            {
                "name": "oc_performance_analysis",
                "description": "Compute per-type aggregate performance metrics (min/max/mean/median duration)",
                "params": ["ocel_handle"],
                "returns": "JSON {object_type: {min_ms, max_ms, mean_ms, median_ms, count}}"
            }
        ]
    });

    to_js(&info).unwrap_or(JsValue::NULL)
}

// -------------------------------------------------------------------------
// Internal helpers
// -------------------------------------------------------------------------

fn compute_duration_stats(durations: &[f64]) -> serde_json::Value {
    if durations.is_empty() {
        return json!({
            "min_ms": 0.0,
            "max_ms": 0.0,
            "mean_ms": 0.0,
            "median_ms": 0.0,
            "count": 0
        });
    }

    let mut sorted = durations.to_vec();
    sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

    let min = sorted.first().copied().unwrap_or(0.0);
    let max = sorted.last().copied().unwrap_or(0.0);
    let mean = sorted.iter().sum::<f64>() / sorted.len() as f64;
    let median = if sorted.len().is_multiple_of(2) {
        (sorted[sorted.len() / 2 - 1] + sorted[sorted.len() / 2]) / 2.0
    } else {
        sorted[sorted.len() / 2]
    };

    json!({
        "min_ms": min,
        "max_ms": max,
        "mean_ms": mean,
        "median_ms": median,
        "count": sorted.len()
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::{OCELEvent, OCELObject, OCEL};

    fn create_test_ocel() -> OCEL {
        OCEL {
            event_types: vec!["A".to_string(), "B".to_string()],
            object_types: vec!["Order".to_string()],
            events: vec![
                OCELEvent {
                    id: "e1".to_string(),
                    event_type: "A".to_string(),
                    timestamp: "2024-01-01T10:00:00Z".to_string(),
                    attributes: std::collections::HashMap::new(),
                    object_ids: vec!["order1".to_string()],
                    object_refs: vec![],
                },
                OCELEvent {
                    id: "e2".to_string(),
                    event_type: "B".to_string(),
                    timestamp: "2024-01-01T11:00:00Z".to_string(),
                    attributes: std::collections::HashMap::new(),
                    object_ids: vec!["order1".to_string()],
                    object_refs: vec![],
                },
            ],
            objects: vec![OCELObject {
                id: "order1".to_string(),
                object_type: "Order".to_string(),
                attributes: std::collections::HashMap::new(),
                changes: vec![],
                embedded_relations: vec![],
            }],
            object_relations: vec![],
        }
    }

    #[test]
    fn test_oc_performance_basic() {
        let ocel = create_test_ocel();
        // Call inner pure-Rust function directly — no JsValue/WASM context needed
        let result = oc_performance_analysis_inner(&ocel);
        assert!(result.is_object(), "Result must be a JSON object");
        let obj = result.as_object().unwrap();
        assert!(obj.contains_key("Order"), "Result must contain 'Order' key");
        let order = &obj["Order"];
        assert!(order["mean_ms"].is_f64());
        assert!(order["count"].is_number());
        // 1 inter-event gap of 1 hour = 3_600_000 ms
        assert_eq!(order["count"].as_u64().unwrap(), 1);
        let mean = order["mean_ms"].as_f64().unwrap();
        assert!(
            (mean - 3_600_000.0).abs() < 1.0,
            "mean should be 1h = 3600000ms, got {}",
            mean
        );
    }

    #[test]
    fn test_oc_performance_empty_ocel() {
        let ocel = OCEL {
            event_types: vec![],
            object_types: vec![],
            events: vec![],
            objects: vec![],
            object_relations: vec![],
        };
        // Empty OCEL: result should be an empty JSON object
        let result = oc_performance_analysis_inner(&ocel);
        assert!(result.is_object());
        assert_eq!(
            result.as_object().unwrap().len(),
            0,
            "Empty OCEL should produce empty result"
        );
    }

    #[test]
    fn test_compute_duration_stats_empty() {
        let stats = compute_duration_stats(&[]);
        assert_eq!(stats["count"].as_u64().unwrap(), 0);
        assert_eq!(stats["mean_ms"].as_f64().unwrap(), 0.0);
    }

    #[test]
    fn test_compute_duration_stats_values() {
        let stats = compute_duration_stats(&[1000.0, 2000.0, 3000.0, 4000.0]);
        assert_eq!(stats["count"].as_u64().unwrap(), 4);
        assert_eq!(stats["min_ms"].as_f64().unwrap(), 1000.0);
        assert_eq!(stats["max_ms"].as_f64().unwrap(), 4000.0);
        assert_eq!(stats["mean_ms"].as_f64().unwrap(), 2500.0);
    }
}