commit e1140d8ac3a9ccaf94d706c0fe28538dd26099b6
Author: horkym <37276521+horkym@users.noreply.github.com>
Date:   Mon May 21 18:44:21 2018 +0200

    refs #6758: Cron - Uprava pro prumer dat za den
    
    Implementovano ulozeni dat do tabulky zaznam_prum_den. Pridano pole pro sber dat za cely den.

diff --git a/cron/cron.php b/cron/cron.php
index 0a50060..12e3f24 100644
--- a/cron/cron.php
+++ b/cron/cron.php
@@ -18,6 +18,7 @@ function cron() {
         $parser->doWork($date->format("Ymd"));
         
         $traffic = $parser->getTraffic();
+        $trafficOneDay = $parser->getTrafficOneDay();
         $locations = $parser->getLocations();
         
         $DAO->insertVehicles(); // Pokud nejsou typy vozidel v tabulce - pridat.
@@ -30,8 +31,9 @@ function cron() {
         // Pridat zaznamy z vybraneho dne.
         $insertRTT = array();
         $insertRT = array();
-        process_traffic_matrix($parser, $traffic, $DAO->findFirstId("zaznam_cas"), $DAO->findFirstId("zaznam"), $insertRTT, $insertRT, $date->format("Y-m-d"));
-        $DAO->insertTrafficData($insertRTT, $insertRT);
+        $insertOneDay = array();
+        process_traffic_matrix($parser, $traffic, $trafficOneDay, $DAO->findFirstId("zaznam_cas"), $DAO->findFirstId("zaznam"), $DAO->findFirstId("zaznam_prum_den"), $insertRTT, $insertRT, $insertOneDay, $date->format("Y-m-d"));
+        $DAO->insertTrafficData($insertRTT, $insertRT, $insertOneDay);
     }
 }
 
diff --git a/cron/dao/dao.php b/cron/dao/dao.php
index 3092ad9..3a6cb33 100644
--- a/cron/dao/dao.php
+++ b/cron/dao/dao.php
@@ -41,8 +41,8 @@ class DAO {
         return $stmt->fetchAssoc()["total"] < 1;
     }
     
-    public function insertTrafficData($insertRTT, $insertRT) {
-        for ($i = 0; $i < 2; $i++) {
+    public function insertTrafficData($insertRTT, $insertRT, $insertOneDay) {
+        for ($i = 0; $i < 3; $i++) {
             $query = "";
             $values = "";
             $counter = 0;
@@ -51,9 +51,12 @@ class DAO {
             if ($i == 0) {
                 $query = "INSERT INTO zaznam_cas VALUES ";
                 $array = &$insertRTT;
-            } else {
+            } else if ($i == 1) {
                 $query = "INSERT INTO zaznam VALUES ";
                 $array = &$insertRT;
+            } else {
+                $query = "INSERT INTO zaznam_prum_den VALUES ";
+                $array = &$insertOneDay;
             }
             
             for ($j = 0; $j < count($array); $j++) {
diff --git a/cron/parser.php b/cron/parser.php
index 398dee4..f4763a9 100644
--- a/cron/parser.php
+++ b/cron/parser.php
@@ -23,6 +23,9 @@ class Parser {
     // prvcich (jedenact typu vozidel). Pro kazde vozidlo je vytvoreno pole o dvou prvcich (pocet vozidel, suma rychlosti).
     private $traffic;
     
+    // Skoro to same jako $traffic, akorat bez rozmeru pro casove intervaly.
+    private $trafficOneDay;
+    
     public function __construct() {
         $this->name = "DOPR_D_";
         $this->path = "http://doprava.plzensky-kraj.cz/opendata/doprava/den/".$this->name;
@@ -33,6 +36,7 @@ class Parser {
         // Naopak u zaznamu je prilis zbytecnych informaci - k predzpracovani dojit musi.
         $this->intervalMilli = (int) (24 * 3600000 / $this->HOW_MANY_INTERVALS);
         $this->traffic = array();
+        $this->trafficOneDay = array();
     }
     
     public function doWork($date) {
@@ -76,10 +80,21 @@ class Parser {
     private function saveVehicleInfo($t) {
         // Kontrola, jestli je pro dane zarizeni vytvorene pole casu.
         if (!isSet($this->traffic[$t->device])) {
+            // Vytvorit prvni dva rozmery pole pro dopravni data s rozdelenim na casove intervaly.
             $this->traffic[$t->device] = array();
             for ($i = 0; $i < $this->HOW_MANY_INTERVALS; $i++) {
                 $this->traffic[$t->device][$i] = NULL;
             }
+            
+            // U pole s prumery za cely den rovnou vytvorit vsechny rozmery.
+            $this->trafficOneDay[$t->device] = array();
+            for ($i = 0; $i < 2; $i++) {
+                $this->trafficOneDay[$t->device][$i] = array();
+                for ($j = 0; $j < 11; $j++) {
+                    $this->trafficOneDay[$t->device][$i][$j] = array(0, 0); // Pocet danych vozidel, suma jejich rychlosti.
+                }
+            }
+            
         }
         
         // Zjisteni, do jakeho casoveho intervalu patri zaznam.
@@ -101,6 +116,10 @@ class Parser {
         // Ulozeni dulezitych informaci o danem zaznamu.
         $this->traffic[$t->device][$interval][$t->direction][$t->type10][0]++;
         $this->traffic[$t->device][$interval][$t->direction][$t->type10][1] += $t->speed;
+        
+        // Ulozeni i do pole s prumery za cely den.
+        $this->trafficOneDay[$t->device][$t->direction][$t->type10][0]++;
+        $this->trafficOneDay[$t->device][$t->direction][$t->type10][1] += $t->speed;
     }
     
     private function download($date, $zipUrl, $dir, $downloaded) {
@@ -163,6 +182,10 @@ class Parser {
         return $this->traffic;
     }
     
+    public function getTrafficOneDay() {
+        return $this->trafficOneDay;
+    }
+    
     public function getLocations() {
         return $this->locations;
     }
diff --git a/cron/process_traffic_matrix.php b/cron/process_traffic_matrix.php
index 70c299e..3fcae3e 100644
--- a/cron/process_traffic_matrix.php
+++ b/cron/process_traffic_matrix.php
@@ -1,6 +1,6 @@
 <?php
 
-function process_traffic_matrix($parser, $traffic, $idRecordTimeTable, $idRecordTable, &$insertRTT, &$insertRT, $date) {
+function process_traffic_matrix($parser, $traffic, $trafficOneDay, $idRecordTimeTable, $idRecordTable, $idRecordOneDayTable, &$insertRTT, &$insertRT, &$insertOneDay, $date) {
     $times = array();
     
     for ($i = 0; $i < $parser->HOW_MANY_INTERVALS; $i++) {
@@ -43,6 +43,17 @@ function process_traffic_matrix($parser, $traffic, $idRecordTimeTable, $idRecord
             }
         }
     }
+    
+    foreach ($trafficOneDay as $device => $direction) {
+        for ($d = 0; $d < 2; $d++) {
+            for ($v = 0; $v < 11; $v++) {
+                if ($direction[$d][$v][0] > 0) {
+                    $insertOneDay[] = "('".$idRecordOneDayTable++."', '".$direction[$d][$v][0]."', '".($direction[$d][$v][1] / $direction[$d][$v][0])."', '$date', '".($d + 1)."', '$device', '$v')";
+                }
+            }
+        }
+    }
+    
 }
 
 ?>
