feat: 初始化 bls-onoffline-backend 项目基础结构

添加 Kafka 消费者、数据库写入、Redis 集成等核心模块,实现设备上下线事件处理
- 创建项目基础目录结构与配置文件
- 实现 Kafka 消费逻辑与手动提交偏移量
- 添加 PostgreSQL 数据库连接与分区表管理
- 集成 Redis 用于错误队列和项目心跳
- 包含数据处理逻辑,区分重启与非重启数据
- 提供数据库初始化脚本与分区创建工具
- 添加单元测试与代码校验脚本
This commit is contained in:
2026-02-04 17:51:50 +08:00
commit a8c7cf74e6
41 changed files with 6760 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
CREATE SCHEMA IF NOT EXISTS onoffline;
CREATE TABLE IF NOT EXISTS onoffline.onoffline_record (
guid VARCHAR(32) NOT NULL,
ts_ms BIGINT NOT NULL,
write_ts_ms BIGINT NOT NULL,
hotel_id SMALLINT NOT NULL,
mac VARCHAR(21) NOT NULL,
device_id VARCHAR(64) NOT NULL,
room_id VARCHAR(64) NOT NULL,
ip VARCHAR(21),
current_status VARCHAR(255),
launcher_version VARCHAR(255),
reboot_reason VARCHAR(255),
PRIMARY KEY (ts_ms, mac, device_id, room_id)
) PARTITION BY RANGE (ts_ms);
CREATE INDEX IF NOT EXISTS idx_onoffline_ts_ms ON onoffline.onoffline_record (ts_ms);
CREATE INDEX IF NOT EXISTS idx_onoffline_hotel_id ON onoffline.onoffline_record (hotel_id);
CREATE INDEX IF NOT EXISTS idx_onoffline_mac ON onoffline.onoffline_record (mac);
CREATE INDEX IF NOT EXISTS idx_onoffline_device_id ON onoffline.onoffline_record (device_id);
CREATE INDEX IF NOT EXISTS idx_onoffline_room_id ON onoffline.onoffline_record (room_id);
CREATE INDEX IF NOT EXISTS idx_onoffline_current_status ON onoffline.onoffline_record (current_status);

View File

@@ -0,0 +1,41 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { spawnSync } from 'child_process';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const projectRoot = path.resolve(__dirname, '..');
const targets = ['src', 'tests'];
const collectFiles = (dir) => {
if (!fs.existsSync(dir)) {
return [];
}
const entries = fs.readdirSync(dir, { withFileTypes: true });
return entries.flatMap((entry) => {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
return collectFiles(fullPath);
}
if (entry.isFile() && fullPath.endsWith('.js')) {
return [fullPath];
}
return [];
});
};
const files = targets.flatMap((target) => collectFiles(path.join(projectRoot, target)));
const failures = [];
files.forEach((file) => {
const result = spawnSync(process.execPath, ['--check', file], { stdio: 'inherit' });
if (result.status !== 0) {
failures.push(file);
}
});
if (failures.length > 0) {
process.exit(1);
}

View File

@@ -0,0 +1,36 @@
import { config } from '../src/config/config.js';
import dbManager from '../src/db/databaseManager.js';
import { logger } from '../src/utils/logger.js';
const verifyData = async () => {
const client = await dbManager.pool.connect();
try {
console.log('Verifying data in database...');
// Count total rows
const countSql = `SELECT count(*) FROM ${config.db.schema}.${config.db.table}`;
const countRes = await client.query(countSql);
console.log(`Total rows in ${config.db.schema}.${config.db.table}: ${countRes.rows[0].count}`);
// Check recent rows
const recentSql = `
SELECT * FROM ${config.db.schema}.${config.db.table}
ORDER BY ts_ms DESC
LIMIT 5
`;
const recentRes = await client.query(recentSql);
console.log('Recent 5 rows:');
recentRes.rows.forEach(row => {
console.log(JSON.stringify(row));
});
} catch (err) {
console.error('Error verifying data:', err);
} finally {
client.release();
await dbManager.pool.end();
}
};
verifyData();

View File

@@ -0,0 +1,61 @@
import pg from 'pg';
import { config } from '../src/config/config.js';
const { Pool } = pg;
const verify = async () => {
const pool = new Pool({
host: config.db.host,
port: config.db.port,
user: config.db.user,
password: config.db.password,
database: config.db.database,
ssl: config.db.ssl,
});
try {
console.log('Verifying partitions for table onoffline_record...');
const client = await pool.connect();
// Check parent table
const parentRes = await client.query(`
SELECT to_regclass('onoffline.onoffline_record') as oid;
`);
if (!parentRes.rows[0].oid) {
console.error('Parent table onoffline.onoffline_record DOES NOT EXIST.');
return;
}
console.log('Parent table onoffline.onoffline_record exists.');
// Check partitions
const res = await client.query(`
SELECT
child.relname AS partition_name
FROM pg_inherits
JOIN pg_class parent ON pg_inherits.inhparent = parent.oid
JOIN pg_class child ON pg_inherits.inhrelid = child.oid
JOIN pg_namespace ns ON parent.relnamespace = ns.oid
WHERE parent.relname = 'onoffline_record' AND ns.nspname = 'onoffline'
ORDER BY child.relname;
`);
console.log(`Found ${res.rowCount} partitions.`);
res.rows.forEach(row => {
console.log(`- ${row.partition_name}`);
});
if (res.rowCount >= 30) {
console.log('SUCCESS: At least 30 partitions exist.');
} else {
console.warn(`WARNING: Expected 30+ partitions, found ${res.rowCount}.`);
}
client.release();
} catch (err) {
console.error('Verification failed:', err);
} finally {
await pool.end();
}
};
verify();