Commits

Takahiro KITAHARA committed 4bce1e9

some features implemented

show date time on processed.
timeout option implemented.
callback function option implemented.

Comments (0)

Files changed (4)

+syntax: glob
+.idea/

jenkins_hta_monitor.hta

 
     <div id="jenkins2"></div>
 
+    <!-- div for callback sample -->
+    <div id="callback" style="width:300px; height:60px;border: solid 1px black;"></div>
+
 
     <script lang="text/javascript" src="./js/jquery-1.7.2.min.js"></script>
     <script lang="text/javascript" src="./js/jenkins_hta_monitor.js"></script>
              interval is option, default interval is 5 min (60000 * 5)
              */
             $("#jenkins2").jenkinsMonitor({server:"http://localhost:8080/", interval:1000 * 60});
+
+            // specify timeout, ( default: interval - 10(ms) )
+            // $("#jenkins2").jenkinsMonitor({server:"http://localhost:8080/", interval:1000 * 60, timeout: 1000});
+
+            /* callback sample
+            $("#jenkins2").jenkinsMonitor({server:"http://localhost:8080/"
+                , interval:1000 * 60
+                , callback: function(el, json){
+                        el.css("background-color","black");
+                        $("#callback").text(json.url);
+                }
+            });
+            */
+
+            /* callback ERROR sample
+            $("#jenkins2").jenkinsMonitor({server:"http://localhost:8080/"
+                , interval:1000 * 60
+                , callback: function(el, json){
+                    el.css("background-color",undefinedvalue);
+                    $("#callback").text(json.url);
+                }
+            });
+            */
+
         })();
     </script>
 </body>

jenkins_hta_monitor.html

     <title>jenkins hta monitor</title>
 </head>
 <body>
+
     <div id="jenkins2"></div>
 
+    <!-- div for callback sample -->
+    <div id="callback" style="width:300px; height:60px;border: solid 1px black;"></div>
+
+
     <script lang="text/javascript" src="./js/jquery-1.7.2.min.js"></script>
     <script lang="text/javascript" src="./js/jenkins_hta_monitor.js"></script>
     <script lang="text/javascript">
             window.resizeTo(400,270);
 
             /*
-             you need change address to your jenkins server's address.
+             you need change 'localhost:8080' to your jenkins server's address and port.
              interval is option, default interval is 5 min (60000 * 5)
-            */
+             */
             $("#jenkins2").jenkinsMonitor({server:"http://localhost:8080/", interval:1000 * 60});
+
+            // specify timeout, ( default: interval - 10(ms) )
+            // $("#jenkins2").jenkinsMonitor({server:"http://localhost:8080/", interval:1000 * 60, timeout: 1000});
+
+            /* callback sample
+            $("#jenkins2").jenkinsMonitor({server:"http://localhost:8080/"
+                , interval:1000 * 60
+                , callback: function(el, json){
+                    el.css("background-color","black");
+                    $("#callback").text(json.url);
+                }
+            });
+             */
+
+            /* callback ERROR sample
+            $("#jenkins2").jenkinsMonitor({server:"http://localhost:8080/"
+                , interval:1000 * 60
+                , callback: function(el, json){
+                    el.css("background-color",undefinedvalue);
+                    $("#callback").text(json.url);
+                }
+            });
+             */
+
         })();
     </script>
 </body>

js/jenkins_hta_monitor.js

 (function($){
     $.fn.jenkinsMonitor = function(options){
-        var settings = { server : null , interval: 60000 * 5 };
+        var settings = { server : null , interval: 5 * 60 * 1000, timeout: null
+            , callback: function(targetJobLiElement, targetJobJson){} };
         if (options) {$.extend(settings, options);}
 
         if (settings.server){
                 "<span> Jenkins: </span><a href=\""
                 +  settings.server + "\">"
                 + "<span jm_serveraddress=\""+ settings.server +"\">"
-                +  settings.server +" </span></a>");
+                +  settings.server +" </span></a> : <span class=\"jm_refreshdate\"></span>");
             this.append("<div><ul class=\"tabhead\"></ul></div>");
         }else{
+            // server is not specified nothing to do
             return;
         }
 
+        if (settings.interval < 0) settings.interval = 0;
+
+        if (!settings.timeout) settings.timeout = settings.interval - 10;
+        if (settings.timeout < 0) settings.timeout = 0;
+
         var p = this;
         var handle = undefined;
 
                 return;
             }
 
+            // var timeoutHandle = null
+            if (settings.timeout > 0){
+                var timeoutHandle = setTimeout( function(){
+                    p.find(".jm_refreshdate").html(
+                        new Date().toLocaleString()
+                        + "<br /> <b> timed out </b> <span class=\"jm_tryrefresh\">click to retry</span></b>");
+                    p.find(".jm_tryrefresh")
+                        .click( setRefreshTimer)
+                        .css("text-decoration","underline")
+                        .css("color","red");
+
+                    if(handle) clearInterval(handle);
+                    return;
+                }, settings.timeout );
+            }
 
             $.getJSON( settings.server + "api/json?jsonp=?", function(jsonval){
 
+                if(timeoutHandle)  clearInterval(timeoutHandle);
+                $.each(jsonval.jobs, function(){
+                    var tabHead = p.find(".tabhead");
 
-                $.each(jsonval.jobs, function(){
-
-                    if(p.find(".tabhead").find("a[jm_job='"+ this.url +"']").length > 0){
+                    if(tabHead.find("a[jm_job='"+ this.url +"']").length > 0){
 
                     }else{
-                        p.find(".tabhead")
+                        tabHead
                             .append("<li><a href='" + this.url + "' jm_job='"+ this.url + "' "
                                 + " target='jenkins' >"+ this.name +"</a></li>");
                     }
 
-                    $(p.find(".tabhead").find("a[jm_job='"+ this.url +"']"))
-                        .parent()
+                    var element = $(tabHead.find("a[jm_job='"+ this.url +"']"))
+                                    .parent();
+                    element
                         .css("background-color", this.color.replace("grey","gray").replace("blue","#99eecc"))
                         .css("color","black");
+
+                    try{
+                        settings.callback(element, this)
+                    }catch(e){
+                        element.append("<span>" + e.message + "</span>")
+                    }
+
                 });
+
+                p.find(".jm_refreshdate").text(new Date().toLocaleString());
+
             });
         }
 
-        setInterval(refreshStatus, settings.interval);
+        function setRefreshTimer(){
+            p.find(".jm_refreshdate").html("");
+            if(settings.interval > 0){
+                handle = setInterval(refreshStatus, settings.interval);
+            }
+        }
+
+        setRefreshTimer();
         refreshStatus();
     }
 })(jQuery);