Files
drone-detector/server/models/AlertRule.js
2025-09-15 15:18:47 +02:00

135 lines
3.4 KiB
JavaScript

const { DataTypes } = require('sequelize');
module.exports = (sequelize) => {
const AlertRule = sequelize.define('AlertRule', {
id: {
type: DataTypes.UUID,
defaultValue: sequelize.Sequelize.UUIDV4,
primaryKey: true
},
user_id: {
type: DataTypes.UUID,
allowNull: true, // Allow null for testing
references: {
model: 'users',
key: 'id'
}
},
name: {
type: DataTypes.STRING,
allowNull: false,
comment: 'Human-readable name for the alert rule'
},
description: {
type: DataTypes.TEXT,
allowNull: true,
comment: 'Description of what triggers this alert'
},
is_active: {
type: DataTypes.BOOLEAN,
defaultValue: true
},
device_ids: {
type: DataTypes.JSON,
allowNull: true,
comment: 'Array of device IDs to monitor (null = all devices)'
},
drone_types: {
type: DataTypes.JSON,
allowNull: true,
comment: 'Array of drone types to alert on (null = all types)'
},
min_rssi: {
type: DataTypes.INTEGER,
allowNull: true,
comment: 'Minimum RSSI threshold for alert'
},
max_rssi: {
type: DataTypes.INTEGER,
allowNull: true,
comment: 'Maximum RSSI threshold for alert'
},
frequency_ranges: {
type: DataTypes.JSON,
allowNull: true,
comment: 'Array of frequency ranges to monitor [{min: 20, max: 30}]'
},
time_window: {
type: DataTypes.INTEGER,
defaultValue: 300,
comment: 'Time window in seconds to group detections'
},
min_detections: {
type: DataTypes.INTEGER,
defaultValue: 1,
comment: 'Minimum number of detections in time window to trigger alert'
},
cooldown_period: {
type: DataTypes.INTEGER,
defaultValue: 600,
comment: 'Cooldown period in seconds between alerts for same drone'
},
alert_channels: {
type: DataTypes.JSON,
defaultValue: ['sms'],
comment: 'Array of alert channels: sms, email, webhook'
},
sms_phone_number: {
type: DataTypes.STRING,
allowNull: true,
comment: 'Phone number for SMS alerts'
},
webhook_url: {
type: DataTypes.STRING,
allowNull: true,
comment: 'Webhook URL for custom integrations'
},
active_hours: {
type: DataTypes.JSON,
allowNull: true,
comment: 'Active hours for alerts {start: "09:00", end: "17:00"}'
},
active_days: {
type: DataTypes.JSON,
defaultValue: [1, 2, 3, 4, 5, 6, 7],
comment: 'Active days of week (1=Monday, 7=Sunday)'
},
priority: {
type: DataTypes.ENUM('low', 'medium', 'high', 'critical'),
defaultValue: 'medium',
comment: 'Alert priority level'
},
min_threat_level: {
type: DataTypes.ENUM('monitoring', 'low', 'medium', 'high', 'critical'),
allowNull: true,
comment: 'Minimum threat level required to trigger alert'
},
created_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
},
updated_at: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
}
}, {
tableName: 'alert_rules',
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
indexes: [
{
fields: ['user_id']
},
{
fields: ['is_active']
},
{
fields: ['priority']
}
]
});
return AlertRule;
};