1
0
mirror of https://github.com/beestat/app.git synced 2025-07-09 03:04:07 -04:00

Fixed #301 - Downloading data doesn't show header on first line sometimes

This commit is contained in:
Jon Ziebell 2020-10-05 21:10:17 -04:00
parent ad589f8641
commit 9e621fb335

View File

@ -915,6 +915,7 @@ class runtime extends cora\api {
$output = fopen('php://output', 'w'); $output = fopen('php://output', 'w');
$needs_header = true; $needs_header = true;
$first_row_out = false;
do { do {
$chunk_end = strtotime('+1 week', $chunk_begin); $chunk_end = strtotime('+1 week', $chunk_begin);
$chunk_end = min($chunk_end, $download_end); $chunk_end = min($chunk_end, $download_end);
@ -983,7 +984,8 @@ class runtime extends cora\api {
] ]
); );
// If a header is needed and there is data available to figure out what
// the header columns should actually be...
if ($needs_header === true && count($runtime_thermostats) > 0) { if ($needs_header === true && count($runtime_thermostats) > 0) {
$headers = array_keys($runtime_thermostats[0]); $headers = array_keys($runtime_thermostats[0]);
@ -1050,6 +1052,7 @@ class runtime extends cora\api {
); );
if(isset($runtime_thermostats_by_timestamp[$current_timestamp]) === true) { if(isset($runtime_thermostats_by_timestamp[$current_timestamp]) === true) {
$first_row_out = true;
$csv_row = array_merge( $csv_row = array_merge(
[$local_datetime], [$local_datetime],
$runtime_thermostats_by_timestamp[$current_timestamp] $runtime_thermostats_by_timestamp[$current_timestamp]
@ -1065,11 +1068,15 @@ class runtime extends cora\api {
$csv_row[] = $runtime_sensors_by_timestamp[$current_timestamp][$sensor['sensor_id']]['occupancy']; $csv_row[] = $runtime_sensors_by_timestamp[$current_timestamp][$sensor['sensor_id']]['occupancy'];
} }
} }
} else { } else {
$csv_row = [$local_datetime]; $csv_row = [$local_datetime];
} }
$bytes += fputcsv($output, $csv_row);
// Don't write any output unless the first row of data exists. This
// allows blank rows but not before data actually exists. See #301.
if($first_row_out === true) {
$bytes += fputcsv($output, $csv_row);
}
$current_timestamp += 300; $current_timestamp += 300;
} }