diff options
-rwxr-xr-x | web/api/read/comments | 15 | ||||
-rwxr-xr-x | web/api/read/oplog | 2 | ||||
-rwxr-xr-x | web/api/write/comment-add | 24 | ||||
-rwxr-xr-x | web/api/write/comment-change | 25 | ||||
-rw-r--r-- | web/index.html | 5 | ||||
-rw-r--r-- | web/js/nms-info-box.js | 101 | ||||
-rw-r--r-- | web/js/nms-map-handlers.js | 61 | ||||
-rw-r--r-- | web/js/nms-oplog.js | 18 | ||||
-rw-r--r-- | web/js/nms.js | 64 |
9 files changed, 42 insertions, 273 deletions
diff --git a/web/api/read/comments b/web/api/read/comments deleted file mode 100755 index d7c8cdb..0000000 --- a/web/api/read/comments +++ /dev/null @@ -1,15 +0,0 @@ -#! /usr/bin/perl -# vim:ts=8:sw=8 - -use lib '/opt/gondul/include'; -use nms::web; -use strict; -use warnings; - -my $query = $nms::web::dbh->prepare('select sysname,extract(epoch from date_trunc(\'second\',time)) as time,state,username,id,comment from switch_comments natural join switches where state != \'delete\' order by time desc'); -$query->execute(); -while (my $ref = $query->fetchrow_hashref()) { - push @{$nms::web::json{'comments'}{$ref->{'sysname'}}{'comments'}},$ref; -} - -nms::web::finalize_output(); diff --git a/web/api/read/oplog b/web/api/read/oplog index d56890a..92511bb 100755 --- a/web/api/read/oplog +++ b/web/api/read/oplog @@ -6,7 +6,7 @@ use nms::web; use strict; use warnings; -my $query = $nms::web::dbh->prepare('select id,time as timestamp,extract(hour from time) as h, extract(minute from time) as m,systems,username,log from oplog order by id desc;'); +my $query = $nms::web::dbh->prepare('select id,date_trunc(\'second\',time) as timestamp,extract(hour from time) as h, extract(minute from time) as m,systems,username,log from oplog order by id desc;'); $query->execute(); while (my $ref = $query->fetchrow_hashref()) { my %meh; diff --git a/web/api/write/comment-add b/web/api/write/comment-add deleted file mode 100755 index f87a28c..0000000 --- a/web/api/write/comment-add +++ /dev/null @@ -1,24 +0,0 @@ -#! /usr/bin/perl -# vim:ts=8:sw=8 -use lib '/opt/gondul/include'; -use utf8; -use nms::web qw($dbh db_safe_quote get_input finalize_output); -use strict; -use warnings; - -my $in = get_input(); -my %tmp = %{JSON::XS::decode_json($in)}; - -my $data = $dbh->quote($tmp{'comment'}); -my $switch = $dbh->quote($tmp{'switch'}); -my $user = $dbh->quote($ENV{'REMOTE_USER'} || "undefined"); - -my $q = $nms::web::dbh->prepare("INSERT INTO switch_comments (time,username,switch,comment) values (now(),$user,(select switch from switches where sysname = $switch limit 1),$data)"); -$q->execute(); - -$nms::web::cc{'max-age'} = '0'; -$nms::web::cc{'stale-while-revalidate'} = '0'; -$nms::web::json{'state'} = 'ok'; - -print "X-ban: /api/read/comments\n"; -finalize_output(); diff --git a/web/api/write/comment-change b/web/api/write/comment-change deleted file mode 100755 index 6318723..0000000 --- a/web/api/write/comment-change +++ /dev/null @@ -1,25 +0,0 @@ -#! /usr/bin/perl -# vim:ts=8:sw=8 -use lib '/opt/gondul/include'; -use utf8; -use nms; -use nms::web qw($dbh db_safe_quote get_input finalize_output); - -use strict; -use warnings; - -my $in = get_input(); -my %tmp = %{JSON::XS::decode_json($in)}; - -my $id = $dbh->quote($tmp{'comment'}); -my $state = $dbh->quote($tmp{'state'}); - -my $q = $nms::web::dbh->prepare("UPDATE switch_comments SET state = " . $state . " WHERE id = " . $id . ";"); -$q->execute(); - -$nms::web::cc{'max-age'} = '0'; -$nms::web::cc{'stale-while-revalidate'} = '0'; -$nms::web::json{'state'} = 'ok'; - -print "X-ban: /api/read/comments\n"; -finalize_output(); diff --git a/web/index.html b/web/index.html index 0bd7ade..e823ef7 100644 --- a/web/index.html +++ b/web/index.html @@ -124,7 +124,6 @@ <li><a href="#ping" onclick="setUpdater(handler_ping)">Ping map</a></li> <li><a href="#uplink" onclick="setUpdater(handler_uplinks)">Uplink map</a></li> <li><a href="#dhcp" onclick="setUpdater(handler_dhcp)">DHCP map</a></li> - <li class="gondul-is-private"><a href="#comment" onclick="setUpdater(handler_comment)">Comment spotter</a></li> <li><a href="#temp" onclick="setUpdater(handler_temp)">Temperature map</a></li> <li><a href="#traffic" onclick="setUpdater(handler_traffic)">Traffic map</a></li> <li><a href="#traffictot" onclick="setUpdater(handler_traffic_tot)">Total switch traffic</a></li> @@ -235,10 +234,6 @@ <td>View DHCP map</td> </tr> <tr> - <td>4</td> - <td>View comment spotter map</td> - </tr> - <tr> <td>5</td> <td>View temperature map</td> </tr> diff --git a/web/js/nms-info-box.js b/web/js/nms-info-box.js index b83ddd1..bad2960 100644 --- a/web/js/nms-info-box.js +++ b/web/js/nms-info-box.js @@ -22,7 +22,6 @@ * * General TODO: * - Fix broken stuff - * - Test comments * - Add external windows (timetravel, etc) * - Take a critical look at what methods/variables should be marked as "_" * - Currently argument is assumed to be a switch, this should not be the case @@ -835,36 +834,27 @@ var switchCommentsPanel = function () { var domObj = document.createElement("div"); var comments = []; - var commentbox = document.createElement("div"); - commentbox.id = "commentbox"; - commentbox.className = "panel-body"; - commentbox.style.width = "100%"; - commentbox.innerHTML = '<div class="input-group"><input type="text" class="form-control" placeholder="Comment" id="' + this.sw + '-comment"><span class=\"input-group-btn\"><button class="btn btn-default" onclick="addComment(\'' + this.sw + '\',document.getElementById(\'' + this.sw + '-comment\').value); document.getElementById(\'' + this.sw + '-comment\').value = \'\'; document.getElementById(\'' + this.sw + '-comment\').placeholder = \'Comment added. Wait for next refresh.\';">Add comment</button></span></div>'; - + var logs = nmsOplog.getSwitchLogs(this.sw); // We have data - if(!(!nmsData.comments || !nmsData.comments.comments)) { - this.commentsHash = nmsData.comments.hash; - - // We have data for this switch - if(nmsData.comments.comments[this.sw]) { - this.commentsHash = nmsData.comments.hash; - for (var c in nmsData.comments.comments[this.sw]["comments"]) { - var comment = nmsData.comments.comments[this.sw]["comments"][c]; - if (comment["state"] == "active" || comment["state"] == "persist" || comment["state"] == "inactive") { - comments.push(comment); - } - } - - if (comments.length > 0) { - var commenttable = nmsInfoBox._makeCommentTable(comments); - commenttable.id = "info-switch-comments-table"; - domObj.appendChild(commenttable); - } + var table = document.createElement("table"); + var tr; + var td1; + var td2; + var td3; + table.className = "table"; + table.classList.add("table"); + table.classList.add("table-condensed"); + for (var v in logs) { + tr = table.insertRow(-1); + tr.className = + td1 = tr.insertCell(0); + td2 = tr.insertCell(1); + td1.innerHTML = logs[v]['timestamp']; + td2.innerHTML = "[" + logs[v]['username'] + "] " + logs[v]['log']; + } + domObj.appendChild(table); - } - } - domObj.appendChild(commentbox); this._render(domObj); }; }; @@ -1023,51 +1013,17 @@ nmsInfoBox._makeTable = function(content, caption) { return table; }; -/* - * Create and return a table for comments. - * - * Input is an array of comments. - */ -nmsInfoBox._makeCommentTable = function(content) { - var table = document.createElement("table"); - table.className = "table"; - table.classList.add("table"); - table.classList.add("table-condensed"); - var cap = document.createElement("caption"); - cap.textContent = "Comments" - table.appendChild(cap); - for (var commentid in content) { - var tr; - var td1; - var td2; - var comment = content[commentid]; - var col; - if (comment["state"] == "active") - col = "danger"; - else if (comment["state"] == "inactive") - col = false; - else - col = "info"; - tr = table.insertRow(-1); - tr.id = "commentRow" + comment["id"]; - tr.className = col; - - td1 = tr.insertCell(0); - td1.style.whiteSpace = "nowrap"; - td1.style.width = "8em"; - td2 = tr.insertCell(1); - var txt = '<div class="btn-group" role="group" aria-label="..."><button type="button" class="btn btn-xs btn-default" data-trigger="focus" data-toggle="popover" title="Info" data-content="Comment added ' + comment["time"] + " by user " + comment["username"] + ' and listed as ' + comment["state"] + '"><span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span></button>'; - txt += '<button type="button" class="btn btn-xs btn-danger" data-trigger="focus" data-toggle="tooltip" title="Mark as deleted" onclick="commentDelete(' + comment["id"] + ');"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>'; - txt += '<button type="button" class="btn btn-xs btn-success" data-trigger="focus" data-toggle="tooltip" title="Mark as inactive/fixed" onclick="commentInactive(' + comment["id"] + ');"><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></button>'; - txt += '<button type="button" class="btn btn-xs btn-info" data-trigger="focus" data-toggle="tooltip" title="Mark as persistent" onclick="commentPersist(' + comment["id"] + ');"><span class="glyphicon glyphicon-star" aria-hidden="true"></span></button></div>'; - td1.innerHTML = txt; - td2.innerHTML = comment["comment"]; - } - return table; -}; +nmsInfoBox.searchSmart = function(id, sw) { + if (id == "") + return false; + return nmsInfoBox._searchSmart(id, sw); +} nmsInfoBox._searchSmart = function(id, sw) { try { + if(sw.toLowerCase().indexOf(id) > -1) { + return true; + } try { if (nmsData.switches.switches[sw].distro_name.toLowerCase() == id) { return true; @@ -1138,10 +1094,7 @@ nmsInfoBox._search = function() { if(id) { nmsMap.enableHighlights(); for(var sw in nmsData.switches.switches) { - if(sw.toLowerCase().indexOf(id) > -1) { - matches.push(sw); - nmsMap.setSwitchHighlight(sw,true); - } else if (nmsInfoBox._searchSmart(id,sw)) { + if (nmsInfoBox._searchSmart(id,sw)) { matches.push(sw); nmsMap.setSwitchHighlight(sw,true); } else { diff --git a/web/js/nms-map-handlers.js b/web/js/nms-map-handlers.js index 8c1f2b3..f4aba57 100644 --- a/web/js/nms-map-handlers.js +++ b/web/js/nms-map-handlers.js @@ -61,12 +61,6 @@ var handler_disco = { name:"Disco fever" }; -var handler_comment = { - init:commentInit, - tag:"comment", - name:"Fresh comment spotter" -}; - var handler_snmp = { init:snmpInit, tag:"snmp", @@ -85,7 +79,6 @@ var handlers = [ handler_ping, handler_traffic, handler_disco, - handler_comment, handler_traffic_tot, handler_dhcp, handler_snmp, @@ -306,60 +299,6 @@ function pingInit() nmsData.addHandler("ticker", "mapHandler", pingUpdater); } -function commentUpdater() -{ - var realnow = Date.now(); - var now = Math.floor(realnow / 1000); - if (nmsData.comments == undefined || nmsData.comments.comments == undefined) { - return - } - if(!nmsData.switches) - return; - for (var sw in nmsData.switches.switches) { - var c = "white"; - if (nmsData.comments.comments[sw] == undefined) { - nmsMap.setSwitchColor(sw,c); - continue; - } - var s = nmsData.comments.comments[sw]; - var then = 0; - var active = 0; - var persist = 0; - c = "yellow"; - for (var v in s["comments"]) { - var then_test = parseInt(s["comments"][v]["time"]); - if (then_test > then && s["comments"][v]["state"] != "inactive") - then = then_test; - if (s["comments"][v]["state"] == "active") { - active++; - } - if (s["comments"][v]["state"] == "persist") - persist++; - } - if (then > (now - (60*15))) { - c = red; - } else if (active > 0) { - c = orange; - } else if (persist > 0) { - c = blue; - } else { - c = green; - } - nmsMap.setSwitchColor(sw, c); - } -} - - -function commentInit() -{ - nmsData.addHandler("comments","mapHandler",commentUpdater); - setLegend(1,"white","0 comments"); - setLegend(2,blue,"Persistent"); - setLegend(3,red, "New"); - setLegend(4,orange,"Active"); - setLegend(5,green ,"Old/inactive only"); -} - function getDhcpColor(stop) { stop = parseInt(stop); diff --git a/web/js/nms-oplog.js b/web/js/nms-oplog.js index 91782e7..08291f4 100644 --- a/web/js/nms-oplog.js +++ b/web/js/nms-oplog.js @@ -25,6 +25,7 @@ nmsOplog.commit = function() { }); document.getElementById('logbox-id').value = ""; document.getElementById('logbox').value = ""; + document.getElementById('searchbox').value = ""; } @@ -32,6 +33,18 @@ nmsOplog.updateComments = function() { nmsOplog._updateComments(5,"-mini","time"); nmsOplog._updateComments(0,"","timestamp"); } + +nmsOplog.getSwitchLogs = function(sw) { + var logs = []; + for (var v in nmsData['oplog']['oplog']) { + var log = nmsData['oplog']['oplog'][v]; + if (nmsInfoBox.searchSmart(log['systems'],sw)) { + logs.push(log); + } + } + return logs; +} + nmsOplog._updateComments = function(limit,prefix,timefield) { var table = document.createElement("table"); var tr; @@ -44,13 +57,10 @@ nmsOplog._updateComments = function(limit,prefix,timefield) { var i = 0; for (var v in nmsData['oplog']['oplog']) { tr = table.insertRow(-1); - tr.className = td1 = tr.insertCell(0); td2 = tr.insertCell(1); - td3 = tr.insertCell(2); td1.innerHTML = nmsData['oplog']['oplog'][v][timefield]; - td2.innerHTML = nmsData['oplog']['oplog'][v]['username']; - td3.innerHTML = nmsData['oplog']['oplog'][v]['log']; + td2.innerHTML = "[" + nmsData['oplog']['oplog'][v]['username'] + "] " + nmsData['oplog']['oplog'][v]['log']; if (++i == limit) break; } diff --git a/web/js/nms.js b/web/js/nms.js index 8eb1f3d..b979a32 100644 --- a/web/js/nms.js +++ b/web/js/nms.js @@ -440,66 +440,6 @@ function toggleConnect() { toggleLayer("linkCanvas"); } -function commentInactive(id) -{ - commentChange(id,"inactive"); -} - -function commentPersist(id) -{ - commentChange(id,"persist"); -} - -function commentDelete(id) -{ - var r = confirm("Really delete comment? (Delted comments are still stored in the database, but never displayed)"); - if (r == true) { - commentChange(id,"delete"); - } -} - -/* - * FIXME: Neither of these two handle failures in any way, shape or form. - * Nor do they really give user-feedback. They work, but only by magic. - */ -function commentChange(id,state) -{ - var myData = { - comment:id, - state:state - }; - myData = JSON.stringify(myData); - $.ajax({ - type: "POST", - url: "/api/write/comment-change", - dataType: "text", - data:myData, - success: function (data, textStatus, jqXHR) { - nmsData.invalidate("comments"); - } - }); -} - -function addComment(sw,comment) -{ - var myData = { - switch:sw, - comment:comment - }; - myData = JSON.stringify(myData); - $.ajax({ - type: "POST", - url: "/api/write/comment-add", - dataType: "text", - data:myData, - success: function (data, textStatus, jqXHR) { - nmsData.invalidate("comments"); - } - }); -} - - - /* * Returns true if the coordinates (x,y) is inside the box defined by * box.{x,y,w.h} (e.g.: placement of a switch). @@ -610,7 +550,6 @@ function initNMS() { if (!nms._public) { // Private nmsData.registerSource("snmp","/api/read/snmp"); - nmsData.registerSource("comments", "/api/read/comments"); nmsData.registerSource("smanagement","/api/read/switches-management"); nmsData.registerSource("oplog", "/api/read/oplog"); nmsOplog.init(); @@ -684,9 +623,6 @@ function setMapModeFromN(e,key) case '3': setUpdater(handler_dhcp); break; - case '4': - setUpdater(handler_comment); - break; case '5': setUpdater(handler_temp); break; |