further fixes for #335 to key netmap fully off IP

This commit is contained in:
Oliver Gorwits
2017-08-01 10:05:33 +01:00
parent d7f6dccaf6
commit d2c50e9454
2 changed files with 20 additions and 29 deletions

View File

@@ -34,9 +34,8 @@ sub _add_children {
var('seen')->{$c}++; var('seen')->{$c}++;
push @legit, $c; push @legit, $c;
push @{$ptr}, { push @{$ptr}, {
name => _get_name($c),
fullname => (var('devices')->{$c} || $c),
ip => $c, ip => $c,
name => _get_name($c),
}; };
} }
@@ -93,8 +92,7 @@ get '/ajax/data/device/netmap' => require_login sub {
my %tree = ( my %tree = (
ip => $start, ip => $start,
name => _get_name($start), name => _get_name($start), # dns or sysname or ip
fullname => (var('devices')->{$start} || $start),
children => [], children => [],
); );
@@ -107,20 +105,13 @@ get '/ajax/data/device/netmap' => require_login sub {
}; };
ajax '/ajax/data/device/alldevicelinks' => require_login sub { ajax '/ajax/data/device/alldevicelinks' => require_login sub {
my @devices = schema('netdisco')->resultset('Device')->search({}, {
result_class => 'DBIx::Class::ResultClass::HashRefInflator',
columns => ['ip', 'dns'],
})->all;
var(devices => { map { $_->{ip} => $_->{dns} } @devices });
my $rs = schema('netdisco')->resultset('Virtual::DeviceLinks')->search({}, { my $rs = schema('netdisco')->resultset('Virtual::DeviceLinks')->search({}, {
result_class => 'DBIx::Class::ResultClass::HashRefInflator', result_class => 'DBIx::Class::ResultClass::HashRefInflator',
}); });
my %tree = (); my %tree = ();
while (my $l = $rs->next) { while (my $l = $rs->next) {
push @{ $tree{ _get_name($l->{left_ip} )} }, push @{ $tree{ $l->{left_ip} } }, $l->{right_ip};
_get_name($l->{right_ip});
} }
content_type('application/json'); content_type('application/json');

View File

@@ -45,14 +45,14 @@ function redraw() {
// save the x,y of an element into the loc dictionary // save the x,y of an element into the loc dictionary
function recordLocation(d,i) { function recordLocation(d,i) {
var rect = this.getBoundingClientRect(); var rect = this.getBoundingClientRect();
loc[d.name] = { loc[d.ip] = {
'x': (rect.left + ((rect.right - rect.left) / 2)) 'x': (rect.left + ((rect.right - rect.left) / 2))
,'y': (rect.top + ((rect.bottom - rect.top) / 2)) ,'y': (rect.top + ((rect.bottom - rect.top) / 2))
}; };
} }
// convert a device name to a valid CSS class name // convert a device ip to a valid CSS class name
function to_class(name) { return 'nd_' + name.replace(/\./g, "_") } function to_class(ip) { return 'nd_' + ip.replace(/[:.]/g, "_") }
// handler for clicking on a circle - redirect to that device's netmap // handler for clicking on a circle - redirect to that device's netmap
function circleClick(d) { function circleClick(d) {
@@ -65,10 +65,10 @@ function circleClick(d) {
// handler for mouseover on a circle - show that device's real neighbors // handler for mouseover on a circle - show that device's real neighbors
function circleOver(d) { function circleOver(d) {
$('.link').hide(); $('.link').hide();
$('path.' + to_class(d.name)).show(); $('path.' + to_class(d.ip)).show();
$(this).css('cursor', 'pointer'); $(this).css('cursor', 'pointer');
$.each(neighbors_data[d.name], function(idx, target) { $.each(neighbors_data[d.ip], function(idx, target) {
if (! (target in loc)) { return true } if (! (target in loc)) { return true }
$('circle.' + to_class(target)).css('fill', '#e96cfa'); $('circle.' + to_class(target)).css('fill', '#e96cfa');
}); });
@@ -76,13 +76,13 @@ function circleOver(d) {
// handler for mouseout on a circle - hide real neighbours and show treeLinks // handler for mouseout on a circle - hide real neighbours and show treeLinks
function circleOut(d) { function circleOut(d) {
$.each(neighbors_data[d.name], function(idx, target) { $.each(neighbors_data[d.ip], function(idx, target) {
if (! (target in loc)) { return true } if (! (target in loc)) { return true }
$('circle.' + to_class(target)).css('fill', '#fff'); $('circle.' + to_class(target)).css('fill', '#fff');
}); });
$(this).css('cursor', 'auto'); $(this).css('cursor', 'auto');
$('path.' + to_class(d.name)).hide(); $('path.' + to_class(d.ip)).hide();
$('.link').show(); $('.link').show();
} }
@@ -121,7 +121,7 @@ $.getJSON('[% uri_for('/ajax/data/device/alldevicelinks') %]', function(data) {
node.append("circle") node.append("circle")
.attr("r", 4.5) .attr("r", 4.5)
// circle has class name of its device, so we can show/hide it // circle has class name of its device, so we can show/hide it
.attr("class", function(d) { return to_class(d.name) }) .attr("class", function(d) { return to_class(d.ip) })
// store the x,y of every circle we've just drawn // store the x,y of every circle we've just drawn
.each(recordLocation) .each(recordLocation)
// handlers for mouse interaction with the circles // handlers for mouse interaction with the circles
@@ -140,8 +140,8 @@ $.getJSON('[% uri_for('/ajax/data/device/alldevicelinks') %]', function(data) {
.attr("transform", function(d) { .attr("transform", function(d) {
return d.x < 180 ? "rotate(0)translate(0)" : "rotate(180)translate(0)"; }); return d.x < 180 ? "rotate(0)translate(0)" : "rotate(180)translate(0)"; });
// key (name) of the root node in our locations store // key (ip) of the root node in our locations store
var rootname = svg.select(".node").data()[0].name; var rootname = svg.select(".node").data()[0].ip;
// reformatted neighbors_data for the real neighbor links // reformatted neighbors_data for the real neighbor links
var neighbors = []; var neighbors = [];
@@ -150,19 +150,19 @@ $.getJSON('[% uri_for('/ajax/data/device/alldevicelinks') %]', function(data) {
$.each(neighbors_data, function(key, val) { $.each(neighbors_data, function(key, val) {
if (! (key in loc)) { return true } if (! (key in loc)) { return true }
$.each(val, function(idx, name) { $.each(val, function(idx, ip) {
if (! (name in loc)) { return true } if (! (ip in loc)) { return true }
neighbors.push({ neighbors.push({
'source': { 'source': {
'name': key 'ip': key
,'x': loc[key]['x'] ,'x': loc[key]['x']
,'y': loc[key]['y'] ,'y': loc[key]['y']
} }
,'target': { ,'target': {
'name': name 'ip': ip
,'x': loc[name]['x'] ,'x': loc[ip]['x']
,'y': loc[name]['y'] ,'y': loc[ip]['y']
} }
}); });
}); });
@@ -174,7 +174,7 @@ $.getJSON('[% uri_for('/ajax/data/device/alldevicelinks') %]', function(data) {
.enter().insert("path", ".node") .enter().insert("path", ".node")
// add class name of source device, so we can show/hide the link // add class name of source device, so we can show/hide the link
// (also "neighbor" class) // (also "neighbor" class)
.attr("class", function(d) { return ("neighbor " + to_class( d.source.name )) }) .attr("class", function(d) { return ("neighbor " + to_class( d.source.ip )) })
.attr("d", neighLink) .attr("d", neighLink)
.attr("transform", "translate(-" + loc[rootname]['x'] + ",-" + loc[rootname]['y'] + ")"); .attr("transform", "translate(-" + loc[rootname]['x'] + ",-" + loc[rootname]['y'] + ")");
}); });