Projekt

Obecné

Profil

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

    
3
namespace App\OpenData;
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() {
12
        $this->manager = new Manager(
13
            $_ENV['DATABASE_CONNECTION_STRING']
14
        );
15
    }
16

    
17
    public function getCollectionDataByName($name, $date, $hour) {
18
        $valh = $hour < 10 ? '0'.$hour : $hour;
19
        $openData = $this->manager->executeQuery('open-data-db.'.$name.$date, new Query(['date' => $date.'-'.$valh], []));
20

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

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

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

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

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

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

    
52
        return $availableInDate;
53
    }
54

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

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

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

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

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

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

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

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

    
88
        return $result;
89
    }
90

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

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

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

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

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

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

    
114
        return $positions->toArray();
115
    }
116
}
(4-4/4)