refactor(redis): 移除心跳日志长度限制和相关配置

移除Redis集成中对心跳日志和console日志的长度限制功能,包括:
1. 删除heartbeatTtlSeconds和consoleMaxLen配置项
2. 移除相关trim操作和测试用例
3. 简化RedisIntegration类中相关方法

同时为heartbeat_events表添加write_ts_ms字段记录写入时间
This commit is contained in:
2026-01-22 14:06:02 +08:00
parent b90faf4aa4
commit 1a505bfa29
5 changed files with 13 additions and 59 deletions

View File

@@ -14,6 +14,7 @@ CREATE TABLE IF NOT EXISTS heartbeat.heartbeat_events (
guid varchar(32) NOT NULL DEFAULT replace(gen_random_uuid()::text, '-', ''), guid varchar(32) NOT NULL DEFAULT replace(gen_random_uuid()::text, '-', ''),
ts_ms bigint NOT NULL, ts_ms bigint NOT NULL,
write_ts_ms bigint DEFAULT (EXTRACT(EPOCH FROM clock_timestamp()) * 1000)::bigint,
hotel_id int2 NOT NULL, hotel_id int2 NOT NULL,
room_id varchar(50) NOT NULL, room_id varchar(50) NOT NULL,
device_id varchar(64) NOT NULL, device_id varchar(64) NOT NULL,
@@ -62,6 +63,9 @@ CREATE TABLE IF NOT EXISTS heartbeat.heartbeat_events (
) )
PARTITION BY RANGE (ts_ms); PARTITION BY RANGE (ts_ms);
ALTER TABLE heartbeat.heartbeat_events ADD COLUMN IF NOT EXISTS write_ts_ms bigint;
ALTER TABLE heartbeat.heartbeat_events ALTER COLUMN write_ts_ms SET DEFAULT (EXTRACT(EPOCH FROM clock_timestamp()) * 1000)::bigint;
ALTER TABLE heartbeat.heartbeat_events ADD COLUMN IF NOT EXISTS elec_address text[]; ALTER TABLE heartbeat.heartbeat_events ADD COLUMN IF NOT EXISTS elec_address text[];
ALTER TABLE heartbeat.heartbeat_events ADD COLUMN IF NOT EXISTS air_address text[]; ALTER TABLE heartbeat.heartbeat_events ADD COLUMN IF NOT EXISTS air_address text[];
ALTER TABLE heartbeat.heartbeat_events ADD COLUMN IF NOT EXISTS voltage double precision[]; ALTER TABLE heartbeat.heartbeat_events ADD COLUMN IF NOT EXISTS voltage double precision[];

View File

@@ -16,8 +16,6 @@ export default {
url: 'redis://10.8.8.109:6379', url: 'redis://10.8.8.109:6379',
apiBaseUrl: `http://127.0.0.1:${env.PORT ?? 3001}`, apiBaseUrl: `http://127.0.0.1:${env.PORT ?? 3001}`,
heartbeatIntervalMs: 3000, heartbeatIntervalMs: 3000,
heartbeatTtlSeconds: 30,
consoleMaxLen: null,
}, },
// Kafka配置 // Kafka配置

View File

@@ -84,6 +84,7 @@ class DatabaseManager {
guid varchar(32) NOT NULL DEFAULT replace(gen_random_uuid()::text, '-', ''), guid varchar(32) NOT NULL DEFAULT replace(gen_random_uuid()::text, '-', ''),
ts_ms bigint NOT NULL, ts_ms bigint NOT NULL,
write_ts_ms bigint DEFAULT (EXTRACT(EPOCH FROM clock_timestamp()) * 1000)::bigint,
hotel_id int2 NOT NULL, hotel_id int2 NOT NULL,
room_id varchar(50) NOT NULL, room_id varchar(50) NOT NULL,
device_id varchar(64) NOT NULL, device_id varchar(64) NOT NULL,
@@ -130,6 +131,9 @@ class DatabaseManager {
) )
PARTITION BY RANGE (ts_ms); PARTITION BY RANGE (ts_ms);
ALTER TABLE heartbeat.heartbeat_events ADD COLUMN IF NOT EXISTS write_ts_ms bigint;
ALTER TABLE heartbeat.heartbeat_events ALTER COLUMN write_ts_ms SET DEFAULT (EXTRACT(EPOCH FROM clock_timestamp()) * 1000)::bigint;
ALTER TABLE heartbeat.heartbeat_events ADD COLUMN IF NOT EXISTS elec_address text[]; ALTER TABLE heartbeat.heartbeat_events ADD COLUMN IF NOT EXISTS elec_address text[];
ALTER TABLE heartbeat.heartbeat_events ADD COLUMN IF NOT EXISTS air_address text[]; ALTER TABLE heartbeat.heartbeat_events ADD COLUMN IF NOT EXISTS air_address text[];
ALTER TABLE heartbeat.heartbeat_events ADD COLUMN IF NOT EXISTS voltage double precision[]; ALTER TABLE heartbeat.heartbeat_events ADD COLUMN IF NOT EXISTS voltage double precision[];
@@ -396,6 +400,7 @@ class DatabaseManager {
const columns = [ const columns = [
'ts_ms', 'ts_ms',
'write_ts_ms',
'hotel_id', 'hotel_id',
'room_id', 'room_id',
'device_id', 'device_id',
@@ -427,6 +432,7 @@ class DatabaseManager {
const toRowValues = (e) => [ const toRowValues = (e) => [
e.ts_ms, e.ts_ms,
e.write_ts_ms ?? Date.now(),
e.hotel_id, e.hotel_id,
e.room_id, e.room_id,
e.device_id, e.device_id,

View File

@@ -46,28 +46,6 @@ class RedisIntegration {
return Number.isFinite(ms) && ms > 0 ? ms : 3000; return Number.isFinite(ms) && ms > 0 ? ms : 3000;
} }
getHeartbeatTtlSeconds() {
const ttl = this.config?.heartbeatTtlSeconds;
if (ttl === undefined || ttl === null) return null;
const n = Number(ttl);
return Number.isFinite(n) && n > 0 ? Math.floor(n) : null;
}
getHeartbeatMaxLen() {
const v = this.config?.heartbeatMaxLen;
if (v === undefined) return 2000;
if (v === null) return null;
const n = Number(v);
return Number.isFinite(n) && n > 0 ? Math.floor(n) : null;
}
getConsoleMaxLen() {
const v = this.config?.consoleMaxLen;
if (v === undefined || v === null) return null;
const n = Number(v);
return Number.isFinite(n) && n > 0 ? Math.floor(n) : null;
}
async connect() { async connect() {
if (!this.isEnabled()) { if (!this.isEnabled()) {
console.log('[redis] disabled'); console.log('[redis] disabled');
@@ -191,10 +169,6 @@ class RedisIntegration {
this._pendingHeartbeat = null; this._pendingHeartbeat = null;
await this.client.rPush(key, value); await this.client.rPush(key, value);
const maxLen = this.getHeartbeatMaxLen();
if (maxLen) {
await this.client.lTrim(key, -maxLen, -1);
}
} }
async writeHeartbeat() { async writeHeartbeat() {
@@ -218,10 +192,6 @@ class RedisIntegration {
for (let attempt = 1; attempt <= 2; attempt += 1) { for (let attempt = 1; attempt <= 2; attempt += 1) {
try { try {
await this.client.rPush(key, value); await this.client.rPush(key, value);
const maxLen = this.getHeartbeatMaxLen();
if (maxLen) {
await this.client.lTrim(key, -maxLen, -1);
}
this._pendingHeartbeat = null; this._pendingHeartbeat = null;
return; return;
} catch (err) { } catch (err) {
@@ -277,11 +247,6 @@ class RedisIntegration {
while (this._pendingConsoleLogs.length) { while (this._pendingConsoleLogs.length) {
const batch = this._pendingConsoleLogs.splice(0, 200); const batch = this._pendingConsoleLogs.splice(0, 200);
await this.client.rPush(key, ...batch); await this.client.rPush(key, ...batch);
const maxLen = this.getConsoleMaxLen();
if (maxLen) {
await this.client.lTrim(key, -maxLen, -1);
}
} }
} finally { } finally {
this._flushingConsoleLogs = false; this._flushingConsoleLogs = false;
@@ -325,12 +290,6 @@ class RedisIntegration {
const key = this.getConsoleKey(); const key = this.getConsoleKey();
await this.client.rPush(key, value); await this.client.rPush(key, value);
const maxLen = this.getConsoleMaxLen();
if (maxLen) {
// 保留最新 maxLen 条
await this.client.lTrim(key, -maxLen, -1);
}
} }
info(message, metadata) { info(message, metadata) {

View File

@@ -34,15 +34,12 @@ describe('RedisIntegration protocol', () => {
apiBaseUrl: 'http://127.0.0.1:3000', apiBaseUrl: 'http://127.0.0.1:3000',
}); });
const calls = { rPush: [], lTrim: [] }; const calls = { rPush: [] };
redis.client = { redis.client = {
isReady: true, isReady: true,
rPush: async (key, value) => { rPush: async (key, value) => {
calls.rPush.push({ key, value }); calls.rPush.push({ key, value });
}, },
lTrim: async (key, start, stop) => {
calls.lTrim.push({ key, start, stop });
},
}; };
const before = Date.now(); const before = Date.now();
@@ -56,9 +53,6 @@ describe('RedisIntegration protocol', () => {
assert.equal(payload.apiBaseUrl, 'http://127.0.0.1:3000'); assert.equal(payload.apiBaseUrl, 'http://127.0.0.1:3000');
assert.equal(typeof payload.lastActiveAt, 'number'); assert.equal(typeof payload.lastActiveAt, 'number');
assert.ok(payload.lastActiveAt >= before && payload.lastActiveAt <= after); assert.ok(payload.lastActiveAt >= before && payload.lastActiveAt <= after);
assert.equal(calls.lTrim.length, 1);
assert.deepEqual(calls.lTrim[0], { key: '项目心跳', start: -2000, stop: -1 });
}); });
it('caches heartbeat when redis is not ready and flushes later', async () => { it('caches heartbeat when redis is not ready and flushes later', async () => {
@@ -68,16 +62,13 @@ describe('RedisIntegration protocol', () => {
apiBaseUrl: 'http://127.0.0.1:3000', apiBaseUrl: 'http://127.0.0.1:3000',
}); });
const calls = { rPush: [], lTrim: [] }; const calls = { rPush: [] };
redis.client = { redis.client = {
isReady: false, isReady: false,
connect: async () => {}, connect: async () => {},
rPush: async (key, value) => { rPush: async (key, value) => {
calls.rPush.push({ key, value }); calls.rPush.push({ key, value });
}, },
lTrim: async (key, start, stop) => {
calls.lTrim.push({ key, start, stop });
},
}; };
await redis.writeHeartbeat(); await redis.writeHeartbeat();
@@ -93,7 +84,6 @@ describe('RedisIntegration protocol', () => {
assert.equal(payload.projectName, 'BLS主机心跳日志'); assert.equal(payload.projectName, 'BLS主机心跳日志');
assert.equal(payload.apiBaseUrl, 'http://127.0.0.1:3000'); assert.equal(payload.apiBaseUrl, 'http://127.0.0.1:3000');
assert.equal(typeof payload.lastActiveAt, 'number'); assert.equal(typeof payload.lastActiveAt, 'number');
assert.equal(calls.lTrim.length, 1);
}); });
it('buffers console logs when redis is not ready', async () => { it('buffers console logs when redis is not ready', async () => {
@@ -103,16 +93,13 @@ describe('RedisIntegration protocol', () => {
apiBaseUrl: 'http://127.0.0.1:3000', apiBaseUrl: 'http://127.0.0.1:3000',
}); });
const calls = { rPush: [], lTrim: [] }; const calls = { rPush: [] };
redis.client = { redis.client = {
isReady: false, isReady: false,
connect: async () => {}, connect: async () => {},
rPush: async (key, ...values) => { rPush: async (key, ...values) => {
calls.rPush.push({ key, values }); calls.rPush.push({ key, values });
}, },
lTrim: async (key, start, stop) => {
calls.lTrim.push({ key, start, stop });
},
}; };
await redis.info('hello', { module: 'test' }); await redis.info('hello', { module: 'test' });