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 (array_key_exists('list_index', $data)) {
if ($returnType == 'csv_head') {
header('Content-type: text/csv');
foreach ($data['list_index'] as $index) {
echo $index;
if (end($data['list_index']) == $index) {
@ -8445,10 +8446,16 @@ function api_get_module_data($id, $thrash1, $other, $returnType)
return;
}
$separator = ';';
$tstart = null;
$tend = null;
$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 != '')) {
try {
@ -8469,36 +8476,18 @@ function api_get_module_data($id, $thrash1, $other, $returnType)
$date_end = $date_end->format('U');
} catch (Exception $e) {
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 {
if ($periodSeconds == null) {
$sql = sprintf(
'SELECT utimestamp, datos
FROM tagente_datos
WHERE id_agente_modulo = %d
ORDER BY utimestamp DESC',
$id
);
if ($periodSeconds !== null) {
$date_end = get_system_time();
$date_start = (get_system_time() - $periodSeconds);
} else {
$sql = sprintf(
'SELECT utimestamp, datos
FROM tagente_datos
WHERE id_agente_modulo = %d AND utimestamp > %d
ORDER BY utimestamp DESC',
$id,
(get_system_time() - $periodSeconds)
);
$date_end = get_system_time();
$result = modules_get_first_date($id, $tstart);
if ($result !== false) {
$date_start = $result['first_utimestamp'];
}
}
}
@ -8507,13 +8496,30 @@ function api_get_module_data($id, $thrash1, $other, $returnType)
'utimestamp',
'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) {
returnError('error_query_module_data', 'Error in the query of module data.');
} else if ($data['data'] == '') {
returnError('error_query_module_data', 'No data to show.');
} else {
// returnData('csv_head', $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 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
*
* Note: All "unknown" data are marked as NULL