-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmoving-average.html
157 lines (146 loc) · 6.59 KB
/
moving-average.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<script type="text/javascript">
RED.nodes.registerType("moving-average", {
category: "function",
color: "#e2d96e",
defaults: {
name: {
value: ""
},
amount: {
value: 10,
required: true,
validate: function(v) {
return v > 0;
}
},
algorithm: {
value: "avg",
required: true,
validate: function(v) {
switch (v) {
case "avg":
case "min":
case "max":
return true;
default:
return false;
}
}
},
weight: {
value: "cumulative",
required: true,
validate: function(v) {
switch (v) {
case "cumulative":
case "linear":
case "exponential":
return true;
default:
return false;
}
}
}
},
inputs: 1,
outputs: 1,
icon: "font-awesome/fa-line-chart",
label: function() {
return this.name || (this.amount || 10) + " messages " + this.algorithm;
},
paletteLabel: "moving average",
labelStyle: function() {
return this.name ? "node_label_italic" : "";
},
oneditprepare: function() {
$("#node-input-amount, #node-input-algorithm, #node-input-weight").on("change", function() {
var amount = $("#node-input-amount").val();
var algorithm = $("#node-input-algorithm").val();
var weight = $("#node-input-weight").val();
$("#node-input-name").attr("placeholder", amount + " messages " + algorithm);
});
}
});
</script>
<script type="text/html" data-template-name="moving-average">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag" role="presentation"></i> Name:</label>
<input type="text" id="node-input-name" placeholder="{amount} message {type} average" />
</div>
<div class="form-row">
<label for="node-input-amount">Amount:</label>
<input type="number" id="node-input-amount" />
</div>
<div class="form-row">
<label for="node-input-algorithm">Algorithm:</label>
<select id="node-input-algorithm" style="width: 70%;">
<option value="avg">Average</option>
<option value="min">Min</option>
<option value="max">Max</option>
</select>
</div>
<div class="form-row">
<label for="node-input-weight">Average type:</label>
<select id="node-input-weight" style="width: 70%;">
<option value="cumulative">Cumulative (not weigthed)</option>
<option value="linear">Linear (weigthed)</option>
</select>
</div>
</script>
<script type="text/html" data-help-name="moving-average">
<p>Calculates a moving average (or min/max) over a configurable number of messages.</p>
<h3>Properties</h3>
<dl class="message-properties">
<dt>Amount <span class="property-type">number</span></dt>
<dd>
The number of messages over which the moving average is calculated.
</dd>
<dt>Algorithm <span class="property-type">string</span></dt>
<dd>
The default algorightm to use.
</dd>
<dt>Average type <span class="property-type">string</span></dt>
<dd>
Cumulative or linear average.
</dd>
</dl>
<h3>Inputs</h3>
<dl class="message-properties">
<dt>topic <span class="property-type">string</span></dt>
<dd>Moving averages are calculated per topic.</dd>
<dt>payload <span class="property-type">number | boolean | string</span></dt>
<dd>
<p>Whats happens depends on the type of the payload:</p>
<h4><span class="property-type">number</span></h4>
<p>When a number is received, it is added to the array of
numbers over which the moving average is calculated.</p>
<h4><span class="property-type">boolean</span></h4>
<p>When a boolean is received, it is converted to a number, which is added to the array of numbers over which the moving average is calculated.</p>
<h4><span class="property-type">string</span></h4>
<p>When a string is received, it is interpreted as a command for
the node:</p>
<ul>
<li><code>get</code>: no number is added, only returns the current moving average.</li>
<li><code>count</code>: no number is added, returns the number of numbers currently stored.</li>
<li><code>min</code>: no number is added, returns the minimum of the numbers currently stored. Returns positive infinity if no values are stored.</li>
<li><code>max</code>: no number is added, returns the maximum of the numbers currently stored. Returns negative infinity if no values are stored.</li>
<li><code>pop</code>: no number is added but the oldest received number is removed.</li>
<li><code>clear</code>: all stored numbers for the current topic are removed.</li>
</ul>
</dd>
</dl>
<h3>Outputs</h3>
<p>The output is the message that was received, with the payload
changed:</p>
<dl class="message-properties">
<dt>payload <span class="property-type">number</span></dt>
<dd>
<p>The payload is usually set to the current moving average.</p>
<p>If there is no average, (for example after the <code>clear</code> command is used) the payload is set to 0.</p>
<p>If you want to get the current moving average without adding a number, set a message with <code>get</code> as the payload.</p>
<p>If the <code>count</code> command is used, the payload is not set to the current moving average, but to the amount of numbers currently in memory.</p>
<p>If the <code>min</code> or <code>max</code> command is used, the payload is not set to the current moving average, but to the minimum or the maximum of the numbers currently in memory.</p>
</dd>
</dl>
<p>The original payload is returned as <code>payload_in</code>.</p>
</script>