From d7da0098040a688a0b6abcdeb5a701b824e164c3 Mon Sep 17 00:00:00 2001 From: daniel Date: Wed, 6 Jun 2018 17:23:32 +0200 Subject: [PATCH] fixed errors in graphs --- pandora_console/include/chart_generator.php | 8 +- pandora_console/include/functions.php | 52 ++++-- pandora_console/include/functions_api.php | 157 +++++++----------- pandora_console/include/functions_graph.php | 66 ++++++-- .../include/graphs/flot/pandora.flot.js | 82 +++++++-- .../include/graphs/functions_flot.php | 12 +- pandora_console/include/web2image.js | 15 +- 7 files changed, 241 insertions(+), 151 deletions(-) diff --git a/pandora_console/include/chart_generator.php b/pandora_console/include/chart_generator.php index 463d1e2570..bec06cfada 100644 --- a/pandora_console/include/chart_generator.php +++ b/pandora_console/include/chart_generator.php @@ -68,9 +68,7 @@ if (file_exists ('languages/'.$user_language.'.mo')) { - Pandora FMS Graph (<?php echo agents_get_alias($agent_id) . ' - ' . $interface_name; ?>) - @@ -108,7 +106,6 @@ if (file_exists ('languages/'.$user_language.'.mo')) { $type_graph_pdf = $_GET['type_graph_pdf']; if($type_graph_pdf == 'combined'){ - echo '

Grafica molona para combinadaaaaaaaaaaaaa

'; echo '
'; echo graphic_combined_module( $module_list, @@ -118,12 +115,11 @@ if (file_exists ('languages/'.$user_language.'.mo')) { echo '
'; } elseif($type_graph_pdf == 'sparse'){ - echo '

Grafica molona para ' . $params['agent_module_id'] . '

'; echo '
'; - echo grafico_modulo_sparse ($params); + echo grafico_modulo_sparse($params); echo '
'; } ?> - + \ No newline at end of file diff --git a/pandora_console/include/functions.php b/pandora_console/include/functions.php index f84cb8e291..5eb22d313b 100644 --- a/pandora_console/include/functions.php +++ b/pandora_console/include/functions.php @@ -3006,6 +3006,21 @@ function color_graph_array($series_suffix, $compare = false){ function series_type_graph_array($data, $show_elements_graph){ global $config; + if(isset($show_elements_graph['stacked'])){ + switch ($show_elements_graph['stacked']) { + case 2: + case 4: + $type_graph = 'line'; + break; + default: + $type_graph = 'area'; + break; + } + } + else{ + $type_graph = $show_elements_graph['type_graph']; + } + if(isset($data) && is_array($data)){ foreach ($data as $key => $value) { if($show_elements_graph['compare'] == 'overlapped'){ @@ -3015,11 +3030,11 @@ function series_type_graph_array($data, $show_elements_graph){ } if(strpos($key, 'summatory') !== false){ - $data_return['series_type'][$key] = 'area'; + $data_return['series_type'][$key] = $type_graph; $data_return['legend'][$key] = __('Summatory series') . ' ' . $str; } elseif(strpos($key, 'average') !== false){ - $data_return['series_type'][$key] = 'area'; + $data_return['series_type'][$key] = $type_graph; $data_return['legend'][$key] = __('Average series') . ' ' . $str; } elseif(strpos($key, 'sum') !== false || strpos($key, 'baseline') !== false){ @@ -3029,7 +3044,7 @@ function series_type_graph_array($data, $show_elements_graph){ $data_return['series_type'][$key] = 'boolean'; break; default: - $data_return['series_type'][$key] = 'area'; + $data_return['series_type'][$key] = $type_graph; break; } @@ -3112,11 +3127,11 @@ function series_type_graph_array($data, $show_elements_graph){ } } elseif(strpos($key, 'projection') !== false){ - $data_return['series_type'][$key] = 'area'; + $data_return['series_type'][$key] = $type_graph; $data_return['legend'][$key] = __('Projection') . ' ' . $str; } else{ - $data_return['series_type'][$key] = 'area'; + $data_return['series_type'][$key] = $type_graph; $data_return['legend'][$key] = $key; } } @@ -3146,15 +3161,26 @@ function generator_chart_to_pdf($type_graph_pdf, $params, $params_combined = fal if($module_list){ $module_list = urlencode(json_encode($module_list)); } -html_debug_print("phantomjs " . $file_js . " " . $url . " '" . $type_graph_pdf . "' '" . $params_encode_json . "' '" . $params_combined . "' '" . $module_list . "' " . $img_path . " " . $width_img . " " . $height_img, true); - $result = exec("phantomjs " . $file_js . " " . $url . " '" . $type_graph_pdf . "' '" . $params_encode_json . "' '" . $params_combined . "' '" . $module_list . "' " . $img_path . " " . $width_img . " " . $height_img); - html_debug_print($result, true); - return ''; - //html_debug_print('entrando en llamada a phantom.js.......', true); - //header('Content-Type: image/png;'); - //return ''; - //return "la imagen bonica"; + $result = exec( + "phantomjs " . $file_js . " " . + $url . " '" . + $type_graph_pdf . "' '" . + $params_encode_json . "' '" . + $params_combined . "' '" . + $module_list . "' " . + $img_path . " " . + $width_img . " " . + $height_img . " " . + $params['return_img_base_64'] + ); + + if($params['return_img_base_64']){ + return $result; + } + else{ + return ''; + } } /** diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index 0515160238..64f878a2ce 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -6655,7 +6655,7 @@ function api_get_graph_module_data($id, $thrash1, $other, $thrash2) { if (defined ('METACONSOLE')) { return; } - + $period = $other['data'][0]; $width = $other['data'][1]; $height = $other['data'][2]; @@ -6667,38 +6667,40 @@ function api_get_graph_module_data($id, $thrash1, $other, $thrash2) { $avg_only = 0; $start_date = $other['data'][4]; $date = strtotime($start_date); - - + $homeurl = '../'; $ttl = 1; - + global $config; $config['flash_charts'] = 0; - - $image = grafico_modulo_sparse ($id, $period, $draw_events, - $width, $height , $label, null, - $draw_alerts, $avg_only, false, - $date, '', 0, 0,true, - false, $homeurl, $ttl); - - preg_match("/
/", - $image, $match); - - if (!empty($match[0])) { - echo "Error no data"; - } - else { - // Extract url of the image from img tag - preg_match("/src='([^']*)'/i", $image, $match); - - if (empty($match[1])) { - echo "Error getting graph"; - } - else { - header('Content-type: image/png'); - header('Location: ' . $match[1]); - } - } + + $params =array( + 'agent_module_id' => $id, + 'period' => $period, + 'show_events' => $draw_events, + 'width' => $width, + 'height' => $height, + 'show_alerts' => $draw_alerts, + 'date' => $date, + 'unit' => '', + 'baseline' => 0, + 'return_data' => 0, + 'show_title' => true, + 'only_image' => true, + 'homeurl' => $homeurl, + 'compare' => false, + 'show_unknown' => true, + 'backgroundColor' => 'white', + 'percentil' => null, + 'type_graph' => $config['type_module_charts'], + 'fullscale' => false, + 'return_img_base_64' => true + ); + + $image = grafico_modulo_sparse($params); + + header('Content-type: text/html'); + returnData('string', array('type' => 'string', 'data' => '')); } /** @@ -10039,25 +10041,24 @@ function api_set_delete_special_day($id_special_day, $thrash2, $thrash3, $thrash * */ function api_get_module_graph($id_module, $thrash2, $other, $thrash4) { - global $config; if (defined ('METACONSOLE')) { return; } - + if (is_nan($id_module) || $id_module <= 0) { returnError('error_module_graph', __('')); return; } - + $id_exist = (bool) db_get_value ('id_agente_modulo', 'tagente_modulo', 'id_agente_modulo', $id_module); - + if (!$id_exist) { // returnError('id_not_found'); return; } - + $graph_seconds = (!empty($other) && isset($other['data'][0])) ? @@ -10077,67 +10078,37 @@ function api_get_module_graph($id_module, $thrash2, $other, $thrash4) { return; } - $graph_html = grafico_modulo_sparse( - $id_module, $graph_seconds, false, 600, 300, '', - '', false, false, true, time(), '', 0, 0, true, true, - ui_get_full_url(false) . '/', 1, false, '', false, true, - true, 'white', null, false, false, $config['type_module_charts'], - false, false); + $params =array( + 'agent_module_id' => $id_module, + 'period' => $graph_seconds, + 'show_events' => false, + 'width' => $width, + 'height' => $height, + 'show_alerts' => false, + 'date' => time(), + 'unit' => '', + 'baseline' => 0, + 'return_data' => 0, + 'show_title' => true, + 'only_image' => true, + 'homeurl' => ui_get_full_url(false) . '/', + 'compare' => false, + 'show_unknown' => true, + 'backgroundColor' => 'white', + 'percentil' => null, + 'type_graph' => $config['type_module_charts'], + 'fullscale' => false, + 'return_img_base_64' => true, + 'image_treshold' => $graph_threshold + ); - $graph_image_file_encoded = false; - if (preg_match("/ 'string', 'data' => '')); - } else { - returnData('string', array('type' => 'string', 'data' => $graph_image_file_encoded)); - } - // To show only the base64 code, call returnData as: - // returnData('string', array('type' => 'string', 'data' => $graph_image_file_encoded)); + if($other['data'][1]){ + header('Content-type: text/html'); + returnData('string', array('type' => 'string', 'data' => '')); + } else { + returnData('string', array('type' => 'string', 'data' => $graph_html)); } } diff --git a/pandora_console/include/functions_graph.php b/pandora_console/include/functions_graph.php index aaf4abfbcd..722854e481 100644 --- a/pandora_console/include/functions_graph.php +++ b/pandora_console/include/functions_graph.php @@ -695,7 +695,10 @@ function grafico_modulo_sparse_data( 'time_interval' => 300, 'array_data_create' => 0, 'show_legend' => true, - 'show_overview' => true + 'show_overview' => true, + 'return_img_base_64' => false, + 'image_treshold' => false, + 'graph_combined' => false ); */ function grafico_modulo_sparse ($params) { @@ -847,6 +850,18 @@ function grafico_modulo_sparse ($params) { $params['array_data_create'] = 0; } + if(!isset($params['return_img_base_64'])){ + $params['return_img_base_64'] = false; + } + + if(!isset($params['image_treshold'])){ + $params['image_treshold'] = false; + } + + if(!isset($params['graph_combined'])){ + $params['graph_combined'] = false; + } + $params['font'] = $config['fontpath']; $params['font-size'] = $config['font_size']; @@ -1155,7 +1170,7 @@ function graphic_combined_module ( } else { if ($id_graph == 0) { - $params_combined['stacked'] = CUSTOM_GRAPH_LINE; + $params_combined['stacked'] = CUSTOM_GRAPH_AREA; } else { $params_combined['stacked'] = db_get_row('tgraph', 'id_graph', $id_graph); @@ -1263,6 +1278,10 @@ function graphic_combined_module ( $params['menu'] = false; } + if(!isset($params['type_graph'])){ + $params['type_graph'] = $config['type_module_charts']; + } + if(!isset($params['percentil'])){ $params['percentil'] = null; } @@ -1291,6 +1310,20 @@ function graphic_combined_module ( $params['show_export_csv'] = true; } + if(!isset($params['return_img_base_64'])){ + $params['return_img_base_64'] = false; + } + + if(!isset($params['image_treshold'])){ + $params['image_treshold'] = false; + } + + $params['graph_combined'] = true; + + if(!isset($params['show_unknown'])){ + $params['show_unknown'] = false; + } + //XXXX if($params['only_image']){ return generator_chart_to_pdf('combined', $params, $params_combined, $module_list); @@ -1437,6 +1470,7 @@ function graphic_combined_module ( $data_module_graph['c_inv'] = $module_data['critical_inverse']; $data_module_graph['module_id'] = $agent_module_id; + //stract data $array_data_module = grafico_modulo_sparse_data( $agent_module_id, @@ -4016,14 +4050,16 @@ function fullscale_data ( if ($v["datos"] === NULL) { // Unknown - if(!$compare){ - if($flag_unknown){ - $data["unknown" . $series_suffix]['data'][] = array($real_date , 1); - } - else{ - $data["unknown" . $series_suffix]['data'][] = array( ($real_date - 1) , 0); - $data["unknown" . $series_suffix]['data'][] = array($real_date , 1); - $flag_unknown = 1; + if($show_unknown){ + if(!$compare){ + if($flag_unknown){ + $data["unknown" . $series_suffix]['data'][] = array($real_date , 1); + } + else{ + $data["unknown" . $series_suffix]['data'][] = array( ($real_date - 1) , 0); + $data["unknown" . $series_suffix]['data'][] = array($real_date , 1); + $flag_unknown = 1; + } } } @@ -4033,10 +4069,12 @@ function fullscale_data ( //normal $previous_data = $v["datos"]; $data["sum" . $series_suffix]['data'][] = array($real_date , $v["datos"]); - if(!$compare){ - if($flag_unknown){ - $data["unknown" . $series_suffix]['data'][] = array($real_date , 0); - $flag_unknown = 0; + if($show_unknown){ + if(!$compare){ + if($flag_unknown){ + $data["unknown" . $series_suffix]['data'][] = array($real_date , 0); + $flag_unknown = 0; + } } } } diff --git a/pandora_console/include/graphs/flot/pandora.flot.js b/pandora_console/include/graphs/flot/pandora.flot.js index 9822b561fd..febf57da6a 100644 --- a/pandora_console/include/graphs/flot/pandora.flot.js +++ b/pandora_console/include/graphs/flot/pandora.flot.js @@ -865,19 +865,20 @@ function pandoraFlotArea( ) { //diferents vars - var unit = params.unit ? params.unit : ''; - var homeurl = params.homeurl; - var font_size = params.font_size; - var font = params.font; - var width = params.width; - var height = params.height; - var vconsole = params.vconsole; - var dashboard = params.dashboard; - var menu = params.menu; - var min_x = date_array['start_date'] *1000; - var max_x = date_array['final_date'] *1000; - var type = params.stacked; - var show_legend= params.show_legend; + var unit = params.unit ? params.unit : ''; + var homeurl = params.homeurl; + var font_size = params.font_size; + var font = params.font; + var width = params.width; + var height = params.height; + var vconsole = params.vconsole; + var dashboard = params.dashboard; + var menu = params.menu; + var min_x = date_array['start_date'] *1000; + var max_x = date_array['final_date'] *1000; + var type = params.stacked; + var show_legend = params.show_legend; + var image_treshold = params.image_treshold; if(typeof type === 'undefined' || type == ''){ type = params.type_graph; @@ -1481,7 +1482,7 @@ function pandoraFlotArea( } } } - +console.log(type); switch (type) { case 'line': case 2: @@ -1841,7 +1842,7 @@ if (vconsole) { } })); } -console.log(homeurl); + $('#menu_cancelzoom_' + graph_id).attr('src', homeurl + '/images/zoom_cross_grey.png'); // currentRanges = ranges; @@ -2083,6 +2084,46 @@ console.log(homeurl); $('#overview_'+graph_id).bind('mouseout',resetInteractivity); } + if(image_treshold){ + if(!thresholded){ + // Recalculate the y axis + var y_recal = axis_thresholded( + threshold_data, + plot.getAxes().yaxis.min, + plot.getAxes().yaxis.max, + red_threshold, extremes, + red_up + ); + } + else{ + var y_recal = plot.getAxes().yaxis.max + } + + datas_treshold = add_threshold ( + data_base, + threshold_data, + plot.getAxes().yaxis.min, + plot.getAxes().yaxis.max, + red_threshold, + extremes, + red_up, + markins_graph + ); + + plot = $.plot($('#' + graph_id), datas_treshold, + $.extend(true, {}, options, { + yaxis: { + max: y_recal.max, + }, + xaxis: { + min: plot.getAxes().xaxis.min, + max: plot.getAxes().xaxis.max + } + })); + + thresholded = true; + } + // Reset interactivity styles function resetInteractivity(vconsole) { $('#timestamp_'+graph_id).hide(); @@ -2546,7 +2587,14 @@ function add_threshold (data_base, threshold_data, y_min, y_max, threshold_array[index]['max'] = end; threshold_array[index]['color'] = "red"; } else { - end = extremes[this.id + '_1']; + var first = extremes[this.id + '_1']; + var second = extremes[this.id + '_2']; + if(first > second){ + end = first; + } + else{ + end = second; + } threshold_array[index]['min'] = this.data[0][1]; threshold_array[index]['max'] = end; threshold_array[index]['color'] = "yellow"; @@ -2596,7 +2644,7 @@ function add_threshold (data_base, threshold_data, y_min, y_max, var extreme_treshold_array = []; var i = 0; var flag = true; -console.log(threshold_array); + $.each(threshold_array, function(index, value) { flag = true; extreme_treshold_array[i] = { diff --git a/pandora_console/include/graphs/functions_flot.php b/pandora_console/include/graphs/functions_flot.php index c96dfc3d17..6b02ab3859 100644 --- a/pandora_console/include/graphs/functions_flot.php +++ b/pandora_console/include/graphs/functions_flot.php @@ -131,13 +131,19 @@ function flot_area_graph ( ///XXXXXXX los px caca // Parent layer - $return = "
"; + $return = "
"; // Set some containers to legend, graph, timestamp tooltip, etc. if($params['show_legend']){ $return .= "

"; } - - if(!isset($params['combined']) || !$params['combined']){ + if(isset($params['graph_combined']) && $params['graph_combined'] && + (!isset($params['from_interface']) || !$params['from_interface']) ){ + $yellow_up = 0; + $red_up = 0; + $yellow_inverse = false; + $red_inverse = false; + } + elseif(!isset($params['combined']) || !$params['combined']){ $yellow_threshold = $data_module_graph['w_min']; $red_threshold = $data_module_graph['c_min']; // Get other required module datas to draw warning and critical diff --git a/pandora_console/include/web2image.js b/pandora_console/include/web2image.js index 85ae6ac37a..a57dd8376d 100644 --- a/pandora_console/include/web2image.js +++ b/pandora_console/include/web2image.js @@ -1,6 +1,6 @@ var system = require('system'); -if (system.args.length < 3 || system.args.length > 9) { +if (system.args.length < 3 || system.args.length > 10) { phantom.exit(1); } @@ -14,6 +14,7 @@ var url_module_list = system.args[5]; var output_filename = system.args[6]; var _width = system.args[7]; var _height = system.args[8]; +var base_64 = system.args[9]; if (!_width) { _width = 750; @@ -38,13 +39,17 @@ page.viewportSize = { width: _width, height: _height }; //page.zoomFactor = 1.75; page.open(finish_url, function start(status) { - page.includeJs('./javascript/pandora.js'); + }); page.onLoadFinished = function (status) { - page.render(output_filename, {format: 'png'}); - //var base64 = page.renderBase64('JPG'); - //console.log(base64); + if(!base_64){ + page.render(output_filename, {format: 'png'}); + } + else{ + var base64 = page.renderBase64('png'); + console.log(base64); + } phantom.exit(); }