2022-11-15 07:22:04 +13:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Appwrite\Utopia\Response;
|
|
|
|
use Utopia\App;
|
2022-12-11 22:00:00 +13:00
|
|
|
use Utopia\Config\Config;
|
2022-11-15 07:22:04 +13:00
|
|
|
use Utopia\Database\Database;
|
|
|
|
use Utopia\Database\DateTime;
|
|
|
|
use Utopia\Database\Document;
|
|
|
|
use Utopia\Database\Query;
|
|
|
|
use Utopia\Database\Validator\Authorization;
|
|
|
|
use Utopia\Validator\WhiteList;
|
|
|
|
|
|
|
|
App::get('/v1/project/usage')
|
|
|
|
->desc('Get usage stats for a project')
|
|
|
|
->groups(['api'])
|
|
|
|
->label('scope', 'projects.read')
|
|
|
|
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN])
|
|
|
|
->label('sdk.namespace', 'project')
|
|
|
|
->label('sdk.method', 'getUsage')
|
|
|
|
->label('sdk.response.code', Response::STATUS_CODE_OK)
|
|
|
|
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
|
|
|
->label('sdk.response.model', Response::MODEL_USAGE_PROJECT)
|
|
|
|
->param('range', '30d', new WhiteList(['24h', '7d', '30d', '90d'], true), 'Date range.', true)
|
|
|
|
->inject('response')
|
|
|
|
->inject('dbForProject')
|
|
|
|
->action(function (string $range, Response $response, Database $dbForProject) {
|
|
|
|
|
2022-12-11 22:00:00 +13:00
|
|
|
$periods = Config::getParam('usage', []);
|
|
|
|
$stats = $usage = [];
|
|
|
|
$days = $periods[$range];
|
|
|
|
$metrics = [
|
|
|
|
'network.inbound',
|
|
|
|
'network.outbound',
|
|
|
|
'executions',
|
|
|
|
'documents',
|
|
|
|
'databases',
|
|
|
|
'users',
|
|
|
|
'files.storage',
|
|
|
|
'buckets',
|
|
|
|
];
|
2022-11-15 07:22:04 +13:00
|
|
|
|
2022-12-11 22:00:00 +13:00
|
|
|
Authorization::skip(function () use ($dbForProject, $days, $metrics, &$stats) {
|
|
|
|
foreach ($metrics as $metric) {
|
|
|
|
$limit = $days['limit'];
|
|
|
|
$period = $days['period'];
|
|
|
|
$results = $dbForProject->find('stats', [
|
|
|
|
Query::equal('period', [$period]),
|
|
|
|
Query::equal('metric', [$metric]),
|
|
|
|
Query::limit($limit),
|
|
|
|
Query::orderDesc('time'),
|
|
|
|
]);
|
|
|
|
$stats[$metric] = [];
|
|
|
|
foreach ($results as $result) {
|
|
|
|
$stats[$metric][$result->getAttribute('time')] = [
|
|
|
|
'value' => $result->getAttribute('value'),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-11-15 07:22:04 +13:00
|
|
|
|
2022-12-11 22:00:00 +13:00
|
|
|
$format = match ($days['period']) {
|
|
|
|
'1h' => 'Y-m-d\TH:00:00.000P',
|
|
|
|
'1d' => 'Y-m-d\T00:00:00.000P',
|
|
|
|
};
|
2022-11-15 07:22:04 +13:00
|
|
|
|
2022-12-11 22:00:00 +13:00
|
|
|
foreach ($metrics as $metric) {
|
|
|
|
$usage[$metric] = [];
|
|
|
|
$leap = time() - ($days['limit'] * $days['factor']);
|
|
|
|
while ($leap < time()) {
|
|
|
|
$leap += $days['factor'];
|
|
|
|
$formatDate = date($format, $leap);
|
|
|
|
$usage[$metric][] = [
|
|
|
|
'value' => $stats[$metric][$formatDate]['value'] ?? 0,
|
|
|
|
'date' => $formatDate,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
$usage[$metric] = array_reverse($usage[$metric]);
|
|
|
|
}
|
2022-11-15 07:22:04 +13:00
|
|
|
|
2022-12-11 22:00:00 +13:00
|
|
|
$response->dynamic(new Document([
|
|
|
|
'range' => $range,
|
|
|
|
'network' => ($usage[$metrics[0]] + $usage[$metrics[1]]),
|
|
|
|
'executions' => $usage[$metrics[2]],
|
|
|
|
'documents' => $usage[$metrics[3]],
|
|
|
|
'databases' => $usage[$metrics[4]],
|
|
|
|
'users' => $usage[$metrics[5]],
|
|
|
|
'storage' => $usage[$metrics[6]],
|
|
|
|
'buckets' => $usage[$metrics[8]],
|
|
|
|
]), Response::MODEL_USAGE_PROJECT);
|
2022-11-15 07:47:44 +13:00
|
|
|
});
|