@kne/fastify-mq 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -25,4 +25,50 @@ module.exports = fp(async (fastify, options) => {
|
|
|
25
25
|
});
|
|
26
26
|
}
|
|
27
27
|
);
|
|
28
|
+
|
|
29
|
+
fastify.get(
|
|
30
|
+
`${options.prefix}/dashboard/sse`,
|
|
31
|
+
{
|
|
32
|
+
onRequest: options.getAuthenticate('dashboard'),
|
|
33
|
+
schema: {
|
|
34
|
+
summary: 'Dashboard SSE 实时推送接口',
|
|
35
|
+
query: {
|
|
36
|
+
type: 'object',
|
|
37
|
+
properties: {
|
|
38
|
+
window: { type: 'number', default: 300000, description: 'Rate计算窗口(ms), 默认5分钟' },
|
|
39
|
+
step: { type: 'number', default: 60000, description: '时序数据步长(ms), 默认1分钟' },
|
|
40
|
+
interval: { type: 'number', default: 5000, description: '推送间隔(ms), 默认5秒' }
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
(request, reply) => {
|
|
46
|
+
const windowMs = request.query.window || 300000;
|
|
47
|
+
const stepMs = request.query.step || 60000;
|
|
48
|
+
const intervalMs = request.query.interval || 5000;
|
|
49
|
+
|
|
50
|
+
reply.raw.writeHead(200, {
|
|
51
|
+
'Content-Type': 'text/event-stream',
|
|
52
|
+
'Cache-Control': 'no-cache',
|
|
53
|
+
Connection: 'keep-alive',
|
|
54
|
+
'X-Accel-Buffering': 'no'
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const sendEvent = async () => {
|
|
58
|
+
try {
|
|
59
|
+
const data = await services.dashboard.getData({ windowMs, stepMs });
|
|
60
|
+
reply.raw.write(`data: ${JSON.stringify(data)}\n\n`);
|
|
61
|
+
} catch (e) {
|
|
62
|
+
reply.raw.write(`event: error\ndata: ${JSON.stringify({ message: e.message })}\n\n`);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
sendEvent();
|
|
67
|
+
const timer = setInterval(sendEvent, intervalMs);
|
|
68
|
+
|
|
69
|
+
request.raw.on('close', () => {
|
|
70
|
+
clearInterval(timer);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
);
|
|
28
74
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kne/fastify-mq",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Reliable message queue plugin for Fastify based on PostgreSQL, supporting at-least-once delivery, DLQ, delayed messages, priority, tracing and Kafka migration",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|