Skip to content

Commit

Permalink
Add status UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkarex committed Mar 8, 2022
1 parent 3cc3893 commit 67ab406
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-contrib-postgresql",
"version": "0.8.0",
"version": "0.9.0",
"description": "Node-RED node for PostgreSQL, supporting parameters, split, back-pressure",
"author": {
"name": "Alexandre Alapetite",
Expand Down
43 changes: 40 additions & 3 deletions postgresql.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,39 @@ module.exports = function (RED) {
let cursor;
let getNextRows;

// Do not update status faster than x ms
const updateStatusPeriodMs = 1000;

let nbQueue = 0;
let hasError = false;
let statusTimer = null;
const updateStatus = (incQueue = 0, isError = false) => {
nbQueue += incQueue;
hasError |= isError;
if (!statusTimer) {
statusTimer = setTimeout(() => {
let fill = 'grey';
if (hasError) {
fill = 'red';
} else if (nbQueue <= 0) {
fill = 'blue';
} else if (nbQueue <= node.config.pgPool.totalCount) {
fill = 'green';
} else {
fill = 'yellow';
}
node.status({
fill,
shape: hasError || nbQueue > node.config.pgPool.totalCount ? 'ring' : 'dot',
text: 'Queue: ' + nbQueue + (hasError ? ' Error!' : ''),
});
hasError = false;
statusTimer = null;
}, updateStatusPeriodMs);
}
};
updateStatus(0, false);

node.on('input', async (msg, send, done) => {
// 'send' and 'done' require Node-RED 1.0+
send = send || function () { node.send.apply(node, arguments); };
Expand All @@ -134,7 +167,7 @@ module.exports = function (RED) {

let client = null;

const handleDone = async () => {
const handleDone = async (isError = false) => {
if (cursor) {
cursor.close();
cursor = null;
Expand All @@ -146,13 +179,16 @@ module.exports = function (RED) {
await client.end();
}
client = null;
updateStatus(-1, isError);
} else if (isError) {
updateStatus(-1, isError);
}
getNextRows = null;
};

const handleError = (err) => {
const error = (err ? err.toString() : 'Unknown error!') + ' ' + query;
handleDone();
handleDone(true);
msg.payload = error;
msg.parts = {
id: partsId,
Expand All @@ -171,6 +207,7 @@ module.exports = function (RED) {
};

handleDone();
updateStatus(+1);
downstreamReady = true;

try {
Expand Down Expand Up @@ -200,7 +237,7 @@ module.exports = function (RED) {
} else {
const complete = rows.length < node.rowsPerMsg;
if (complete) {
handleDone();
handleDone(false);
}
const msg2 = Object.assign({}, msg, {
payload: (node.rowsPerMsg || 1) > 1 ? rows : rows[0],
Expand Down

0 comments on commit 67ab406

Please sign in to comment.