Projekt

Obecné

Profil

Stáhnout (4.12 KB) Statistiky
| Větev: | Revize:
1 3fc08f2d vastja
<?php
2
3 ea92a5e5 vastja
namespace App\Repository;
4 3fc08f2d vastja
5
use MongoDB\Driver\Query;
6
use MongoDB\Driver\Manager;
7
8
class OpenDataManager implements IOpenDataManager {
9
    private $manager;
10
11 ea92a5e5 vastja
    public function __construct($connectionString) {
12 3fc08f2d vastja
        $this->manager = new Manager(
13 ea92a5e5 vastja
            $connectionString
14
            // $_ENV['DATABASE_CONNECTION_STRING']
15 3fc08f2d vastja
        );
16
    }
17
18 64bc2934 vastja
    public function getCollectionDataByName($name, $date, $hour) {
19 afb0cc02 vastja
        if (null == $name || null == $date || null == $hour) {
20
            return [];
21
        }
22
23 4cc90563 vastja
        $valh = $hour < 10 ? '0'.$hour : $hour;
24 03ccdd65 vastja
        $openData = $this->manager->executeQuery('open-data-db.'.$name.$date, new Query(['date' => $date.'-'.$valh], []));
25 3fc08f2d vastja
26
        $openData->setTypeMap([
27
            'array' => 'array',
28
            'document' => 'array',
29
            'root' => 'array',
30
        ]);
31
32
        return $openData->toArray();
33
    }
34 03c02899 vastja
35
    public function getAvailableCollections() {
36 61ff7718 vastja
        $openData = $this->manager->executeQuery('open-data-db.DATASETS', new Query([], ['projection' => ['key-name' => 1, 'display-name' => 1, '_id' => 0]]));
37 03c02899 vastja
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 61ff7718 vastja
            if ($this->isCollectionAvailable($value['key-name'], $date) && false == array_key_exists($value['key-name'], $availableInDate)) {
53
                $availableInDate[$value['key-name']] = $value['display-name'];
54 03c02899 vastja
            }
55
        }
56
57
        return $availableInDate;
58
    }
59 3ae59f75 vastja
60 dfe43218 vastja
    public function isCollectionAvailable($name, $date) {
61 afb0cc02 vastja
        if (null == $name || null == $date) {
62
            return false;
63
        }
64
65 72a438f3 vastja
        $result = $this->manager->executeQuery('open-data-db.'.$name, new Query(['date' => $date], []));
66 dfe43218 vastja
67 72a438f3 vastja
        return !empty($result->toArray());
68 dfe43218 vastja
    }
69
70 d8f6e6f2 vastja
    public function getDatesWithAvailableCollection() {
71 72a438f3 vastja
        $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 d8f6e6f2 vastja
91 72a438f3 vastja
            $date_array = $date->toArray();
92
            if (!empty($date_array)) {
93
                $result[$value['key-name']] = $date_array[0]['date'];
94 d8f6e6f2 vastja
            }
95
        }
96
97 72a438f3 vastja
        return $result;
98 d8f6e6f2 vastja
    }
99
100 3ae59f75 vastja
    public function getMaxCollectionNumberAtDay($name, $date) {
101 afb0cc02 vastja
        if (null == $name || null == $date) {
102
            return 0;
103
        }
104
105 3ae59f75 vastja
        $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 4cc90563 vastja
        $result = $max->toArray();
114
115 afb0cc02 vastja
        return empty($result) ? 0 : $result[0]['number'];
116 3ae59f75 vastja
    }
117 61ff7718 vastja
118
    public function getDataSourcePositions($name = 'NONE') {
119 afb0cc02 vastja
        if (null == $name) {
120
            return [];
121
        }
122
123 61ff7718 vastja
        $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 3fc08f2d vastja
}