node-red-contrib-self-healing
Version:
SHEN: Self-healing extensions for Node-RED.
144 lines (125 loc) • 4.1 kB
HTML
<script type="text/javascript">
RED.nodes.registerType("balancing", {
category: "SHEN",
defaults: {
name: { value: "" },
outputs: { value: 3 },
algorithm: { value: "1" },
weights: { value: "1.1.1" },
},
inputs: 1,
color: "#74b9ff",
label: function () {
return this.name || "balancing";
},
icon: "status.png",
align: "left",
});
function showWeights() {
let algorithm = document.getElementById("node-input-algorithm").value;
let div = document.getElementById("weights-list");
if (algorithm == "2") {
div.style.visibility = "visible";
} else {
div.style.visibility = "hidden";
}
}
function addWeight(output) {
let list = document.getElementById("weight-list");
let weights = document.getElementById("node-input-weights").value;
let weightsArray = weights.split(".").map(Number);
list.innerHTML = "";
for (let i = 1; i <= output.value; i++) {
let element = document.createElement("li");
element.innerHTML = "Weight for output " + i;
let input = document.createElement("input");
input.setAttribute("type", "number");
input.setAttribute("min", "1");
input.setAttribute("value", weightsArray[i - 1]);
input.setAttribute("onChange", "weightChange()");
element.append(input);
list.append(element);
}
}
function weightChange() {
let list = document.getElementById("weight-list");
let weights = document.getElementById("node-input-weights");
let children = list.children;
weights.value = "";
for (let i = 0; i < children.length; i++) {
weights.value += children[i].children[0].value + ".";
}
weights.value = weights.value.substring(0, weights.value.length - 1);
}
</script>
<script type="text/html" data-template-name="balancing">
<div class="form-row">
<label for="node-input-name">Name</label>
<input type="text" id="node-input-name" placeholder="Name" />
</div>
<div class="form-row">
<label for="node-input-outputs">Number of outputs</label>
<input
type="number"
id="node-input-outputs"
min="0"
onChange="addWeight(this)"
/>
</div>
<div class="form-row">
<label for="node-input-algorithm">Distribution Algorithm</label>
<select type="text" id="node-input-algorithm" onChange="showWeights()">
<option value="1">Round Robin (recommended)</option>
<option value="2">Weighted Round Robin</option>
<option value="3">Random Distribution</option>
</select>
</div>
<div id="weights-list" class="form-row" style="visibility: hidden;">
<label for="weight-list">Output Weights</label>
<ol id="weight-list"></ol>
</div>
<div class="form-row">
<input type="hidden" id="node-input-weights" value="1.1.1" />
</div>
</script>
<script type="text/html" data-help-name="balancing">
<p>
This node is used to balance the distribution of messages through multiple
outputs.
</p>
<h3>Properties</h3>
<dl class="message-properties">
<dt>
name
<span class="property-type">string</span>
</dt>
<dd>name of node to be displayed in editor</dd>
<dt>outputs</dt>
<dd>number of outputs of the node</dd>
<dt>algorithm</dt>
<dd>distribution algorithm to be implemented inside the node</dd>
<dt>weights</dt>
<dd>
if the Weighted Round Robin algorithm is chosen, each row represents the
weight associated to each output
</dd>
</dl>
<h3>Inputs</h3>
<dl class="message-properties">
<p>Any message.</p>
</dl>
<h3>Outputs</h3>
<dl class="message-properties">
<p>Input message is passed unchanged to only one of the outputs.</p>
</dl>
<h3>Details</h3>
<p>
The balancing node provides a way of balancing message traffic through
multiple flows in order to avoid the overloading of some nodes while others
are left idle.
</p>
<p>
When a message arrives, the node will run the selected algorithm and decide
to which output it will send the received message.
</p>
</script>