MongoDB::getMeasurements()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 14
nc 4
nop 3
1
<?php
2
3
/*
4
 * This file is part of the PHPProm package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PHPProm\Storage;
13
14
/**
15
 * Class MongoDB
16
 * Storage implementation using MongoDB.
17
 * @package PHPProm\Storage
18
 */
19
class MongoDB extends AbstractStorage {
20
21
    /**
22
     * @var \MongoDB\Driver\Manager
23
     * The MongoDB Driver Manager.
24
     */
25
    protected $mongoDBManager;
26
27
    /**
28
     * @var string
29
     * The database name for the data.
30
     */
31
    protected $database;
32
33
    /**
34
     * @var string
35
     * The collection name for the data.
36
     */
37
    protected $collection;
38
39
    /**
40
     * MongoDB constructor.
41
     *
42
     * @param string $host
43
     * a mongodb:// connection URI
44
     * @param string $database
45
     * the database to use, defaults to "phppromdb"
46
     * @param string $collection
47
     * the collection to use, defaults to "measurements"
48
     * @param array $options
49
     * connection string options, defaults to []
50
     * @param array $driverOptions
51
     * any driver-specific options not included in MongoDB connection spec, defaults to []
52
     */
53
    public function __construct($host, $database = 'phppromdb', $collection = 'measurements', array $options = [], array $driverOptions = []) {
54
        parent::__construct();
55
        $this->mongoDBManager = new \MongoDB\Driver\Manager($host, $options, $driverOptions);
56
        $this->database       = $database;
57
        $this->collection     = $collection;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function storeMeasurement($metric, $key, $value) {
64
        $bulkWrite = new \MongoDB\Driver\BulkWrite;
65
        $document  = ['key' => $metric.':'.$key, 'value' => $value];
66
        $filter    = ['key' => $metric.':'.$key];
67
        $bulkWrite->update($filter, $document, ['upsert' => true]);
68
        $this->mongoDBManager->executeBulkWrite($this->database.'.'.$this->collection, $bulkWrite);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function incrementMeasurement($metric, $key) {
75
        $filter  = ['key' => $metric.':'.$key];
76
        $options = ['limit' => 1];
77
        $query   = new \MongoDB\Driver\Query($filter, $options);
78
        $results = $this->mongoDBManager->executeQuery($this->database.'.'.$this->collection, $query);
79
        $value   = 1;
80
        foreach ($results as $result) {
81
            $value += $result->value;
82
            break;
83
        }
84
        $this->storeMeasurement($metric, $key, $value);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getMeasurements($metric, array $keys, $defaultValue = 'Nan') {
91
        $prefixedKeys = array_map(function($key) use ($metric) {
92
            return $metric.':'.$key;
93
        }, $keys);
94
95
        $measurements = [];
96
        foreach ($keys as $key) {
97
            $measurements[$key] = $defaultValue;
98
        }
99
        $filter  = ['key' => ['$in' => $prefixedKeys]];
100
        $query   = new \MongoDB\Driver\Query($filter);
101
        $results = $this->mongoDBManager->executeQuery($this->database.'.'.$this->collection, $query);
102
        foreach ($results as $result) {
103
            $unprefixedKey                = substr($result->key, strlen($metric) + 1);
104
            $measurements[$unprefixedKey] = (float)$result->value;
105
        }
106
        return $measurements;
107
    }
108
}
109