project usage from appwrite db (wip)
This commit is contained in:
parent
3e0cc30bdd
commit
62e422b76e
|
@ -5,13 +5,15 @@ use Appwrite\Database\Validator\CustomId;
|
|||
use Appwrite\Network\Validator\CNAME;
|
||||
use Appwrite\Network\Validator\Domain as DomainValidator;
|
||||
use Appwrite\Network\Validator\URL;
|
||||
use Appwrite\Task\Validator\Cron;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Cron\CronExpression;
|
||||
use Utopia\Abuse\Adapters\TimeLimit;
|
||||
use Utopia\App;
|
||||
use Utopia\Audit\Audit;
|
||||
use Utopia\Config\Config;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Database\Validator\Authorization;
|
||||
use Utopia\Database\Validator\UID;
|
||||
use Utopia\Domains\Domain;
|
||||
use Utopia\Exception;
|
||||
|
@ -21,13 +23,11 @@ use Utopia\Validator\Integer;
|
|||
use Utopia\Validator\Range;
|
||||
use Utopia\Validator\Text;
|
||||
use Utopia\Validator\WhiteList;
|
||||
use Utopia\Audit\Audit;
|
||||
use Utopia\Abuse\Adapters\TimeLimit;
|
||||
|
||||
App::init(function ($project) {
|
||||
/** @var Utopia\Database\Document $project */
|
||||
|
||||
if($project->getId() !== 'console') {
|
||||
if ($project->getId() !== 'console') {
|
||||
throw new Exception('Access to this API is forbidden.', 401);
|
||||
}
|
||||
}, ['project'], 'projects');
|
||||
|
@ -71,7 +71,7 @@ App::post('/v1/projects')
|
|||
if ($team->isEmpty()) {
|
||||
throw new Exception('Team not found', 404);
|
||||
}
|
||||
|
||||
|
||||
$auth = Config::getParam('auth', []);
|
||||
$auths = ['limit' => 0];
|
||||
foreach ($auth as $index => $method) {
|
||||
|
@ -228,12 +228,12 @@ App::get('/v1/projects/:projectId/usage')
|
|||
->param('range', '30d', new WhiteList(['24h', '7d', '30d', '90d'], true), 'Date range.', true)
|
||||
->inject('response')
|
||||
->inject('dbForConsole')
|
||||
->inject('projectDB')
|
||||
->inject('dbForInternal')
|
||||
->inject('register')
|
||||
->action(function ($projectId, $range, $response, $dbForConsole, $projectDB, $register) {
|
||||
->action(function ($projectId, $range, $response, $dbForConsole, $dbForInternal, $register) {
|
||||
/** @var Appwrite\Utopia\Response $response */
|
||||
/** @var Utopia\Database\Database $dbForConsole */
|
||||
/** @var Appwrite\Database\Database $projectDB */
|
||||
/** @var Utopia\Database\Database $dbForInternal */
|
||||
/** @var Utopia\Registry\Registry $register */
|
||||
|
||||
$project = $dbForConsole->getDocument('projects', $projectId);
|
||||
|
@ -246,71 +246,126 @@ App::get('/v1/projects/:projectId/usage')
|
|||
|
||||
$period = [
|
||||
'24h' => [
|
||||
'start' => DateTime::createFromFormat('U', \strtotime('-24 hours')),
|
||||
'end' => DateTime::createFromFormat('U', \strtotime('+1 hour')),
|
||||
'group' => '30m',
|
||||
// 'start' => DateTime::createFromFormat('U', \strtotime('-24 hours')),
|
||||
// 'end' => DateTime::createFromFormat('U', \strtotime('+1 hour')),
|
||||
'period' => '30m',
|
||||
'limit' => 48,
|
||||
],
|
||||
'7d' => [
|
||||
'start' => DateTime::createFromFormat('U', \strtotime('-7 days')),
|
||||
'end' => DateTime::createFromFormat('U', \strtotime('now')),
|
||||
'group' => '1d',
|
||||
// 'start' => DateTime::createFromFormat('U', \strtotime('-7 days')),
|
||||
// 'end' => DateTime::createFromFormat('U', \strtotime('now')),
|
||||
'period' => '1d',
|
||||
'limit' => 7,
|
||||
],
|
||||
'30d' => [
|
||||
'start' => DateTime::createFromFormat('U', \strtotime('-30 days')),
|
||||
'end' => DateTime::createFromFormat('U', \strtotime('now')),
|
||||
'group' => '1d',
|
||||
// 'start' => DateTime::createFromFormat('U', \strtotime('-30 days')),
|
||||
// 'end' => DateTime::createFromFormat('U', \strtotime('now')),
|
||||
'period' => '1d',
|
||||
'limit' => 30,
|
||||
],
|
||||
'90d' => [
|
||||
'start' => DateTime::createFromFormat('U', \strtotime('-90 days')),
|
||||
'end' => DateTime::createFromFormat('U', \strtotime('now')),
|
||||
'group' => '1d',
|
||||
// 'start' => DateTime::createFromFormat('U', \strtotime('-90 days')),
|
||||
// 'end' => DateTime::createFromFormat('U', \strtotime('now')),
|
||||
'period' => '1d',
|
||||
'limit' => 90,
|
||||
],
|
||||
];
|
||||
|
||||
$client = $register->get('influxdb');
|
||||
$dbForInternal->setNamespace('project_' . $projectId . '_internal');
|
||||
|
||||
$requestDocs = Authorization::skip(function () use ($dbForInternal, $period, $range) {
|
||||
return $dbForInternal->find('stats', [
|
||||
new Query('period', Query::TYPE_EQUAL, [$period[$range]['period']]),
|
||||
new Query('metric', Query::TYPE_EQUAL, ['requests']),
|
||||
], $period[$range]['limit'], 0, ['time'], [Database::ORDER_DESC]);
|
||||
});
|
||||
|
||||
$requests = [];
|
||||
$network = [];
|
||||
$functions = [];
|
||||
|
||||
if ($client) {
|
||||
$start = $period[$range]['start']->format(DateTime::RFC3339);
|
||||
$end = $period[$range]['end']->format(DateTime::RFC3339);
|
||||
$database = $client->selectDB('telegraf');
|
||||
|
||||
// Requests
|
||||
$result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_requests_all" WHERE time > \'' . $start . '\' AND time < \'' . $end . '\' AND "metric_type"=\'counter\' AND "projectId"=\'' . $project->getId() . '\' GROUP BY time(' . $period[$range]['group'] . ') FILL(null)');
|
||||
$points = $result->getPoints();
|
||||
|
||||
foreach ($points as $point) {
|
||||
$requests[] = [
|
||||
'value' => (!empty($point['value'])) ? $point['value'] : 0,
|
||||
'date' => \strtotime($point['time']),
|
||||
];
|
||||
}
|
||||
|
||||
// Network
|
||||
$result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_network_all" WHERE time > \'' . $start . '\' AND time < \'' . $end . '\' AND "metric_type"=\'counter\' AND "projectId"=\'' . $project->getId() . '\' GROUP BY time(' . $period[$range]['group'] . ') FILL(null)');
|
||||
$points = $result->getPoints();
|
||||
|
||||
foreach ($points as $point) {
|
||||
$network[] = [
|
||||
'value' => (!empty($point['value'])) ? $point['value'] : 0,
|
||||
'date' => \strtotime($point['time']),
|
||||
];
|
||||
}
|
||||
|
||||
// Functions
|
||||
$result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_executions_all" WHERE time > \'' . $start . '\' AND time < \'' . $end . '\' AND "metric_type"=\'counter\' AND "projectId"=\'' . $project->getId() . '\' GROUP BY time(' . $period[$range]['group'] . ') FILL(null)');
|
||||
$points = $result->getPoints();
|
||||
|
||||
foreach ($points as $point) {
|
||||
$functions[] = [
|
||||
'value' => (!empty($point['value'])) ? $point['value'] : 0,
|
||||
'date' => \strtotime($point['time']),
|
||||
];
|
||||
}
|
||||
foreach ($requestDocs as $requestDoc) {
|
||||
$requests[] = [
|
||||
'value' => $requestDoc->getAttribute('value'),
|
||||
'date' => $requestDoc->getAttribute('time'),
|
||||
];
|
||||
}
|
||||
|
||||
$requests = array_reverse($requests);
|
||||
|
||||
$networkDocs = Authorization::skip(function () use ($dbForInternal, $period, $range) {
|
||||
return $dbForInternal->find('stats', [
|
||||
new Query('period', Query::TYPE_EQUAL, [$period[$range]['period']]),
|
||||
new Query('metric', Query::TYPE_EQUAL, ['network']),
|
||||
], $period[$range]['limit'], 0, ['time'], [Database::ORDER_DESC]);
|
||||
});
|
||||
|
||||
$network = [];
|
||||
foreach ($networkDocs as $networkDoc) {
|
||||
$network[] = [
|
||||
'value' => $networkDoc->getAttribute('value'),
|
||||
'date' => $networkDoc->getAttribute('time'),
|
||||
];
|
||||
}
|
||||
|
||||
$network = array_reverse($network);
|
||||
|
||||
$functionsDocs = Authorization::skip(function () use ($dbForInternal, $period, $range) {
|
||||
return $dbForInternal->find('stats', [
|
||||
new Query('period', Query::TYPE_EQUAL, [$period[$range]['period']]),
|
||||
new Query('metric', Query::TYPE_EQUAL, ['executions']),
|
||||
], $period[$range]['limit'], 0, ['time'], [Database::ORDER_DESC]);
|
||||
});
|
||||
|
||||
$functions = [];
|
||||
foreach ($functionsDocs as $functionDoc) {
|
||||
$functions[] = [
|
||||
'value' => $functionDoc->getAttribute('value'),
|
||||
'date' => $functionDoc->getAttribute('time'),
|
||||
];
|
||||
}
|
||||
$functions = array_reverse($functions);
|
||||
|
||||
// $requests = [];
|
||||
// $network = [];
|
||||
// $functions = [];
|
||||
|
||||
// $client = $register->get('influxdb');
|
||||
// if ($client) {
|
||||
// $start = $period[$range]['start']->format(DateTime::RFC3339);
|
||||
// $end = $period[$range]['end']->format(DateTime::RFC3339);
|
||||
// $database = $client->selectDB('telegraf');
|
||||
|
||||
// // Requests
|
||||
// $result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_requests_all" WHERE time > \'' . $start . '\' AND time < \'' . $end . '\' AND "metric_type"=\'counter\' AND "projectId"=\'' . $project->getId() . '\' GROUP BY time(' . $period[$range]['group'] . ') FILL(null)');
|
||||
// $points = $result->getPoints();
|
||||
|
||||
// foreach ($points as $point) {
|
||||
// $requests[] = [
|
||||
// 'value' => (!empty($point['value'])) ? $point['value'] : 0,
|
||||
// 'date' => \strtotime($point['time']),
|
||||
// ];
|
||||
// }
|
||||
|
||||
// // Network
|
||||
// $result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_network_all" WHERE time > \'' . $start . '\' AND time < \'' . $end . '\' AND "metric_type"=\'counter\' AND "projectId"=\'' . $project->getId() . '\' GROUP BY time(' . $period[$range]['group'] . ') FILL(null)');
|
||||
// $points = $result->getPoints();
|
||||
|
||||
// foreach ($points as $point) {
|
||||
// $network[] = [
|
||||
// 'value' => (!empty($point['value'])) ? $point['value'] : 0,
|
||||
// 'date' => \strtotime($point['time']),
|
||||
// ];
|
||||
// }
|
||||
|
||||
// // Functions
|
||||
// $result = $database->query('SELECT sum(value) AS "value" FROM "appwrite_usage_executions_all" WHERE time > \'' . $start . '\' AND time < \'' . $end . '\' AND "metric_type"=\'counter\' AND "projectId"=\'' . $project->getId() . '\' GROUP BY time(' . $period[$range]['group'] . ') FILL(null)');
|
||||
// $points = $result->getPoints();
|
||||
|
||||
// foreach ($points as $point) {
|
||||
// $functions[] = [
|
||||
// 'value' => (!empty($point['value'])) ? $point['value'] : 0,
|
||||
// 'date' => \strtotime($point['time']),
|
||||
// ];
|
||||
// }
|
||||
// }
|
||||
} else {
|
||||
$requests = [];
|
||||
$network = [];
|
||||
|
@ -319,41 +374,41 @@ App::get('/v1/projects/:projectId/usage')
|
|||
|
||||
// Users
|
||||
|
||||
$projectDB->getCollection([
|
||||
'limit' => 0,
|
||||
'offset' => 0,
|
||||
'filters' => [
|
||||
'$collection=users',
|
||||
],
|
||||
]);
|
||||
// $projectDB->getCollection([
|
||||
// 'limit' => 0,
|
||||
// 'offset' => 0,
|
||||
// 'filters' => [
|
||||
// '$collection=users',
|
||||
// ],
|
||||
// ]);
|
||||
|
||||
$usersTotal = $projectDB->getSum();
|
||||
// $usersTotal = $projectDB->getSum();
|
||||
|
||||
// Documents
|
||||
// // Documents
|
||||
|
||||
$collections = $projectDB->getCollection([
|
||||
'limit' => 100,
|
||||
'offset' => 0,
|
||||
'filters' => [
|
||||
'$collection=collections',
|
||||
],
|
||||
]);
|
||||
// $collections = $projectDB->getCollection([
|
||||
// 'limit' => 100,
|
||||
// 'offset' => 0,
|
||||
// 'filters' => [
|
||||
// '$collection=collections',
|
||||
// ],
|
||||
// ]);
|
||||
|
||||
$collectionsTotal = $projectDB->getSum();
|
||||
// $collectionsTotal = $projectDB->getSum();
|
||||
|
||||
$documents = [];
|
||||
// $documents = [];
|
||||
|
||||
foreach ($collections as $collection) {
|
||||
$result = $projectDB->getCollection([
|
||||
'limit' => 0,
|
||||
'offset' => 0,
|
||||
'filters' => [
|
||||
'$collection=' . $collection['$id'],
|
||||
],
|
||||
]);
|
||||
// foreach ($collections as $collection) {
|
||||
// $result = $projectDB->getCollection([
|
||||
// 'limit' => 0,
|
||||
// 'offset' => 0,
|
||||
// 'filters' => [
|
||||
// '$collection=' . $collection['$id'],
|
||||
// ],
|
||||
// ]);
|
||||
|
||||
$documents[] = ['name' => $collection['name'], 'total' => $projectDB->getSum()];
|
||||
}
|
||||
// $documents[] = ['name' => $collection['name'], 'total' => $projectDB->getSum()];
|
||||
// }
|
||||
|
||||
$response->json([
|
||||
'range' => $range,
|
||||
|
@ -375,38 +430,38 @@ App::get('/v1/projects/:projectId/usage')
|
|||
return $item['value'];
|
||||
}, $functions)),
|
||||
],
|
||||
'collections' => [
|
||||
'data' => $collections,
|
||||
'total' => $collectionsTotal,
|
||||
],
|
||||
'documents' => [
|
||||
'data' => $documents,
|
||||
'total' => \array_sum(\array_map(function ($item) {
|
||||
return $item['total'];
|
||||
}, $documents)),
|
||||
],
|
||||
'users' => [
|
||||
'data' => [],
|
||||
'total' => $usersTotal,
|
||||
],
|
||||
'storage' => [
|
||||
'total' => $projectDB->getCount(
|
||||
[
|
||||
'attribute' => 'sizeOriginal',
|
||||
'filters' => [
|
||||
'$collection=files',
|
||||
],
|
||||
]
|
||||
) +
|
||||
$projectDB->getCount(
|
||||
[
|
||||
'attribute' => 'size',
|
||||
'filters' => [
|
||||
'$collection=tags',
|
||||
],
|
||||
]
|
||||
),
|
||||
],
|
||||
// 'collections' => [
|
||||
// 'data' => $collections,
|
||||
// 'total' => $collectionsTotal,
|
||||
// ],
|
||||
// 'documents' => [
|
||||
// 'data' => $documents,
|
||||
// 'total' => \array_sum(\array_map(function ($item) {
|
||||
// return $item['total'];
|
||||
// }, $documents)),
|
||||
// ],
|
||||
// 'users' => [
|
||||
// 'data' => [],
|
||||
// 'total' => $usersTotal,
|
||||
// ],
|
||||
// 'storage' => [
|
||||
// 'total' => $projectDB->getCount(
|
||||
// [
|
||||
// 'attribute' => 'sizeOriginal',
|
||||
// 'filters' => [
|
||||
// '$collection=files',
|
||||
// ],
|
||||
// ]
|
||||
// ) +
|
||||
// $projectDB->getCount(
|
||||
// [
|
||||
// 'attribute' => 'size',
|
||||
// 'filters' => [
|
||||
// '$collection=tags',
|
||||
// ],
|
||||
// ]
|
||||
// ),
|
||||
// ],
|
||||
]);
|
||||
});
|
||||
|
||||
|
@ -470,7 +525,7 @@ App::patch('/v1/projects/:projectId/service')
|
|||
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
||||
->label('sdk.response.model', Response::MODEL_PROJECT)
|
||||
->param('projectId', '', new UID(), 'Project unique ID.')
|
||||
->param('service', '', new WhiteList(array_keys(array_filter(Config::getParam('services'), function($element) {return $element['optional'];})), true), 'Service name.')
|
||||
->param('service', '', new WhiteList(array_keys(array_filter(Config::getParam('services'), function ($element) {return $element['optional'];})), true), 'Service name.')
|
||||
->param('status', null, new Boolean(), 'Service status.')
|
||||
->inject('response')
|
||||
->inject('dbForConsole')
|
||||
|
|
Loading…
Reference in a new issue