UNPKG

valdo-js

Version:

JavaScript/WASM bindings for Valdo time series anomaly detection library

287 lines (239 loc) 8.86 kB
let wasm; const cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } ); if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); }; let cachedUint8ArrayMemory0 = null; function getUint8ArrayMemory0() { if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); } return cachedUint8ArrayMemory0; } function getStringFromWasm0(ptr, len) { ptr = ptr >>> 0; return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); } function takeFromExternrefTable0(idx) { const value = wasm.__wbindgen_export_0.get(idx); wasm.__externref_table_dealloc(idx); return value; } /** * Initialize the WASM module */ export function main() { wasm.main(); } function isLikeNone(x) { return x === undefined || x === null; } let cachedFloat64ArrayMemory0 = null; function getFloat64ArrayMemory0() { if (cachedFloat64ArrayMemory0 === null || cachedFloat64ArrayMemory0.byteLength === 0) { cachedFloat64ArrayMemory0 = new Float64Array(wasm.memory.buffer); } return cachedFloat64ArrayMemory0; } let WASM_VECTOR_LEN = 0; function passArrayF64ToWasm0(arg, malloc) { const ptr = malloc(arg.length * 8, 8) >>> 0; getFloat64ArrayMemory0().set(arg, ptr / 8); WASM_VECTOR_LEN = arg.length; return ptr; } /** * @enum {0 | 1} */ export const AnomalyStatus = Object.freeze({ Normal: 0, "0": "Normal", Anomaly: 1, "1": "Anomaly", }); const DetectorFinalization = (typeof FinalizationRegistry === 'undefined') ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry(ptr => wasm.__wbg_detector_free(ptr >>> 0, 1)); export class Detector { static __wrap(ptr) { ptr = ptr >>> 0; const obj = Object.create(Detector.prototype); obj.__wbg_ptr = ptr; DetectorFinalization.register(obj, obj.__wbg_ptr, obj); return obj; } __destroy_into_raw() { const ptr = this.__wbg_ptr; this.__wbg_ptr = 0; DetectorFinalization.unregister(this); return ptr; } free() { const ptr = this.__destroy_into_raw(); wasm.__wbg_detector_free(ptr, 0); } /** * Create a detector with default parameters for quick setup * @param {bigint} window_size * @returns {Detector} */ static createDefault(window_size) { const ret = wasm.detector_createDefault(window_size); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return Detector.__wrap(ret[0]); } /** * Create a new Detector with specified window size and optional parameters * * # Arguments * * `window_size` - Size of the sliding window for processing * * `quantile` - Optional quantile parameter for SPOT detector (default: 0.0001) * * `level` - Optional level parameter for SPOT detector (default: 0.998) * * `max_excess` - Optional maximum excess for SPOT detector (default: 200) * @param {bigint} window_size * @param {number | null} [quantile] * @param {number | null} [level] * @param {number | null} [max_excess] */ constructor(window_size, quantile, level, max_excess) { const ret = wasm.detector_new(window_size, !isLikeNone(quantile), isLikeNone(quantile) ? 0 : quantile, !isLikeNone(level), isLikeNone(level) ? 0 : level, isLikeNone(max_excess) ? 0x100000001 : (max_excess) >>> 0); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } this.__wbg_ptr = ret[0] >>> 0; DetectorFinalization.register(this, this.__wbg_ptr, this); return this; } /** * Train the detector on historical data * * # Arguments * * `timestamps` - Array of timestamps * * `values` - Array of values * @param {Float64Array} timestamps * @param {Float64Array} values */ train(timestamps, values) { const ptr0 = passArrayF64ToWasm0(timestamps, wasm.__wbindgen_malloc); const len0 = WASM_VECTOR_LEN; const ptr1 = passArrayF64ToWasm0(values, wasm.__wbindgen_malloc); const len1 = WASM_VECTOR_LEN; const ret = wasm.detector_train(this.__wbg_ptr, ptr0, len0, ptr1, len1); if (ret[1]) { throw takeFromExternrefTable0(ret[0]); } } /** * Detect anomalies in a new data point * * # Arguments * * `timestamp` - Timestamp of the data point * * `value` - Value of the data point * * # Returns * AnomalyStatus: Normal or Anomaly * @param {number} timestamp * @param {number} value * @returns {AnomalyStatus} */ detect(timestamp, value) { const ret = wasm.detector_detect(this.__wbg_ptr, timestamp, value); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return ret[0]; } } async function __wbg_load(module, imports) { if (typeof Response === 'function' && module instanceof Response) { if (typeof WebAssembly.instantiateStreaming === 'function') { try { return await WebAssembly.instantiateStreaming(module, imports); } catch (e) { if (module.headers.get('Content-Type') != 'application/wasm') { console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); } else { throw e; } } } const bytes = await module.arrayBuffer(); return await WebAssembly.instantiate(bytes, imports); } else { const instance = await WebAssembly.instantiate(module, imports); if (instance instanceof WebAssembly.Instance) { return { instance, module }; } else { return instance; } } } function __wbg_get_imports() { const imports = {}; imports.wbg = {}; imports.wbg.__wbindgen_init_externref_table = function() { const table = wasm.__wbindgen_export_0; const offset = table.grow(4); table.set(0, undefined); table.set(offset + 0, undefined); table.set(offset + 1, null); table.set(offset + 2, true); table.set(offset + 3, false); ; }; imports.wbg.__wbindgen_string_new = function(arg0, arg1) { const ret = getStringFromWasm0(arg0, arg1); return ret; }; imports.wbg.__wbindgen_throw = function(arg0, arg1) { throw new Error(getStringFromWasm0(arg0, arg1)); }; return imports; } function __wbg_init_memory(imports, memory) { } function __wbg_finalize_init(instance, module) { wasm = instance.exports; __wbg_init.__wbindgen_wasm_module = module; cachedFloat64ArrayMemory0 = null; cachedUint8ArrayMemory0 = null; wasm.__wbindgen_start(); return wasm; } function initSync(module) { if (wasm !== undefined) return wasm; if (typeof module !== 'undefined') { if (Object.getPrototypeOf(module) === Object.prototype) { ({module} = module) } else { console.warn('using deprecated parameters for `initSync()`; pass a single object instead') } } const imports = __wbg_get_imports(); __wbg_init_memory(imports); if (!(module instanceof WebAssembly.Module)) { module = new WebAssembly.Module(module); } const instance = new WebAssembly.Instance(module, imports); return __wbg_finalize_init(instance, module); } async function __wbg_init(module_or_path) { if (wasm !== undefined) return wasm; if (typeof module_or_path !== 'undefined') { if (Object.getPrototypeOf(module_or_path) === Object.prototype) { ({module_or_path} = module_or_path) } else { console.warn('using deprecated parameters for the initialization function; pass a single object instead') } } if (typeof module_or_path === 'undefined') { module_or_path = new URL('valdo_js_bg.wasm', import.meta.url); } const imports = __wbg_get_imports(); if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) { module_or_path = fetch(module_or_path); } __wbg_init_memory(imports); const { instance, module } = await __wbg_load(await module_or_path, imports); return __wbg_finalize_init(instance, module); } export { initSync }; export default __wbg_init;