Projekt

Obecné

Profil

Stáhnout (4.61 KB) Statistiky
| Větev: | Revize:
1
<?php
2

    
3
namespace App\Repository;
4

    
5
use MongoDB\Driver\Query;
6
use MongoDB\Driver\Manager;
7

    
8
/**
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
class OpenDataManager implements IOpenDataManager {
15
    /**
16
     * @var IOpenDataManager autowired connection to database
17
     */
18
    private $manager;
19

    
20
    /**
21
     * @param string connection string to database
22
     */
23
    public function __construct($connectionString) {
24
        $this->manager = new Manager(
25
            $connectionString
26
        );
27
    }
28

    
29
    public function getCollectionDataByName($name, $date, $hour) {
30
        if (null == $name || null == $date || null == $hour) {
31
            return [];
32
        }
33

    
34
        // Number has to be two digit
35
        $valh = $hour < 10 ? '0'.$hour : $hour;
36
        $openData = $this->manager->executeQuery('open-data-db.'.$name.$date, new Query(['date' => $date.'-'.$valh], []));
37

    
38
        // Converts result to php array
39
        $openData->setTypeMap([
40
            'array' => 'array',
41
            'document' => 'array',
42
            'root' => 'array',
43
        ]);
44

    
45
        return $openData->toArray();
46
    }
47

    
48
    public function getAvailableCollections() {
49
        $openData = $this->manager->executeQuery('open-data-db.DATASETS', new Query([], ['projection' => ['key-name' => 1, 'display-name' => 1, '_id' => 0]]));
50

    
51
        // Converts result to php array
52
        $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
            if ($this->isCollectionAvailable($value['key-name'], $date) && false == array_key_exists($value['key-name'], $availableInDate)) {
67
                $availableInDate[$value['key-name']] = $value['display-name'];
68
            }
69
        }
70

    
71
        return $availableInDate;
72
    }
73

    
74
    public function isCollectionAvailable($name, $date) {
75
        if (null == $name || null == $date) {
76
            return false;
77
        }
78

    
79
        $result = $this->manager->executeQuery('open-data-db.'.$name, new Query(['date' => $date], []));
80

    
81
        return !empty($result->toArray());
82
    }
83

    
84
    public function getDatesWithAvailableCollection() {
85
        $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

    
105
            $date_array = $date->toArray();
106
            if (!empty($date_array)) {
107
                $result[$value['key-name']] = $date_array[0]['date'];
108
            }
109
        }
110

    
111
        return $result;
112
    }
113

    
114
    public function getMaxCollectionNumberAtDay($name, $date) {
115
        if (null == $name || null == $date) {
116
            return 0;
117
        }
118

    
119
        $max = $this->manager->executeQuery('open-data-db.'.$name.$date, new Query([], ['sort' => ['number' => -1], 'limit' => 1]));
120

    
121
        // Converts result to php array
122
        $max->setTypeMap([
123
            'array' => 'array',
124
            'document' => 'array',
125
            'root' => 'array',
126
        ]);
127

    
128
        $result = $max->toArray();
129

    
130
        return empty($result) ? 0 : $result[0]['number'];
131
    }
132

    
133
    public function getDataSourcePositions($name = 'NONE') {
134
        if (null == $name) {
135
            return [];
136
        }
137

    
138
        $positions = $this->manager->executeQuery('open-data-db.'.$name.'DEVICES', new Query([], []));
139

    
140
        // Converts result to php array
141
        $positions->setTypeMap([
142
            'array' => 'array',
143
            'document' => 'array',
144
            'root' => 'array',
145
        ]);
146

    
147
        return $positions->toArray();
148
    }
149
}
(2-2/2)