1 <?php
2
3 /**
4 * ArangoDB PHP client: admin document handler
5 *
6 * @package triagens\ArangoDb
7 * @author Jan Steemann
8 * @author Frank Mayer
9 * @copyright Copyright 2012, triagens GmbH, Cologne, Germany
10 * @since 1.2
11 */
12
13 namespace triagens\ArangoDb;
14
15 /**
16 * Provides access to ArangoDB's administration interface
17 *
18 * The admin handler utilizes ArangoDB's Admin API.
19 *
20 * @package triagens\ArangoDb
21 * @since 1.2
22 */
23
24 class AdminHandler extends
25 Handler
26 {
27 /**
28 * details for server version
29 */
30 const OPTION_DETAILS = 'details';
31
32 /**
33 * Get the server version
34 *
35 * This will throw if the version cannot be retrieved
36 *
37 * @param bool $details - True to get a more detailed response
38 *
39 * @throws Exception
40 *
41 * @return string - a string holding the ArangoDB version
42 * @since 1.2
43 */
44 public function getServerVersion($details = false)
45 {
46 $url = Urls::URL_ADMIN_VERSION;
47
48 if ($details) {
49 $url = UrlHelper::appendParamsUrl($url, ['details' => true]);
50 }
51
52 $response = $this->getConnection()->get($url);
53 $data = $response->getJson();
54
55 if ($details) {
56 return $data;
57 } else {
58 return $data['version'];
59 }
60 }
61
62 /**
63 * Get the server role
64 *
65 * This will throw if the role cannot be retrieved
66 *
67 * @throws Exception
68 *
69 * @return string - a string holding the server role (e.g. UNDEFINED, COORDINATOR, DBSERVER)
70 * @since 2.0
71 */
72 public function getServerRole()
73 {
74 $url = Urls::URL_ADMIN_SERVER_ROLE;
75 $response = $this->getConnection()->get($url);
76 $data = $response->getJson();
77
78 return $data['role'];
79 }
80
81
82 /**
83 * Get the server time
84 *
85 * This will throw if the time cannot be retrieved
86 *
87 * @throws Exception
88 *
89 * @return double - a double holding the timestamp
90 * @since 1.2
91 */
92 public function getServerTime()
93 {
94 $response = $this->getConnection()->get(Urls::URL_ADMIN_TIME);
95 $data = $response->getJson();
96
97 return $data['time'];
98 }
99
100
101 /**
102 * Get the server log
103 *
104 * This will throw if the log cannot be retrieved
105 *
106 * @throws Exception
107 *
108 * @param array $options - an array of options that define the result-set:
109 *
110 * <p>Options are :<br>
111 * <li>'upto' - returns all log entries up to a log-level. Note that log-level must be one of:</li>
112 * <p>
113 * <li>fatal / 0</li>
114 * <li>error / 1</li>
115 * <li>warning / 2</li>
116 * <li>info / 3</li>
117 * <li>debug / 4</li>
118 * </p>
119 * <li>'level' - limits the log entries to the ones defined in level. Note that `level` and `upto` are mutably exclusive.</li>
120 * <li>'offset' - skip the first offset entries.</li>
121 * <li>'size' - limit the number of returned log-entries to size.</li>
122 * <li>'start' - Returns all log entries such that their log-entry identifier is greater or equal to lid.</li>
123 * <li>'sort' - Sort the log-entries either ascending if direction is asc, or descending if it is desc according to their lid. Note that the lid imposes a chronological order.</li>
124 * <li>'search' - Only return the log-entries containing the text string...</li>
125 * </p>
126 *
127 * @return array - an array holding the various attributes of a log: lid, level, timestamp, text and the total amount of log entries before pagination.
128 * @since 1.2
129 */
130 public function getServerLog(array $options = [])
131 {
132 $url = UrlHelper::appendParamsUrl(Urls::URL_ADMIN_LOG, $options);
133 $response = $this->getConnection()->get($url);
134
135 return $response->getJson();
136 }
137
138
139 /**
140 * Reload the server's routing information
141 * The call triggers a reload of the routing information from the _routing collection
142 *
143 * This will throw if the routing cannot be reloaded
144 *
145 * @throws Exception
146 *
147 * @return bool
148 * @since 1.2
149 */
150 public function reloadServerRouting()
151 {
152 $this->getConnection()->post(Urls::URL_ADMIN_ROUTING_RELOAD, '');
153
154 return true;
155 }
156
157
158 /**
159 * Get the server statistics
160 * Returns the statistics information. The returned objects contains the statistics figures, grouped together
161 * according to the description returned by _admin/statistics-description.
162 * For instance, to access a figure userTime from the group system, you first select the sub-object
163 * describing the group stored in system and in that sub-object the value for userTime is stored in the
164 * attribute of the same name.In case of a distribution, the returned object contains the total count in count
165 * and the distribution list in counts.
166 * For more information on the statistics returned, please lookup the statistics interface description at
167 *
168 * @link https://docs.arangodb.com/HTTP/AdministrationAndMonitoring/index.html
169 *
170 * This will throw if the statistics cannot be retrieved
171 *
172 * @throws Exception
173 *
174 * @return array
175 *
176 * @see getServerStatisticsDescription()
177 *
178 * @since 1.3
179 */
180 public function getServerStatistics()
181 {
182 $url = UrlHelper::appendParamsUrl(Urls::URL_ADMIN_STATISTICS, []);
183 $response = $this->getConnection()->get($url);
184
185 return $response->getJson();
186 }
187
188
189 /**
190 * Returns a description of the statistics returned by getServerStatistics().
191 * The returned objects contains a list of statistics groups in the attribute groups
192 * and a list of statistics figures in the attribute figures.
193 * For more information on the statistics returned, please lookup the statistics interface description at
194 *
195 * @link https://docs.arangodb.com/HTTP/AdministrationAndMonitoring/index.html
196 *
197 * This will throw if the statistics-description cannot be retrieved
198 *
199 * @throws Exception
200 *
201 * @param array $options - an array of options that define the result-set:
202 *
203 * <p>Options are :<br>
204 * <li>'granularity' - use minutes for a granularity of minutes, hours for hours, and days for days. The default is minutes.</li>
205 * <li>'figures' - a list of figures, comma-separated. Possible figures are httpConnections. You can use all to get all figures. The default is httpConnections.</li>
206 * <li>'length' - If you want a time series, the maximal length of the series as integer. You can use all to get all available information. You can use current to get the latest interval.</li>
207 *
208 * @return array
209 *
210 * @see getServerStatistics()
211 *
212 * @since 1.3
213 */
214 public function getServerStatisticsDescription(array $options = [])
215 {
216 $url = UrlHelper::appendParamsUrl(Urls::URL_ADMIN_STATISTICS_DESCRIPTION, $options);
217 $response = $this->getConnection()->get($url);
218
219 return $response->getJson();
220 }
221 }
222