@iota-big3/sdk-gateway
Version:
Universal API Gateway with protocol translation, intelligent routing, rate limiting, health checking, and caching
133 lines • 4.12 kB
JavaScript
"use strict";
/**
* @iota-big3/sdk-gateway
* Minimal Analytics Replacement - Temporary implementation to replace corrupted sdk-analytics dependency
* This is a minimal viable replacement for SimpleTimeSeriesStore to get sdk-gateway working
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.SimpleTimeSeriesStore = void 0;
/**
* Minimal implementation of SimpleTimeSeriesStore
* Replaces the corrupted @iota-big3/sdk-analytics dependency temporarily
*/
class SimpleTimeSeriesStore {
constructor() {
this.data = new Map();
// Initialize with empty data store
}
/**
* Record a metric data point
*/
record(metric, value, tags) {
const dataPoint = {
timestamp: Date.now(),
value,
tags: tags || {}
};
if (!this.data.has(metric)) {
this.data.set(metric, []);
}
const points = this.data.get(metric);
points.push(dataPoint);
// Keep only last 1000 points per metric to prevent memory issues
if (points.length > 1000) {
points.shift();
}
}
/**
* Query metric data
*/
query(query) {
const points = this.data.get(query.metric) || [];
return points.filter(point => {
// Filter by time range if specified
if (query.startTime && point.timestamp < query.startTime) {
return false;
}
if (query.endTime && point.timestamp > query.endTime) {
return false;
}
// Filter by tags if specified
if (query.tags) {
for (const [key, value] of Object.entries(query.tags)) {
if (!point.tags || point.tags[key] !== value) {
return false;
}
}
}
return true;
});
}
/**
* Get latest value for a metric
*/
getLatest(metric) {
const points = this.data.get(metric) || [];
if (points.length > 0) {
const lastPoint = points[points.length - 1];
return lastPoint || null;
}
return null;
}
/**
* Get average value for a metric over time range
*/
getAverage(metric, startTime, endTime) {
const query = { metric, startTime, endTime };
const points = this.query(query);
if (points.length === 0) {
return 0;
}
const sum = points.reduce((acc, point) => acc + point.value, 0);
return sum / points.length;
}
/**
* Clear all data
*/
clear() {
this.data.clear();
}
/**
* Get all metric names
*/
getMetrics() {
return Array.from(this.data.keys());
}
/**
* Get rate of change for a metric
*/
getRate(metric, windowMs = 60000) {
const now = Date.now();
const points = this.query({
metric,
startTime: now - windowMs,
endTime: now
});
if (points.length < 2) {
return 0;
}
const first = points[0];
const last = points[points.length - 1];
if (!first || !last) {
return 0;
}
const timeDiff = last.timestamp - first.timestamp;
const valueDiff = last.value - first.value;
return timeDiff > 0 ? (valueDiff / timeDiff) * 1000 : 0; // per second
}
/**
* Detect anomalies in metric data
*/
detectAnomalies(metric, threshold = 2) {
const points = this.data.get(metric) || [];
if (points.length < 3) {
return [];
}
const avg = points.reduce((sum, p) => sum + p.value, 0) / points.length;
const variance = points.reduce((sum, p) => sum + Math.pow(p.value - avg, 2), 0) / points.length;
const stdDev = Math.sqrt(variance);
return points.filter(point => Math.abs(point.value - avg) > threshold * stdDev);
}
}
exports.SimpleTimeSeriesStore = SimpleTimeSeriesStore;
//# sourceMappingURL=minimal-analytics.js.map