$target_data['module'], 'period' => (strtotime($payload['range']['to']) - strtotime($payload['range']['from'])), 'date' => strtotime($payload['range']['to']), 'return_data' => 1, 'show_unknown' => true, 'fullscale' => (bool) $target_data['tip'], 'time_interval' => $target_data['interval'], ]; // Get all data. $data = grafico_modulo_sparse($params); $unknown_timestamps = []; // Set unknown data as null. foreach ($data['unknown1']['data'] as $d) { if (($d[1] == 1 && !$params['fullscale']) || ($d[1] == 0 && $params['fullscale'])) { $result_data[] = [ null, $d[0], ]; } $unknown_timestamps[] = $d[0]; } // Get each data if not in unknown timestamps foreach ($data['sum1']['data'] as $d) { if ($d[1] != false && !in_array($d[0], $unknown_timestamps)) { $result_data[] = [ $d[1], $d[0], ]; } } // Sort all data by utimestamp (Grafana needs it). usort( $result_data, function ($a, $b) { return $a[1] > $b[1] ? 1 : -1; } ); $rows = []; foreach ($result_data as $k => $v) { if (($result_data[$k][0] !== $result_data[($k - 1)][0] || $result_data[$k][0] !== $result_data[($k + 1)][0]) || ($result_data[($k - 1)][0] === null && $result_data[$k][0] !== null && $result_data[$k][1] != (strtotime($payload['range']['to']) * 1000)) || ($result_data[($k - 1)][0] === $result_data[$k][0] && $result_data[$k][1] == (strtotime($payload['range']['to']) * 1000)) ) { $rows[] = $result_data[$k]; } } if (!$params['fullscale']) { $target_data['target'] .= ' (avg)'; } // Set all target info and data $result_array[] = [ 'type' => 'table', 'target' => $target_data['target'], 'refId' => $target_data['target'], 'columns' => [ [ 'text' => $target_data['target'], ], [ 'text' => 'Time', 'type' => 'time', ], ], 'datapoints' => array_values($rows), ]; } } } } } // Numeric data in array must be numeric data in json (not text). $result = json_encode($result_array, JSON_NUMERIC_CHECK); echo $result;