Skip to content

Commit

Permalink
Working in the refactoring the code of reports (custom_graph).
Browse files Browse the repository at this point in the history
  • Loading branch information
mdtrooper committed Mar 25, 2015
1 parent d17fbbc commit 803c039
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 182 deletions.
2 changes: 2 additions & 0 deletions pandora_console/include/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@
define('REPORT_GENERAL_NOT_GROUP_BY_AGENT', 0);
define('REPORT_GENERAL_GROUP_BY_AGENT', 1);

define('REPORTING_CUSTOM_GRAPH_LEGEND_EACH_MODULE_VERTICAL_SIZE', 15);

/* POLICIES */

define("POLICY_UPDATED", 0);
Expand Down
228 changes: 208 additions & 20 deletions pandora_console/include/functions_reporting.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ function reporting_make_reporting_data($id_report, $date, $time,
$report,
$content);
break;
case 'custom_graph':
case 'automatic_custom_graph':
$report['contents'][] =
reporting_custom_graph(
$report,
$content,
$type,
$force_width_chart,
$force_height_chart);
break;
}
}

Expand Down Expand Up @@ -486,6 +496,72 @@ function reporting_general($report, $content) {
return reporting_check_structure_content($return);
}

function reporting_custom_graph($report, $content, $type = 'dinamic',
$force_width_chart = null, $force_height_chart = null) {

global $config;

require_once ($config["homedir"] . '/include/functions_graph.php');

$graph = db_get_row ("tgraph", "id_graph", $content['id_gs']);

$return = array();
$return['type'] = 'custom_graph';

if (empty($content['name'])) {
$content['name'] = __('Simple graph');
}

$return['title'] = $content['name'];
$return['subtitle'] = $graph['name'];
$return["description"] = $content["description"];
$return["date"] = reporting_get_date_text(
$report,
$content);

$graphs = db_get_all_rows_field_filter ("tgraph_source",
"id_graph", $content['id_gs']);
$modules = array ();
$weights = array ();
if ($graphs === false)
$graphs = array();

foreach ($graphs as $graph_item) {
array_push ($modules, $graph_item['id_agent_module']);
array_push ($weights, $graph_item["weight"]);
}

$return['chart'] = '';
// Get chart
reporting_set_conf_charts($width, $height, $only_image, $type, $content);

$height += count($modules) * REPORTING_CUSTOM_GRAPH_LEGEND_EACH_MODULE_VERTICAL_SIZE;

switch ($type) {
case 'dinamic':
case 'static':
$return['chart'] = graphic_combined_module(
$modules,
$weights,
$content['period'],
$width, $height,
'Combined%20Sample%20Graph',
'',
0,
0,
0,
$graph["stacked"],
$report["datetime"],
$only_image,
ui_get_full_url(false, false, false, false));
break;
case 'data':
break;
}

return reporting_check_structure_content($return);
}

function reporting_simple_graph($report, $content, $type = 'dinamic',
$force_width_chart = null, $force_height_chart = null) {

Expand Down Expand Up @@ -526,26 +602,7 @@ function reporting_simple_graph($report, $content, $type = 'dinamic',

$return['chart'] = '';
// Get chart
switch ($type) {
case 'dinamic':
$only_image = false;
$width = 900;
$height = 230;
break;
case 'static':
$only_image = true;
if ($content['style']['show_in_landscape']) {
$height = 1100;
$width = 1700;
}
else {
$height = 360;
$width = 780;
}
break;
case 'data':
break;
}
reporting_set_conf_charts($width, $height, $only_image, $type, $content);

if (!empty($force_width_chart)) {
$width = $force_width_chart;
Expand Down Expand Up @@ -675,6 +732,32 @@ function reporting_check_structure_content($report) {
return $report;
}

function reporting_set_conf_charts(&$width, &$height, &$only_image, $type, $content) {
switch ($type) {
case 'dinamic':
$only_image = false;
$width = 900;
$height = 230;
break;
case 'static':
$only_image = true;
if ($content['style']['show_in_landscape']) {
$height = 1100;
$width = 1700;
}
else {
$height = 360;
$width = 780;
}
break;
case 'data':
break;
}
}

////////////////////////////////////////////////////////////////////////
// MAYBE MOVE THE NEXT FUNCTIONS TO A FILE NAMED AS FUNCTION_REPORTING.UTILS.PHP //
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

/**
Expand Down Expand Up @@ -1271,4 +1354,109 @@ function reporting_get_stats_users($data) {

return $output;
}

/**
* Get the average value of an agent module in a period of time.
*
* @param int Agent module id
* @param int Period of time to check (in seconds)
* @param int Top date to check the values. Default current time.
*
* @return float The average module value in the interval.
*/
function reporting_get_agentmodule_data_average ($id_agent_module, $period=0, $date = 0) {
global $config;

// Initialize variables
if (empty ($date)) $date = get_system_time ();
$datelimit = $date - $period;

$search_in_history_db = db_search_in_history_db($datelimit);

$id_module_type = modules_get_agentmodule_type ($id_agent_module);
$module_type = modules_get_moduletype_name ($id_module_type);
$uncompressed_module = is_module_uncompressed ($module_type);

// Get module data
$interval_data = db_get_all_rows_sql ('SELECT *
FROM tagente_datos
WHERE id_agente_modulo = ' . (int) $id_agent_module .
' AND utimestamp > ' . (int) $datelimit .
' AND utimestamp < ' . (int) $date .
' ORDER BY utimestamp ASC', $search_in_history_db);
if ($interval_data === false) $interval_data = array ();

// Uncompressed module data
if ($uncompressed_module) {
$min_necessary = 1;

// Compressed module data
}
else {
// Get previous data
$previous_data = modules_get_previous_data ($id_agent_module, $datelimit);
if ($previous_data !== false) {
$previous_data['utimestamp'] = $datelimit;
array_unshift ($interval_data, $previous_data);
}

// Get next data
$next_data = modules_get_next_data ($id_agent_module, $date);
if ($next_data !== false) {
$next_data['utimestamp'] = $date;
array_push ($interval_data, $next_data);
}
else if (count ($interval_data) > 0) {
// Propagate the last known data to the end of the interval
$next_data = array_pop ($interval_data);
array_push ($interval_data, $next_data);
$next_data['utimestamp'] = $date;
array_push ($interval_data, $next_data);
}

$min_necessary = 2;
}

if (count ($interval_data) < $min_necessary) {
return false;
}

// Set initial conditions
$total = 0;
$count = 0;
if (! $uncompressed_module) {
$previous_data = array_shift ($interval_data);

// Do not count the empty start of an interval as 0
if ($previous_data['utimestamp'] != $datelimit) {
$period = $date - $previous_data['utimestamp'];
}
}
foreach ($interval_data as $data) {
if (! $uncompressed_module) {
$total += $previous_data['datos'] * ($data['utimestamp'] - $previous_data['utimestamp']);
$previous_data = $data;
}
else {
$total += $data['datos'];
$count++;
}
}

// Compressed module data
if (! $uncompressed_module) {
if ($period == 0) {
return 0;
}

return $total / $period;
}

// Uncompressed module data
if ($count == 0) {
return 0;
}

return $total / $count;
}
?>
Loading

0 comments on commit 803c039

Please sign in to comment.