project.php
This commit is contained in:
parent
ad63a377ab
commit
d54615d376
2 changed files with 54 additions and 41 deletions
|
@ -32,22 +32,35 @@ App::get('/v1/project/usage')
|
||||||
->action(function (string $range, Response $response, Database $dbForProject) {
|
->action(function (string $range, Response $response, Database $dbForProject) {
|
||||||
|
|
||||||
$periods = Config::getParam('usage', []);
|
$periods = Config::getParam('usage', []);
|
||||||
$stats = $usage = [];
|
$stats = $total = $usage = [];
|
||||||
$days = $periods[$range];
|
$days = $periods[$range];
|
||||||
|
|
||||||
$metrics = [
|
$metrics = [
|
||||||
METRIC_NETWORK_REQUESTS,
|
'total' => [
|
||||||
METRIC_NETWORK_INBOUND,
|
|
||||||
METRIC_NETWORK_OUTBOUND,
|
|
||||||
METRIC_EXECUTIONS,
|
METRIC_EXECUTIONS,
|
||||||
METRIC_DOCUMENTS,
|
METRIC_DOCUMENTS,
|
||||||
METRIC_DATABASES,
|
METRIC_DATABASES,
|
||||||
METRIC_USERS,
|
METRIC_USERS,
|
||||||
METRIC_BUCKETS,
|
METRIC_BUCKETS,
|
||||||
METRIC_FILES_STORAGE
|
METRIC_FILES_STORAGE
|
||||||
|
],
|
||||||
|
'period' => [
|
||||||
|
METRIC_NETWORK_REQUESTS,
|
||||||
|
METRIC_NETWORK_INBOUND,
|
||||||
|
METRIC_NETWORK_OUTBOUND,
|
||||||
|
]
|
||||||
];
|
];
|
||||||
|
|
||||||
Authorization::skip(function () use ($dbForProject, $days, $metrics, &$stats) {
|
Authorization::skip(function () use ($dbForProject, $days, $metrics, &$total, &$stats) {
|
||||||
foreach ($metrics as $metric) {
|
foreach ($metrics['total'] as $metric) {
|
||||||
|
$result = $dbForProject->findOne('stats', [
|
||||||
|
Query::equal('metric', [$metric]),
|
||||||
|
Query::equal('period', ['inf'])
|
||||||
|
]);
|
||||||
|
$total[$metric] = $result['value'] ?? 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($metrics['period'] as $metric) {
|
||||||
$limit = $days['limit'];
|
$limit = $days['limit'];
|
||||||
$period = $days['period'];
|
$period = $days['period'];
|
||||||
$results = $dbForProject->find('stats', [
|
$results = $dbForProject->find('stats', [
|
||||||
|
@ -66,13 +79,12 @@ App::get('/v1/project/usage')
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
$format = match ($days['period']) {
|
$format = match ($days['period']) {
|
||||||
'1h' => 'Y-m-d\TH:00:00.000P',
|
'1h' => 'Y-m-d\TH:00:00.000P',
|
||||||
'1d' => 'Y-m-d\T00:00:00.000P',
|
'1d' => 'Y-m-d\T00:00:00.000P',
|
||||||
};
|
};
|
||||||
|
|
||||||
foreach ($metrics as $metric) {
|
foreach ($metrics['period'] as $metric) {
|
||||||
$usage[$metric] = [];
|
$usage[$metric] = [];
|
||||||
$leap = time() - ($days['limit'] * $days['factor']);
|
$leap = time() - ($days['limit'] * $days['factor']);
|
||||||
while ($leap < time()) {
|
while ($leap < time()) {
|
||||||
|
@ -84,17 +96,16 @@ App::get('/v1/project/usage')
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$response->dynamic(new Document([
|
$response->dynamic(new Document([
|
||||||
'range' => $range,
|
'range' => $range,
|
||||||
'requestsTotal' => ($usage[$metrics[0]]),
|
'requests' => ($usage[METRIC_NETWORK_REQUESTS]),
|
||||||
'networkTotal' => ($usage[$metrics[1]] + $usage[$metrics[2]]),
|
'network' => ($usage[METRIC_NETWORK_INBOUND] + $usage[METRIC_NETWORK_OUTBOUND]),
|
||||||
'executionsTotal' => $usage[$metrics[3]],
|
'executionsTotal' => $total[METRIC_EXECUTIONS],
|
||||||
'documentsTotal' => $usage[$metrics[4]],
|
'documentsTotal' => $total[METRIC_DOCUMENTS],
|
||||||
'databasesTotal' => $usage[$metrics[5]],
|
'databasesTotal' => $total[METRIC_DATABASES],
|
||||||
'usersTotal' => $usage[$metrics[6]],
|
'usersTotal' => $total[METRIC_USERS],
|
||||||
'bucketsTotal' => $usage[$metrics[7]],
|
'bucketsTotal' => $total[METRIC_BUCKETS],
|
||||||
'filesStorageTotal' => $usage[$metrics[8]],
|
'filesStorageTotal' => $total[METRIC_FILES_STORAGE],
|
||||||
]), Response::MODEL_USAGE_PROJECT);
|
]), Response::MODEL_USAGE_PROJECT);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -16,54 +16,56 @@ class UsageProject extends Model
|
||||||
'default' => '',
|
'default' => '',
|
||||||
'example' => '30d',
|
'example' => '30d',
|
||||||
])
|
])
|
||||||
->addRule('requestsTotal', [
|
|
||||||
'type' => self::TYPE_INTEGER,
|
|
||||||
'description' => 'Aggregated stats for number of requests.',
|
|
||||||
'default' => 0,
|
|
||||||
'example' => 0,
|
|
||||||
])
|
|
||||||
->addRule('networkTotal', [
|
|
||||||
'type' => self::TYPE_INTEGER,
|
|
||||||
'description' => 'Aggregated stats for consumed bandwidth.',
|
|
||||||
'default' => 0,
|
|
||||||
'example' => 0,
|
|
||||||
])
|
|
||||||
->addRule('executionsTotal', [
|
->addRule('executionsTotal', [
|
||||||
'type' => self::TYPE_INTEGER,
|
'type' => self::TYPE_INTEGER,
|
||||||
'description' => 'Aggregated stats for function executions.',
|
'description' => 'Aggregated statistics of total function executions.',
|
||||||
'default' => 0,
|
'default' => 0,
|
||||||
'example' => 0,
|
'example' => 0,
|
||||||
])
|
])
|
||||||
->addRule('documentsTotal', [
|
->addRule('documentsTotal', [
|
||||||
'type' => self::TYPE_INTEGER,
|
'type' => self::TYPE_INTEGER,
|
||||||
'description' => 'Aggregated stats for number of documents.',
|
'description' => 'Aggregated statistics of total number of documents.',
|
||||||
'default' => 0,
|
'default' => 0,
|
||||||
'example' => 0,
|
'example' => 0,
|
||||||
])
|
])
|
||||||
->addRule('databasesTotal', [
|
->addRule('databasesTotal', [
|
||||||
'type' => self::TYPE_INTEGER,
|
'type' => self::TYPE_INTEGER,
|
||||||
'description' => 'Aggregated stats for number of databases.',
|
'description' => 'Aggregated statistics of total number of databases.',
|
||||||
'default' => 0,
|
'default' => 0,
|
||||||
'example' => 0,
|
'example' => 0,
|
||||||
])
|
])
|
||||||
->addRule('usersTotal', [
|
->addRule('usersTotal', [
|
||||||
'type' => self::TYPE_INTEGER,
|
'type' => self::TYPE_INTEGER,
|
||||||
'description' => 'Aggregated stats for number of users.',
|
'description' => 'Aggregated statistics of total number of users.',
|
||||||
'default' => 0,
|
'default' => 0,
|
||||||
'example' => 0,
|
'example' => 0,
|
||||||
])
|
])
|
||||||
->addRule('filesStorageTotal', [
|
->addRule('filesStorageTotal', [
|
||||||
'type' => self::TYPE_INTEGER,
|
'type' => self::TYPE_INTEGER,
|
||||||
'description' => 'Aggregated stats for the occupied storage size (in bytes).',
|
'description' => 'Aggregated statistics of total occupied storage size (in bytes).',
|
||||||
'default' => 0,
|
'default' => 0,
|
||||||
'example' => 0,
|
'example' => 0,
|
||||||
])
|
])
|
||||||
->addRule('bucketsTotal', [
|
->addRule('bucketsTotal', [
|
||||||
'type' => self::TYPE_INTEGER,
|
'type' => self::TYPE_INTEGER,
|
||||||
'description' => 'Aggregated stats for number of buckets.',
|
'description' => 'Aggregated statistics of total number of buckets.',
|
||||||
'default' => 0,
|
'default' => 0,
|
||||||
'example' => 0,
|
'example' => 0,
|
||||||
])
|
])
|
||||||
|
->addRule('requests', [
|
||||||
|
'type' => self::TYPE_INTEGER,
|
||||||
|
'description' => 'Aggregated statistics of number of requests per period.',
|
||||||
|
'default' => [],
|
||||||
|
'example' => [],
|
||||||
|
'array' => true
|
||||||
|
])
|
||||||
|
->addRule('network', [
|
||||||
|
'type' => self::TYPE_INTEGER,
|
||||||
|
'description' => 'Aggregated statistics of consumed bandwidth per period.',
|
||||||
|
'default' => [],
|
||||||
|
'example' => [],
|
||||||
|
'array' => true
|
||||||
|
])
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue