<NME> automatron.js
<BEF> /*

Automatron: UI javascript

  - Pull Target info from API and update `div`
  - Pull Runbook status from API and update `div`
  - Pull Events from API and update `div`

*/


// Update Target list
function updateTargets(target) {
    "use strict";
    var status = "default";
    var hostname = target.hostname.split(".");
    var counts = {
        "OK": 0,
        "WARNING": 0,
        "CRITICAL": 0,
        "UNKNOWN": 0
    };

    $.each(target.runbooks, function (key, value) {
        // Check status and set bootstrap class accordingly
        if (value.status.OK > 0 && status === "default") {
            status = "success";
        }
        if (value.status.WARNING > 0) {
            status = "warning";
        }
        if (value.status.CRITICAL > 0) {
            status = "danger";
        }
        $.each(value.status, function (k, v) {
            if (v > 0) {
                counts[k] = counts[k] + 1;
            }
        });
    });

    $("div.servers-group").append("<div class=\"col-md-4\"><div class=\"panel panel-default\"><div class=\"panel-body\"> \
    <h2><a href=\"#\" class=\"server-info\" data-target=\".server-modal-lg\" data-toggle=\"modal\" data-hostname=\"" + target.hostname + "\">" + hostname[0] + "</a> <i class=\"fa fa-server text-" + status + " pull-right\" aria-hidden=\"true\"></i></h2><hr> \
    <p class=\"text-muted lead\">" + target.hostname + " <span class=\"label label-primary\">" + target.facts.os + "</span></p><hr> \
    <span class=\"label label-success\">" + counts.OK + " OK</span> \
    <span class=\"label label-warning\">" + counts.WARNING + " WARNING</span> \
    <span class=\"label label-danger\">" + counts.CRITICAL + " CRITICAL</span> \
    <span class=\"label label-info\">" + counts.UNKNOWN + " UNKNOWN</span> \
    </div></div></div>").fadeIn("slow");
}

// Update Target Counter
function updateTargetCount(count) {
    "use strict";
    $("div.target-count").text(count).fadeIn("slow");
}

// Update Runbook OK Counter
function updateOkCount(count) {
    "use strict";
    $("div.ok-count").text(count).fadeIn("slow");
}

// Update Runbook Unknown Counter
function updateUnknownCount(count) {
    "use strict";
    $("div.unknown-count").text(count).fadeIn("slow");
}

// Update Runbook Warning Counter
function updateWarningCount(count) {
    "use strict";
    $("div.warning-count").text(count).fadeIn("slow");
}

// Update Runbook Critical Counter
function updateCriticalCount(count) {
    "use strict";
    $("div.critical-count").text(count).fadeIn("slow");
}

// Add Runbook Event to event stream
function addEvent(value) {
    "use strict";
    var hostname = value.target.split(".");
    if (value.status === "CRITICAL") {
        $("ul.status-group").append("<li class=\"list-group-item list-group-item-danger status-item\"><span class=\"badge\">" + value.status + "</span><b>" + hostname[0] + "</b>: " + value.runbook + "</li>").fadeIn("slow");
    }
    if (value.status === "OK" && value.count <= 1) {
        $("ul.status-group").append("<li class=\"list-group-item list-group-item-success status-item\"><span class=\"badge\">" + value.status + "</span><b>" + hostname[0] + "</b>: " + value.runbook + "</li>").fadeIn("slow");
    }
    if (value.status === "WARNING") {
        $("ul.status-group").append("<li class=\"list-group-item list-group-item-warning status-item\"><span class=\"badge\">" + value.status + "</span><b>" + hostname[0] + "</b>: " + value.runbook + "</li>").fadeIn("slow");
    }
    if (value.status === "UNKNOWN") {
        $("ul.status-group").append("<li class=\"list-group-item list-group-item-info status-item\"><span class=\"badge\">" + value.status + "</span><b>" + hostname[0] + "</b>: " + value.runbook + "</li>").fadeIn("slow");
    }
}

// Perform API call and process results for Status and Events
function getStatus() {
    "use strict";
    $.ajax({
        url: "/api/status",
        dataType: "json",
        success: function (json) {
            updateTargetCount(json.targets);
            updateOkCount(json.runbooks.OK);
            updateWarningCount(json.runbooks.WARNING);
            updateCriticalCount(json.runbooks.CRITICAL);
            updateUnknownCount(json.runbooks.UNKNOWN);
            // Clear existing status
            if (json.events.length >= 1) {
                $("ul.status-group").empty();
                $.each(json.events, function (key, value) {
                    addEvent(value);
                });
                if ($("li.status-item").length === 0) {
                    $("ul.status-group").append("<li class=\"list-group-item list-group-item-default status-item\">No events found</li>");
                }
            } else {
                $("ul.status-group").empty();
                $("ul.status-group").append("<li class=\"list-group-item list-group-item-default status-item\">No events found</li>");
            }
        }
    });
}

// Perform API call and process results for Target list
function getTargets() {
    "use strict";
    $.ajax({
        url: "/api/targets",
        dataType: "json",
        success: function (json) {
            $("div.servers-group").empty();
            var added = 0;
            $("div.servers-group").append("<div class=\"row\">");
            $.each(json, function (key, value) {
                if (added % 3 === 0) {
                    $("div.servers-group").append("</div><div class=\"row\">");
                }
                updateTargets(value);
                added = added + 1;
            });
            $("div.servers-group").append("</div>");
        }
    });
}


$(document).on("click", ".server-info", function() {
    "use strict";
    var name = $(this).data().hostname;
    $("h4.modal-title").text(name);
    $.ajax({
        url: "/api/targets/" + name,
        dataType: "json",
        success: function (json) {
            // Update Server info
            $("div.server-panel-info").html("<div class=\"row\">" +
            "<div class=\"col-md-6\"><b>Name:</b> " + json.hostname + "</div><div class=\"col-md-6\"><b>Operating System:</b> " + json.facts.os + "</div>" +
            "<div class=\"col-md-6\"><b>Address:</b> " + json.ip + " </div><div class=\"col-md-6\"><b>Kernel:</b> " + json.facts.kernel + "</div>" +
            "</div>")

            // Update Runbook status
            $("ul.server-runbooks-group").empty();
            $.each(json.runbooks, function (key, value) {
                if (value.last_status === "CRITICAL") {
                    $("ul.server-runbooks-group").append("<li class=\"list-group-item list-group-item-danger status-item\"><span class=\"badge\">" + value.last_status + "</span><b>" + value.name + "</b></li>");
                }
                if (value.last_status === "OK") {
                    $("ul.server-runbooks-group").append("<li class=\"list-group-item list-group-item-success status-item\"><span class=\"badge\">" + value.last_status + "</span><b>" + value.name + "</b></li>");
                }
                if (value.last_status === "WARNING") {
                    $("ul.server-runbooks-group").append("<li class=\"list-group-item list-group-item-warning status-item\"><span class=\"badge\">" + value.last_status + "</span><b>" + value.name + "</b></li>");
                }
                if (value.last_status === "UNKNOWN") {
                    $("ul.server-runbooks-group").append("<li class=\"list-group-item list-group-item-info status-item\"><span class=\"badge\">" + value.last_status + "</span><b>" + value.name + "</b></li>");
                }
            });
            if (json.runbooks.length < 0) {
                $("ul.server-runbooks-group").append("<li class=\"list-group-item list-group-item-default status-item\">No Runbooks found</li>")
            }
        }
    });
});


// Wrapper run function
function run() {
    "use strict";
    getStatus();
    getTargets();
}


// Run run() on load and every 10 seconds
window.onload = run();
window.setInterval(function () {
    "use strict";
    run();
}, 10000);

<MSG> Merge branch 'develop' into update-readme
<DFF> @@ -23,20 +23,22 @@ function updateTargets(target) {
 
     $.each(target.runbooks, function (key, value) {
         // Check status and set bootstrap class accordingly
-        if (value.status.OK > 0 && status === "default") {
-            status = "success";
-        }
-        if (value.status.WARNING > 0) {
-            status = "warning";
-        }
-        if (value.status.CRITICAL > 0) {
-            status = "danger";
-        }
-        $.each(value.status, function (k, v) {
-            if (v > 0) {
-                counts[k] = counts[k] + 1;
+        if (value.hasOwnProperty('status')) {
+            if (value.status.OK > 0 && status === "default") {
+                status = "success";
+            }
+            if (value.status.WARNING > 0) {
+                status = "warning";
             }
-        });
+            if (value.status.CRITICAL > 0) {
+                status = "danger";
+            }
+            $.each(value.status, function (k, v) {
+                if (v > 0) {
+                    counts[k] = counts[k] + 1;
+                }
+            });
+        }
     });
 
     $("div.servers-group").append("<div class=\"col-md-4\"><div class=\"panel panel-default\"><div class=\"panel-body\"> \

{'commit': 'diff --git a/static/js/automatron.js b/static/js/automatron.js\nindex f309882..86a1d3f 100644\n--- a/static/js/automatron.js\n+++ b/static/js/automatron.js\n@@ -23,20 +23,22 @@ function updateTargets(target) {\n \n     $.each(target.runbooks, function (key, value) {\n         // Check status and set bootstrap class accordingly\n-        if (value.status.OK > 0 && status === "default") {\n-            status = "success";\n-        }\n-        if (value.status.WARNING > 0) {\n-            status = "warning";\n-        }\n-        if (value.status.CRITICAL > 0) {\n-            status = "danger";\n-        }\n-        $.each(value.status, function (k, v) {\n-            if (v > 0) {\n-                counts[k] = counts[k] + 1;\n+        if (value.hasOwnProperty(\'status\')) {\n+            if (value.status.OK > 0 && status === "default") {\n+                status = "success";\n+            }\n+            if (value.status.WARNING > 0) {\n+                status = "warning";\n             }\n-        });\n+            if (value.status.CRITICAL > 0) {\n+                status = "danger";\n+            }\n+            $.each(value.status, function (k, v) {\n+                if (v > 0) {\n+                    counts[k] = counts[k] + 1;\n+                }\n+            });\n+        }\n     });\n \n     $("div.servers-group").append("<div class=\\"col-md-4\\"><div class=\\"panel panel-default\\"><div class=\\"panel-body\\"> \\\n', 'commit_subject': "Merge branch 'develop' into update-readme", 'repo_name': 'madflojo/automatron', 'committer': {'name': 'GitHub', 'email': '1505422b2465e9a84f6fdfaa161078890c593f06@github.com', 'time_sec': '1497241312', 'tz_offset': '-420', 'date': {'seconds': '1497241312'}}, 'commit_tree': 'd8c1721773c605d2c77c2261575ee02c6251fcc6', 'commit_message': "Merge branch 'develop' into update-readme", 'language_name': 'HTML', 'license': 'apache-2.0', 'hunks': '@@ -23,20 +23,22 @@ function updateTargets(target) {\n \n     $.each(target.runbooks, function (key, value) {\n         // Check status and set bootstrap class accordingly\n-        if (value.status.OK > 0 && status === "default") {\n-            status = "success";\n-        }\n-        if (value.status.WARNING > 0) {\n-            status = "warning";\n-        }\n-        if (value.status.CRITICAL > 0) {\n-            status = "danger";\n-        }\n-        $.each(value.status, function (k, v) {\n-            if (v > 0) {\n-                counts[k] = counts[k] + 1;\n+        if (value.hasOwnProperty(\'status\')) {\n+            if (value.status.OK > 0 && status === "default") {\n+                status = "success";\n+            }\n+            if (value.status.WARNING > 0) {\n+                status = "warning";\n             }\n-        });\n+            if (value.status.CRITICAL > 0) {\n+                status = "danger";\n+            }\n+            $.each(value.status, function (k, v) {\n+                if (v > 0) {\n+                    counts[k] = counts[k] + 1;\n+                }\n+            });\n+        }\n     });\n \n     $("div.servers-group").append("<div class=\\"col-md-4\\"><div class=\\"panel panel-default\\"><div class=\\"panel-body\\"> \\\n', 'addition_count': 15, 'deletion_count': 13, 'src_file': 'a/static/js/automatron.js', 'tgt_file': 'b/static/js/automatron.js', 'file_extension': '.js', 'before_file': '/*\n\nAutomatron: UI javascript\n\n  - Pull Target info from API and update `div`\n  - Pull Runbook status from API and update `div`\n  - Pull Events from API and update `div`\n\n*/\n\n\n// Update Target list\nfunction updateTargets(target) {\n    "use strict";\n    var status = "default";\n    var hostname = target.hostname.split(".");\n    var counts = {\n        "OK": 0,\n        "WARNING": 0,\n        "CRITICAL": 0,\n        "UNKNOWN": 0\n    };\n\n    $.each(target.runbooks, function (key, value) {\n        // Check status and set bootstrap class accordingly\n        if (value.status.OK > 0 && status === "default") {\n            status = "success";\n        }\n        if (value.status.WARNING > 0) {\n            status = "warning";\n        }\n        if (value.status.CRITICAL > 0) {\n            status = "danger";\n        }\n        $.each(value.status, function (k, v) {\n            if (v > 0) {\n                counts[k] = counts[k] + 1;\n            }\n        });\n    });\n\n    $("div.servers-group").append("<div class=\\"col-md-4\\"><div class=\\"panel panel-default\\"><div class=\\"panel-body\\"> \\\n    <h2><a href=\\"#\\" class=\\"server-info\\" data-target=\\".server-modal-lg\\" data-toggle=\\"modal\\" data-hostname=\\"" + target.hostname + "\\">" + hostname[0] + "</a> <i class=\\"fa fa-server text-" + status + " pull-right\\" aria-hidden=\\"true\\"></i></h2><hr> \\\n    <p class=\\"text-muted lead\\">" + target.hostname + " <span class=\\"label label-primary\\">" + target.facts.os + "</span></p><hr> \\\n    <span class=\\"label label-success\\">" + counts.OK + " OK</span> \\\n    <span class=\\"label label-warning\\">" + counts.WARNING + " WARNING</span> \\\n    <span class=\\"label label-danger\\">" + counts.CRITICAL + " CRITICAL</span> \\\n    <span class=\\"label label-info\\">" + counts.UNKNOWN + " UNKNOWN</span> \\\n    </div></div></div>").fadeIn("slow");\n}\n\n// Update Target Counter\nfunction updateTargetCount(count) {\n    "use strict";\n    $("div.target-count").text(count).fadeIn("slow");\n}\n\n// Update Runbook OK Counter\nfunction updateOkCount(count) {\n    "use strict";\n    $("div.ok-count").text(count).fadeIn("slow");\n}\n\n// Update Runbook Unknown Counter\nfunction updateUnknownCount(count) {\n    "use strict";\n    $("div.unknown-count").text(count).fadeIn("slow");\n}\n\n// Update Runbook Warning Counter\nfunction updateWarningCount(count) {\n    "use strict";\n    $("div.warning-count").text(count).fadeIn("slow");\n}\n\n// Update Runbook Critical Counter\nfunction updateCriticalCount(count) {\n    "use strict";\n    $("div.critical-count").text(count).fadeIn("slow");\n}\n\n// Add Runbook Event to event stream\nfunction addEvent(value) {\n    "use strict";\n    var hostname = value.target.split(".");\n    if (value.status === "CRITICAL") {\n        $("ul.status-group").append("<li class=\\"list-group-item list-group-item-danger status-item\\"><span class=\\"badge\\">" + value.status + "</span><b>" + hostname[0] + "</b>: " + value.runbook + "</li>").fadeIn("slow");\n    }\n    if (value.status === "OK" && value.count <= 1) {\n        $("ul.status-group").append("<li class=\\"list-group-item list-group-item-success status-item\\"><span class=\\"badge\\">" + value.status + "</span><b>" + hostname[0] + "</b>: " + value.runbook + "</li>").fadeIn("slow");\n    }\n    if (value.status === "WARNING") {\n        $("ul.status-group").append("<li class=\\"list-group-item list-group-item-warning status-item\\"><span class=\\"badge\\">" + value.status + "</span><b>" + hostname[0] + "</b>: " + value.runbook + "</li>").fadeIn("slow");\n    }\n    if (value.status === "UNKNOWN") {\n        $("ul.status-group").append("<li class=\\"list-group-item list-group-item-info status-item\\"><span class=\\"badge\\">" + value.status + "</span><b>" + hostname[0] + "</b>: " + value.runbook + "</li>").fadeIn("slow");\n    }\n}\n\n// Perform API call and process results for Status and Events\nfunction getStatus() {\n    "use strict";\n    $.ajax({\n        url: "/api/status",\n        dataType: "json",\n        success: function (json) {\n            updateTargetCount(json.targets);\n            updateOkCount(json.runbooks.OK);\n            updateWarningCount(json.runbooks.WARNING);\n            updateCriticalCount(json.runbooks.CRITICAL);\n            updateUnknownCount(json.runbooks.UNKNOWN);\n            // Clear existing status\n            if (json.events.length >= 1) {\n                $("ul.status-group").empty();\n                $.each(json.events, function (key, value) {\n                    addEvent(value);\n                });\n                if ($("li.status-item").length === 0) {\n                    $("ul.status-group").append("<li class=\\"list-group-item list-group-item-default status-item\\">No events found</li>");\n                }\n            } else {\n                $("ul.status-group").empty();\n                $("ul.status-group").append("<li class=\\"list-group-item list-group-item-default status-item\\">No events found</li>");\n            }\n        }\n    });\n}\n\n// Perform API call and process results for Target list\nfunction getTargets() {\n    "use strict";\n    $.ajax({\n        url: "/api/targets",\n        dataType: "json",\n        success: function (json) {\n            $("div.servers-group").empty();\n            var added = 0;\n            $("div.servers-group").append("<div class=\\"row\\">");\n            $.each(json, function (key, value) {\n                if (added % 3 === 0) {\n                    $("div.servers-group").append("</div><div class=\\"row\\">");\n                }\n                updateTargets(value);\n                added = added + 1;\n            });\n            $("div.servers-group").append("</div>");\n        }\n    });\n}\n\n\n$(document).on("click", ".server-info", function() {\n    "use strict";\n    var name = $(this).data().hostname;\n    $("h4.modal-title").text(name);\n    $.ajax({\n        url: "/api/targets/" + name,\n        dataType: "json",\n        success: function (json) {\n            // Update Server info\n            $("div.server-panel-info").html("<div class=\\"row\\">" +\n            "<div class=\\"col-md-6\\"><b>Name:</b> " + json.hostname + "</div><div class=\\"col-md-6\\"><b>Operating System:</b> " + json.facts.os + "</div>" +\n            "<div class=\\"col-md-6\\"><b>Address:</b> " + json.ip + " </div><div class=\\"col-md-6\\"><b>Kernel:</b> " + json.facts.kernel + "</div>" +\n            "</div>")\n\n            // Update Runbook status\n            $("ul.server-runbooks-group").empty();\n            $.each(json.runbooks, function (key, value) {\n                if (value.last_status === "CRITICAL") {\n                    $("ul.server-runbooks-group").append("<li class=\\"list-group-item list-group-item-danger status-item\\"><span class=\\"badge\\">" + value.last_status + "</span><b>" + value.name + "</b></li>");\n                }\n                if (value.last_status === "OK") {\n                    $("ul.server-runbooks-group").append("<li class=\\"list-group-item list-group-item-success status-item\\"><span class=\\"badge\\">" + value.last_status + "</span><b>" + value.name + "</b></li>");\n                }\n                if (value.last_status === "WARNING") {\n                    $("ul.server-runbooks-group").append("<li class=\\"list-group-item list-group-item-warning status-item\\"><span class=\\"badge\\">" + value.last_status + "</span><b>" + value.name + "</b></li>");\n                }\n                if (value.last_status === "UNKNOWN") {\n                    $("ul.server-runbooks-group").append("<li class=\\"list-group-item list-group-item-info status-item\\"><span class=\\"badge\\">" + value.last_status + "</span><b>" + value.name + "</b></li>");\n                }\n            });\n            if (json.runbooks.length < 0) {\n                $("ul.server-runbooks-group").append("<li class=\\"list-group-item list-group-item-default status-item\\">No Runbooks found</li>")\n            }\n        }\n    });\n});\n\n\n// Wrapper run function\nfunction run() {\n    "use strict";\n    getStatus();\n    getTargets();\n}\n\n\n// Run run() on load and every 10 seconds\nwindow.onload = run();\nwindow.setInterval(function () {\n    "use strict";\n    run();\n}, 10000);\n', 'message': "Merge branch 'develop' into update-readme", 'author_name': 'GitHub', 'lang': 'js'}