Files
telegram-management-system/marketing-agent/services/analytics/src/models/Event.js
你的用户名 237c7802e5
Some checks failed
Deploy / deploy (push) Has been cancelled
Initial commit: Telegram Management System
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>
2025-11-04 15:37:50 +08:00

80 lines
2.0 KiB
JavaScript

import mongoose from 'mongoose';
const eventSchema = new mongoose.Schema({
// Multi-tenant support
tenantId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Tenant',
required: true,
index: true
},
id: {
type: String,
required: true,
unique: true,
index: true
},
type: {
type: String,
required: true,
index: true
},
accountId: {
type: String,
required: true,
index: true
},
userId: {
type: String,
index: true
},
sessionId: {
type: String,
index: true
},
action: {
type: String,
required: true,
index: true
},
target: String,
value: Number,
metadata: {
type: Map,
of: mongoose.Schema.Types.Mixed
},
properties: {
type: Map,
of: mongoose.Schema.Types.Mixed
},
timestamp: {
type: Date,
required: true,
index: true
}
}, {
timestamps: true,
timeseries: {
timeField: 'timestamp',
metaField: 'metadata',
granularity: 'seconds'
}
});
// Compound indexes for common queries
eventSchema.index({ accountId: 1, timestamp: -1 });
eventSchema.index({ accountId: 1, type: 1, timestamp: -1 });
eventSchema.index({ accountId: 1, userId: 1, timestamp: -1 });
eventSchema.index({ type: 1, action: 1, timestamp: -1 });
// TTL index to auto-delete old events after 1 year
eventSchema.index({ timestamp: 1 }, { expireAfterSeconds: 31536000 });
// Multi-tenant indexes
eventSchema.index({ tenantId: 1, accountId: 1, timestamp: -1 });
eventSchema.index({ tenantId: 1, accountId: 1, type: 1, timestamp: -1 });
eventSchema.index({ tenantId: 1, accountId: 1, userId: 1, timestamp: -1 });
eventSchema.index({ tenantId: 1, type: 1, action: 1, timestamp: -1 });
eventSchema.index({ tenantId: 1, timestamp: 1 }, { expireAfterSeconds: 31536000 });
export const Event = mongoose.model('Event', eventSchema);