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
|
|
|
$writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000); |
69
|
|
|
$this->mongoDBManager->executeBulkWrite($this->database.'.'.$this->collection, $bulkWrite, $writeConcern); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function incrementMeasurement($metric, $key) { |
76
|
|
|
$filter = ['key' => $metric.':'.$key]; |
|
|
|
|
77
|
|
|
$options = ['limit' => 1]; |
78
|
|
|
$query = new \MongoDB\Driver\Query($filter, $options); |
|
|
|
|
79
|
|
|
$results = $this->mongoDBManager->executeQuery($this->database.'.'.$this->collection, $query); |
80
|
|
|
$value = 1; |
|
|
|
|
81
|
|
|
foreach ($results as $result) { |
82
|
|
|
$value += $result->value; |
83
|
|
|
break; |
84
|
|
|
} |
85
|
|
|
$this->storeMeasurement($metric, $key, $value); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function getMeasurements($metric, array $keys, $defaultValue = 'Nan') { |
92
|
|
|
$prefixedKeys = array_map(function($key) use ($metric) { |
93
|
|
|
return $metric.':'.$key; |
94
|
|
|
}, $keys); |
95
|
|
|
|
96
|
|
|
$measurements = []; |
97
|
|
|
foreach ($keys as $key) { |
98
|
|
|
$measurements[$key] = $defaultValue; |
99
|
|
|
} |
100
|
|
|
$filter = ['key' => ['$in' => $prefixedKeys]]; |
|
|
|
|
101
|
|
|
$query = new \MongoDB\Driver\Query($filter); |
|
|
|
|
102
|
|
|
$results = $this->mongoDBManager->executeQuery($this->database.'.'.$this->collection, $query); |
103
|
|
|
foreach ($results as $result) { |
104
|
|
|
$unprefixedKey = substr($result->key, strlen($metric) + 1); |
105
|
|
|
$measurements[$unprefixedKey] = (float)$result->value; |
106
|
|
|
} |
107
|
|
|
return $measurements; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.