All files / lib/awareness/collective/analysis stability-detector.ts

0% Statements 0/26
0% Branches 0/1
0% Functions 0/1
0% Lines 0/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                               
// src/lib/awareness/collective/analysis/stability-detector.ts
import { ChartDataPoint } from '../components/charts/types';
 
export interface StabilityZone {
  start: number;
  end: number;
  trend: number;
}
 
export function detectStabilityZones(
  data: ChartDataPoint[],
  windowSize: number = 5,
  threshold: number = 5
): StabilityZone[] {
  if (data.length < windowSize) return [];
 
  const zones: StabilityZone[] = [];
  let startIndex = 0;
 
  for (let i = 0; i < data.length - windowSize; i++) {
    const slice = data.slice(i, i + windowSize);
    const avgChange = slice.reduce((s, l) => s + Math.abs(l.changeRate), 0) / slice.length;
 
    if (avgChange < threshold) {
      if (zones.length === 0 || i > startIndex + windowSize) {
        startIndex = i;
        zones.push({
          start: data[i].timestamp,
          end: data[i + windowSize - 1].timestamp,
          trend: data[i].trendValue,
        });
      } else {
        zones[zones.length - 1].end = data[i + windowSize - 1].timestamp;
      }
    }
  }
 
  return zones;
}