Files
drone-detector/server/models/Heartbeat.js
2025-09-17 06:02:55 +02:00

103 lines
2.4 KiB
JavaScript

const { DataTypes } = require('sequelize');
module.exports = (sequelize) => {
const Heartbeat = sequelize.define('Heartbeat', {
id: {
type: DataTypes.UUID,
defaultValue: sequelize.Sequelize.UUIDV4,
primaryKey: true
},
device_id: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'devices',
key: 'id'
},
comment: 'ID of the device sending heartbeat'
},
tenant_id: {
type: DataTypes.UUID,
allowNull: true,
references: {
model: 'tenants',
key: 'id'
},
comment: 'Tenant ID for multi-tenancy support'
},
device_key: {
type: DataTypes.STRING,
allowNull: true, // Allow null for testing
defaultValue: 'test-device-key',
comment: 'Unique key of the sensor from heartbeat message'
},
status: {
type: DataTypes.STRING,
allowNull: true,
comment: 'Device status (online, offline, error, etc.)'
},
timestamp: {
type: DataTypes.DATE,
allowNull: true,
comment: 'Timestamp from device'
},
uptime: {
type: DataTypes.BIGINT,
allowNull: true,
comment: 'Device uptime in seconds'
},
memory_usage: {
type: DataTypes.FLOAT,
allowNull: true,
comment: 'Memory usage percentage'
},
cpu_usage: {
type: DataTypes.FLOAT,
allowNull: true,
comment: 'CPU usage percentage'
},
disk_usage: {
type: DataTypes.FLOAT,
allowNull: true,
comment: 'Disk usage percentage'
},
firmware_version: {
type: DataTypes.STRING,
allowNull: true,
comment: 'Firmware version reported in heartbeat'
},
received_at: {
type: DataTypes.DATE,
defaultValue: sequelize.Sequelize.NOW,
comment: 'When heartbeat was received by server'
},
raw_payload: {
type: DataTypes.JSON,
allowNull: true,
comment: 'Complete raw payload received from detector (for debugging)'
},
created_at: {
type: DataTypes.DATE,
defaultValue: sequelize.Sequelize.NOW
}
}, {
tableName: 'heartbeats',
timestamps: true,
createdAt: 'created_at',
updatedAt: false,
indexes: [
{
fields: ['device_id']
},
{
fields: ['received_at']
},
{
fields: ['device_id', 'received_at']
}
]
});
return Heartbeat;
};