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\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')
|
2023-08-05 07:15:32 +12:00
|
|
|
->groups(['api', 'usage'])
|
2022-11-15 07:22:04 +13:00
|
|
|
->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 = [
|
2023-02-06 09:07:46 +13:00
|
|
|
METRIC_NETWORK_REQUESTS,
|
|
|
|
METRIC_NETWORK_INBOUND,
|
|
|
|
METRIC_NETWORK_OUTBOUND,
|
|
|
|
METRIC_EXECUTIONS,
|
|
|
|
METRIC_DOCUMENTS,
|
|
|
|
METRIC_DATABASES,
|
|
|
|
METRIC_USERS,
|
|
|
|
METRIC_BUCKETS,
|
2023-08-24 05:12:43 +12:00
|
|
|
METRIC_FILES_STORAGE,
|
2022-12-11 22:00:00 +13:00
|
|
|
];
|
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'),
|
|
|
|
]);
|
2023-02-06 09:07:46 +13:00
|
|
|
|
2022-12-11 22:00:00 +13:00
|
|
|
$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
|
|
|
|
2023-08-24 05:12:43 +12: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,
|
|
|
|
];
|
|
|
|
}
|
2022-12-11 22:00:00 +13:00
|
|
|
}
|
2023-02-06 09:07:46 +13:00
|
|
|
|
2022-12-11 22:00:00 +13:00
|
|
|
$response->dynamic(new Document([
|
|
|
|
'range' => $range,
|
2023-02-06 09:07:46 +13:00
|
|
|
'requestsTotal' => ($usage[$metrics[0]]),
|
2022-12-12 20:54:53 +13:00
|
|
|
'network' => ($usage[$metrics[1]] + $usage[$metrics[2]]),
|
2023-02-06 09:07:46 +13:00
|
|
|
'executionsTotal' => $usage[$metrics[3]],
|
|
|
|
'documentsTotal' => $usage[$metrics[4]],
|
|
|
|
'databasesTotal' => $usage[$metrics[5]],
|
|
|
|
'usersTotal' => $usage[$metrics[6]],
|
|
|
|
'bucketsTotal' => $usage[$metrics[7]],
|
|
|
|
'filesStorage' => $usage[$metrics[8]],
|
2022-12-11 22:00:00 +13:00
|
|
|
]), Response::MODEL_USAGE_PROJECT);
|
2022-11-15 07:47:44 +13:00
|
|
|
});
|