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-25 23:16:25 +13:00
|
|
|
use Utopia\Config\Config;
|
2022-11-15 07:22:04 +13:00
|
|
|
use Utopia\Database\Database;
|
|
|
|
use Utopia\Database\Document;
|
2023-12-10 22:26:33 +13:00
|
|
|
use Utopia\Database\Validator\Datetime as DateTimeValidator;
|
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-25 20:39:59 +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)
|
2023-12-10 22:26:33 +13:00
|
|
|
->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)
|
2022-11-15 07:22:04 +13:00
|
|
|
->inject('response')
|
|
|
|
->inject('dbForProject')
|
2023-12-10 22:26:33 +13:00
|
|
|
->action(function (string $startDate, string $endDate, string $period, Response $response, Database $dbForProject) {
|
2023-11-09 05:12:36 +13:00
|
|
|
$stats = $total = $usage = [];
|
2023-12-10 22:26:33 +13:00
|
|
|
$format = 'Y-m-d 00:00:00';
|
|
|
|
$firstDay = (new DateTime($startDate))->format($format);
|
|
|
|
$lastDay = (new DateTime($endDate))->format($format);
|
2023-11-09 05:12:36 +13:00
|
|
|
|
2023-10-25 20:39:59 +13:00
|
|
|
$metrics = [
|
2023-11-09 05:12:36 +13:00
|
|
|
'total' => [
|
|
|
|
METRIC_EXECUTIONS,
|
|
|
|
METRIC_DOCUMENTS,
|
|
|
|
METRIC_DATABASES,
|
|
|
|
METRIC_USERS,
|
|
|
|
METRIC_BUCKETS,
|
|
|
|
METRIC_FILES_STORAGE
|
|
|
|
],
|
|
|
|
'period' => [
|
|
|
|
METRIC_NETWORK_REQUESTS,
|
|
|
|
METRIC_NETWORK_INBOUND,
|
|
|
|
METRIC_NETWORK_OUTBOUND,
|
2023-12-12 04:19:08 +13:00
|
|
|
METRIC_USERS,
|
2023-12-13 06:48:21 +13:00
|
|
|
METRIC_EXECUTIONS
|
2023-11-09 05:12:36 +13:00
|
|
|
]
|
2023-10-25 20:39:59 +13:00
|
|
|
];
|
2023-03-12 05:06:02 +13:00
|
|
|
|
2023-12-10 22:26:33 +13:00
|
|
|
$factor = match ($period) {
|
|
|
|
'1h' => 3600,
|
|
|
|
'1d' => 86400,
|
|
|
|
};
|
|
|
|
|
|
|
|
$limit = match ($period) {
|
2023-12-12 04:19:08 +13:00
|
|
|
'1h' => (new DateTime($startDate))->diff(new DateTime($endDate))->days * 24,
|
|
|
|
'1d' => (new DateTime($startDate))->diff(new DateTime($endDate))->days
|
2023-12-10 22:26:33 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
$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) {
|
2023-11-09 05:12:36 +13:00
|
|
|
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) {
|
2023-10-25 20:39:59 +13:00
|
|
|
$results = $dbForProject->find('stats', [
|
|
|
|
Query::equal('metric', [$metric]),
|
2023-10-26 01:06:54 +13:00
|
|
|
Query::equal('period', [$period]),
|
2023-12-10 22:26:33 +13:00
|
|
|
Query::greaterThanEqual('time', $firstDay),
|
|
|
|
Query::lessThan('time', $lastDay),
|
2023-10-25 20:39:59 +13:00
|
|
|
Query::orderDesc('time'),
|
|
|
|
]);
|
2023-08-21 00:29:43 +12:00
|
|
|
|
2023-10-25 20:39:59 +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-12-11 08:58:38 +13:00
|
|
|
$now = time();
|
2023-12-10 22:26:33 +13:00
|
|
|
foreach ($metrics['period'] as $metric) {
|
|
|
|
$usage[$metric] = [];
|
2023-12-11 08:58:38 +13:00
|
|
|
$leap = $now - ($limit * $factor);
|
|
|
|
while ($leap < $now) {
|
2023-12-10 22:26:33 +13:00
|
|
|
$leap += $factor;
|
|
|
|
$formatDate = date($format, $leap);
|
|
|
|
$usage[$metric][] = [
|
2023-12-11 08:58:38 +13:00
|
|
|
'value' => $stats[$metric][$formatDate]['value'] ?? 0,
|
|
|
|
'date' => $formatDate,
|
2023-12-10 22:26:33 +13:00
|
|
|
];
|
|
|
|
}
|
2023-03-12 05:06:02 +13:00
|
|
|
}
|
2023-12-10 22:26:33 +13:00
|
|
|
|
2023-12-12 04:33:15 +13:00
|
|
|
$executionsBreakdown = array_map(function ($function) use ($dbForProject) {
|
2023-12-12 04:19:08 +13:00
|
|
|
$id = $function->getId();
|
|
|
|
$name = $function->getAttribute('name');
|
2023-12-12 05:12:43 +13:00
|
|
|
$metric = str_replace('{functionInternalId}', $function->getInternalId(), METRIC_FUNCTION_ID_EXECUTIONS);
|
2023-12-12 04:19:08 +13:00
|
|
|
$value = $dbForProject->findOne('stats', [
|
|
|
|
Query::equal('metric', [$metric]),
|
|
|
|
Query::equal('period', ['inf'])
|
|
|
|
]);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'resourceId' => $id,
|
|
|
|
'name' => $name,
|
|
|
|
'value' => $value['value'] ?? 0,
|
|
|
|
];
|
|
|
|
}, $dbForProject->find('functions'));
|
|
|
|
|
2023-12-12 04:33:15 +13:00
|
|
|
$bucketsBreakdown = array_map(function ($bucket) use ($dbForProject) {
|
2023-12-12 04:19:08 +13:00
|
|
|
$id = $bucket->getId();
|
|
|
|
$name = $bucket->getAttribute('name');
|
|
|
|
$metric = str_replace('{bucketInternalId}', $bucket->getInternalId(), METRIC_BUCKET_ID_FILES_STORAGE);
|
|
|
|
$value = $dbForProject->findOne('stats', [
|
|
|
|
Query::equal('metric', [$metric]),
|
|
|
|
Query::equal('period', ['inf'])
|
|
|
|
]);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'resourceId' => $id,
|
|
|
|
'name' => $name,
|
|
|
|
'value' => $value['value'] ?? 0,
|
|
|
|
];
|
|
|
|
}, $dbForProject->find('buckets'));
|
|
|
|
|
2023-10-25 20:39:59 +13:00
|
|
|
$response->dynamic(new Document([
|
2023-11-09 05:12:36 +13:00
|
|
|
'requests' => ($usage[METRIC_NETWORK_REQUESTS]),
|
|
|
|
'network' => ($usage[METRIC_NETWORK_INBOUND] + $usage[METRIC_NETWORK_OUTBOUND]),
|
2023-12-12 04:19:08 +13:00
|
|
|
'users' => ($usage[METRIC_USERS]),
|
2023-12-13 06:48:21 +13:00
|
|
|
'executions' => ($usage[METRIC_EXECUTIONS]),
|
2023-11-09 05:12:36 +13:00
|
|
|
'executionsTotal' => $total[METRIC_EXECUTIONS],
|
|
|
|
'documentsTotal' => $total[METRIC_DOCUMENTS],
|
|
|
|
'databasesTotal' => $total[METRIC_DATABASES],
|
|
|
|
'usersTotal' => $total[METRIC_USERS],
|
|
|
|
'bucketsTotal' => $total[METRIC_BUCKETS],
|
|
|
|
'filesStorageTotal' => $total[METRIC_FILES_STORAGE],
|
2023-12-12 04:19:08 +13:00
|
|
|
'executionsBreakdown' => $executionsBreakdown,
|
|
|
|
'bucketsBreakdown' => $bucketsBreakdown
|
2023-10-25 20:39:59 +13:00
|
|
|
]), Response::MODEL_USAGE_PROJECT);
|
2023-03-12 05:06:02 +13:00
|
|
|
});
|
|
|
|
|
2023-10-25 20:39:59 +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();
|
|
|
|
});
|