[Network usage map] Refactorized some code

Former-commit-id: 83fe7cedef01b4d4d38045b15c6b37ec936d5fb3
This commit is contained in:
fermin831 2019-03-26 09:10:28 +01:00
parent 58e74c1db1
commit 189d4864dc
2 changed files with 81 additions and 65 deletions

View File

@ -1801,6 +1801,7 @@ function netflow_aggregate_is_ip($aggregate)
*/ */
function netflow_build_map_data($start_date, $end_date, $top, $aggregate) function netflow_build_map_data($start_date, $end_date, $top, $aggregate)
{ {
// Pass an empty filter data structure.
$data = netflow_get_relationships_raw_data( $data = netflow_get_relationships_raw_data(
$start_date, $start_date,
$end_date, $end_date,
@ -1821,19 +1822,12 @@ function netflow_build_map_data($start_date, $end_date, $top, $aggregate)
$nodes = array_map( $nodes = array_map(
function ($elem) { function ($elem) {
return [ return network_init_node_map($elem);
'name' => $elem,
'type' => NODE_GENERIC,
'width' => 20,
'height' => 20,
'status' => '#82B92E',
];
}, },
array_merge($data['sources'], [__('Others')]) array_merge($data['sources'], [__('Others')])
); );
$relations = []; $relations = [];
$inverse_nodes = array_flip($data['sources']); $inverse_nodes = array_flip($data['sources']);
// Port are situated in a different places from addreses. // Port are situated in a different places from addreses.
@ -1869,14 +1863,7 @@ function netflow_build_map_data($start_date, $end_date, $top, $aggregate)
$relations[$index_rel]['text_start'] += $value; $relations[$index_rel]['text_start'] += $value;
} else { } else {
// Update the value. // Update the value.
$relations[$index_rel] = [ network_init_relation_map($relations, $src_item, $dst_item, $value);
'id_parent' => $src_item,
'parent_type' => NODE_GENERIC,
'child_type' => NODE_GENERIC,
'id_child' => $dst_item,
'link_color' => '#82B92E',
'text_start' => $value,
];
} }
} }
@ -1896,13 +1883,7 @@ function netflow_build_map_data($start_date, $end_date, $top, $aggregate)
continue; continue;
} }
$orphan_hosts[$position.'-'.$orphan_index] = [ network_init_relation_map($orphan_hosts, $position, $orphan_index);
'id_parent' => $orphan_index,
'parent_type' => NODE_GENERIC,
'child_type' => NODE_GENERIC,
'id_child' => $position,
'link_color' => '#82B92E',
];
} }
// If there is not any orphan node, delete it. // If there is not any orphan node, delete it.
@ -1910,17 +1891,8 @@ function netflow_build_map_data($start_date, $end_date, $top, $aggregate)
array_pop($nodes); array_pop($nodes);
} }
return [ return network_general_map_configuration(
'nodes' => $nodes, $nodes,
'relations' => array_merge($relations, $orphan_hosts), array_merge($relations, $orphan_hosts)
'pure' => 1, );
'no_pandora_node' => 1,
'map_options' => [
'generation_method' => LAYOUT_SPRING1,
'map_filter' => [
'node_radius' => 40,
'node_sep' => 7,
],
],
];
} }

View File

@ -193,13 +193,7 @@ function network_build_map_data($start, $end, $top, $talker)
$nodes = array_map( $nodes = array_map(
function ($elem) { function ($elem) {
return [ return network_init_node_map($elem);
'name' => $elem,
'type' => NODE_GENERIC,
'width' => 20,
'height' => 20,
'status' => '#82B92E',
];
}, },
$hosts $hosts
); );
@ -223,42 +217,46 @@ function network_build_map_data($start, $end, $top, $talker)
continue; continue;
} }
$relations[$src_index.'-'.$dst_index] = [ network_init_relation_map(
'id_parent' => $inverse_hosts[$sd['host']], $relations,
'parent_type' => NODE_GENERIC, $src_index,
'child_type' => NODE_GENERIC, $dst_index,
'id_child' => $inverse_hosts[$host], network_format_bytes($sd['sum_bytes'])
'link_color' => '#82B92E', );
'text_start' => network_format_bytes($sd['sum_bytes']),
];
} }
// Put the orphans on Other node. // Put the orphans on Other node.
if (empty($host_top)) { if (empty($host_top)) {
$other_id = (end($inverse_hosts) + 1); $other_id = (end($inverse_hosts) + 1);
// TODOS: Add the data. // TODOS: Add the data.
$orphan_relations[$inverse_hosts[$host].'-'.$other_id] = [ network_init_relation_map(
'id_parent' => $other_id, $orphan_relations,
'parent_type' => NODE_GENERIC, $other_id,
'child_type' => NODE_GENERIC, $inverse_hosts[$host]
'id_child' => $inverse_hosts[$host], );
'link_color' => '#82B92E',
];
} }
} }
// Put the Others node and their relations. // Put the Others node and their relations.
if (empty($orphan_relations) === false) { if (empty($orphan_relations) === false) {
$nodes[] = [ $nodes[] = network_init_node_map(__('Others'));
'name' => __('Others'),
'type' => NODE_GENERIC,
'width' => 20,
'height' => 20,
'status' => '#82B92E',
];
$relations = array_merge($relations, $orphan_relations); $relations = array_merge($relations, $orphan_relations);
} }
return network_general_map_configuration($nodes, $relations);
}
/**
* Return the array to pass to constructor to NetworkMap.
*
* @param array $nodes Nodes data structure.
* @param array $relations Relations data structure.
*
* @return array To be passed to NetworMap class.
*/
function network_general_map_configuration($nodes, $relations)
{
return [ return [
'nodes' => $nodes, 'nodes' => $nodes,
'relations' => $relations, 'relations' => $relations,
@ -273,3 +271,49 @@ function network_build_map_data($start, $end, $top, $talker)
], ],
]; ];
} }
/**
* Added a relation to relations array
*
* @param array $relations Relations array (passed by reference).
* @param integer $parent Parent id (numeric).
* @param integer $child Child id (numeric).
* @param string $text Text to show at the end of edge (optional).
*
* @return void Relations will be modified (passed by reference).
*/
function network_init_relation_map(&$relations, $parent, $child, $text='')
{
$index = $parent.'-'.$child;
$relations[$index] = [
'id_parent' => $parent,
'parent_type' => NODE_GENERIC,
'child_type' => NODE_GENERIC,
'id_child' => $child,
'link_color' => '#82B92E',
];
if (empty($text) === false) {
$relations[$index]['text_start'] = $text;
}
}
/**
* Initialize a node structure to NetworkMap class.
*
* @param string $name Node name.
*
* @return array Node data structure.
*/
function network_init_node_map($name)
{
return [
'name' => $name,
'type' => NODE_GENERIC,
'width' => 20,
'height' => 20,
'status' => '#82B92E',
];
}