Skip to content

Commit 67ab406

Browse files
committed
Add status UI
1 parent 3cc3893 commit 67ab406

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-red-contrib-postgresql",
3-
"version": "0.8.0",
3+
"version": "0.9.0",
44
"description": "Node-RED node for PostgreSQL, supporting parameters, split, back-pressure",
55
"author": {
66
"name": "Alexandre Alapetite",

postgresql.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,39 @@ module.exports = function (RED) {
114114
let cursor;
115115
let getNextRows;
116116

117+
// Do not update status faster than x ms
118+
const updateStatusPeriodMs = 1000;
119+
120+
let nbQueue = 0;
121+
let hasError = false;
122+
let statusTimer = null;
123+
const updateStatus = (incQueue = 0, isError = false) => {
124+
nbQueue += incQueue;
125+
hasError |= isError;
126+
if (!statusTimer) {
127+
statusTimer = setTimeout(() => {
128+
let fill = 'grey';
129+
if (hasError) {
130+
fill = 'red';
131+
} else if (nbQueue <= 0) {
132+
fill = 'blue';
133+
} else if (nbQueue <= node.config.pgPool.totalCount) {
134+
fill = 'green';
135+
} else {
136+
fill = 'yellow';
137+
}
138+
node.status({
139+
fill,
140+
shape: hasError || nbQueue > node.config.pgPool.totalCount ? 'ring' : 'dot',
141+
text: 'Queue: ' + nbQueue + (hasError ? ' Error!' : ''),
142+
});
143+
hasError = false;
144+
statusTimer = null;
145+
}, updateStatusPeriodMs);
146+
}
147+
};
148+
updateStatus(0, false);
149+
117150
node.on('input', async (msg, send, done) => {
118151
// 'send' and 'done' require Node-RED 1.0+
119152
send = send || function () { node.send.apply(node, arguments); };
@@ -134,7 +167,7 @@ module.exports = function (RED) {
134167

135168
let client = null;
136169

137-
const handleDone = async () => {
170+
const handleDone = async (isError = false) => {
138171
if (cursor) {
139172
cursor.close();
140173
cursor = null;
@@ -146,13 +179,16 @@ module.exports = function (RED) {
146179
await client.end();
147180
}
148181
client = null;
182+
updateStatus(-1, isError);
183+
} else if (isError) {
184+
updateStatus(-1, isError);
149185
}
150186
getNextRows = null;
151187
};
152188

153189
const handleError = (err) => {
154190
const error = (err ? err.toString() : 'Unknown error!') + ' ' + query;
155-
handleDone();
191+
handleDone(true);
156192
msg.payload = error;
157193
msg.parts = {
158194
id: partsId,
@@ -171,6 +207,7 @@ module.exports = function (RED) {
171207
};
172208

173209
handleDone();
210+
updateStatus(+1);
174211
downstreamReady = true;
175212

176213
try {
@@ -200,7 +237,7 @@ module.exports = function (RED) {
200237
} else {
201238
const complete = rows.length < node.rowsPerMsg;
202239
if (complete) {
203-
handleDone();
240+
handleDone(false);
204241
}
205242
const msg2 = Object.assign({}, msg, {
206243
payload: (node.rowsPerMsg || 1) > 1 ? rows : rows[0],

0 commit comments

Comments
 (0)