agent-team-composer
Version:
Transform README files into GitHub project plans with AI-powered agent teams
117 lines • 4.13 kB
JavaScript
export class TelemetryService {
static instance;
events = [];
metrics = [];
sessionId;
constructor() {
this.sessionId = this.generateSessionId();
}
static getInstance() {
if (!TelemetryService.instance) {
TelemetryService.instance = new TelemetryService();
}
return TelemetryService.instance;
}
generateSessionId() {
return `session_${Date.now()}_${Math.random().toString(36).substring(7)}`;
}
trackEvent(event, properties = {}) {
const telemetryEvent = {
event,
properties,
timestamp: new Date(),
sessionId: this.sessionId
};
this.events.push(telemetryEvent);
// In production, send to analytics service
if (process.env.NODE_ENV === 'production') {
this.sendToAnalytics(telemetryEvent);
}
else {
console.debug('[Telemetry]', event, properties);
}
}
trackExtractionMetrics(metrics) {
this.metrics.push(metrics);
// Track patterns for improvement
if (!metrics.success) {
this.trackEvent('extraction_failure', {
method: metrics.method,
context: metrics.context,
errorType: metrics.errorType,
duration: metrics.duration
});
}
}
getExtractionSuccessRate() {
if (this.metrics.length === 0)
return 0;
const successful = this.metrics.filter(m => m.success).length;
return (successful / this.metrics.length) * 100;
}
getMethodSuccessRates() {
const methodGroups = this.groupBy(this.metrics, 'method');
const rates = {};
for (const [method, metrics] of Object.entries(methodGroups)) {
const successful = metrics.filter(m => m.success).length;
rates[method] = (successful / metrics.length) * 100;
}
return rates;
}
getMostCommonErrors() {
const errors = this.metrics
.filter(m => !m.success && m.errorType)
.map(m => m.errorType);
const errorCounts = errors.reduce((acc, error) => {
acc[error] = (acc[error] || 0) + 1;
return acc;
}, {});
return Object.entries(errorCounts)
.map(([error, count]) => ({ error, count }))
.sort((a, b) => b.count - a.count)
.slice(0, 10);
}
groupBy(array, key) {
return array.reduce((result, item) => {
const group = String(item[key]);
if (!result[group])
result[group] = [];
result[group].push(item);
return result;
}, {});
}
async sendToAnalytics(event) {
// In production, this would send to your analytics service
// For now, we'll just log to a file or remote service
try {
// Example: await fetch('https://analytics.yourservice.com/track', {
// method: 'POST',
// headers: { 'Content-Type': 'application/json' },
// body: JSON.stringify(event)
// });
}
catch (error) {
console.error('Failed to send telemetry:', error);
}
}
generateDailyReport() {
const report = {
sessionId: this.sessionId,
date: new Date().toISOString(),
totalEvents: this.events.length,
extractionMetrics: {
totalAttempts: this.metrics.length,
successRate: this.getExtractionSuccessRate(),
methodSuccessRates: this.getMethodSuccessRates(),
commonErrors: this.getMostCommonErrors(),
averageRetries: this.metrics.reduce((sum, m) => sum + m.retryCount, 0) / this.metrics.length || 0
},
eventSummary: this.events.reduce((acc, event) => {
acc[event.event] = (acc[event.event] || 0) + 1;
return acc;
}, {})
};
return JSON.stringify(report, null, 2);
}
}
//# sourceMappingURL=telemetry.js.map