-
Notifications
You must be signed in to change notification settings - Fork 30
/
developer_app_analytics.php
78 lines (71 loc) · 2.99 KB
/
developer_app_analytics.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use Apigee\Edge\Api\Management\Controller\DeveloperController;
use Apigee\Edge\Api\Management\Query\StatsQuery;
use Apigee\Edge\Exception\ApiException;
use Apigee\Edge\Exception\ApiRequestException;
use Apigee\Edge\Exception\ClientErrorException;
use Apigee\Edge\Exception\ServerErrorException;
use League\Period\Period;
require_once 'authentication.inc';
$environment = getenv('APIGEE_EDGE_PHP_CLIENT_ENVIRONMENT') ?: 'test';
$developerMail = getenv('APIGEE_EDGE_PHP_EXAMPLE_DEVELOPER_MAIL') ?: 'developer1@example.com';
$developerAppName = getenv('APIGEE_EDGE_PHP_EXAMPLE_DEVELOPER_APP_NAME') ?: 'test_app';
try {
$dc = new DeveloperController($clientFactory->getOrganization(), $clientFactory->getClient());
/** @var Apigee\Edge\Api\Management\Entity\DeveloperInterface $developer */
$developer = $dc->load($developerMail);
} catch (ClientErrorException $e) {
// HTTP code >= 400 and < 500. Ex.: 401 Unauthorised.
if ($e->getEdgeErrorCode()) {
echo $e->getEdgeErrorCode();
} else {
echo $e;
}
throw $e;
} catch (ServerErrorException $e) {
// HTTP code >= 500 and < 600. Ex.: 500 Server error.
throw $e;
} catch (ApiRequestException $e) {
// The request has failed, ex.: networking issues.
} catch (ApiException $e) {
// Anything else, because this is the parent class of all the above.
}
$sc = new Apigee\Edge\Api\Management\Controller\StatsController($environment, $clientFactory->getOrganization(), $clientFactory->getClient());
// Read more about Period library usage here: http://period.thephpleague.com/3.0
$q = new StatsQuery(['total_response_time'], new Period('now - 7 days', 'now'));
$q->setFilter("(developer_email eq '{$developer->getEmail()}' and developer_app eq '{$developerAppName}')")
->setTimeUnit('hour')
->setSortBy('total_response_time')
->setSort(StatsQuery::SORT_ASC);
try {
$result = $sc->getOptimizedMetricsByDimensions(['apps'], $q);
print_r($result);
} catch (ClientErrorException $e) {
// HTTP code >= 400 and < 500. Ex.: 401 Unauthorised.
if ($e->getEdgeErrorCode()) {
echo $e->getEdgeErrorCode();
} else {
echo $e;
}
} catch (ServerErrorException $e) {
// HTTP code >= 500 and < 600. Ex.: 500 Server error.
} catch (ApiRequestException $e) {
// The request has failed, ex.: networking issues.
} catch (ApiException $e) {
// Anything else, because this is the parent class of all the above.
}