Projekt

Obecné

Profil

Stáhnout (4.12 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
class OpenDataManager implements IOpenDataManager {
9
    private $manager;
10

    
11
    public function __construct($connectionString) {
12
        $this->manager = new Manager(
13
            $connectionString
14
            // $_ENV['DATABASE_CONNECTION_STRING']
15
        );
16
    }
17

    
18
    public function getCollectionDataByName($name, $date, $hour) {
19
        if (null == $name || null == $date || null == $hour) {
20
            return [];
21
        }
22

    
23
        $valh = $hour < 10 ? '0'.$hour : $hour;
24
        $openData = $this->manager->executeQuery('open-data-db.'.$name.$date, new Query(['date' => $date.'-'.$valh], []));
25

    
26
        $openData->setTypeMap([
27
            'array' => 'array',
28
            'document' => 'array',
29
            'root' => 'array',
30
        ]);
31

    
32
        return $openData->toArray();
33
    }
34

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

    
38
        $openData->setTypeMap([
39
            'array' => 'array',
40
            'document' => 'array',
41
            'root' => 'array',
42
        ]);
43

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

    
47
    public function getAvailableCollectionsByDay($date) {
48
        $availableInDate = [];
49
        $available = $this->getAvailableCollections();
50
        $index = 0;
51
        foreach ($available as $key => $value) {
52
            if ($this->isCollectionAvailable($value['key-name'], $date) && false == array_key_exists($value['key-name'], $availableInDate)) {
53
                $availableInDate[$value['key-name']] = $value['display-name'];
54
            }
55
        }
56

    
57
        return $availableInDate;
58
    }
59

    
60
    public function isCollectionAvailable($name, $date) {
61
        if (null == $name || null == $date) {
62
            return false;
63
        }
64

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

    
67
        return !empty($result->toArray());
68
    }
69

    
70
    public function getDatesWithAvailableCollection() {
71
        $available = $this->getAvailableCollections();
72
        $result = [];
73

    
74
        foreach ($available as $key => $value) {
75
            $dates = $this->manager->executeQuery('open-data-db.'.$value['key-name'], new Query([], ['projection' => ['date' => true, '_id' => false]]));
76
            $dates->setTypeMap(['root' => 'array']);
77
            $result = array_merge($result, array_map(function ($item) {return $item['date']; }, $dates->toArray()));
78
        }
79

    
80
        return array_values(array_unique($result));
81
    }
82

    
83
    public function getLastAvailableCollections() {
84
        $available = $this->getAvailableCollections();
85
        $result = [];
86

    
87
        foreach ($available as $key => $value) {
88
            $date = $this->manager->executeQuery('open-data-db.'.$value['key-name'], new Query([], ['sort' => ['date' => -1], 'limit' => 1, 'projection' => ['date' => true, '_id' => false]]));
89
            $date->setTypeMap(['root' => 'array']);
90

    
91
            $date_array = $date->toArray();
92
            if (!empty($date_array)) {
93
                $result[$value['key-name']] = $date_array[0]['date'];
94
            }
95
        }
96

    
97
        return $result;
98
    }
99

    
100
    public function getMaxCollectionNumberAtDay($name, $date) {
101
        if (null == $name || null == $date) {
102
            return 0;
103
        }
104

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

    
107
        $max->setTypeMap([
108
            'array' => 'array',
109
            'document' => 'array',
110
            'root' => 'array',
111
        ]);
112

    
113
        $result = $max->toArray();
114

    
115
        return empty($result) ? 0 : $result[0]['number'];
116
    }
117

    
118
    public function getDataSourcePositions($name = 'NONE') {
119
        if (null == $name) {
120
            return [];
121
        }
122

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

    
125
        $positions->setTypeMap([
126
            'array' => 'array',
127
            'document' => 'array',
128
            'root' => 'array',
129
        ]);
130

    
131
        return $positions->toArray();
132
    }
133
}
(2-2/2)