[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)
{
// Pass an empty filter data structure.
$data = netflow_get_relationships_raw_data(
$start_date,
$end_date,
@ -1821,19 +1822,12 @@ function netflow_build_map_data($start_date, $end_date, $top, $aggregate)
$nodes = array_map(
function ($elem) {
return [
'name' => $elem,
'type' => NODE_GENERIC,
'width' => 20,
'height' => 20,
'status' => '#82B92E',
];
return network_init_node_map($elem);
},
array_merge($data['sources'], [__('Others')])
);
$relations = [];
$inverse_nodes = array_flip($data['sources']);
// 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;
} else {
// Update the value.
$relations[$index_rel] = [
'id_parent' => $src_item,
'parent_type' => NODE_GENERIC,
'child_type' => NODE_GENERIC,
'id_child' => $dst_item,
'link_color' => '#82B92E',
'text_start' => $value,
];
network_init_relation_map($relations, $src_item, $dst_item, $value);
}
}
@ -1896,13 +1883,7 @@ function netflow_build_map_data($start_date, $end_date, $top, $aggregate)
continue;
}
$orphan_hosts[$position.'-'.$orphan_index] = [
'id_parent' => $orphan_index,
'parent_type' => NODE_GENERIC,
'child_type' => NODE_GENERIC,
'id_child' => $position,
'link_color' => '#82B92E',
];
network_init_relation_map($orphan_hosts, $position, $orphan_index);
}
// 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);
}
return [
'nodes' => $nodes,
'relations' => 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,
],
],
];
return network_general_map_configuration(
$nodes,
array_merge($relations, $orphan_hosts)
);
}

View File

@ -193,13 +193,7 @@ function network_build_map_data($start, $end, $top, $talker)
$nodes = array_map(
function ($elem) {
return [
'name' => $elem,
'type' => NODE_GENERIC,
'width' => 20,
'height' => 20,
'status' => '#82B92E',
];
return network_init_node_map($elem);
},
$hosts
);
@ -223,42 +217,46 @@ function network_build_map_data($start, $end, $top, $talker)
continue;
}
$relations[$src_index.'-'.$dst_index] = [
'id_parent' => $inverse_hosts[$sd['host']],
'parent_type' => NODE_GENERIC,
'child_type' => NODE_GENERIC,
'id_child' => $inverse_hosts[$host],
'link_color' => '#82B92E',
'text_start' => network_format_bytes($sd['sum_bytes']),
];
network_init_relation_map(
$relations,
$src_index,
$dst_index,
network_format_bytes($sd['sum_bytes'])
);
}
// Put the orphans on Other node.
if (empty($host_top)) {
$other_id = (end($inverse_hosts) + 1);
// TODOS: Add the data.
$orphan_relations[$inverse_hosts[$host].'-'.$other_id] = [
'id_parent' => $other_id,
'parent_type' => NODE_GENERIC,
'child_type' => NODE_GENERIC,
'id_child' => $inverse_hosts[$host],
'link_color' => '#82B92E',
];
network_init_relation_map(
$orphan_relations,
$other_id,
$inverse_hosts[$host]
);
}
}
// Put the Others node and their relations.
if (empty($orphan_relations) === false) {
$nodes[] = [
'name' => __('Others'),
'type' => NODE_GENERIC,
'width' => 20,
'height' => 20,
'status' => '#82B92E',
];
$nodes[] = network_init_node_map(__('Others'));
$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 [
'nodes' => $nodes,
'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',
];
}