Projekt

Obecné

Profil

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