Projekt

Obecné

Profil

Stáhnout (3.8 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
        $valh = $hour < 10 ? '0'.$hour : $hour;
20
        $openData = $this->manager->executeQuery('open-data-db.'.$name.$date, new Query(['date' => $date.'-'.$valh], []));
21

    
22
        $openData->setTypeMap([
23
            'array' => 'array',
24
            'document' => 'array',
25
            'root' => 'array',
26
        ]);
27

    
28
        return $openData->toArray();
29
    }
30

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

    
34
        $openData->setTypeMap([
35
            'array' => 'array',
36
            'document' => 'array',
37
            'root' => 'array',
38
        ]);
39

    
40
        return $openData->toArray();
41
    }
42

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

    
53
        return $availableInDate;
54
    }
55

    
56
    public function isCollectionAvailable($name, $date) {
57
        $result = $this->manager->executeQuery('open-data-db.'.$name, new Query(['date' => $date], []));
58

    
59
        return !empty($result->toArray());
60
    }
61

    
62
    public function getDatesWithAvailableCollection() {
63
        $available = $this->getAvailableCollections();
64
        $result = [];
65

    
66
        foreach ($available as $key => $value) {
67
            $dates = $this->manager->executeQuery('open-data-db.'.$value['key-name'], new Query([], ['projection' => ['date' => true, '_id' => false]]));
68
            $dates->setTypeMap(['root' => 'array']);
69
            $result = array_merge($result, array_map(function ($item) {return $item['date']; }, $dates->toArray()));
70
        }
71

    
72
        return array_values(array_unique($result));
73
    }
74

    
75
    public function getLastAvailableCollections() {
76
        $available = $this->getAvailableCollections();
77
        $result = [];
78

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

    
83
            $date_array = $date->toArray();
84
            if (!empty($date_array)) {
85
                $result[$value['key-name']] = $date_array[0]['date'];
86
            }
87
        }
88

    
89
        return $result;
90
    }
91

    
92
    public function getMaxCollectionNumberAtDay($name, $date) {
93
        $max = $this->manager->executeQuery('open-data-db.'.$name.$date, new Query([], ['sort' => ['number' => -1], 'limit' => 1]));
94

    
95
        $max->setTypeMap([
96
            'array' => 'array',
97
            'document' => 'array',
98
            'root' => 'array',
99
        ]);
100

    
101
        $result = $max->toArray();
102

    
103
        return empty($result) ? 1 : $result[0]['number'];
104
    }
105

    
106
    public function getDataSourcePositions($name = 'NONE') {
107
        $positions = $this->manager->executeQuery('open-data-db.'.$name.'DEVICES', new Query([], []));
108

    
109
        $positions->setTypeMap([
110
            'array' => 'array',
111
            'document' => 'array',
112
            'root' => 'array',
113
        ]);
114

    
115
        return $positions->toArray();
116
    }
117
}
(2-2/2)