Skip to content

Commit

Permalink
Dynamic map objects can now be sorted by state
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsMichelsen committed Mar 17, 2018
1 parent a4ccd9e commit 2923179
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 15 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
@@ -1,5 +1,7 @@
1.9.6
Core:
* Dynamic map objects can now be sorted by state (Using the new map global
settings dynmap_sort and dynmap_order)
* FIX: Improved error handling when trying to use aggregation objects with
backends that don't support these object types
* FIX: Fixed "A non-numeric value encountered" when creating new automaps
Expand Down
11 changes: 11 additions & 0 deletions docs/en_US/dynmap.html
Expand Up @@ -86,6 +86,17 @@ <h2>Configuring dynmaps</h2>
<td>30</td>
<td>Objects to add in the grid per row. The Y offset is applied once a row reaches this number.</td>
</tr>
<tr>
<td>dynmap_sort</td>
<td></td>
<td>Sort method to use. Can be <code>a</code> for alphabetically order (default), <code>s</code> to
sort objects by state or <code>k</code> to keep the order of the backend.</td>
</tr>
<tr>
<td>dynmap_order</td>
<td>asc</td>
<td>Order of the objects (Available: asc/desc)</td>
</tr>
</table>
</body>
</html>
2 changes: 1 addition & 1 deletion share/server/core/classes/GlobalMapCfg.php
Expand Up @@ -130,7 +130,7 @@ private function registerConfigVars($configVars, $configVarMap, $source_name = n
* @param Boolean Only fetch global type settings
* @author Lars Michelsen <lm@larsmichelsen.com>
*/
private function gatherTypeDefaults($onlyGlobal) {
public function gatherTypeDefaults($onlyGlobal) {
$types = array('global');

if (!$onlyGlobal)
Expand Down
25 changes: 16 additions & 9 deletions share/server/core/classes/objects/NagVisObject.php
Expand Up @@ -489,9 +489,20 @@ private static function sortObjectsAlphabetical($OBJ1, $OBJ2) {
* @author Lars Michelsen <lm@larsmichelsen.com>
*/
private static function sortObjectsByState($OBJ1, $OBJ2) {
global $_MAINCFG;
$state1 = $OBJ1->sum[STATE];
$subState1 = $OBJ1->getSubState(SUMMARY_STATE);

$state2 = $OBJ2->sum[STATE];
$subState2 = $OBJ2->getSubState(SUMMARY_STATE);

return NagVisObject::sortObjectsByState($state1, $subState1, $state2, $subState2, self::$sSortOrder);
}

/**
* Helper to sort states independent of objects
*/
public static function sortStatesByStateValues($state1, $subState1, $state2, $subState2, $sortOrder) {
global $_MAINCFG;

// Quit when nothing to compare
if($state1 === null || $state2 === null) {
Expand All @@ -501,22 +512,18 @@ private static function sortObjectsByState($OBJ1, $OBJ2) {
$stateWeight = $_MAINCFG->getStateWeight();

// Handle normal/ack/downtime states

$stubState1 = $OBJ1->getSubState(SUMMARY_STATE);
$stubState2 = $OBJ2->getSubState(SUMMARY_STATE);

if($stateWeight[$state1][$stubState1] == $stateWeight[$state2][$stubState2]) {
if($stateWeight[$state1][$subState1] == $stateWeight[$state2][$subState2]) {
return 0;
} elseif($stateWeight[$state1][$stubState1] < $stateWeight[$state2][$stubState2]) {
} elseif($stateWeight[$state1][$subState1] < $stateWeight[$state2][$subState2]) {
// Sort depending on configured direction
if(self::$sSortOrder === 'asc') {
if($sortOrder === 'asc') {
return +1;
} else {
return -1;
}
} else {
// Sort depending on configured direction
if(self::$sSortOrder === 'asc') {
if($sortOrder === 'asc') {
return -1;
} else {
return +1;
Expand Down
84 changes: 79 additions & 5 deletions share/server/core/sources/dynmap.php
Expand Up @@ -107,6 +107,20 @@ function listDynObjectTypes($CORE) {
'default' => 30,
'match' => MATCH_COORD_SIMPLE,
),
'dynmap_sort' => Array(
'must' => false,
'default' => 'a',
'match' => MATCH_STRING_NO_SPACE,
'field_type' => 'dropdown',
'list' => 'listHoverChildSorters',
),
'dynmap_order' => Array(
'must' => false,
'default' => 'asc',
'match' => MATCH_ORDER,
'field_type' => 'dropdown',
'list' => 'listHoverChildOrders',
),
);

// Assign config variables to specific object types
Expand All @@ -121,6 +135,8 @@ function listDynObjectTypes($CORE) {
'dynmap_offset_x' => null,
'dynmap_offset_y' => null,
'dynmap_per_row' => null,
'dynmap_sort' => null,
'dynmap_order' => null,
),
),
);
Expand All @@ -135,7 +151,52 @@ function dynmap_object_in_grid($params, $map_object) {
&& $map_object['y'] - $top > 0 && ($map_object['y'] - $top) % $step_y === 0;
}

function dynmap_sort_objects($o1, $o2) {
function dynmap_sort_objects($MAPCFG, $map_name, &$map_config, &$params, &$objects) {
global $g_dynmap_order, $g_map_obj, $_BACKEND;

$g_dynmap_order = $params['dynmap_order'];

// Now recalculate and reposition all map objects which are currently
// positioned on the map using the grid mechanism. But first sort all
// objects.
switch($params['dynmap_sort']) {
case 's':
$SORT_MAPCFG = new GlobalMapCfg($map_name);
$SORT_MAPCFG->gatherTypeDefaults(false);
foreach ($objects AS $object_id => $object) {
$SORT_MAPCFG->addElement($object['type'], $object, false, $object_id);
}

$g_map_obj = new NagVisMapObj($SORT_MAPCFG, !IS_VIEW);
$g_map_obj->fetchMapObjects();
$g_map_obj->queueState(GET_STATE, DONT_GET_SINGLE_MEMBER_STATES);
$_BACKEND->execute();
$g_map_obj->applyState();

// Add keys for sorting to $objects entries
// TODO: Improve this: Adding these temporary keys should not be necessary
foreach($g_map_obj->getStateRelevantMembers() AS $OBJ) {
$object_id = $OBJ->getObjectId();
$objects[$object_id]['.state'] = $OBJ->sum[STATE];
$objects[$object_id]['.sub_state'] = $OBJ->getSubState(SUMMARY_STATE);
}

usort($objects, 'dynmap_sort_objects_by_state');

// Cleanup sort specific keys again
foreach ($objects AS $object_id => $object) {
unset($object['.state']);
unset($object['.sub_state']);
}
break;
case 'a':
default:
usort($objects, 'dynmap_sort_objects_by_name');
break;
}
}

function dynmap_sort_objects_by_name($o1, $o2) {
$o1_str = '';
$o2_str = '';
if ($o1['type'] == 'service') {
Expand All @@ -152,6 +213,14 @@ function dynmap_sort_objects($o1, $o2) {
return ($o1_str > $o2_str) ? 1 : -1;
}

function dynmap_sort_objects_by_state($o1, $o2) {
global $g_dynmap_order;
return NagVisObject::sortStatesByStateValues($o1['.state'], $o1['.sub_state'],
$o2['.state'], $o2['.sub_state'], $g_dynmap_order);
}

$g_dynmap_order = 'asc';

function process_dynmap($MAPCFG, $map_name, &$map_config) {
$params = $MAPCFG->getSourceParams();

Expand Down Expand Up @@ -185,10 +254,7 @@ function process_dynmap($MAPCFG, $map_name, &$map_config) {
}
}

// Now recalculate and reposition all map objects which are currently
// positioned on the map using the grid mechanism. But first sort all
// objects by their names.
usort($objects, 'dynmap_sort_objects');
dynmap_sort_objects($MAPCFG, $map_name, $map_config, $params, $objects);

$x = $params['dynmap_init_x'];
$y = $params['dynmap_init_y'];
Expand Down Expand Up @@ -221,6 +287,14 @@ function changed_dynmap($MAPCFG, $compare_time) {
if($t > $compare_time)
return true;

// When sorted by state the state of the objects is relevant for
// the order of objects. Therefore we need to track state changes
// here.
if ($params["dynmap_sort"] === 's') {
// TODO
return true;
}

return false;
}

Expand Down

0 comments on commit 2923179

Please sign in to comment.