aboutsummaryrefslogtreecommitdiffstats
path: root/dgbuilder/core_nodes/core/20-inject.html
blob: 38aa6efeb8b0dfbc031dbe708f6d11bc6f31b3a4 (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<!--
  Copyright 2013 IBM Corp.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<script type="text/x-red" data-template-name="inject">
    <div class="form-row node-input-payload">
        <label for="node-input-payloadType"><i class="fa fa-envelope"></i> Payload</label>
        <select id="node-input-payloadType" style="width:125px !important">
          <option value="date">timestamp</option>
          <option value="none">blank</option>
          <option value="string">string</option>
        </select>
    </div>

    <div class="form-row" id="node-input-row-payload">
        <label for="node-input-payload"></label>
        <input type="text" id="node-input-payload" placeholder="payload">
    </div>

    <div class="form-row">
        <label for="node-input-topic"><i class="fa fa-tasks"></i> Topic</label>
        <input type="text" id="node-input-topic" placeholder="topic">
    </div>

    <div class="form-row">
        <label for=""><i class="fa fa-repeat"></i> Repeat</label>
        <select id="inject-time-type-select"  style="width: 288px"><option value="none">none</option><option value="interval">interval</option><option value="interval-time">interval between times</option><option value="time">at a specific time</option></select>
        <input type="hidden" id="node-input-repeat" placeholder="payload">
        <input type="hidden" id="node-input-crontab" placeholder="payload">
    </div>

    <div class="form-row inject-time-row hidden" id="inject-time-row-interval">
        every <input id="inject-time-interval-count" class="inject-time-count" value="1"></input>
              <select style="width: 100px" id="inject-time-interval-units"><option value="s">seconds</option><option value="m">minutes</option><option value="h">hours</option></select><br/>
        <!-- on <select disabled id="inject-time-interval-days" class="inject-time-days"></select> -->
    </div>

    <div class="form-row inject-time-row hidden" id="inject-time-row-interval-time">
        at every <select style="width: 90px" id="inject-time-interval-time-units" class="inject-time-int-count" value="1">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="10">10</option>
            <option value="12">12</option>
            <option value="15">15</option>
            <option value="20">20</option>
            <option value="30">30</option>
            <option value="0">60</option>
        </select> minutes<br/>
        between <select id="inject-time-interval-time-start" class="inject-time-times"></select>
        and <select id="inject-time-interval-time-end" class="inject-time-times"></select><br/>
        on <select id="inject-time-interval-time-days" class="inject-time-days"></select>
    </div>

    <div class="form-row inject-time-row hidden" id="inject-time-row-time">
        at <input id="inject-time-time" value="12:00"></input><br/>
        on <select id="inject-time-time-days" class="inject-time-days"></select>
    </div>

    <div class="form-row" id="node-once">
        <label>&nbsp;</label>
        <input type="checkbox" id="node-input-once" style="display: inline-block; width: auto; vertical-align: top;">
        <label for="node-input-once" style="width: 70%;">Fire once at start ?</label>
    </div>

    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
        <input type="text" id="node-input-name" placeholder="name">
    </div>

    <div class="form-tips"><b>Note:</b> "interval between times" and "at a specific time" will use cron.<br/>See info box for details.</div>
</script>
<style>
    .inject-time-row {
        padding-left: 110px;
    }
    .inject-time-row select {
        margin: 3px 0;
    }
    .inject-time-days {
        width: 262px;
    }
    .inject-time-times {
        width: 90px;
    }
    .inject-time-row > .ui-spinner {
        height: 28px;
        margin: 3px 0;
        border-color: rgb(204, 204, 204);
    }
    #inject-time-time {
        margin-top: 3px;
        width: 75px;
    }
    .inject-time-count {
        width: 40px !important;
    }
</style>
<script type="text/x-red" data-help-name="inject">
    <p>Pressing the button on the left side of the node allows a message on a topic to be injected into the flow. This is mainly for test purposes.</p>
    <p>If no payload is specified the payload is set to the current time in millisecs since 1970. This allows subsequent functions to perform time based actions.</p>
    <p>The repeat function does what it says on the tin and continuously sends the payload every x seconds.</p>
    <p>The Fire once at start option actually waits 50mS before firing to give other nodes a chance to instantiate properly.</p>
    <p><b>Note: </b>"Interval between times" and "at a specific time" will use cron. This means that 20 minutes will be at the next hour, 20 minutes past and 40 minutes past - not in 20 minutes time.
    If you want every 20 minutes from now - use the basic "interval" option.</p>
</script>

<script type="text/javascript">
    RED.nodes.registerType('inject',{
        category: 'input',
        color:"#a6bbcf",
        defaults: {
            name: {value:""},
            topic: {value:""},
            payload: {value:""},
            payloadType: {value:"date"},
            repeat: {value:""},
            crontab: {value:""},
            once: {value:false}
        },
        inputs:0,
        outputs:1,
        icon: "inject.png",
        label: function() {
            if (this.payloadType === "string") {
                return this.name||this.topic||this.payload||"inject";
            }
            else { return this.name||this.topic||"inject"; }
        },
        labelStyle: function() {
            return this.name?"node_label_italic":"";
        },
        oneditprepare: function() {
            $("#inject-time-type-select").change(function() {
                $("#node-input-crontab").val('');
                var id = $("#inject-time-type-select option:selected").val();
                $(".inject-time-row").hide();
                $("#inject-time-row-"+id).show();
                if ((id == "none") || (id == "interval")) {
                    $("#node-once").show();
                }
                else {
                    $("#node-once").hide();
                    $("#node-input-once").prop('checked', false);
                }
            });

            var days = [
                {v:"*",t:"every day"},
                {v:"1-5",t:"Mondays to Fridays"},
                {v:"0,6",t:"Saturdays and Sundays"},
                {v:"1",t:"Mondays"},
                {v:"2",t:"Tuesdays"},
                {v:"3",t:"Wednesdays"},
                {v:"4",t:"Thursdays"},
                {v:"5",t:"Fridays"},
                {v:"6",t:"Saturdays"},
                {v:"0",t:"Sundays"}
            ];

            $(".inject-time-days").each(function() {
                for (var d in days) {
                    $(this).append($("<option></option>").val(days[d].v).text(days[d].t));
                }
            });

            $(".inject-time-times").each(function() {
                for (var i=0;i<24;i++) {
                    var l = (i<10?"0":"")+i+":00";
                    $(this).append($("<option></option>").val(i).text(l));
                }
            });

            $(".inject-time-count").spinner({
                //max:60,
                min:1
            });

            $("#inject-time-interval-units").change(function() {
                var units = $("#inject-time-interval-units option:selected").val();
                //$("#inject-time-interval-days").prop("disabled",(units == "s")?"disabled":false);
                //$(".inject-time-count").spinner("option","max",(units == "h")?24:60);
            });

            $.widget( "ui.injecttimespinner", $.ui.spinner, {
                options: {
                    // seconds
                    step: 60 * 1000,
                    // hours
                    page: 60
                },
                _parse: function( value ) {
                    if ( typeof value === "string" ) {
                        // already a timestamp
                        if ( Number( value ) == value ) {
                            return Number( value );
                        }
                        var p = value.split(":");
                        var offset = new Date().getTimezoneOffset();
                        return (((Number(p[0])+1)*60)+Number(p[1])+offset)*60*1000;
                    }
                    return value;
                },
                _format: function( value ) {
                    var d = new Date(value);
                    var h = d.getHours();
                    var m = d.getMinutes();
                    return ((h < 10)?"0":"")+h+":"+((m < 10)?"0":"")+m;
                }
            });

            $("#inject-time-time").injecttimespinner();

            var repeattype = "none";
            if (this.repeat != "" && this.repeat != 0) {
                repeattype = "interval";
                var r = "s";
                var c = this.repeat;
                if (this.repeat % 60 === 0) { r = "m"; c = c/60; }
                if (this.repeat % 1440 === 0) { r = "h"; c = c/24; }
                $("#inject-time-interval-count").val(c);
                $("#inject-time-interval-units").val(r);
                //$("#inject-time-interval-units option").filter(function() {return $(this).val() == "s";}).attr('selected',true);
                $("#inject-time-interval-days").prop("disabled","disabled");
            } else if (this.crontab) {
                var cronparts = this.crontab.split(" ");
                var days = cronparts[4];
                if (!isNaN(cronparts[0]) && !isNaN(cronparts[1])) {
                    repeattype = "time";
                    // Fixed time
                    var time = cronparts[1]+":"+cronparts[0];
                    $("#inject-time-time").val(time);
                    $("#inject-time-type-select option").filter(function() {return $(this).val() == "s";}).attr('selected',true);
                    $("#inject-time-time-days option").filter(function() {return $(this).val() == days;}).attr('selected',true);
                }
                //else if (cronparts[0] == "0") {
                //    // interval - hours
                //    var hours = cronparts[1].slice(2);
                //    repeattype = "interval";
                //    $("#inject-time-interval-days").prop("disabled",false);
                //    $("#inject-time-interval-days option").filter(function() {return $(this).val() == days;}).attr('selected',true);
                //    $("#inject-time-interval-count").val(hours)
                //    $("#inject-time-interval-units option").filter(function() {return $(this).val() == "h";}).attr('selected',true);
                //} else if (cronparts[1] == "*") {
                //    // interval - minutes
                //    var minutes = cronparts[0].slice(2);
                //    repeattype = "interval";
                //    $("#inject-time-interval-days").prop("disabled",false);
                //    $("#inject-time-interval-days option").filter(function() {return $(this).val() == days;}).attr('selected',true);
                //    $("#inject-time-interval-count").val(minutes)
                //    $("#inject-time-interval-units option").filter(function() {return $(this).val() == "m";}).attr('selected',true);
                //}
                else {
                    repeattype = "interval-time";
                    // interval - time period
                    var minutes = cronparts[0].slice(2);
                    if (minutes === "") { minutes = "0"; }
                    $("#inject-time-interval-time-units").val(minutes);
                    $("#inject-time-interval-time-days option").filter(function() {return $(this).val() == days;}).attr('selected',true);
                    var time = cronparts[1];
                    var timeparts = time.split(",");
                    var start;
                    var end;
                    if (timeparts.length == 1) {
                        // 0 or 0-10
                        var hours = timeparts[0].split("-");
                        if (hours.length == 1) {
                            if (hours[0] === "") {
                                start = "0";
                                end = "0";
                            }
                            else {
                                start = hours[0];
                                end = Number(hours[0])+1;
                            }
                        } else {
                            start = hours[0];
                            end = (Number(hours[1])+1)%24;
                        }
                    } else {
                        // 23,0 or 17-23,0-10 or 23,0-2 or 17-23,0
                        var startparts = timeparts[0].split("-");
                        start = startparts[0];

                        var endparts = timeparts[1].split("-");
                        if (endparts.length == 1) {
                            end = Number(endparts[0])+1;
                        } else {
                            end = Number(endparts[1])+1;
                        }
                    }
                    $("#inject-time-interval-time-start option").filter(function() {return $(this).val() == start;}).attr('selected',true);
                    $("#inject-time-interval-time-end option").filter(function() {return $(this).val() == end;}).attr('selected',true);

                }
            } else {
                $("#inject-time-type-select option").filter(function() {return $(this).val() == "none";}).attr('selected',true);
            }

            $(".inject-time-row").hide();
            $("#inject-time-type-select option").filter(function() {return $(this).val() == repeattype;}).attr('selected',true);
            $("#inject-time-row-"+repeattype).show();

            if (this.payloadType == null) {
                if (this.payload == "") {
                    this.payloadType = "date";
                } else {
                    this.payloadType = "string";
                }
            }

            $("#node-input-payloadType").change(function() {
                var id = $("#node-input-payloadType option:selected").val();
                if (id == "string") {
                    $("#node-input-row-payload").show();
                } else {
                    $("#node-input-row-payload").hide();
                }
            });
            $("#node-input-payloadType").val(this.payloadType);
            $("#node-input-payloadType").change();
            $("#inject-time-type-select").change();

        },
        oneditsave: function() {
            var repeat = "";
            var crontab = "";
            var type = $("#inject-time-type-select option:selected").val();
            if (type == "none") {
                // nothing
            } else if (type == "interval") {
                var count = $("#inject-time-interval-count").val();
                var units = $("#inject-time-interval-units option:selected").val();
                var days = $("#inject-time-interval-days option:selected").val();
                if (units == "s") {
                    repeat = count;
                } else {
                    if (units == "m") {
                        //crontab = "*/"+count+" * * * "+days;
                        repeat = count * 60;
                    } else if (units == "h") {
                        //crontab = "0 */"+count+" * * "+days;
                        repeat = count * 60 * 24;
                    }
                }
            } else if (type == "interval-time") {
                var count = $("#inject-time-interval-time-units").val();
                var startTime = Number($("#inject-time-interval-time-start option:selected").val());
                var endTime = Number($("#inject-time-interval-time-end option:selected").val());
                var days = $("#inject-time-interval-time-days option:selected").val();
                var timerange = "*";
                if (startTime == endTime) {
                    //TODO: invalid
                    repeat = "";
                    crontab = "";
                } else if (endTime == 0) {
                    timerange = startTime+"-23";
                } else if (startTime+1 < endTime) {
                    timerange = startTime+"-"+(endTime-1);
                } else if (startTime+1 == endTime) {
                    timerange = startTime;
                } else {
                    var startpart = "";
                    var endpart = "";
                    if (startTime == 23) {
                        startpart = "23";
                    } else {
                        startpart = startTime+"-23";
                    }
                    if (endTime == 1) {
                        endpart = "0";
                    } else {
                        endpart = "0-"+(endTime-1);
                    }
                    timerange = startpart+","+endpart;
                }
                repeat = "";
                if (count === "0") {
                    crontab = count+" "+timerange+" * * "+days;
                }
                else {
                    crontab = "*/"+count+" "+timerange+" * * "+days;
                }
            } else if (type == "time") {
                var time = $("#inject-time-time").val();
                var days = $("#inject-time-time-days option:selected").val();
                var parts = time.split(":");
                repeat = "";
                crontab = parts[1]+" "+parts[0]+" * * "+days;
            }

            $("#node-input-repeat").val(repeat);
            $("#node-input-crontab").val(crontab);

        },
        button: {
            onclick: function() {
                var label = (this.name||this.payload).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
                if (this.payloadType === "date") { label = "timestamp"; }
                if (this.payloadType === "none") { label = "blank"; }
                d3.xhr("inject/"+this.id).post(function(err,resp) {
                        if (err) {
                            if (err.status == 404) {
                                RED.notify("<strong>Error</strong>: inject node not deployed","error");
                            } else if (err.status == 500) {
                                RED.notify("<strong>Error</strong>: inject failed, see log for details.","error");
                            } else if (err.status == 0) {
                                RED.notify("<strong>Error</strong>: no response from server","error");
                            } else {
                                RED.notify("<strong>Error</strong>: unexpected error: ("+err.status+")"+err.response,"error");
                            }
                        } else if (resp.status == 200) {
                            RED.notify("Successfully injected: "+label,"success");
                        } else {
                            RED.notify("<strong>Error</strong>: unexpected response: ("+resp.status+") "+resp.response,"error");
                        }
                });
            }
        }
    });

</script>