Projekt

Obecné

Profil

Stáhnout (2.74 KB) Statistiky
| Větev: | Revize:
1 3fc08f2d vastja
<?php
2
3
namespace App\OpenData;
4
5
use MongoDB\Driver\Query;
6 4cc90563 vastja
use MongoDB\Driver\Command;
7 3fc08f2d vastja
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 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
        $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 dfe43218 vastja
            if ($this->isCollectionAvailable($value['name'], $date) && false == array_key_exists($value['name'], $availableInDate)) {
49 03ccdd65 vastja
                $availableInDate[$value['name']] = $value['name'];
50 03c02899 vastja
            }
51
        }
52
53
        return $availableInDate;
54
    }
55 3ae59f75 vastja
56 dfe43218 vastja
    public function isCollectionAvailable($name, $date) {
57
        $command = new Command(['listCollections' => 1, 'filter' => ['name' => $name.$date]]);
58
        $result = $this->manager->executeCommand('open-data-db', $command)->toArray();
59
60
        return !empty($result);
61
    }
62
63 d8f6e6f2 vastja
    public function getDatesWithAvailableCollection() {
64
        $command = new Command(['listCollections' => 1]);
65
        $result = $this->manager->executeCommand('open-data-db', $command)->toArray();
66
67
        $dates = [];
68
        foreach ($result as $value) {
69
            if (preg_match('/\\d{4}-\\d{2}-\\d{2}/', $value->name)) {
70
                array_push($dates, substr($value->name, -10));
71
            }
72
        }
73
74
        return array_values(array_unique($dates));
75
    }
76
77 3ae59f75 vastja
    public function getMaxCollectionNumberAtDay($name, $date) {
78
        $max = $this->manager->executeQuery('open-data-db.'.$name.$date, new Query([], ['sort' => ['number' => -1], 'limit' => 1]));
79
80
        $max->setTypeMap([
81
            'array' => 'array',
82
            'document' => 'array',
83
            'root' => 'array',
84
        ]);
85
86 4cc90563 vastja
        $result = $max->toArray();
87
88
        return empty($result) ? 1 : $result[0]['number'];
89 3ae59f75 vastja
    }
90 3fc08f2d vastja
}