aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormarcinzelent <zelent.marcin@gmail.com>2017-12-12 19:31:38 +0100
committermarcinzelent <zelent.marcin@gmail.com>2017-12-12 19:31:38 +0100
commitdb3728baaf458aca26d740cbf2be4cf4775c6921 (patch)
treea9d8b802d12b61a5540ba86cbda3c7265e069bde /src/AppBundle/Controller/LastWeekDataAverageController.php
parentadddfc4c26f5a20acd44b9f6cab14c6996e54104 (diff)
Fixed counting of average pollution by day.
Diffstat (limited to 'src/AppBundle/Controller/LastWeekDataAverageController.php')
-rw-r--r--src/AppBundle/Controller/LastWeekDataAverageController.php24
1 files changed, 9 insertions, 15 deletions
diff --git a/src/AppBundle/Controller/LastWeekDataAverageController.php b/src/AppBundle/Controller/LastWeekDataAverageController.php
index fe30fd4..e25b0a8 100644
--- a/src/AppBundle/Controller/LastWeekDataAverageController.php
+++ b/src/AppBundle/Controller/LastWeekDataAverageController.php
@@ -25,9 +25,12 @@ class LastWeekDataAverageController extends Controller
curl_close($curl);
$data = json_decode($resp, true);
-
$readings = array();
+ $results = array();
+ usort($data, function($a,$b){
+ return $a['TimeStamp'] - $b['TimeStamp'];
+ });
foreach($data as $index => $item)
{
@@ -35,14 +38,11 @@ class LastWeekDataAverageController extends Controller
$readings[$data[$index]['TimeStamp']][] = $data[$index];
}
-
-
- $gasAverage = array('Co' => 0, 'No' => 0, 'So' => 0);
- foreach($readings as $key => $item)
+ foreach ($readings as $key => $item)
{
- foreach($readings[$key] as $index => $values)
+ $gasAverage = array('Co' => 0, 'No' => 0, 'So' => 0);
+ foreach ($readings[$key] as $index => $values)
{
-
$gasAverage['Co'] += $readings[$key][$index]['Co'];
$gasAverage['No'] += $readings[$key][$index]['No'];
$gasAverage['So'] += $readings[$key][$index]['So'];
@@ -53,20 +53,14 @@ class LastWeekDataAverageController extends Controller
$gasAverage['No'] /= $index + 1;
$gasAverage['So'] /= $index + 1;
}
+ $results[$key] = $gasAverage;
}
-
- $readings[$key] = $gasAverage;
-
}
-
- $data = json_encode($readings);
-
+ $data = json_encode($results);
$response = new Response($data);
$response->headers->set('Content-Type', 'application/json');
return $response;
-
-
}
} \ No newline at end of file