2022-11-15 07:22:04 +13:00
|
|
|
<?php
|
|
|
|
|
2023-03-12 05:06:02 +13:00
|
|
|
use Appwrite\Extend\Exception;
|
2022-11-15 07:22:04 +13:00
|
|
|
use Appwrite\Utopia\Response;
|
|
|
|
use Utopia\App;
|
2023-10-03 03:02:48 +13:00
|
|
|
use Utopia\Config\Config;
|
2022-11-15 07:22:04 +13:00
|
|
|
use Utopia\Database\Database;
|
|
|
|
use Utopia\Database\Document;
|
2023-03-12 05:06:02 +13:00
|
|
|
use Utopia\Database\Exception\Duplicate as DuplicateException;
|
2023-08-21 19:34:34 +12:00
|
|
|
use Utopia\Database\Helpers\ID;
|
|
|
|
use Utopia\Database\Helpers\Permission;
|
|
|
|
use Utopia\Database\Helpers\Role;
|
2022-11-15 07:22:04 +13:00
|
|
|
use Utopia\Database\Query;
|
|
|
|
use Utopia\Database\Validator\Authorization;
|
2023-03-12 05:06:02 +13:00
|
|
|
use Utopia\Database\Validator\UID;
|
|
|
|
use Utopia\Validator\Text;
|
2022-11-15 07:22:04 +13:00
|
|
|
use Utopia\Validator\WhiteList;
|
|
|
|
|
|
|
|
App::get('/v1/project/usage')
|
|
|
|
->desc('Get usage stats for a project')
|
2023-10-02 07:30:52 +13: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) {
|
2023-03-12 05:06:02 +13:00
|
|
|
|
2023-10-02 07:30:52 +13:00
|
|
|
$periods = Config::getParam('usage', []);
|
|
|
|
$stats = $usage = [];
|
|
|
|
$days = $periods[$range];
|
|
|
|
$metrics = [
|
|
|
|
METRIC_NETWORK_REQUESTS,
|
|
|
|
METRIC_NETWORK_INBOUND,
|
|
|
|
METRIC_NETWORK_OUTBOUND,
|
|
|
|
METRIC_EXECUTIONS,
|
|
|
|
METRIC_DOCUMENTS,
|
|
|
|
METRIC_DATABASES,
|
|
|
|
METRIC_USERS,
|
|
|
|
METRIC_BUCKETS,
|
|
|
|
METRIC_FILES_STORAGE
|
|
|
|
];
|
2023-08-21 00:29:43 +12:00
|
|
|
|
2023-10-02 07:30:52 +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-08-21 00:29:43 +12:00
|
|
|
|
2023-10-02 07:30:52 +13:00
|
|
|
$stats[$metric] = [];
|
|
|
|
foreach ($results as $result) {
|
|
|
|
$stats[$metric][$result->getAttribute('time')] = [
|
|
|
|
'value' => $result->getAttribute('value'),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2023-08-21 00:29:43 +12:00
|
|
|
|
|
|
|
|
2023-10-02 07:30:52 +13:00
|
|
|
$format = match ($days['period']) {
|
|
|
|
'1h' => 'Y-m-d\TH:00:00.000P',
|
|
|
|
'1d' => 'Y-m-d\T00:00:00.000P',
|
|
|
|
};
|
2023-08-21 00:29:43 +12:00
|
|
|
|
2023-10-03 03:02:48 +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,
|
|
|
|
];
|
2023-03-12 05:06:02 +13:00
|
|
|
}
|
2023-10-03 03:02:48 +13:00
|
|
|
}
|
2023-03-12 05:06:02 +13:00
|
|
|
|
2023-10-02 07:30:52 +13:00
|
|
|
|
|
|
|
$response->dynamic(new Document([
|
|
|
|
'range' => $range,
|
|
|
|
'requestsTotal' => ($usage[$metrics[0]]),
|
|
|
|
'network' => ($usage[$metrics[1]] + $usage[$metrics[2]]),
|
|
|
|
'executionsTotal' => $usage[$metrics[3]],
|
|
|
|
'documentsTotal' => $usage[$metrics[4]],
|
|
|
|
'databasesTotal' => $usage[$metrics[5]],
|
|
|
|
'usersTotal' => $usage[$metrics[6]],
|
|
|
|
'bucketsTotal' => $usage[$metrics[7]],
|
|
|
|
'filesStorage' => $usage[$metrics[8]],
|
|
|
|
]), Response::MODEL_USAGE_PROJECT);
|
2023-03-12 05:06:02 +13:00
|
|
|
});
|
|
|
|
|
2023-10-02 07:30:52 +13:00
|
|
|
|
2023-08-21 19:34:34 +12:00
|
|
|
// Variables
|
|
|
|
|
|
|
|
App::post('/v1/project/variables')
|
|
|
|
->desc('Create Variable')
|
|
|
|
->groups(['api'])
|
|
|
|
->label('scope', 'projects.write')
|
|
|
|
->label('audits.event', 'variable.create')
|
2023-08-30 23:28:43 +12:00
|
|
|
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN])
|
2023-08-21 19:34:34 +12:00
|
|
|
->label('sdk.namespace', 'project')
|
|
|
|
->label('sdk.method', 'createVariable')
|
|
|
|
->label('sdk.description', '/docs/references/project/create-variable.md')
|
|
|
|
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
|
|
|
|
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
|
|
|
->label('sdk.response.model', Response::MODEL_VARIABLE)
|
|
|
|
->param('key', null, new Text(Database::LENGTH_KEY), 'Variable key. Max length: ' . Database::LENGTH_KEY . ' chars.', false)
|
|
|
|
->param('value', null, new Text(8192, 0), 'Variable value. Max length: 8192 chars.', false)
|
|
|
|
->inject('project')
|
|
|
|
->inject('response')
|
|
|
|
->inject('dbForProject')
|
|
|
|
->inject('dbForConsole')
|
|
|
|
->action(function (string $key, string $value, Document $project, Response $response, Database $dbForProject, Database $dbForConsole) {
|
|
|
|
$variableId = ID::unique();
|
|
|
|
|
|
|
|
$variable = new Document([
|
|
|
|
'$id' => $variableId,
|
|
|
|
'$permissions' => [
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
],
|
|
|
|
'resourceInternalId' => '',
|
|
|
|
'resourceId' => '',
|
|
|
|
'resourceType' => 'project',
|
|
|
|
'key' => $key,
|
|
|
|
'value' => $value,
|
|
|
|
'search' => implode(' ', [$variableId, $key, 'project']),
|
|
|
|
]);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$variable = $dbForProject->createDocument('variables', $variable);
|
|
|
|
} catch (DuplicateException $th) {
|
|
|
|
throw new Exception(Exception::VARIABLE_ALREADY_EXISTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
$functions = $dbForProject->find('functions', [
|
|
|
|
Query::limit(APP_LIMIT_SUBQUERY)
|
|
|
|
]);
|
|
|
|
|
|
|
|
foreach ($functions as $function) {
|
|
|
|
$dbForProject->updateDocument('functions', $function->getId(), $function->setAttribute('live', false));
|
|
|
|
}
|
|
|
|
|
|
|
|
$response
|
|
|
|
->setStatusCode(Response::STATUS_CODE_CREATED)
|
|
|
|
->dynamic($variable, Response::MODEL_VARIABLE);
|
|
|
|
});
|
|
|
|
|
|
|
|
App::get('/v1/project/variables')
|
|
|
|
->desc('List Variables')
|
|
|
|
->groups(['api'])
|
|
|
|
->label('scope', 'projects.read')
|
2023-08-30 23:28:43 +12:00
|
|
|
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN])
|
2023-08-21 19:34:34 +12:00
|
|
|
->label('sdk.namespace', 'project')
|
|
|
|
->label('sdk.method', 'listVariables')
|
|
|
|
->label('sdk.description', '/docs/references/project/list-variables.md')
|
|
|
|
->label('sdk.response.code', Response::STATUS_CODE_OK)
|
|
|
|
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
|
|
|
->label('sdk.response.model', Response::MODEL_VARIABLE_LIST)
|
|
|
|
->inject('response')
|
|
|
|
->inject('dbForProject')
|
|
|
|
->action(function (Response $response, Database $dbForProject) {
|
|
|
|
$variables = $dbForProject->find('variables', [
|
|
|
|
Query::equal('resourceType', ['project']),
|
|
|
|
Query::limit(APP_LIMIT_SUBQUERY)
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response->dynamic(new Document([
|
|
|
|
'variables' => $variables,
|
|
|
|
'total' => \count($variables),
|
|
|
|
]), Response::MODEL_VARIABLE_LIST);
|
|
|
|
});
|
|
|
|
|
|
|
|
App::get('/v1/project/variables/:variableId')
|
|
|
|
->desc('Get Variable')
|
|
|
|
->groups(['api'])
|
|
|
|
->label('scope', 'projects.read')
|
2023-08-30 23:28:43 +12:00
|
|
|
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN])
|
2023-08-21 19:34:34 +12:00
|
|
|
->label('sdk.namespace', 'project')
|
|
|
|
->label('sdk.method', 'getVariable')
|
|
|
|
->label('sdk.description', '/docs/references/project/get-variable.md')
|
|
|
|
->label('sdk.response.code', Response::STATUS_CODE_OK)
|
|
|
|
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
|
|
|
->label('sdk.response.model', Response::MODEL_VARIABLE)
|
|
|
|
->param('variableId', '', new UID(), 'Variable unique ID.', false)
|
|
|
|
->inject('response')
|
|
|
|
->inject('project')
|
|
|
|
->inject('dbForProject')
|
|
|
|
->action(function (string $variableId, Response $response, Document $project, Database $dbForProject) {
|
|
|
|
$variable = $dbForProject->getDocument('variables', $variableId);
|
|
|
|
if ($variable === false || $variable->isEmpty() || $variable->getAttribute('resourceType') !== 'project') {
|
|
|
|
throw new Exception(Exception::VARIABLE_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->dynamic($variable, Response::MODEL_VARIABLE);
|
|
|
|
});
|
|
|
|
|
2023-03-12 05:06:02 +13:00
|
|
|
App::put('/v1/project/variables/:variableId')
|
|
|
|
->desc('Update Variable')
|
|
|
|
->groups(['api'])
|
|
|
|
->label('scope', 'projects.write')
|
2023-08-30 23:28:43 +12:00
|
|
|
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN])
|
2023-03-12 05:06:02 +13:00
|
|
|
->label('sdk.namespace', 'project')
|
|
|
|
->label('sdk.method', 'updateVariable')
|
|
|
|
->label('sdk.description', '/docs/references/project/update-variable.md')
|
|
|
|
->label('sdk.response.code', Response::STATUS_CODE_OK)
|
|
|
|
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
|
|
|
->label('sdk.response.model', Response::MODEL_VARIABLE)
|
|
|
|
->param('variableId', '', new UID(), 'Variable unique ID.', false)
|
|
|
|
->param('key', null, new Text(255), 'Variable key. Max length: 255 chars.', false)
|
2023-08-13 07:08:44 +12:00
|
|
|
->param('value', null, new Text(8192, 0), 'Variable value. Max length: 8192 chars.', true)
|
2023-03-12 05:06:02 +13:00
|
|
|
->inject('project')
|
|
|
|
->inject('response')
|
|
|
|
->inject('dbForProject')
|
2023-07-28 19:56:07 +12:00
|
|
|
->inject('dbForConsole')
|
|
|
|
->action(function (string $variableId, string $key, ?string $value, Document $project, Response $response, Database $dbForProject, Database $dbForConsole) {
|
2023-07-25 01:12:36 +12:00
|
|
|
$variable = $dbForProject->getDocument('variables', $variableId);
|
|
|
|
if ($variable === false || $variable->isEmpty() || $variable->getAttribute('resourceType') !== 'project') {
|
2023-03-12 05:06:02 +13:00
|
|
|
throw new Exception(Exception::VARIABLE_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
$variable
|
|
|
|
->setAttribute('key', $key)
|
|
|
|
->setAttribute('value', $value ?? $variable->getAttribute('value'))
|
2023-06-22 22:59:41 +12:00
|
|
|
->setAttribute('search', implode(' ', [$variableId, $key, 'project']));
|
2023-03-12 05:06:02 +13:00
|
|
|
|
|
|
|
try {
|
|
|
|
$dbForProject->updateDocument('variables', $variable->getId(), $variable);
|
|
|
|
} catch (DuplicateException $th) {
|
|
|
|
throw new Exception(Exception::VARIABLE_ALREADY_EXISTS);
|
|
|
|
}
|
|
|
|
|
2023-06-22 22:59:41 +12:00
|
|
|
$functions = $dbForProject->find('functions', [
|
|
|
|
Query::limit(APP_LIMIT_SUBQUERY)
|
|
|
|
]);
|
2023-03-12 05:06:02 +13:00
|
|
|
|
2023-06-22 22:59:41 +12:00
|
|
|
foreach ($functions as $function) {
|
|
|
|
$dbForProject->updateDocument('functions', $function->getId(), $function->setAttribute('live', false));
|
|
|
|
}
|
|
|
|
|
2023-03-12 05:06:02 +13:00
|
|
|
$response->dynamic($variable, Response::MODEL_VARIABLE);
|
|
|
|
});
|
|
|
|
|
|
|
|
App::delete('/v1/project/variables/:variableId')
|
|
|
|
->desc('Delete Variable')
|
|
|
|
->groups(['api'])
|
|
|
|
->label('scope', 'projects.write')
|
2023-08-30 23:28:43 +12:00
|
|
|
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN])
|
2023-03-12 05:06:02 +13:00
|
|
|
->label('sdk.namespace', 'project')
|
|
|
|
->label('sdk.method', 'deleteVariable')
|
|
|
|
->label('sdk.description', '/docs/references/project/delete-variable.md')
|
|
|
|
->label('sdk.response.code', Response::STATUS_CODE_NOCONTENT)
|
|
|
|
->label('sdk.response.model', Response::MODEL_NONE)
|
|
|
|
->param('variableId', '', new UID(), 'Variable unique ID.', false)
|
|
|
|
->inject('project')
|
|
|
|
->inject('response')
|
|
|
|
->inject('dbForProject')
|
|
|
|
->action(function (string $variableId, Document $project, Response $response, Database $dbForProject) {
|
2023-07-25 01:12:36 +12:00
|
|
|
$variable = $dbForProject->getDocument('variables', $variableId);
|
|
|
|
if ($variable === false || $variable->isEmpty() || $variable->getAttribute('resourceType') !== 'project') {
|
2023-03-12 05:06:02 +13:00
|
|
|
throw new Exception(Exception::VARIABLE_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
2023-09-05 23:55:02 +12:00
|
|
|
$dbForProject->deleteDocument('variables', $variable->getId());
|
|
|
|
|
2023-06-22 22:59:41 +12:00
|
|
|
$functions = $dbForProject->find('functions', [
|
|
|
|
Query::limit(APP_LIMIT_SUBQUERY)
|
|
|
|
]);
|
|
|
|
|
|
|
|
foreach ($functions as $function) {
|
|
|
|
$dbForProject->updateDocument('functions', $function->getId(), $function->setAttribute('live', false));
|
|
|
|
}
|
|
|
|
|
2023-03-12 05:06:02 +13:00
|
|
|
$response->noContent();
|
|
|
|
});
|