Projekt

Obecné

Profil

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

    
3
namespace App\OpenData;
4

    
5
use MongoDB\Driver\Query;
6
use MongoDB\Driver\Command;
7
use MongoDB\Driver\Manager;
8

    
9
class OpenDataManager implements IOpenDataManager {
10
    private $manager;
11

    
12
    public function __construct() {
13
        $this->manager = new Manager(
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' => ['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['name'], $date) && false == array_key_exists($value['name'], $availableInDate)) {
49
                $availableInDate[$value['name']] = $index++;
50
            }
51
        }
52

    
53
        return $availableInDate;
54
    }
55

    
56
    public function getXthAvailableCollectionByDay($index, $date) {
57
        $availableInDate = [];
58
        $available = $this->getAvailableCollections();
59
        $currentIndex = 0;
60

    
61
        foreach ($available as $key => $value) {
62
            if ($this->isCollectionAvailable($value['name'], $date) && false == array_key_exists($value['name'], $availableInDate)) {
63
                if ($currentIndex == $index) {
64
                    return $value['name'];
65
                } else {
66
                    ++$currentIndex;
67
                }
68
            }
69
        }
70

    
71
        return '';
72
    }
73

    
74
    public function isCollectionAvailable($name, $date) {
75
        $command = new Command(['listCollections' => 1, 'filter' => ['name' => $name.$date]]);
76
        $result = $this->manager->executeCommand('open-data-db', $command)->toArray();
77

    
78
        return !empty($result);
79
    }
80

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

    
84
        $max->setTypeMap([
85
            'array' => 'array',
86
            'document' => 'array',
87
            'root' => 'array',
88
        ]);
89

    
90
        $result = $max->toArray();
91

    
92
        return empty($result) ? 1 : $result[0]['number'];
93
    }
94
}
(4-4/4)