Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 01f99166

Přidáno uživatelem Jan Kohlíček před téměř 7 roky(ů)

refs #6948: Oprava REST API (/devices, /devices/{id}/time-period) a script pro přidání souřadnic

Zobrazit rozdíly:

backend/.env
6 6
DB_HOST=localhost
7 7
DB_PORT=3306
8 8
DB_DATABASE=prujezd_vozidel
9
DB_USERNAME=aswi
10
DB_PASSWORD=aswi
9
DB_USERNAME=root
10
DB_PASSWORD=
11 11

  
12 12
CACHE_DRIVER=memcached
13 13
QUEUE_DRIVER=sync
backend/app/Http/Controllers/DeviceController.php
25 25
    const DIRECTION_PARAM = 'direction';
26 26

  
27 27

  
28
    public function getDevice(Request $request) {
28
    public function getDevice(Request $request)
29
    {
29 30
        $address = null;
30
        $showDirection=0;
31
        $showDirection = 0;
31 32
        if ($request->has(self::ADDRESS_PARAM)) {
32 33
            $address = $request->input(self::ADDRESS_PARAM);
33 34
        }
......
54 55
     * @param $id
55 56
     * @return \Symfony\Component\HttpFoundation\Response
56 57
     */
57
    public function getDeviceById(Request $request, $id) {
58
    public function getDeviceById(Request $request, $id)
59
    {
58 60

  
59 61
        // nacti parametry
60 62
        $params = $this->loadDateTimeDirectionConstraints($request);
......
66 68

  
67 69
        $device = Zarizeni::findByIdJoinAddress($id);
68 70
        if ($device != null) {
69
            $device->traffic = Zaznam::findByDevice($id, $dateFrom, $dateTo, $timeFrom, $timeTo, $direction);
71
            $device->traffics = Zaznam::findByDevice($id, $dateFrom, $dateTo, $timeFrom, $timeTo, $direction);
70 72
            return json_encode($device);
71 73
        } else {
72 74
            return response('Not found.', 404);
73 75
        }
74 76
    }
75 77

  
76
    public function getTrafficAverageByDevice(Request $request, $id) {
78
    public function getTrafficAverageByDevice(Request $request, $id)
79
    {
77 80
        // nacti parametry
78 81
        $params = $this->loadDateTimeDirectionConstraints($request);
79 82
        $dateFrom = $params[self::DATE_FROM_PARAM];
......
84 87

  
85 88
        $device = Zarizeni::findByIdJoinAddress($id);
86 89
        if ($device != null) {
87
            $device[0]->traffic = Zaznam::averageByDevice($id, $dateFrom, $dateTo, $timeFrom, $timeTo, $direction);
88
        } else if ($device == null || count($device) == 0) {
90
            $device->dateFrom = $dateFrom;
91
            $device->dateTo = $dateTo;
92

  
93
            if ($direction != null) {
94
                $device->direction = intval($direction);
95
            }
96

  
97
            $device->traffics = Zaznam::averageByDevice($id, $dateFrom, $dateTo, $timeFrom, $timeTo, $direction);
98
            return json_encode($device);
99
        } else {
89 100
            return response('Not found.', 404);
90 101
        }
91

  
92
        return $device;
93 102
    }
94 103

  
95
    public function getAll() {
104
    public function getAll()
105
    {
96 106
        return Zarizeni::getAllJoinAddress();
97 107
    }
98 108

  
99
    public function lastDay() {
109
    public function lastDay()
110
    {
100 111
        return Zaznam::lastInsertedDate();
101 112
    }
102 113

  
103
    public function headerTest(Request $request) {
114
    public function headerTest(Request $request)
115
    {
104 116
        $authHeader = $request->header("jwt");
105 117

  
106
        if($authHeader != null) {
118
        if ($authHeader != null) {
107 119
            return $authHeader;
108 120
        } else {
109 121
            return $request->header("jwt");
......
116 128
     *
117 129
     * @param Request $request Request ze ktere budou nacitany parametry.
118 130
     */
119
    private function loadDateTimeDirectionConstraints(Request $request) {
131
    private function loadDateTimeDirectionConstraints(Request $request)
132
    {
120 133
        $params = array();
121 134
        $params[self::DATE_FROM_PARAM] = null;
122 135
        $params[self::DATE_TO_PARAM] = null;
backend/app/Model/Zarizeni.php
30 30
     *
31 31
     * @return mixed
32 32
     */
33
    public static function getAllJoinAddress() {
33
    public static function getAllJoinAddress()
34
    {
34 35
        return DB::table('zarizeni')
35 36
            ->join('ulice', 'zarizeni.ulice_id', '=', 'ulice.id')
36 37
            ->join('mesto', 'ulice.mesto_id', '=', 'mesto.id')
......
52 53
     * @param $showDirection 1 pokud má být rozlišen směr zařízení.
53 54
     * @return mixed
54 55
     */
55
    public static function findByAddressJoinAddress($address, $showDirection) {
56
    public static function findByAddressJoinAddress($address, $showDirection)
57
    {
56 58
        $query = DB::table('zarizeni')
57 59
            ->join('ulice', 'zarizeni.ulice_id', '=', 'ulice.id')
58 60
            ->join('mesto', 'ulice.mesto_id', '=', 'mesto.id')
61
            ->join('zaznam_cas', 'zaznam_cas.zarizeni_id', '=', 'zarizeni.id')
59 62
            ->select('zarizeni.id as id',
60 63
                'zarizeni.smer_popis as name',
61 64
                'ulice.nazev as street',
......
63 66
                'ulice.lat as lat',
64 67
                'ulice.lng as lng',
65 68
                'mesto.nazev as town',
66
                'mesto.id as town_id')
67
            ->where('ulice.nazev', 'like', '%'.$address.'%')
68
            ->orWhere('mesto.nazev', 'like', '%'.$address.'%');
69
                'mesto.id as town_id',
70
                $showDirection ? 'zaznam_cas.smer as direction' : DB::Raw('0 as direction'))
71
            ->where('ulice.nazev', 'like', '%' . $address . '%')
72
            ->orWhere('mesto.nazev', 'like', '%' . $address . '%')
73
            ->orWhere('zarizeni.smer_popis', 'like', '%' . $address . '%');
69 74

  
70 75
        if (!$showDirection) {
71
            $query = $query->groupBy('zarizeni.ulice_id');
76
            $query = $query->groupBy('zarizeni.id');
77
        } else {
78
            $query = $query->groupBy('zarizeni.id', 'zaznam_cas.smer');
72 79
        }
73 80

  
74 81

  
......
82 89
     * @param $id Id zarizeni.
83 90
     * @return mixed
84 91
     */
85
    public static function findByIdJoinAddress($id) {
92
    public static function findByIdJoinAddress($id)
93
    {
86 94
        return DB::table('zarizeni')
87 95
            ->join('ulice', 'zarizeni.ulice_id', '=', 'ulice.id')
88 96
            ->join('mesto', 'ulice.mesto_id', '=', 'mesto.id')
......
120 128
     */
121 129
    public $stav;
122 130

  
123
    public function ulice() {
131
    public function ulice()
132
    {
124 133
        return $this->belongsTo('App\Model\Ulice');
125 134
    }
126 135
}
backend/app/Model/Zaznam.php
23 23
     * Vrati posledni datum pro ktere existuji nejake zaznamy.
24 24
     * @return String Posledni datum pro ktere existuji zaznamy.
25 25
     */
26
    public static function lastInsertedDate() {
26
    public static function lastInsertedDate()
27
    {
27 28
        return DB::table('zaznam_cas')->select(DB::raw('max(date(datetime_od)) as last_day'))->get();
28 29
    }
29 30

  
......
38 39
     * @param int $direction Pozadovany smer. Null znamena oba smery.
39 40
     * @return array Prumery dopravy pro casovy usek podle typu vozidla.
40 41
     */
41
    public static function averageByDevice($deviceId, $dateFrom, $dateTo, $timeFrom, $timeTo, $direction) {
42
        $dateTimeFrom = null;
43
        $dateTimeTo = null;
42
    public static function averageByDevice($deviceId, $dateFrom, $dateTo, $timeFrom, $timeTo, $direction)
43
    {
44 44
        $lastDate = null;
45 45
        $dir = null;
46 46

  
......
54 54
                $lastDate = $lastDate[0]->last_day;
55 55
            }
56 56
        }
57
        $dateTimeFrom = self::concatDateTime($dateFrom, $timeFrom, $lastDate, '00:00:00');
58
        $dateTimeTo = self::concatDateTime($dateTo, $timeTo, $lastDate, '23:59:59');
59

  
60 57

  
61 58
        // vytvoreni query - vsechno to dat dohromady
62
        $query = DB::table('zaznam')
63
            ->join('zaznam_cas', 'zaznam.zaznam_cas_id', '=', 'zaznam_cas.id')
64
            ->join('vozidlo', 'zaznam.vozidlo_id', '=', 'vozidlo.id')
59
        $query = DB::table('zaznam_cas')
60
            ->join('zaznam', 'zaznam.zaznam_cas_id', '=', 'zaznam_cas.id')
65 61
            ->select(DB::raw("
66
                date_format(zaznam_cas.datetime_od, '%Y-%m-%d') as dateFrom,
67
                date_format(zaznam_cas.datetime_do,  '%Y-%m-%d') as dateTo,
68
                date_format(zaznam_cas.datetime_od, '%H:%i:%s') as timeFrom,
69
                date_format(zaznam_cas.datetime_do, '%H:%i:%s') as timeTo,
70
                zaznam_cas.smer as direction,
71
                avg(zaznam.rychlost_prumer) as speedAverage,
72
                sum(zaznam.vozidla_pocet) as numberVehicle,
73
                vozidlo.nazev as typeVehicle,
74
                vozidlo.id as typeVehicleId
62
                date_format(zaznam_cas.datetime_od, '%H:%i') as timeFrom,
63
                date_format(zaznam_cas.datetime_do, '%H:%i') as timeTo,
64
                ROUND(avg(zaznam.rychlost_prumer),0) as speedAverage,
65
                CAST(sum(zaznam.vozidla_pocet) as UNSIGNED) as numberVehicle,
66
                ROUND(avg(zaznam.vozidla_pocet),0) as numberVehicleAverage,
67
                zaznam.vozidlo_id as typeVehicleId
75 68
            "))
76
            ->where('zaznam_cas.datetime_od', '>=', $dateTimeFrom)
77
            ->where('zaznam_cas.datetime_do', '<=', $dateTimeTo)
78
            ->where('zaznam_cas.zarizeni_id', '=', $deviceId)
79
        ;
69
            ->whereDate('zaznam_cas.datetime_od', '>=', $dateFrom == null ? $lastDate : $dateFrom)
70
            ->whereDate('zaznam_cas.datetime_do', '<=', $dateTo == null ? $lastDate : $dateTo)
71
            ->whereTime('zaznam_cas.datetime_od', '>=', $timeFrom == null ? '08:00:00' : $timeFrom)
72
            ->whereTime('zaznam_cas.datetime_od', '<=', $timeTo == null ? '23:59:59' : $timeTo)
73
            ->where('zaznam_cas.zarizeni_id', '=', $deviceId);
80 74

  
81
        if($direction != null) {
75
        if ($direction != null) {
82 76
            $query = $query->where('zaznam_cas.smer', '=', $direction);
83 77
        }
84 78

  
85 79
        // pridat grouping a razeni nakonec
86 80
        $query = $query
87
                ->groupBy('timeFrom', 'timeTo', 'typeVehicleId')
88
                ->orderBy('dateFrom', 'asc')
89
                ->orderBy('timeFrom', 'asc')
90
                ->orderBy('typeVehicleId', 'asc');
81
            ->groupBy('zaznam_cas.datetime_od', 'zaznam.vozidlo_id')
82
            ->orderBy('zaznam_cas.datetime_od', 'asc')
83
            ->orderBy('zaznam.vozidlo_id', 'asc');
91 84

  
92 85
        return $query->get();
93 86
    }
......
104 97
     * @param int $direction Pozadovany smer. Null znamena oba smery.
105 98
     * @return array Zaznamy o doprave v casovem useku pro dane zarizeni.
106 99
     */
107
    public static function findByDevice($deviceId, $dateFrom, $dateTo, $timeFrom, $timeTo, $direction) {
100
    public static function findByDevice($deviceId, $dateFrom, $dateTo, $timeFrom, $timeTo, $direction)
101
    {
108 102
        $dateTimeFrom = null;
109 103
        $dateTimeTo = null;
110 104
        $lastDate = null;
......
139 133
            ->where('zaznam_cas.datetime_do', '<=', $dateTimeTo)
140 134
            ->where('zaznam_cas.zarizeni_id', '=', $deviceId);
141 135

  
142
        if($direction != null) {
136
        if ($direction != null) {
143 137
            $query = $query->where('zaznam_cas.smer', '=', $direction);
144 138
        }
145 139

  
......
160 154
     * @param String $defTime Defaultni hodnota pro casovou slozku.
161 155
     * @return String Spojene datum a cas.
162 156
     */
163
    private static function concatDateTime($date, $time, $defDate, $defTime) {
157
    private static function concatDateTime($date, $time, $defDate, $defTime)
158
    {
164 159
        $dateTime = null;
165 160
        $d = $date == null ? $defDate : $date;
166 161
        $t = $time == null ? $defTime : $time;
......
168 163
        $dateTime = date('Y-m-d H:i:s', strtotime("$d $t"));
169 164
        return $dateTime;
170 165
    }
166

  
171 167
}
db/v5__geosouradnice_update.sql
1

2
UPDATE `ulice` SET lat=49.369388,lng=12.858804400000054 WHERE id=1; 
3
UPDATE `ulice` SET lat=49.5942514,lng=13.100573599999962 WHERE id=2;
4
UPDATE `ulice` SET lat=49.5325795,lng=12.932922299999973 WHERE id=3;
5
UPDATE `ulice` SET lat=49.5331401,lng=12.944697200000064 WHERE id=4;
6
UPDATE `ulice` SET lat=49.5290441,lng=12.924181900000008 WHERE id=5;
7
UPDATE `ulice` SET lat=49.4348481,lng=12.814734700000031 WHERE id=6;
8
UPDATE `ulice` SET lat=49.3661949,lng=13.134370499999932 WHERE id=7;
9
UPDATE `ulice` SET lat=49.5073853,lng=12.801840500000026 WHERE id=8;
10
UPDATE `ulice` SET lat=49.5511106,lng=13.065398100000039 WHERE id=9;
11
UPDATE `ulice` SET lat=49.3016091,lng=13.312576700000022 WHERE id=10;
12
UPDATE `ulice` SET lat=49.5018924,lng=13.294453100000055 WHERE id=11;
13
UPDATE `ulice` SET lat=49.1962834,lng=13.509260100000006 WHERE id=12;
14
UPDATE `ulice` SET lat=49.2989568,lng=13.439006199999994 WHERE id=13;
15
UPDATE `ulice` SET lat=49.726864,lng=12.901699000000008 WHERE id=14;
16
UPDATE `ulice` SET lat=49.8893779,lng=12.606704600000057 WHERE id=15;
17
UPDATE `ulice` SET lat=49.8067796,lng=12.642488400000047 WHERE id=16;
18
UPDATE `ulice` SET lat=49.7911169,lng=12.648667199999977 WHERE id=17;
19
UPDATE `ulice` SET lat=49.8030504,lng=12.63480770000001 WHERE id=18;
20
UPDATE `ulice` SET lat=49.4329948,lng=12.951485300000058 WHERE id=19;
21
UPDATE `ulice` SET lat=49.5615961,lng=12.77715139999998 WHERE id=20;
22
UPDATE `ulice` SET lat=49.5891548,lng=12.881511600000067 WHERE id=21;
23
UPDATE `ulice` SET lat=49.4365627,lng=12.720542000000023 WHERE id=22;
24
UPDATE `ulice` SET lat=49.3667667,lng=13.009845799999994 WHERE id=23;
25
UPDATE `ulice` SET lat=49.8416809,lng=13.229441100000031 WHERE id=24;
26
UPDATE `ulice` SET lat=49.34425969999999,lng=13.219951100000003 WHERE id=25;
27
UPDATE `ulice` SET lat=49.3590594,lng=13.384042300000033 WHERE id=26;
28
UPDATE `ulice` SET lat=49.3603773,lng=13.356904600000007 WHERE id=27;
29
UPDATE `ulice` SET lat=49.3909746,lng=13.468732700000032 WHERE id=28;
30
UPDATE `ulice` SET lat=49.6655193,lng=12.827393700000016 WHERE id=29;
31
UPDATE `ulice` SET lat=49.591775,lng=12.705252900000005 WHERE id=30;
32
UPDATE `ulice` SET lat=49.5893638,lng=12.720079899999973 WHERE id=31;
33
UPDATE `ulice` SET lat=49.3247489,lng=13.69061750000003 WHERE id=32;
34
UPDATE `ulice` SET lat=49.3195422,lng=13.71081079999999 WHERE id=33;
35
UPDATE `ulice` SET lat=49.323759,lng=13.703250000000025 WHERE id=34;
36
UPDATE `ulice` SET lat=49.1460905,lng=13.564452399999936 WHERE id=35;
37
UPDATE `ulice` SET lat=49.0240725,lng=13.499317499999961 WHERE id=36;
38
UPDATE `ulice` SET lat=49.0285263,lng=13.517740499999945 WHERE id=37;
39
UPDATE `ulice` SET lat=49.2881779,lng=13.143561800000043 WHERE id=38;
40
UPDATE `ulice` SET lat=49.2863376,lng=13.12969309999994 WHERE id=39;
41
UPDATE `ulice` SET lat=49.293581,lng=13.125704600000063 WHERE id=40;
42
UPDATE `ulice` SET lat=49.2951762,lng=13.150141200000007 WHERE id=41;
43
UPDATE `ulice` SET lat=49.2276062,lng=13.520478400000002 WHERE id=42;
44
UPDATE `ulice` SET lat=49.2234632,lng=13.511268900000005 WHERE id=43;
45
UPDATE `ulice` SET lat=49.24249409999999,lng=13.532533599999965 WHERE id=44;
46
UPDATE `ulice` SET lat=49.1374427,lng=13.235204599999975 WHERE id=45;
47
UPDATE `ulice` SET lat=49.7173149,lng=12.777892800000018 WHERE id=46;
48
UPDATE `ulice` SET lat=49.893186,lng=12.730072699999937 WHERE id=47;
49
UPDATE `ulice` SET lat=49.863975,lng=12.725196099999948 WHERE id=48;
50
UPDATE `ulice` SET lat=49.6681792,lng=12.551898199999982 WHERE id=49;
51
UPDATE `ulice` SET lat=49.752874,lng=12.989615299999969 WHERE id=50;
52
UPDATE `ulice` SET lat=49.7515438,lng=13.00579289999996 WHERE id=51;
53
UPDATE `ulice` SET lat=49.3986612,lng=12.862793099999976 WHERE id=52;
54
UPDATE `ulice` SET lat=49.3825977,lng=13.049589999999966 WHERE id=53;
55
UPDATE `ulice` SET lat=49.59149559999999,lng=12.715902400000004 WHERE id=54;
56
UPDATE `ulice` SET lat=49.4462758,lng=12.932268199999953 WHERE id=55;
57
UPDATE `ulice` SET lat=49.4417745,lng=12.917492100000004 WHERE id=56;
58
UPDATE `ulice` SET lat=49.5316687,lng=12.951474599999983 WHERE id=57;
59
UPDATE `ulice` SET lat=49.5256309,lng=12.93846629999996 WHERE id=58;
60
UPDATE `ulice` SET lat=49.4188422,lng=12.716298300000062 WHERE id=59;
61
UPDATE `ulice` SET lat=49.3434229,lng=13.133497300000045 WHERE id=60;
62
UPDATE `ulice` SET lat=49.4249957,lng=13.285349600000018 WHERE id=61;
63
UPDATE `ulice` SET lat=49.3858178,lng=13.304274299999975 WHERE id=62;
64
UPDATE `ulice` SET lat=49.3831621,lng=13.297191399999974 WHERE id=63;
65
UPDATE `ulice` SET lat=49.4024071,lng=13.293027400000028 WHERE id=64;
66
UPDATE `ulice` SET lat=49.3939022,lng=13.28203080000003 WHERE id=65;
67
UPDATE `ulice` SET lat=49.402985,lng=13.28112520000002 WHERE id=66;
68
UPDATE `ulice` SET lat=49.3954174,lng=13.304848499999935 WHERE id=67;
69
UPDATE `ulice` SET lat=49.7114696,lng=12.772997000000032 WHERE id=68;
70
UPDATE `ulice` SET lat=49.7088027,lng=12.782785699999977 WHERE id=69;
71
UPDATE `ulice` SET lat=49.6779674,lng=12.654376100000036 WHERE id=70;
72
UPDATE `ulice` SET lat=49.0871919,lng=13.480447700000013 WHERE id=71;
73
UPDATE `ulice` SET lat=49.8255089,lng=12.523725000000013 WHERE id=72;
74
UPDATE `ulice` SET lat=49.348875,lng=12.85527849999994 WHERE id=73;
75
UPDATE `ulice` SET lat=49.2316758,lng=13.520409299999983 WHERE id=74;
76
UPDATE `ulice` SET lat=49.6692003,lng=13.448342300000036 WHERE id=75;
77
UPDATE `ulice` SET lat=49.696732,lng=13.459893899999997 WHERE id=76;
78
UPDATE `ulice` SET lat=49.62768089999999,lng=13.152081500000008 WHERE id=77;
79
UPDATE `ulice` SET lat=49.4861699,lng=13.582260899999937 WHERE id=78;
80
UPDATE `ulice` SET lat=49.6404353,lng=13.519626600000038 WHERE id=79;
81
UPDATE `ulice` SET lat=49.89313869999999,lng=13.382958300000041 WHERE id=80;
82
UPDATE `ulice` SET lat=49.981924,lng=13.487483500000053 WHERE id=81;
83
UPDATE `ulice` SET lat=49.76380169999999,lng=13.252113799999961 WHERE id=82;
84
UPDATE `ulice` SET lat=49.6784086,lng=13.22565439999994 WHERE id=83;
85
UPDATE `ulice` SET lat=49.6548327,lng=13.29307730000005 WHERE id=84;
86
UPDATE `ulice` SET lat=49.9344975,lng=13.390364999999974 WHERE id=85;
87
UPDATE `ulice` SET lat=49.7298911,lng=13.276285899999948 WHERE id=86;
88
UPDATE `ulice` SET lat=49.5729906,lng=13.33350710000002 WHERE id=87;

Také k dispozici: Unified diff