1: <?php
2:
3: namespace OpenCloud\CloudMonitoring\Resource;
4:
5: use OpenCloud\CloudMonitoring\Exception;
6:
7: 8: 9: 10: 11:
12: class Metric extends ReadOnlyResource implements ResourceInterface
13: {
14:
15: protected static $json_name = 'metrics';
16: protected static $json_collection_name = 'values';
17: protected static $url_resource = 'metrics';
18:
19: protected $dataPointParams = array(
20: 'from',
21: 'to',
22: 'points',
23: 'resolution',
24: 'select'
25: );
26:
27: public function baseUrl()
28: {
29: return $this->getParent()->url($this->getParent()->id . '/'. $this->resourceName());
30: }
31:
32: public function fetchDataPoints($metricName, array $options = array())
33: {
34: $url = $this->url($metricName . '/plot');
35:
36: $parts = array();
37:
38:
39: foreach (array('to', 'from', 'points') as $param) {
40: if (isset($options[$param])) {
41: $parts[$param] = $options[$param];
42: }
43: }
44:
45: if (!isset($parts['to'])) {
46: throw new Exception\MetricException(sprintf(
47: 'Please specify a "to" value'
48: ));
49: }
50:
51: if (!isset($parts['from'])) {
52: throw new Exception\MetricException(sprintf(
53: 'Please specify a "from" value'
54: ));
55: }
56:
57: if (isset($options['resolution'])) {
58: $allowedResolutions = array('FULL', 'MIN5', 'MIN20', 'MIN60', 'MIN240', 'MIN1440');
59: if (!in_array($options['resolution'], $allowedResolutions)) {
60: throw new Exception\MetricException(sprintf(
61: '%s is an invalid resolution type. Please use one of the following: %s',
62: $options['resolution'],
63: implode(', ', $allowedResolutions)
64: ));
65: }
66: $parts['resolution'] = $options['resolution'];
67: }
68:
69: if (isset($options['select'])) {
70: $allowedStats = array('average', 'variance', 'min', 'max');
71: if (!in_array($options['select'], $allowedStats)) {
72: throw new Exception\MetricException(sprintf(
73: '%s is an invalid stat type. Please use one of the following: %s',
74: $options['select'],
75: implode(', ', $allowedStats)
76: ));
77: }
78: $parts['select'] = $options['select'];
79: }
80:
81: if (!isset($parts['points']) && !isset($parts['resolution'])) {
82: throw new Exception\MetricException(sprintf(
83: 'Please specify at least one point or resolution value'
84: ));
85: }
86:
87: $url .= "?to={$parts['to']}";
88: unset($parts['to']);
89: foreach ($parts as $type => $val) {
90: $url .= "&$type=$val";
91: }
92:
93: return $this->getService()->collection(get_class(), $url);
94: }
95:
96: }