|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sjdaws\NewRelicApi\Account; |
|
4
|
|
|
|
|
5
|
|
|
use Monolog\Logger; |
|
6
|
|
|
use Sjdaws\NewRelicApi\Resource; |
|
7
|
|
|
|
|
8
|
|
|
class Usage extends Resource |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The end date in Y-m-d format |
|
12
|
|
|
* |
|
13
|
|
|
* @param string |
|
14
|
|
|
*/ |
|
15
|
|
|
private $endDate; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Whether sub accounts should be included in the request or not |
|
19
|
|
|
* |
|
20
|
|
|
* @param boolean |
|
21
|
|
|
*/ |
|
22
|
|
|
private $includeSubAccounts; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The product type which will be called by this resource |
|
26
|
|
|
* |
|
27
|
|
|
* @param string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $productType; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The product types that can be used with this resource |
|
33
|
|
|
* |
|
34
|
|
|
* @param array |
|
35
|
|
|
*/ |
|
36
|
|
|
private $productTypes = ['apm', 'browser', 'mobile']; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The start date in Y-m-d format |
|
40
|
|
|
* |
|
41
|
|
|
* @param string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $startDate; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Create a new usage resource and set the start and end to today |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $apiKey |
|
49
|
|
|
* @param Logger $logger |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct($apiKey, Logger $logger = null) |
|
52
|
|
|
{ |
|
53
|
|
|
parent::__construct($apiKey, $logger); |
|
54
|
|
|
|
|
55
|
|
|
// Use today as default date range |
|
56
|
|
|
$this->startDate(time()); |
|
57
|
|
|
$this->endDate(time()); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Set the date to get data to |
|
62
|
|
|
* |
|
63
|
|
|
* @param mixed $date |
|
64
|
|
|
* @return Usage |
|
65
|
|
|
*/ |
|
66
|
|
|
public function endDate($date) |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->setDate('end', $date); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Perform API request |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $type |
|
75
|
|
|
* @throws GuzzleHttp\Exception\ClientException Exception thrown by Guzzle |
|
76
|
|
|
* @return GuzzleHttp\Psr7\Stream The guzzle response body |
|
77
|
|
|
*/ |
|
78
|
|
|
public function get($type = '') |
|
79
|
|
|
{ |
|
80
|
|
|
// If we have a product type passed, use it from here |
|
81
|
|
|
if ($type) { |
|
82
|
|
|
$this->productType($type); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// If no type has been specified the request won't complete |
|
86
|
|
|
if (!$this->productType) { |
|
87
|
|
|
$this->throwException('No product type specified. Must be one of: [' . implode(', ', $this->productTypes) . '].'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$this->addData('start_date', $this->startDate); |
|
91
|
|
|
$this->addData('end_date', $this->endDate); |
|
92
|
|
|
|
|
93
|
|
|
if ($this->includeSubAccounts) { |
|
94
|
|
|
// New Relic expects the boolean to be sent as string |
|
95
|
|
|
$this->addData('include_subaccounts', 'true'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $this->request('usages/' . $this->productType . '.json'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Toggle include sub accounts |
|
103
|
|
|
* |
|
104
|
|
|
* @param boolean $include |
|
105
|
|
|
* @throws Exception If $include isn't a boolean |
|
106
|
|
|
* @return Usage |
|
107
|
|
|
*/ |
|
108
|
|
|
public function includeSubAccounts($include) |
|
109
|
|
|
{ |
|
110
|
|
|
// Make sure include is a boolean |
|
111
|
|
|
if (!is_bool($include)) { |
|
112
|
|
|
$this->throwException('Invalid value specified: ' . gettype($include) . '. Must be a boolean.'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$this->includeSubAccounts = $include; |
|
116
|
|
|
$this->addLog('debug', 'Setting include sub accounts to ' . $this->includeSubAccounts); |
|
117
|
|
|
|
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Set the product type |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $type |
|
125
|
|
|
* @throws Exception If $type isn't a string or isn't a valid product type |
|
126
|
|
|
* @return Usage |
|
127
|
|
|
*/ |
|
128
|
|
|
public function productType($type) |
|
129
|
|
|
{ |
|
130
|
|
|
$type = mb_strtolower($type); |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Make sure the product type is apm, browser or mobile |
|
134
|
|
|
* @see https://docs.newrelic.com/docs/apis/rest-api-v2/account-examples-v2/retrieving-account-usage-metrics-rest-api#product_names |
|
135
|
|
|
*/ |
|
136
|
|
|
if (!$this->checkString('product type', $type) || !in_array($type, $this->productTypes)) { |
|
137
|
|
|
$this->throwException('Invalid product type specified: ' . $type . '. Must be one of: [' . implode(', ', $this->productTypes) . '].'); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$this->productType = $type; |
|
141
|
|
|
$this->addLog('debug', 'Setting product type to ' . $this->productType); |
|
142
|
|
|
|
|
143
|
|
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Set a date to something |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $type |
|
150
|
|
|
* @param mixed $date |
|
151
|
|
|
* @throws Exception If $date isn't an integer or string |
|
152
|
|
|
* @return Usage |
|
153
|
|
|
*/ |
|
154
|
|
|
private function setDate($type, $date) |
|
155
|
|
|
{ |
|
156
|
|
|
$variable = $type . 'Date'; |
|
157
|
|
|
|
|
158
|
|
|
// Make sure date is a string or integer |
|
159
|
|
|
if (!is_string($date) && !is_integer($date)) { |
|
160
|
|
|
$this->throwException('Invalid date specified: ' . gettype($date) . '. Must be a unix timestamp or parseable string.'); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
$this->$variable = $this->getDate($date, 'Y-m-d'); |
|
164
|
|
|
$this->addLog('debug', 'Setting ' . $type . ' date to ' . $this->$variable); |
|
165
|
|
|
|
|
166
|
|
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Set the date to get data from |
|
171
|
|
|
* |
|
172
|
|
|
* @param mixed $date |
|
173
|
|
|
* @return Usage |
|
174
|
|
|
*/ |
|
175
|
|
|
public function startDate($date) |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->setDate('start', $date); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|