Projekt

Obecné

Profil

Stáhnout (3.75 KB) Statistiky
| Větev: | Revize:
1 3fc08f2d vastja
<?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 64bc2934 vastja
    public function getCollectionDataByName($name, $date, $hour) {
18 4cc90563 vastja
        $valh = $hour < 10 ? '0'.$hour : $hour;
19 03ccdd65 vastja
        $openData = $this->manager->executeQuery('open-data-db.'.$name.$date, new Query(['date' => $date.'-'.$valh], []));
20 3fc08f2d vastja
21
        $openData->setTypeMap([
22
            'array' => 'array',
23
            'document' => 'array',
24
            'root' => 'array',
25
        ]);
26
27
        return $openData->toArray();
28
    }
29 03c02899 vastja
30
    public function getAvailableCollections() {
31 61ff7718 vastja
        $openData = $this->manager->executeQuery('open-data-db.DATASETS', new Query([], ['projection' => ['key-name' => 1, 'display-name' => 1, '_id' => 0]]));
32 03c02899 vastja
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 61ff7718 vastja
            if ($this->isCollectionAvailable($value['key-name'], $date) && false == array_key_exists($value['key-name'], $availableInDate)) {
48
                $availableInDate[$value['key-name']] = $value['display-name'];
49 03c02899 vastja
            }
50
        }
51
52
        return $availableInDate;
53
    }
54 3ae59f75 vastja
55 dfe43218 vastja
    public function isCollectionAvailable($name, $date) {
56 72a438f3 vastja
        $result = $this->manager->executeQuery('open-data-db.'.$name, new Query(['date' => $date], []));
57 dfe43218 vastja
58 72a438f3 vastja
        return !empty($result->toArray());
59 dfe43218 vastja
    }
60
61 d8f6e6f2 vastja
    public function getDatesWithAvailableCollection() {
62 72a438f3 vastja
        $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 d8f6e6f2 vastja
82 72a438f3 vastja
            $date_array = $date->toArray();
83
            if (!empty($date_array)) {
84
                $result[$value['key-name']] = $date_array[0]['date'];
85 d8f6e6f2 vastja
            }
86
        }
87
88 72a438f3 vastja
        return $result;
89 d8f6e6f2 vastja
    }
90
91 3ae59f75 vastja
    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 4cc90563 vastja
        $result = $max->toArray();
101
102
        return empty($result) ? 1 : $result[0]['number'];
103 3ae59f75 vastja
    }
104 61ff7718 vastja
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 3fc08f2d vastja
}