1
0
Fork 0
mirror of synced 2024-07-09 00:16:13 +12:00

feat: project usage custom date range

This commit is contained in:
Torsten Dittmann 2023-12-10 10:26:33 +01:00
parent 3b7fa3d664
commit bb934080ba

View file

@ -6,6 +6,7 @@ use Utopia\App;
use Utopia\Config\Config; use Utopia\Config\Config;
use Utopia\Database\Database; use Utopia\Database\Database;
use Utopia\Database\Document; use Utopia\Database\Document;
use Utopia\Database\Validator\Datetime as DateTimeValidator;
use Utopia\Database\Exception\Duplicate as DuplicateException; use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Permission;
@ -26,14 +27,16 @@ App::get('/v1/project/usage')
->label('sdk.response.code', Response::STATUS_CODE_OK) ->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON) ->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_USAGE_PROJECT) ->label('sdk.response.model', Response::MODEL_USAGE_PROJECT)
->param('range', '30d', new WhiteList(['24h', '30d', '90d'], true), 'Date range.', true) ->param('startDate', '', new DateTimeValidator(), 'Starting date for the usage')
->param('endDate', '', new DateTimeValidator(), 'End date for the usage')
->param('period', '1d', new WhiteList(['1h', '1d']), 'Period used', true)
->inject('response') ->inject('response')
->inject('dbForProject') ->inject('dbForProject')
->action(function (string $range, Response $response, Database $dbForProject) { ->action(function (string $startDate, string $endDate, string $period, Response $response, Database $dbForProject) {
$periods = Config::getParam('usage', []);
$stats = $total = $usage = []; $stats = $total = $usage = [];
$days = $periods[$range]; $format = 'Y-m-d 00:00:00';
$firstDay = (new DateTime($startDate))->format($format);
$lastDay = (new DateTime($endDate))->format($format);
$metrics = [ $metrics = [
'total' => [ 'total' => [
@ -51,7 +54,22 @@ App::get('/v1/project/usage')
] ]
]; ];
Authorization::skip(function () use ($dbForProject, $days, $metrics, &$total, &$stats) { $factor = match ($period) {
'1h' => 3600,
'1d' => 86400,
};
$limit = match ($period) {
'1h' => (new DateTime($endDate))->diff(new DateTime($startDate))->h,
'1d' => (new DateTime($endDate))->diff(new DateTime($startDate))->days
};
$format = match ($period) {
'1h' => 'Y-m-d\TH:00:00.000P',
'1d' => 'Y-m-d\T00:00:00.000P',
};
Authorization::skip(function () use ($dbForProject, $firstDay, $lastDay, $period, $metrics, &$total, &$stats) {
foreach ($metrics['total'] as $metric) { foreach ($metrics['total'] as $metric) {
$result = $dbForProject->findOne('stats', [ $result = $dbForProject->findOne('stats', [
Query::equal('metric', [$metric]), Query::equal('metric', [$metric]),
@ -61,12 +79,11 @@ App::get('/v1/project/usage')
} }
foreach ($metrics['period'] as $metric) { foreach ($metrics['period'] as $metric) {
$limit = $days['limit'];
$period = $days['period'];
$results = $dbForProject->find('stats', [ $results = $dbForProject->find('stats', [
Query::equal('metric', [$metric]), Query::equal('metric', [$metric]),
Query::equal('period', [$period]), Query::equal('period', [$period]),
Query::limit($limit), Query::greaterThanEqual('time', $firstDay),
Query::lessThan('time', $lastDay),
Query::orderDesc('time'), Query::orderDesc('time'),
]); ]);
@ -79,16 +96,11 @@ App::get('/v1/project/usage')
} }
}); });
$format = match ($days['period']) {
'1h' => 'Y-m-d\TH:00:00.000P',
'1d' => 'Y-m-d\T00:00:00.000P',
};
foreach ($metrics['period'] as $metric) { foreach ($metrics['period'] as $metric) {
$usage[$metric] = []; $usage[$metric] = [];
$leap = time() - ($days['limit'] * $days['factor']); $leap = time() - ($limit * $factor);
while ($leap < time()) { while ($leap < time()) {
$leap += $days['factor']; $leap += $factor;
$formatDate = date($format, $leap); $formatDate = date($format, $leap);
$usage[$metric][] = [ $usage[$metric][] = [
'value' => $stats[$metric][$formatDate]['value'] ?? 0, 'value' => $stats[$metric][$formatDate]['value'] ?? 0,
@ -96,8 +108,8 @@ App::get('/v1/project/usage')
]; ];
} }
} }
$response->dynamic(new Document([ $response->dynamic(new Document([
'range' => $range,
'requests' => ($usage[METRIC_NETWORK_REQUESTS]), 'requests' => ($usage[METRIC_NETWORK_REQUESTS]),
'network' => ($usage[METRIC_NETWORK_INBOUND] + $usage[METRIC_NETWORK_OUTBOUND]), 'network' => ($usage[METRIC_NETWORK_INBOUND] + $usage[METRIC_NETWORK_OUTBOUND]),
'executionsTotal' => $total[METRIC_EXECUTIONS], 'executionsTotal' => $total[METRIC_EXECUTIONS],
@ -111,7 +123,6 @@ App::get('/v1/project/usage')
// Variables // Variables
App::post('/v1/project/variables') App::post('/v1/project/variables')
->desc('Create Variable') ->desc('Create Variable')
->groups(['api']) ->groups(['api'])