use eventlevels; use samplequota; fn raven_num(value: i64) -> f64 { at([value], 0) } fn raven_finite(values: vector) -> bool { let mut i = 0; while i < len(values) { let value = at(values, i); if !(value - value == 0.0) { return false; } i += 1; } true } fn raven_counts(values: vector) -> bool { let mut i = 0; while i < len(values) { let value = at(values, i); if !(value >= 0.0 && value <= 9007199254740991.0 && value % 1.0 == 0.0) { return false; } i += 1; } true } fn raven_compute(severity: vector, threshold: f64, capacity: f64) -> vector { let levels = eventlevels_count(severity, threshold); if len(levels) == 0 { return []; } let important = at(levels, 0); let slots = samplequota_plan(important, raven_num(len(severity)) - important, capacity); if len(slots) == 0 { return []; } [at(slots, 0), at(slots, 1), at(slots, 2), important - at(slots, 0)] } // Budget a bounded log buffer while preserving higher-severity events first. // Returns [important retained, ordinary retained, dropped, high-severity drops]. Use the plan to allocate slots in a host logger. pub fn raven_buffer(severity: vector, threshold: f64, capacity: f64) -> vector { if !raven_counts(severity) { return []; } if !(threshold >= 0.0 && threshold <= 9007199254740991.0 && threshold % 1.0 == 0.0) { return []; } if !(capacity >= 0.0 && capacity <= 9007199254740991.0 && capacity % 1.0 == 0.0) { return []; } let result = raven_compute(severity, threshold, capacity); if !raven_finite(result) { return []; } result }