Some checks failed
Deploy / deploy (push) Has been cancelled
Full-stack web application for Telegram management - Frontend: Vue 3 + Vben Admin - Backend: NestJS - Features: User management, group broadcast, statistics 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
7.2 KiB
7.2 KiB
Analytics Service
Real-time analytics and reporting service for the Telegram Marketing Intelligence Agent system.
Overview
The Analytics service provides comprehensive event tracking, metrics processing, real-time analytics, report generation, and alert management capabilities.
Features
Event Tracking
- Real-time event ingestion
- Batch event processing
- Event validation and enrichment
- Multi-dimensional event storage
Metrics Processing
- Custom metric definitions
- Real-time metric calculation
- Aggregation and rollups
- Time-series data management
Report Generation
- Scheduled report generation
- Custom report templates
- Multiple export formats (PDF, Excel, CSV)
- Report distribution
Real-time Analytics
- WebSocket streaming
- Live dashboards
- Real-time alerts
- Performance monitoring
Alert Management
- Threshold-based alerts
- Anomaly detection
- Multi-channel notifications
- Alert history tracking
Architecture
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ API Gateway │────▶│ Analytics Service │────▶│ Elasticsearch │
└─────────────────┘ └──────────────────┘ └─────────────────┘
│ │
├───────────────────────────┤
▼ ▼
┌─────────────┐ ┌──────────────┐
│ MongoDB │ │ ClickHouse │
└─────────────┘ └──────────────┘
API Endpoints
Events
POST /api/events- Track single eventPOST /api/events/batch- Track multiple eventsGET /api/events/search- Search events
Metrics
GET /api/metrics- List all metricsPOST /api/metrics- Create custom metricGET /api/metrics/:id- Get metric detailsGET /api/metrics/:id/data- Get metric data
Reports
GET /api/reports- List reportsPOST /api/reports- Generate reportGET /api/reports/:id- Get report detailsGET /api/reports/:id/download- Download report
Alerts
GET /api/alerts- List alertsPOST /api/alerts- Create alertPUT /api/alerts/:id- Update alertDELETE /api/alerts/:id- Delete alertGET /api/alerts/:id/history- Get alert history
Real-time
WS /ws/analytics- Real-time analytics streamWS /ws/metrics/:id- Metric-specific stream
Data Models
Event Schema
{
eventId: String,
accountId: String,
sessionId: String,
eventType: String,
eventName: String,
timestamp: Date,
properties: Object,
context: {
ip: String,
userAgent: String,
locale: String
}
}
Metric Schema
{
metricId: String,
accountId: String,
name: String,
type: String, // 'counter', 'gauge', 'histogram'
unit: String,
formula: String,
aggregation: String,
filters: Array,
dimensions: Array
}
Report Schema
{
reportId: String,
accountId: String,
name: String,
template: String,
schedule: String,
parameters: Object,
recipients: Array,
format: String
}
Alert Schema
{
alertId: String,
accountId: String,
name: String,
metric: String,
condition: Object,
threshold: Number,
severity: String,
channels: Array,
cooldown: Number
}
Configuration
Environment Variables
PORT- Service port (default: 3004)MONGODB_URI- MongoDB connection stringELASTICSEARCH_NODE- Elasticsearch URLCLICKHOUSE_HOST- ClickHouse hostREDIS_URL- Redis connection URLRABBITMQ_URL- RabbitMQ connection URL
Storage Configuration
REPORTS_DIR- Report storage directoryEXPORTS_DIR- Export storage directoryRETENTION_DAYS- Data retention period
Processing Configuration
BATCH_SIZE- Event batch sizePROCESSING_INTERVAL- Processing interval (ms)STREAM_BUFFER_SIZE- Real-time buffer size
Deployment
Docker
docker build -t analytics-service .
docker run -p 3004:3004 --env-file .env analytics-service
Kubernetes
kubectl apply -f k8s/deployment.yaml
kubectl apply -f k8s/service.yaml
Usage Examples
Track Event
const event = {
eventType: 'message',
eventName: 'message_sent',
properties: {
campaignId: '123',
groupId: '456',
messageType: 'text',
charactersCount: 150
}
};
await analyticsClient.trackEvent(event);
Create Custom Metric
const metric = {
name: 'Message Delivery Rate',
type: 'gauge',
formula: '(delivered / sent) * 100',
unit: 'percentage',
aggregation: 'avg',
dimensions: ['campaignId', 'groupId']
};
await analyticsClient.createMetric(metric);
Generate Report
const report = {
template: 'campaign_performance',
parameters: {
campaignId: '123',
dateRange: {
start: '2024-01-01',
end: '2024-01-31'
}
},
format: 'pdf'
};
const result = await analyticsClient.generateReport(report);
Create Alert
const alert = {
name: 'High Error Rate',
metric: 'error_rate',
condition: 'greater_than',
threshold: 5,
severity: 'critical',
channels: ['email', 'slack']
};
await analyticsClient.createAlert(alert);
Monitoring
Health Check
curl http://localhost:3004/health
Metrics
- Prometheus metrics available at
/metrics - Grafana dashboards included in
/dashboards
Logging
- Structured JSON logging
- Log levels: error, warn, info, debug
- Logs shipped to Elasticsearch
Development
Setup
npm install
cp .env.example .env
npm run dev
Testing
npm test
npm run test:integration
npm run test:e2e
Code Quality
npm run lint
npm run format
npm run type-check
Best Practices
-
Event Tracking
- Use consistent event naming
- Include relevant context
- Batch events when possible
- Validate event schema
-
Metrics Design
- Keep metrics simple and focused
- Use appropriate aggregations
- Consider cardinality
- Plan for scale
-
Report Generation
- Schedule during off-peak hours
- Use caching for common reports
- Optimize queries
- Monitor generation time
-
Alert Configuration
- Set appropriate thresholds
- Use severity levels wisely
- Configure cooldown periods
- Test alert channels
Security
- JWT authentication for API access
- Field-level encryption for sensitive data
- Rate limiting per account
- Audit logging for all operations
- RBAC for multi-tenant access
Performance
- Event ingestion: 10K events/second
- Query response: <100ms p99
- Report generation: <30s for standard reports
- Real-time latency: <50ms
Support
For issues and questions:
- Check the documentation
- Review common issues in FAQ
- Contact the development team