Merge branch 'ent-6396-10757-api-get-module_data-no-obtiene-los-rangos-indicados-no-db-historico' into 'develop'

uncompressing data before serving it api_get_module_data -absurd format kept-

See merge request artica/pandorafms!3575
This commit is contained in:
Daniel Rodriguez 2020-11-26 18:14:55 +01:00
commit a5883da41c
2 changed files with 39 additions and 33 deletions

View File

@ -194,6 +194,7 @@ function returnData($returnType, $data, $separator=';')
if (is_array($data['data'])) { if (is_array($data['data'])) {
if (array_key_exists('list_index', $data)) { if (array_key_exists('list_index', $data)) {
if ($returnType == 'csv_head') { if ($returnType == 'csv_head') {
header('Content-type: text/csv');
foreach ($data['list_index'] as $index) { foreach ($data['list_index'] as $index) {
echo $index; echo $index;
if (end($data['list_index']) == $index) { if (end($data['list_index']) == $index) {
@ -8445,10 +8446,16 @@ function api_get_module_data($id, $thrash1, $other, $returnType)
return; return;
} }
$separator = $other['data'][0]; $separator = ';';
$periodSeconds = $other['data'][1]; $tstart = null;
$tstart = $other['data'][2]; $tend = null;
$tend = $other['data'][3]; $periodSeconds = null;
if (is_array($other) === true && is_array($other['data']) === true) {
$separator = $other['data'][0];
$periodSeconds = $other['data'][1];
$tstart = $other['data'][2];
$tend = $other['data'][3];
}
if (($tstart != '') && ($tend != '')) { if (($tstart != '') && ($tend != '')) {
try { try {
@ -8469,36 +8476,18 @@ function api_get_module_data($id, $thrash1, $other, $returnType)
$date_end = $date_end->format('U'); $date_end = $date_end->format('U');
} catch (Exception $e) { } catch (Exception $e) {
returnError('error_query_module_data', 'Error in date format. '); returnError('error_query_module_data', 'Error in date format. ');
return;
} }
$sql = sprintf(
'SELECT utimestamp, datos
FROM tagente_datos
WHERE id_agente_modulo = %d AND utimestamp > %d
AND utimestamp < %d
ORDER BY utimestamp DESC',
$id,
$date_start,
$date_end
);
} else { } else {
if ($periodSeconds == null) { if ($periodSeconds !== null) {
$sql = sprintf( $date_end = get_system_time();
'SELECT utimestamp, datos $date_start = (get_system_time() - $periodSeconds);
FROM tagente_datos
WHERE id_agente_modulo = %d
ORDER BY utimestamp DESC',
$id
);
} else { } else {
$sql = sprintf( $date_end = get_system_time();
'SELECT utimestamp, datos $result = modules_get_first_date($id, $tstart);
FROM tagente_datos if ($result !== false) {
WHERE id_agente_modulo = %d AND utimestamp > %d $date_start = $result['first_utimestamp'];
ORDER BY utimestamp DESC', }
$id,
(get_system_time() - $periodSeconds)
);
} }
} }
@ -8507,13 +8496,30 @@ function api_get_module_data($id, $thrash1, $other, $returnType)
'utimestamp', 'utimestamp',
'datos', 'datos',
]; ];
$data['data'] = db_get_all_rows_sql($sql);
$data['data'] = array_reduce(
db_uncompress_module_data($id, $date_start, $date_end),
function ($carry, $item) {
if (is_array($item['data']) === true) {
foreach ($item['data'] as $i => $v) {
$carry[] = [
'utimestamp' => $v['utimestamp'],
'datos' => $v['datos'],
];
}
}
return $carry;
},
[]
);
if ($data === false) { if ($data === false) {
returnError('error_query_module_data', 'Error in the query of module data.'); returnError('error_query_module_data', 'Error in the query of module data.');
} else if ($data['data'] == '') { } else if ($data['data'] == '') {
returnError('error_query_module_data', 'No data to show.'); returnError('error_query_module_data', 'No data to show.');
} else { } else {
// returnData('csv_head', $data, $separator);
returnData('csv', $data, $separator); returnData('csv', $data, $separator);
} }
} }

View File

@ -702,7 +702,7 @@ function db_get_module_ranges_unknown(
* @param utimestamp $tend End of the catch. * @param utimestamp $tend End of the catch.
* @param integer $slice_size Size of slice(default-> module_interval). * @param integer $slice_size Size of slice(default-> module_interval).
* *
* @return hash with the data uncompressed in blocks of module_interval * @return array with the data uncompressed in blocks of module_interval
* false in case of empty result * false in case of empty result
* *
* Note: All "unknown" data are marked as NULL * Note: All "unknown" data are marked as NULL