Projekt

Obecné

Profil

Stáhnout (4.63 KB) Statistiky
| Větev: | Revize:
1 3fc08f2d vastja
<?php
2
3 ea92a5e5 vastja
namespace App\Repository;
4 3fc08f2d vastja
5
use MongoDB\Driver\Query;
6
use MongoDB\Driver\Manager;
7
8 70a3df53 vastja
/**
9
 * Implementation of IOpenDataManager
10
 * autowired @see website/config/services.yaml and https://symfony.com/doc/current/service_container/autowiring.html.
11
 *
12
 * @see IOpenDataManager for methods comments
13
 */
14 3fc08f2d vastja
class OpenDataManager implements IOpenDataManager {
15 70a3df53 vastja
    /**
16
     * @var IOpenDataManager autowired connection to database
17
     */
18 3fc08f2d vastja
    private $manager;
19
20 70a3df53 vastja
    /**
21 15a58d5d vastja
     * @param string connection string to database
22 70a3df53 vastja
     */
23 ea92a5e5 vastja
    public function __construct($connectionString) {
24 3fc08f2d vastja
        $this->manager = new Manager(
25 ea92a5e5 vastja
            $connectionString
26 3fc08f2d vastja
        );
27
    }
28
29 64bc2934 vastja
    public function getCollectionDataByName($name, $date, $hour) {
30 afb0cc02 vastja
        if (null == $name || null == $date || null == $hour) {
31
            return [];
32
        }
33
34 70a3df53 vastja
        // Number has to be two digit
35 4cc90563 vastja
        $valh = $hour < 10 ? '0'.$hour : $hour;
36 03ccdd65 vastja
        $openData = $this->manager->executeQuery('open-data-db.'.$name.$date, new Query(['date' => $date.'-'.$valh], []));
37 3fc08f2d vastja
38 70a3df53 vastja
        // Converts result to php array
39 3fc08f2d vastja
        $openData->setTypeMap([
40
            'array' => 'array',
41
            'document' => 'array',
42
            'root' => 'array',
43
        ]);
44
45
        return $openData->toArray();
46
    }
47 03c02899 vastja
48
    public function getAvailableCollections() {
49 2f227a6c ballakt
        $openData = $this->manager->executeQuery('open-data-db.DATASETS', new Query([], ['projection' => ['key-name' => 1, 'display-name' => 1, 'display-color' => 1, '_id' => 0]]));
50 03c02899 vastja
51 70a3df53 vastja
        // Converts result to php array
52 03c02899 vastja
        $openData->setTypeMap([
53
            'array' => 'array',
54
            'document' => 'array',
55
            'root' => 'array',
56
        ]);
57
58
        return $openData->toArray();
59
    }
60
61
    public function getAvailableCollectionsByDay($date) {
62
        $availableInDate = [];
63
        $available = $this->getAvailableCollections();
64
        $index = 0;
65
        foreach ($available as $key => $value) {
66 61ff7718 vastja
            if ($this->isCollectionAvailable($value['key-name'], $date) && false == array_key_exists($value['key-name'], $availableInDate)) {
67
                $availableInDate[$value['key-name']] = $value['display-name'];
68 03c02899 vastja
            }
69
        }
70
71
        return $availableInDate;
72
    }
73 3ae59f75 vastja
74 dfe43218 vastja
    public function isCollectionAvailable($name, $date) {
75 afb0cc02 vastja
        if (null == $name || null == $date) {
76
            return false;
77
        }
78
79 72a438f3 vastja
        $result = $this->manager->executeQuery('open-data-db.'.$name, new Query(['date' => $date], []));
80 dfe43218 vastja
81 72a438f3 vastja
        return !empty($result->toArray());
82 dfe43218 vastja
    }
83
84 d8f6e6f2 vastja
    public function getDatesWithAvailableCollection() {
85 72a438f3 vastja
        $available = $this->getAvailableCollections();
86
        $result = [];
87
88
        foreach ($available as $key => $value) {
89
            $dates = $this->manager->executeQuery('open-data-db.'.$value['key-name'], new Query([], ['projection' => ['date' => true, '_id' => false]]));
90
            $dates->setTypeMap(['root' => 'array']);
91
            $result = array_merge($result, array_map(function ($item) {return $item['date']; }, $dates->toArray()));
92
        }
93
94
        return array_values(array_unique($result));
95
    }
96
97
    public function getLastAvailableCollections() {
98
        $available = $this->getAvailableCollections();
99
        $result = [];
100
101
        foreach ($available as $key => $value) {
102
            $date = $this->manager->executeQuery('open-data-db.'.$value['key-name'], new Query([], ['sort' => ['date' => -1], 'limit' => 1, 'projection' => ['date' => true, '_id' => false]]));
103
            $date->setTypeMap(['root' => 'array']);
104 d8f6e6f2 vastja
105 72a438f3 vastja
            $date_array = $date->toArray();
106
            if (!empty($date_array)) {
107
                $result[$value['key-name']] = $date_array[0]['date'];
108 d8f6e6f2 vastja
            }
109
        }
110
111 72a438f3 vastja
        return $result;
112 d8f6e6f2 vastja
    }
113
114 3ae59f75 vastja
    public function getMaxCollectionNumberAtDay($name, $date) {
115 afb0cc02 vastja
        if (null == $name || null == $date) {
116
            return 0;
117
        }
118
119 3ae59f75 vastja
        $max = $this->manager->executeQuery('open-data-db.'.$name.$date, new Query([], ['sort' => ['number' => -1], 'limit' => 1]));
120
121 70a3df53 vastja
        // Converts result to php array
122 3ae59f75 vastja
        $max->setTypeMap([
123
            'array' => 'array',
124
            'document' => 'array',
125
            'root' => 'array',
126
        ]);
127
128 4cc90563 vastja
        $result = $max->toArray();
129
130 afb0cc02 vastja
        return empty($result) ? 0 : $result[0]['number'];
131 3ae59f75 vastja
    }
132 61ff7718 vastja
133
    public function getDataSourcePositions($name = 'NONE') {
134 afb0cc02 vastja
        if (null == $name) {
135
            return [];
136
        }
137
138 61ff7718 vastja
        $positions = $this->manager->executeQuery('open-data-db.'.$name.'DEVICES', new Query([], []));
139
140 70a3df53 vastja
        // Converts result to php array
141 61ff7718 vastja
        $positions->setTypeMap([
142
            'array' => 'array',
143
            'document' => 'array',
144
            'root' => 'array',
145
        ]);
146
147
        return $positions->toArray();
148
    }
149 3fc08f2d vastja
}