All files / lib/telemetry evolution.telemetry.ts

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

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 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                                                                                                                                                                     
// lib/telemetry/evolution.telemetry.ts
import { predictNext } from '../evolution/predictive-engine';
import { buildTimeline } from '../evolution/timeline';
type EvolutionLogEntry = {
  ts: string;
  event: string;
  extra?: Record<string, any> | null;
};
 
type EvolutionReport = {
  lastUpdated?: string;
  phase?: number;
  lastContext?: {
    device?: string;
    perfMode?: boolean;
    animations?: string;
    density?: string;
    confidence?: number;
  };
  metrics?: Record<string, number>;
  history?: Array<Record<string, any>>;
};
 
/** يسجّل حدث تطوّر عام */
export function recordEvolutionEvent(event: string, extra?: Record<string, any>) {
  const entry: EvolutionLogEntry = {
    ts: new Date().toISOString(),
    event,
    extra: extra ?? null,
  };
  try {
    const key = 'EVOLUTION_LOG';
    const prev = JSON.parse(localStorage.getItem(key) || '[]');
    prev.push(entry);
    localStorage.setItem(key, JSON.stringify(prev.slice(-200))); // آخر 200 حدث
  } catch {}
}
 
/** قراءة تقرير التطوّر الحالي (إن وجد) */
export function readEvolutionReport(): EvolutionReport {
  try {
    const raw = localStorage.getItem('EVOLUTION_REPORT');
    return raw ? (JSON.parse(raw) as EvolutionReport) : {};
  } catch {
    return {};
  }
}
 
/** دمج/تحديث تقرير التطوّر الحالي (upsert) */
export function upsertEvolutionReport(patch: Partial<EvolutionReport>) {
  const current = readEvolutionReport();
  const next: EvolutionReport = deepMerge(current, patch);
  try {
    localStorage.setItem('EVOLUTION_REPORT', JSON.stringify(next));
  } catch {}
  return next;
}
 
export function recordPrediction() {
  const tl = buildTimeline();
  const pred = predictNext(tl);
  if (!pred) return;
 
  const reportKey = 'Evolution_Report';
  const report = JSON.parse(localStorage.getItem(reportKey) || '{}');
  report.prediction = pred;
  localStorage.setItem(reportKey, JSON.stringify(report));
  console.log('🧠 [Predictive] Forecast recorded:', pred);
}
 
/** دمج بسيط للـ objects */
function deepMerge<T>(base: T, patch?: Partial<T>): T {
  if (!patch) return base;
  const out: any = Array.isArray(base) ? [...(base as any)] : { ...(base as any) };
  for (const k of Object.keys(patch)) {
    const v = (patch as any)[k];
    if (v && typeof v === 'object' && !Array.isArray(v))
      out[k] = deepMerge((out as any)[k] ?? {}, v);
    else out[k] = v;
  }
  return out;
}