Skip to content

Commit

Permalink
Closes #358: worldmap errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mohierf committed Nov 23, 2017
1 parent 4311931 commit 0fa481e
Show file tree
Hide file tree
Showing 5 changed files with 1,092 additions and 16 deletions.
2 changes: 1 addition & 1 deletion alignak_webui/plugins/worldmap/settings.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
; Plugin global configuration
; enabled=false

;default_zoom=8
default_zoom=4

default_longitude=2.294601
default_latitude=48.858293
Expand Down
58 changes: 47 additions & 11 deletions alignak_webui/plugins/worldmap/static/js/worldmap.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// Set true to activate javascript console logs
var debugMaps = true;
var debugMaps = false;
if (debugMaps && !window.console) {
alert('Your web browser does not have any console object ... you should stop using IE ;-) !');
}
if (debugMaps) {
console.log("Activated debug maps logs");
}

var positionHosts = false;
// Set to true to activate the host positioning feature...
var positionHosts = true;

var defaultZoom = 12;
var defaultZoom = 6;
if (debugMaps) console.log('Default zoom: ', defaultZoom);
var defaultCenter = L.latLng(46.60611, 1.87528);
if (debugMaps) console.log('Default center: ', defaultCenter);
Expand All @@ -21,8 +22,9 @@ var geocoder_control;
var geocoder;
var geocoder_marker;

var selectedHosts = [];
var selectedHostIndex = -1;
var selectedHost = null;
// var selectedHosts = [];
// var selectedHostIndex = -1;
var selectedPosition = null;

// Markers ...
Expand Down Expand Up @@ -129,13 +131,16 @@ function setHostPosition(host, marker){

function selectHost(host){
if (debugMaps) console.log("Select an host: ", host.name);
selectedHosts.push(host);
// selectedHosts.push(host);
selectedHost = host;
var newElt = $('<li data-hostname="' + host.name + '"><a href="#" class="list-group-item">' + host.name + '</a></li>');
newElt.on("click", function () {
if (debugMaps) console.log("Unselect an host: ", $(this).data("hostname"));
for (var i = 0; i < pos_hosts.length; i++) {
if (pos_hosts[i].name == $(this).data("hostname")) {
selectedHost = null;
$('#selected_hosts li[data-hostname="' + pos_hosts[i].name + '"]').remove();
$("#selected_hosts").hide();

if (! pos_hosts[i].positioned) {
var newElt = $('<li data-hostname="' + pos_hosts[i].name + '"><a href="#" class="list-group-item">' + pos_hosts[i].name + '</a></li>');
Expand All @@ -145,6 +150,7 @@ function selectHost(host){
}
});
$("#selected_hosts ul").append(newElt);
$("#selected_hosts").show();
if (debugMaps) console.log("Selected host: ", host.name);
};

Expand Down Expand Up @@ -274,10 +280,12 @@ mapInit = function(map_id, editable, callback) {
iconSize: new L.Point(60, 60)
});
}
}).addTo($map);
// }).addTo($map);
});

if (debugMaps) console.log('Manage positioned hosts.');
if (positionHosts) {
if (debugMaps) console.log('Manage positioned hosts.');
// Geocoder
geocoder_control = L.Control.geocoder({
collapsed: false,
Expand All @@ -287,17 +295,38 @@ mapInit = function(map_id, editable, callback) {
showResultIcons:true
}).addTo($map);
$map.on('click', function(e) {
geocoder_control.geocoder.reverse(e.latlng, map.options.crs.scale(map.getZoom()), function(results) {
if (debugMaps) console.log('Geocoder:', geocoder_control, geocoder_control.geocoder);
geocoder_control.options.geocoder.reverse(e.latlng, $map.options.crs.scale($map.getZoom()), function(results) {
var r = results[0];
if (r) {
if (selectedHost) {
var marker = markerCreate($map, selectedHost, markerCluster, editable);
allMarkers.push(marker);
setHostPosition(selectedHost, marker);

raise_message_ok('Host: ' + selectedHost.name + 'is now positioned.');
selectedHost = null;
$("#selected_hosts").hide();
}

/*
if (geocoder_marker) {
geocoder_marker.
setLatLng(r.center).
setPopupContent(r.html || r.name).
openPopup();
} else {
geocoder_marker = L.marker(r.center).bindPopup(r.name).addTo(map).openPopup();
geocoder_marker = L.marker(r.center).bindPopup(r.name).addTo($map).openPopup();
geocoder_marker.on('dragstart', function(event) {
var marker = event.target;
marker.opacity = 0.5;
});
geocoder_marker.on('dragend', function(event) {
setHostPosition(host, event.target);
});
}
*/
}
})
})
Expand All @@ -306,11 +335,13 @@ mapInit = function(map_id, editable, callback) {
// Markers ...
var allMarkers = [];
var someHostsAreNotPositioned = false;
var someHostsArePositioned = false;
for (var i = 0; i < pos_hosts.length; i++) {
if (debugMaps) console.log('Host', pos_hosts[i]);
if (pos_hosts[i].positioned) {
var marker = markerCreate($map, pos_hosts[i], markerCluster, editable);
allMarkers.push(marker);
someHostsArePositioned = true;
} else {
var newElt = $('<li data-hostname="' + pos_hosts[i].name + '"><a href="#" class="list-group-item">' + pos_hosts[i].name + '</a></li>');
newElt.on("click", function () {
Expand Down Expand Up @@ -353,11 +384,16 @@ mapInit = function(map_id, editable, callback) {
if (! someHostsAreNotPositioned) {
$("#not_positioned_hosts").remove();
}
if (! selectedHost) {
$("#selected_hosts").hide();
}
markerCluster.addLayers(allMarkers);
$map.addLayer(markerCluster);

if (debugMaps) console.log('Fit bounds for the map.');
// $map.fitBounds(markerCluster.getBounds());
if (someHostsArePositioned) {
if (debugMaps) console.log('Fit bounds for the map.', markerCluster.getBounds());
$map.fitBounds(markerCluster.getBounds());
}

allMaps.push($map);

Expand Down
3 changes: 2 additions & 1 deletion alignak_webui/plugins/worldmap/views/_worldmap.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
),
%end
]
if (debugMaps) console.log("Hosts (positioned):", pos_hosts);
if (debugMaps) {
console.log("Hosts (positioned):", pos_hosts);
for (var i = 0; i < pos_hosts.length; i++) {
console.log("- services", pos_hosts[i].services);
}
Expand Down Expand Up @@ -83,6 +83,7 @@
$.getJsFiles(jsfiles, function(){
// Wait for a while to let the scripts get executed...
window.setTimeout(function() {
if (debugMaps) console.log('Calling initWorldmap after files loading...')
initWorldmap();
}, 1000);
});
Expand Down
7 changes: 4 additions & 3 deletions alignak_webui/plugins/worldmap/worldmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,12 @@ def get_map_elements(self, hosts):
map_host = {}
logger.debug("worldmap, found host '%s'", host.name)

if not host.monitored:
logger.debug("worldmap, host is not monitored")
continue

map_host['positioned'] = True
if 'type' not in host.position or host.position['type'] != 'Point':
# logger.warning("worldmap, host '%s', invalid position: %s",
# host.name, host.position)
# continue
logger.warning("worldmap, host '%s' has an invalid position", host.name)
continue
else:
Expand Down
Loading

0 comments on commit 0fa481e

Please sign in to comment.