refactor usage endpoint
This commit is contained in:
parent
0d47dea97f
commit
e82cf42fe3
|
@ -241,30 +241,22 @@ App::get('/v1/projects/:projectId/usage')
|
||||||
throw new Exception('Project not found', 404);
|
throw new Exception('Project not found', 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$stats = [];
|
||||||
if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') {
|
if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') {
|
||||||
|
|
||||||
$period = [
|
$period = [
|
||||||
'24h' => [
|
'24h' => [
|
||||||
// 'start' => DateTime::createFromFormat('U', \strtotime('-24 hours')),
|
|
||||||
// 'end' => DateTime::createFromFormat('U', \strtotime('+1 hour')),
|
|
||||||
'period' => '30m',
|
'period' => '30m',
|
||||||
'limit' => 48,
|
'limit' => 48,
|
||||||
],
|
],
|
||||||
'7d' => [
|
'7d' => [
|
||||||
// 'start' => DateTime::createFromFormat('U', \strtotime('-7 days')),
|
|
||||||
// 'end' => DateTime::createFromFormat('U', \strtotime('now')),
|
|
||||||
'period' => '1d',
|
'period' => '1d',
|
||||||
'limit' => 7,
|
'limit' => 7,
|
||||||
],
|
],
|
||||||
'30d' => [
|
'30d' => [
|
||||||
// 'start' => DateTime::createFromFormat('U', \strtotime('-30 days')),
|
|
||||||
// 'end' => DateTime::createFromFormat('U', \strtotime('now')),
|
|
||||||
'period' => '1d',
|
'period' => '1d',
|
||||||
'limit' => 30,
|
'limit' => 30,
|
||||||
],
|
],
|
||||||
'90d' => [
|
'90d' => [
|
||||||
// 'start' => DateTime::createFromFormat('U', \strtotime('-90 days')),
|
|
||||||
// 'end' => DateTime::createFromFormat('U', \strtotime('now')),
|
|
||||||
'period' => '1d',
|
'period' => '1d',
|
||||||
'limit' => 90,
|
'limit' => 90,
|
||||||
],
|
],
|
||||||
|
@ -272,59 +264,25 @@ App::get('/v1/projects/:projectId/usage')
|
||||||
|
|
||||||
$dbForInternal->setNamespace('project_' . $projectId . '_internal');
|
$dbForInternal->setNamespace('project_' . $projectId . '_internal');
|
||||||
|
|
||||||
$requestDocs = Authorization::skip(function () use ($dbForInternal, $period, $range) {
|
Authorization::disable();
|
||||||
return $dbForInternal->find('stats', [
|
|
||||||
|
$metrics = ['requests', 'network', 'executions'];
|
||||||
|
foreach ($metrics as $metric) {
|
||||||
|
$requestDocs = $dbForInternal->find('stats', [
|
||||||
new Query('period', Query::TYPE_EQUAL, [$period[$range]['period']]),
|
new Query('period', Query::TYPE_EQUAL, [$period[$range]['period']]),
|
||||||
new Query('metric', Query::TYPE_EQUAL, ['requests']),
|
new Query('metric', Query::TYPE_EQUAL, [$metric]),
|
||||||
], $period[$range]['limit'], 0, ['time'], [Database::ORDER_DESC]);
|
], $period[$range]['limit'], 0, ['time'], [Database::ORDER_DESC]);
|
||||||
});
|
|
||||||
|
|
||||||
$requests = [];
|
$stats[$metric] = [];
|
||||||
foreach ($requestDocs as $requestDoc) {
|
foreach ($requestDocs as $requestDoc) {
|
||||||
$requests[] = [
|
$stats[$metric][] = [
|
||||||
'value' => $requestDoc->getAttribute('value'),
|
'value' => $requestDoc->getAttribute('value'),
|
||||||
'date' => $requestDoc->getAttribute('time'),
|
'date' => $requestDoc->getAttribute('time'),
|
||||||
];
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$stats[$metric] = array_reverse($stats[$metric]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$requests = array_reverse($requests);
|
|
||||||
|
|
||||||
$networkDocs = Authorization::skip(function () use ($dbForInternal, $period, $range) {
|
|
||||||
return $dbForInternal->find('stats', [
|
|
||||||
new Query('period', Query::TYPE_EQUAL, [$period[$range]['period']]),
|
|
||||||
new Query('metric', Query::TYPE_EQUAL, ['network']),
|
|
||||||
], $period[$range]['limit'], 0, ['time'], [Database::ORDER_DESC]);
|
|
||||||
});
|
|
||||||
|
|
||||||
$network = [];
|
|
||||||
foreach ($networkDocs as $networkDoc) {
|
|
||||||
$network[] = [
|
|
||||||
'value' => $networkDoc->getAttribute('value'),
|
|
||||||
'date' => $networkDoc->getAttribute('time'),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
$network = array_reverse($network);
|
|
||||||
|
|
||||||
$functionsDocs = Authorization::skip(function () use ($dbForInternal, $period, $range) {
|
|
||||||
return $dbForInternal->find('stats', [
|
|
||||||
new Query('period', Query::TYPE_EQUAL, [$period[$range]['period']]),
|
|
||||||
new Query('metric', Query::TYPE_EQUAL, ['executions']),
|
|
||||||
], $period[$range]['limit'], 0, ['time'], [Database::ORDER_DESC]);
|
|
||||||
});
|
|
||||||
|
|
||||||
$functions = [];
|
|
||||||
foreach ($functionsDocs as $functionDoc) {
|
|
||||||
$functions[] = [
|
|
||||||
'value' => $functionDoc->getAttribute('value'),
|
|
||||||
'date' => $functionDoc->getAttribute('time'),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
$functions = array_reverse($functions);
|
|
||||||
} else {
|
|
||||||
$requests = [];
|
|
||||||
$network = [];
|
|
||||||
$functions = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$usersCount = Authorization::skip(function () use ($dbForInternal) {
|
$usersCount = Authorization::skip(function () use ($dbForInternal) {
|
||||||
|
@ -336,7 +294,6 @@ App::get('/v1/projects/:projectId/usage')
|
||||||
return $dbForInternal->findOne('stats', [new Query('metric', Query::TYPE_EQUAL, ['collections.count'])], 0, ['time'], [Database::ORDER_DESC]);
|
return $dbForInternal->findOne('stats', [new Query('metric', Query::TYPE_EQUAL, ['collections.count'])], 0, ['time'], [Database::ORDER_DESC]);
|
||||||
});
|
});
|
||||||
$collectionsTotal = $collectionsCount ? $collectionsCount->getAttribute('value', 0) : 0;
|
$collectionsTotal = $collectionsCount ? $collectionsCount->getAttribute('value', 0) : 0;
|
||||||
|
|
||||||
|
|
||||||
// $documents = [];
|
// $documents = [];
|
||||||
|
|
||||||
|
@ -357,25 +314,27 @@ App::get('/v1/projects/:projectId/usage')
|
||||||
});
|
});
|
||||||
$filesTotal = $filesCount ? $filesCount->getAttribute('value', 0) : 0;
|
$filesTotal = $filesCount ? $filesCount->getAttribute('value', 0) : 0;
|
||||||
|
|
||||||
|
Authorization::reset();
|
||||||
|
|
||||||
$response->json([
|
$response->json([
|
||||||
'range' => $range,
|
'range' => $range,
|
||||||
'requests' => [
|
'requests' => [
|
||||||
'data' => $requests,
|
'data' => $stats['requests'] ?? [],
|
||||||
'total' => \array_sum(\array_map(function ($item) {
|
'total' => \array_sum(\array_map(function ($item) {
|
||||||
return $item['value'];
|
return $item['value'];
|
||||||
}, $requests)),
|
}, $stats['requests'] ?? [])),
|
||||||
],
|
],
|
||||||
'network' => [
|
'network' => [
|
||||||
'data' => \array_map(function ($value) {return ['value' => \round($value['value'] / 1000000, 2), 'date' => $value['date']];}, $network), // convert bytes to mb
|
'data' => \array_map(function ($value) {return ['value' => \round($value['value'] / 1000000, 2), 'date' => $value['date']];}, $stats['network'] ?? []), // convert bytes to mb
|
||||||
'total' => \array_sum(\array_map(function ($item) {
|
'total' => \array_sum(\array_map(function ($item) {
|
||||||
return $item['value'];
|
return $item['value'];
|
||||||
}, $network)),
|
}, $stats['network'] ?? [])),
|
||||||
],
|
],
|
||||||
'functions' => [
|
'functions' => [
|
||||||
'data' => $functions,
|
'data' => $stats['executions'] ?? [],
|
||||||
'total' => \array_sum(\array_map(function ($item) {
|
'total' => \array_sum(\array_map(function ($item) {
|
||||||
return $item['value'];
|
return $item['value'];
|
||||||
}, $functions)),
|
}, $stats['executions'] ?? [])),
|
||||||
],
|
],
|
||||||
'collections' => [
|
'collections' => [
|
||||||
'data' => [],
|
'data' => [],
|
||||||
|
|
Loading…
Reference in a new issue