This commit is contained in:
overcuriousity
2025-08-07 09:13:04 +02:00
parent 7f5fdef445
commit 5d05c62a55
9 changed files with 19 additions and 545 deletions

View File

@@ -1,12 +1,10 @@
// src/utils/auditService.ts - Centralized Audit Trail Management
// src/utils/auditService.ts
function env(key: string, fallback: string | undefined = undefined): string | undefined {
// during dev/server-side rendering
if (typeof process !== 'undefined' && process.env?.[key] !== undefined) {
return process.env[key];
}
// during client build / browser
if (typeof import.meta !== 'undefined' && (import.meta as any).env?.[key] !== undefined) {
return (import.meta as any).env[key];
}
@@ -67,7 +65,6 @@ class AuditService {
private config: AuditConfig;
private tempEntries: AuditEntry[] = [];
// Phase configuration with German translations
private readonly phaseConfig = {
'initialization': { icon: '🚀', displayName: 'Initialisierung' },
'retrieval': { icon: '🔍', displayName: 'Datensuche' },
@@ -77,7 +74,6 @@ class AuditService {
'completion': { icon: '✅', displayName: 'Finalisierung' }
};
// Action translations
private readonly actionTranslations = {
'pipeline-start': 'Analyse gestartet',
'embeddings-search': 'Ähnliche Tools gesucht',
@@ -95,7 +91,6 @@ class AuditService {
}
private loadConfig(): AuditConfig {
// use the helper if you added it
const enabledFlag =
(typeof import.meta !== 'undefined' &&
(import.meta as any).env?.PUBLIC_FORENSIC_AUDIT_ENABLED) ?? 'false';
@@ -172,7 +167,6 @@ class AuditService {
try {
console.log('[AUDIT] Processing', rawAuditTrail.length, 'audit entries');
// Calculate summary statistics with safe defaults
const totalTime = rawAuditTrail.reduce((sum, entry) => sum + (entry.processingTimeMs || 0), 0);
const validConfidenceEntries = rawAuditTrail.filter(entry => typeof entry.confidence === 'number');
const avgConfidence = validConfidenceEntries.length > 0
@@ -182,7 +176,6 @@ class AuditService {
const highConfidenceSteps = rawAuditTrail.filter(entry => (entry.confidence || 0) >= 80).length;
const lowConfidenceSteps = rawAuditTrail.filter(entry => (entry.confidence || 0) < 60).length;
// Group entries by phase with safe handling
const groupedEntries = rawAuditTrail.reduce((groups, entry) => {
const phase = entry.phase || 'unknown';
if (!groups[phase]) groups[phase] = [];
@@ -190,7 +183,6 @@ class AuditService {
return groups;
}, {} as Record<string, AuditEntry[]>);
// Process phases with error handling
const phases = Object.entries(groupedEntries).map(([phase, entries]) => {
const phaseConfig = this.phaseConfig[phase] || { icon: '📋', displayName: phase };
const validEntries = entries.filter(entry => entry && typeof entry === 'object');
@@ -211,9 +203,8 @@ class AuditService {
.map(e => this.compressEntry(e))
.filter((e): e is CompressedAuditEntry => e !== null)
};
}).filter(phase => phase.entries.length > 0); // Only include phases with valid entries
}).filter(phase => phase.entries.length > 0);
// Generate analysis summary
const summary = this.generateSummary(rawAuditTrail, avgConfidence, lowConfidenceSteps);
const result: ProcessedAuditTrail = {
@@ -261,12 +252,9 @@ class AuditService {
}
}
/**
* Compress data based on detail level
*/
private compressData(data: any): any {
if (this.config.detailLevel === 'verbose') {
return data; // Keep full data
return data;
} else if (this.config.detailLevel === 'standard') {
return this.summarizeForStorage(data);
} else {
@@ -274,9 +262,6 @@ class AuditService {
}
}
/**
* Summarize data for display purposes
*/
private summarizeData(data: any): string {
if (data === null || data === undefined) return 'null';
if (typeof data === 'string') {
@@ -327,15 +312,11 @@ class AuditService {
return data;
}
/**
* Generate analysis summary
*/
private generateSummary(entries: AuditEntry[], avgConfidence: number, lowConfidenceSteps: number): {
analysisQuality: 'excellent' | 'good' | 'fair' | 'poor';
keyInsights: string[];
potentialIssues: string[];
} {
// Determine analysis quality
let analysisQuality: 'excellent' | 'good' | 'fair' | 'poor';
if (avgConfidence >= 85 && lowConfidenceSteps === 0) {
analysisQuality = 'excellent';
@@ -347,7 +328,6 @@ class AuditService {
analysisQuality = 'poor';
}
// Generate key insights
const keyInsights: string[] = [];
const embeddingsUsed = entries.some(e => e.action === 'embeddings-search');
if (embeddingsUsed) {
@@ -362,7 +342,6 @@ class AuditService {
}
}
// Identify potential issues
const potentialIssues: string[] = [];
if (lowConfidenceSteps > 2) {
potentialIssues.push(`${lowConfidenceSteps} Analyseschritte mit niedriger Konfidenz`);
@@ -380,16 +359,10 @@ class AuditService {
};
}
/**
* Get translated action name
*/
getActionDisplayName(action: string): string {
return this.actionTranslations[action] || action;
}
/**
* Format duration for display
*/
formatDuration(ms: number): string {
if (ms < 1000) return '< 1s';
if (ms < 60000) return `${Math.ceil(ms / 1000)}s`;
@@ -398,30 +371,20 @@ class AuditService {
return seconds > 0 ? `${minutes}m ${seconds}s` : `${minutes}m`;
}
/**
* Get confidence color for UI
*/
getConfidenceColor(confidence: number): string {
if (confidence >= 80) return 'var(--color-accent)';
if (confidence >= 60) return 'var(--color-warning)';
return 'var(--color-error)';
}
/**
* Check if audit is enabled
*/
isEnabled(): boolean {
return this.config.enabled;
}
/**
* Get current configuration
*/
getConfig(): AuditConfig {
return { ...this.config };
}
}
// Export singleton instance
export const auditService = new AuditService();
export type { ProcessedAuditTrail, CompressedAuditEntry };