[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / core_nodes / core / 20-inject.html
diff --git a/dgbuilder/core_nodes/core/20-inject.html b/dgbuilder/core_nodes/core/20-inject.html
new file mode 100644 (file)
index 0000000..38aa6ef
--- /dev/null
@@ -0,0 +1,437 @@
+<!--\r
+  Copyright 2013 IBM Corp.\r
+\r
+  Licensed under the Apache License, Version 2.0 (the "License");\r
+  you may not use this file except in compliance with the License.\r
+  You may obtain a copy of the License at\r
+\r
+  http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+  Unless required by applicable law or agreed to in writing, software\r
+  distributed under the License is distributed on an "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+  See the License for the specific language governing permissions and\r
+  limitations under the License.\r
+-->\r
+\r
+<script type="text/x-red" data-template-name="inject">\r
+    <div class="form-row node-input-payload">\r
+        <label for="node-input-payloadType"><i class="fa fa-envelope"></i> Payload</label>\r
+        <select id="node-input-payloadType" style="width:125px !important">\r
+          <option value="date">timestamp</option>\r
+          <option value="none">blank</option>\r
+          <option value="string">string</option>\r
+        </select>\r
+    </div>\r
+\r
+    <div class="form-row" id="node-input-row-payload">\r
+        <label for="node-input-payload"></label>\r
+        <input type="text" id="node-input-payload" placeholder="payload">\r
+    </div>\r
+\r
+    <div class="form-row">\r
+        <label for="node-input-topic"><i class="fa fa-tasks"></i> Topic</label>\r
+        <input type="text" id="node-input-topic" placeholder="topic">\r
+    </div>\r
+\r
+    <div class="form-row">\r
+        <label for=""><i class="fa fa-repeat"></i> Repeat</label>\r
+        <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>\r
+        <input type="hidden" id="node-input-repeat" placeholder="payload">\r
+        <input type="hidden" id="node-input-crontab" placeholder="payload">\r
+    </div>\r
+\r
+    <div class="form-row inject-time-row hidden" id="inject-time-row-interval">\r
+        every <input id="inject-time-interval-count" class="inject-time-count" value="1"></input>\r
+              <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/>\r
+        <!-- on <select disabled id="inject-time-interval-days" class="inject-time-days"></select> -->\r
+    </div>\r
+\r
+    <div class="form-row inject-time-row hidden" id="inject-time-row-interval-time">\r
+        at every <select style="width: 90px" id="inject-time-interval-time-units" class="inject-time-int-count" value="1">\r
+            <option value="1">1</option>\r
+            <option value="2">2</option>\r
+            <option value="3">3</option>\r
+            <option value="4">4</option>\r
+            <option value="5">5</option>\r
+            <option value="6">6</option>\r
+            <option value="10">10</option>\r
+            <option value="12">12</option>\r
+            <option value="15">15</option>\r
+            <option value="20">20</option>\r
+            <option value="30">30</option>\r
+            <option value="0">60</option>\r
+        </select> minutes<br/>\r
+        between <select id="inject-time-interval-time-start" class="inject-time-times"></select>\r
+        and <select id="inject-time-interval-time-end" class="inject-time-times"></select><br/>\r
+        on <select id="inject-time-interval-time-days" class="inject-time-days"></select>\r
+    </div>\r
+\r
+    <div class="form-row inject-time-row hidden" id="inject-time-row-time">\r
+        at <input id="inject-time-time" value="12:00"></input><br/>\r
+        on <select id="inject-time-time-days" class="inject-time-days"></select>\r
+    </div>\r
+\r
+    <div class="form-row" id="node-once">\r
+        <label>&nbsp;</label>\r
+        <input type="checkbox" id="node-input-once" style="display: inline-block; width: auto; vertical-align: top;">\r
+        <label for="node-input-once" style="width: 70%;">Fire once at start ?</label>\r
+    </div>\r
+\r
+    <div class="form-row">\r
+        <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>\r
+        <input type="text" id="node-input-name" placeholder="name">\r
+    </div>\r
+\r
+    <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>\r
+</script>\r
+<style>\r
+    .inject-time-row {\r
+        padding-left: 110px;\r
+    }\r
+    .inject-time-row select {\r
+        margin: 3px 0;\r
+    }\r
+    .inject-time-days {\r
+        width: 262px;\r
+    }\r
+    .inject-time-times {\r
+        width: 90px;\r
+    }\r
+    .inject-time-row > .ui-spinner {\r
+        height: 28px;\r
+        margin: 3px 0;\r
+        border-color: rgb(204, 204, 204);\r
+    }\r
+    #inject-time-time {\r
+        margin-top: 3px;\r
+        width: 75px;\r
+    }\r
+    .inject-time-count {\r
+        width: 40px !important;\r
+    }\r
+</style>\r
+<script type="text/x-red" data-help-name="inject">\r
+    <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>\r
+    <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>\r
+    <p>The repeat function does what it says on the tin and continuously sends the payload every x seconds.</p>\r
+    <p>The Fire once at start option actually waits 50mS before firing to give other nodes a chance to instantiate properly.</p>\r
+    <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.\r
+    If you want every 20 minutes from now - use the basic "interval" option.</p>\r
+</script>\r
+\r
+<script type="text/javascript">\r
+    RED.nodes.registerType('inject',{\r
+        category: 'input',\r
+        color:"#a6bbcf",\r
+        defaults: {\r
+            name: {value:""},\r
+            topic: {value:""},\r
+            payload: {value:""},\r
+            payloadType: {value:"date"},\r
+            repeat: {value:""},\r
+            crontab: {value:""},\r
+            once: {value:false}\r
+        },\r
+        inputs:0,\r
+        outputs:1,\r
+        icon: "inject.png",\r
+        label: function() {\r
+            if (this.payloadType === "string") {\r
+                return this.name||this.topic||this.payload||"inject";\r
+            }\r
+            else { return this.name||this.topic||"inject"; }\r
+        },\r
+        labelStyle: function() {\r
+            return this.name?"node_label_italic":"";\r
+        },\r
+        oneditprepare: function() {\r
+            $("#inject-time-type-select").change(function() {\r
+                $("#node-input-crontab").val('');\r
+                var id = $("#inject-time-type-select option:selected").val();\r
+                $(".inject-time-row").hide();\r
+                $("#inject-time-row-"+id).show();\r
+                if ((id == "none") || (id == "interval")) {\r
+                    $("#node-once").show();\r
+                }\r
+                else {\r
+                    $("#node-once").hide();\r
+                    $("#node-input-once").prop('checked', false);\r
+                }\r
+            });\r
+\r
+            var days = [\r
+                {v:"*",t:"every day"},\r
+                {v:"1-5",t:"Mondays to Fridays"},\r
+                {v:"0,6",t:"Saturdays and Sundays"},\r
+                {v:"1",t:"Mondays"},\r
+                {v:"2",t:"Tuesdays"},\r
+                {v:"3",t:"Wednesdays"},\r
+                {v:"4",t:"Thursdays"},\r
+                {v:"5",t:"Fridays"},\r
+                {v:"6",t:"Saturdays"},\r
+                {v:"0",t:"Sundays"}\r
+            ];\r
+\r
+            $(".inject-time-days").each(function() {\r
+                for (var d in days) {\r
+                    $(this).append($("<option></option>").val(days[d].v).text(days[d].t));\r
+                }\r
+            });\r
+\r
+            $(".inject-time-times").each(function() {\r
+                for (var i=0;i<24;i++) {\r
+                    var l = (i<10?"0":"")+i+":00";\r
+                    $(this).append($("<option></option>").val(i).text(l));\r
+                }\r
+            });\r
+\r
+            $(".inject-time-count").spinner({\r
+                //max:60,\r
+                min:1\r
+            });\r
+\r
+            $("#inject-time-interval-units").change(function() {\r
+                var units = $("#inject-time-interval-units option:selected").val();\r
+                //$("#inject-time-interval-days").prop("disabled",(units == "s")?"disabled":false);\r
+                //$(".inject-time-count").spinner("option","max",(units == "h")?24:60);\r
+            });\r
+\r
+            $.widget( "ui.injecttimespinner", $.ui.spinner, {\r
+                options: {\r
+                    // seconds\r
+                    step: 60 * 1000,\r
+                    // hours\r
+                    page: 60\r
+                },\r
+                _parse: function( value ) {\r
+                    if ( typeof value === "string" ) {\r
+                        // already a timestamp\r
+                        if ( Number( value ) == value ) {\r
+                            return Number( value );\r
+                        }\r
+                        var p = value.split(":");\r
+                        var offset = new Date().getTimezoneOffset();\r
+                        return (((Number(p[0])+1)*60)+Number(p[1])+offset)*60*1000;\r
+                    }\r
+                    return value;\r
+                },\r
+                _format: function( value ) {\r
+                    var d = new Date(value);\r
+                    var h = d.getHours();\r
+                    var m = d.getMinutes();\r
+                    return ((h < 10)?"0":"")+h+":"+((m < 10)?"0":"")+m;\r
+                }\r
+            });\r
+\r
+            $("#inject-time-time").injecttimespinner();\r
+\r
+            var repeattype = "none";\r
+            if (this.repeat != "" && this.repeat != 0) {\r
+                repeattype = "interval";\r
+                var r = "s";\r
+                var c = this.repeat;\r
+                if (this.repeat % 60 === 0) { r = "m"; c = c/60; }\r
+                if (this.repeat % 1440 === 0) { r = "h"; c = c/24; }\r
+                $("#inject-time-interval-count").val(c);\r
+                $("#inject-time-interval-units").val(r);\r
+                //$("#inject-time-interval-units option").filter(function() {return $(this).val() == "s";}).attr('selected',true);\r
+                $("#inject-time-interval-days").prop("disabled","disabled");\r
+            } else if (this.crontab) {\r
+                var cronparts = this.crontab.split(" ");\r
+                var days = cronparts[4];\r
+                if (!isNaN(cronparts[0]) && !isNaN(cronparts[1])) {\r
+                    repeattype = "time";\r
+                    // Fixed time\r
+                    var time = cronparts[1]+":"+cronparts[0];\r
+                    $("#inject-time-time").val(time);\r
+                    $("#inject-time-type-select option").filter(function() {return $(this).val() == "s";}).attr('selected',true);\r
+                    $("#inject-time-time-days option").filter(function() {return $(this).val() == days;}).attr('selected',true);\r
+                }\r
+                //else if (cronparts[0] == "0") {\r
+                //    // interval - hours\r
+                //    var hours = cronparts[1].slice(2);\r
+                //    repeattype = "interval";\r
+                //    $("#inject-time-interval-days").prop("disabled",false);\r
+                //    $("#inject-time-interval-days option").filter(function() {return $(this).val() == days;}).attr('selected',true);\r
+                //    $("#inject-time-interval-count").val(hours)\r
+                //    $("#inject-time-interval-units option").filter(function() {return $(this).val() == "h";}).attr('selected',true);\r
+                //} else if (cronparts[1] == "*") {\r
+                //    // interval - minutes\r
+                //    var minutes = cronparts[0].slice(2);\r
+                //    repeattype = "interval";\r
+                //    $("#inject-time-interval-days").prop("disabled",false);\r
+                //    $("#inject-time-interval-days option").filter(function() {return $(this).val() == days;}).attr('selected',true);\r
+                //    $("#inject-time-interval-count").val(minutes)\r
+                //    $("#inject-time-interval-units option").filter(function() {return $(this).val() == "m";}).attr('selected',true);\r
+                //}\r
+                else {\r
+                    repeattype = "interval-time";\r
+                    // interval - time period\r
+                    var minutes = cronparts[0].slice(2);\r
+                    if (minutes === "") { minutes = "0"; }\r
+                    $("#inject-time-interval-time-units").val(minutes);\r
+                    $("#inject-time-interval-time-days option").filter(function() {return $(this).val() == days;}).attr('selected',true);\r
+                    var time = cronparts[1];\r
+                    var timeparts = time.split(",");\r
+                    var start;\r
+                    var end;\r
+                    if (timeparts.length == 1) {\r
+                        // 0 or 0-10\r
+                        var hours = timeparts[0].split("-");\r
+                        if (hours.length == 1) {\r
+                            if (hours[0] === "") {\r
+                                start = "0";\r
+                                end = "0";\r
+                            }\r
+                            else {\r
+                                start = hours[0];\r
+                                end = Number(hours[0])+1;\r
+                            }\r
+                        } else {\r
+                            start = hours[0];\r
+                            end = (Number(hours[1])+1)%24;\r
+                        }\r
+                    } else {\r
+                        // 23,0 or 17-23,0-10 or 23,0-2 or 17-23,0\r
+                        var startparts = timeparts[0].split("-");\r
+                        start = startparts[0];\r
+\r
+                        var endparts = timeparts[1].split("-");\r
+                        if (endparts.length == 1) {\r
+                            end = Number(endparts[0])+1;\r
+                        } else {\r
+                            end = Number(endparts[1])+1;\r
+                        }\r
+                    }\r
+                    $("#inject-time-interval-time-start option").filter(function() {return $(this).val() == start;}).attr('selected',true);\r
+                    $("#inject-time-interval-time-end option").filter(function() {return $(this).val() == end;}).attr('selected',true);\r
+\r
+                }\r
+            } else {\r
+                $("#inject-time-type-select option").filter(function() {return $(this).val() == "none";}).attr('selected',true);\r
+            }\r
+\r
+            $(".inject-time-row").hide();\r
+            $("#inject-time-type-select option").filter(function() {return $(this).val() == repeattype;}).attr('selected',true);\r
+            $("#inject-time-row-"+repeattype).show();\r
+\r
+            if (this.payloadType == null) {\r
+                if (this.payload == "") {\r
+                    this.payloadType = "date";\r
+                } else {\r
+                    this.payloadType = "string";\r
+                }\r
+            }\r
+\r
+            $("#node-input-payloadType").change(function() {\r
+                var id = $("#node-input-payloadType option:selected").val();\r
+                if (id == "string") {\r
+                    $("#node-input-row-payload").show();\r
+                } else {\r
+                    $("#node-input-row-payload").hide();\r
+                }\r
+            });\r
+            $("#node-input-payloadType").val(this.payloadType);\r
+            $("#node-input-payloadType").change();\r
+            $("#inject-time-type-select").change();\r
+\r
+        },\r
+        oneditsave: function() {\r
+            var repeat = "";\r
+            var crontab = "";\r
+            var type = $("#inject-time-type-select option:selected").val();\r
+            if (type == "none") {\r
+                // nothing\r
+            } else if (type == "interval") {\r
+                var count = $("#inject-time-interval-count").val();\r
+                var units = $("#inject-time-interval-units option:selected").val();\r
+                var days = $("#inject-time-interval-days option:selected").val();\r
+                if (units == "s") {\r
+                    repeat = count;\r
+                } else {\r
+                    if (units == "m") {\r
+                        //crontab = "*/"+count+" * * * "+days;\r
+                        repeat = count * 60;\r
+                    } else if (units == "h") {\r
+                        //crontab = "0 */"+count+" * * "+days;\r
+                        repeat = count * 60 * 24;\r
+                    }\r
+                }\r
+            } else if (type == "interval-time") {\r
+                var count = $("#inject-time-interval-time-units").val();\r
+                var startTime = Number($("#inject-time-interval-time-start option:selected").val());\r
+                var endTime = Number($("#inject-time-interval-time-end option:selected").val());\r
+                var days = $("#inject-time-interval-time-days option:selected").val();\r
+                var timerange = "*";\r
+                if (startTime == endTime) {\r
+                    //TODO: invalid\r
+                    repeat = "";\r
+                    crontab = "";\r
+                } else if (endTime == 0) {\r
+                    timerange = startTime+"-23";\r
+                } else if (startTime+1 < endTime) {\r
+                    timerange = startTime+"-"+(endTime-1);\r
+                } else if (startTime+1 == endTime) {\r
+                    timerange = startTime;\r
+                } else {\r
+                    var startpart = "";\r
+                    var endpart = "";\r
+                    if (startTime == 23) {\r
+                        startpart = "23";\r
+                    } else {\r
+                        startpart = startTime+"-23";\r
+                    }\r
+                    if (endTime == 1) {\r
+                        endpart = "0";\r
+                    } else {\r
+                        endpart = "0-"+(endTime-1);\r
+                    }\r
+                    timerange = startpart+","+endpart;\r
+                }\r
+                repeat = "";\r
+                if (count === "0") {\r
+                    crontab = count+" "+timerange+" * * "+days;\r
+                }\r
+                else {\r
+                    crontab = "*/"+count+" "+timerange+" * * "+days;\r
+                }\r
+            } else if (type == "time") {\r
+                var time = $("#inject-time-time").val();\r
+                var days = $("#inject-time-time-days option:selected").val();\r
+                var parts = time.split(":");\r
+                repeat = "";\r
+                crontab = parts[1]+" "+parts[0]+" * * "+days;\r
+            }\r
+\r
+            $("#node-input-repeat").val(repeat);\r
+            $("#node-input-crontab").val(crontab);\r
+\r
+        },\r
+        button: {\r
+            onclick: function() {\r
+                var label = (this.name||this.payload).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");\r
+                if (this.payloadType === "date") { label = "timestamp"; }\r
+                if (this.payloadType === "none") { label = "blank"; }\r
+                d3.xhr("inject/"+this.id).post(function(err,resp) {\r
+                        if (err) {\r
+                            if (err.status == 404) {\r
+                                RED.notify("<strong>Error</strong>: inject node not deployed","error");\r
+                            } else if (err.status == 500) {\r
+                                RED.notify("<strong>Error</strong>: inject failed, see log for details.","error");\r
+                            } else if (err.status == 0) {\r
+                                RED.notify("<strong>Error</strong>: no response from server","error");\r
+                            } else {\r
+                                RED.notify("<strong>Error</strong>: unexpected error: ("+err.status+")"+err.response,"error");\r
+                            }\r
+                        } else if (resp.status == 200) {\r
+                            RED.notify("Successfully injected: "+label,"success");\r
+                        } else {\r
+                            RED.notify("<strong>Error</strong>: unexpected response: ("+resp.status+") "+resp.response,"error");\r
+                        }\r
+                });\r
+            }\r
+        }\r
+    });\r
+\r
+</script>\r