1
0
Fork 0
mirror of synced 2024-10-02 10:16:27 +13:00

databases usage

This commit is contained in:
shimon 2022-12-11 11:00:00 +02:00
parent 806a775663
commit ba04f845da
8 changed files with 264 additions and 561 deletions

View file

@ -2386,124 +2386,6 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/documents/:docu
$response->noContent(); $response->noContent();
}); });
App::get('/v1/databases/usage')
->desc('Get usage stats for the database')
->groups(['api', 'database'])
->label('scope', 'collections.read')
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN])
->label('sdk.namespace', 'databases')
->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_DATABASES)
->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) {
// $usage = [];
// if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') {
// $periods = [
// '24h' => [
// 'period' => '1h',
// 'limit' => 24,
// ],
// '7d' => [
// 'period' => '1d',
// 'limit' => 7,
// ],
// '30d' => [
// 'period' => '1d',
// 'limit' => 30,
// ],
// '90d' => [
// 'period' => '1d',
// 'limit' => 90,
// ],
// ];
// $metrics = [
// 'databases.$all.count.total',
// 'documents.$all.count.total',
// 'collections.$all.count.total',
// 'databases.$all.requests.create',
// 'databases.$all.requests.read',
// 'databases.$all.requests.update',
// 'databases.$all.requests.delete',
// 'collections.$all.requests.create',
// 'collections.$all.requests.read',
// 'collections.$all.requests.update',
// 'collections.$all.requests.delete',
// 'documents.$all.requests.create',
// 'documents.$all.requests.read',
// 'documents.$all.requests.update',
// 'documents.$all.requests.delete'
// ];
// $stats = [];
// Authorization::skip(function () use ($dbForProject, $periods, $range, $metrics, &$stats) {
// foreach ($metrics as $metric) {
// $limit = $periods[$range]['limit'];
// $period = $periods[$range]['period'];
// $requestDocs = $dbForProject->find('stats', [
// Query::equal('period', [$period]),
// Query::equal('metric', [$metric]),
// Query::limit($limit),
// Query::orderDesc('time'),
// ]);
// $stats[$metric] = [];
// foreach ($requestDocs as $requestDoc) {
// $stats[$metric][] = [
// 'value' => $requestDoc->getAttribute('value'),
// 'date' => $requestDoc->getAttribute('time'),
// ];
// }
// // backfill metrics with empty values for graphs
// $backfill = $limit - \count($requestDocs);
// while ($backfill > 0) {
// $last = $limit - $backfill - 1; // array index of last added metric
// $diff = match ($period) { // convert period to seconds for unix timestamp math
// '1h' => 3600,
// '1d' => 86400,
// };
// $stats[$metric][] = [
// 'value' => 0,
// 'date' => DateTime::formatTz(DateTime::addSeconds(new \DateTime($stats[$metric][$last]['date'] ?? null), -1 * $diff)),
// ];
// $backfill--;
// }
// // Added 3'rd level to Index [period, metric, time] because of order by.
// $stats[$metric] = array_reverse($stats[$metric]);
// }
// });
// $usage = new Document([
// 'range' => $range,
// 'databasesCount' => $stats['databases.$all.count.total'] ?? [],
// 'documentsCount' => $stats['documents.$all.count.total'] ?? [],
// 'collectionsCount' => $stats['collections.$all.count.total'] ?? [],
// 'documentsCreate' => $stats['documents.$all.requests.create'] ?? [],
// 'documentsRead' => $stats['documents.$all.requests.read'] ?? [],
// 'documentsUpdate' => $stats['documents.$all.requests.update'] ?? [],
// 'documentsDelete' => $stats['documents.$all.requests.delete'] ?? [],
// 'collectionsCreate' => $stats['collections.$all.requests.create'] ?? [],
// 'collectionsRead' => $stats['collections.$all.requests.read'] ?? [],
// 'collectionsUpdate' => $stats['collections.$all.requests.update'] ?? [],
// 'collectionsDelete' => $stats['collections.$all.requests.delete'] ?? [],
// 'databasesCreate' => $stats['databases.$all.requests.create'] ?? [],
// 'databasesRead' => $stats['databases.$all.requests.read'] ?? [],
// 'databasesUpdate' => $stats['databases.$all.requests.update'] ?? [],
// 'databasesDelete' => $stats['databases.$all.requests.delete'] ?? [],
// ]);
// }
// $response->dynamic($usage, Response::MODEL_USAGE_DATABASES);
});
App::get('/v1/databases/:databaseId/usage') App::get('/v1/databases/:databaseId/usage')
->desc('Get usage stats for the database') ->desc('Get usage stats for the database')
->groups(['api', 'database']) ->groups(['api', 'database'])
@ -2520,97 +2402,63 @@ App::get('/v1/databases/:databaseId/usage')
->inject('dbForProject') ->inject('dbForProject')
->action(function (string $databaseId, string $range, Response $response, Database $dbForProject) { ->action(function (string $databaseId, string $range, Response $response, Database $dbForProject) {
// $usage = []; $database = $dbForProject->getDocument('databases', $databaseId);
// if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') {
// $periods = [
// '24h' => [
// 'period' => '1h',
// 'limit' => 24,
// ],
// '7d' => [
// 'period' => '1d',
// 'limit' => 7,
// ],
// '30d' => [
// 'period' => '1d',
// 'limit' => 30,
// ],
// '90d' => [
// 'period' => '1d',
// 'limit' => 90,
// ],
// ];
// $metrics = [ if ($database->isEmpty()) {
// 'collections.' . $databaseId . '.count.total', throw new Exception(Exception::DATABASE_NOT_FOUND);
// 'collections.' . $databaseId . '.requests.create', }
// 'collections.' . $databaseId . '.requests.read',
// 'collections.' . $databaseId . '.requests.update',
// 'collections.' . $databaseId . '.requests.delete',
// 'documents.' . $databaseId . '.count.total',
// 'documents.' . $databaseId . '.requests.create',
// 'documents.' . $databaseId . '.requests.read',
// 'documents.' . $databaseId . '.requests.update',
// 'documents.' . $databaseId . '.requests.delete'
// ];
// $stats = []; $periods = Config::getParam('usage', []);
$stats = $usage = [];
$days = $periods[$range];
$metrics = [
$database->getId() . '.collections',
$database->getId() . '.documents',
];
// Authorization::skip(function () use ($dbForProject, $periods, $range, $metrics, &$stats) { Authorization::skip(function () use ($dbForProject, $days, $metrics, &$stats) {
// foreach ($metrics as $metric) { foreach ($metrics as $metric) {
// $limit = $periods[$range]['limit']; $limit = $days['limit'];
// $period = $periods[$range]['period']; $period = $days['period'];
$results = $dbForProject->find('stats', [
Query::equal('period', [$period]),
Query::equal('metric', [$metric]),
Query::limit($limit),
Query::orderDesc('time'),
]);
$stats[$metric] = [];
foreach ($results as $result) {
$stats[$metric][$result->getAttribute('time')] = [
'value' => $result->getAttribute('value'),
];
}
}
});
// $requestDocs = $dbForProject->find('stats', [ $format = match ($days['period']) {
// Query::equal('period', [$period]), '1h' => 'Y-m-d\TH:00:00.000P',
// Query::equal('metric', [$metric]), '1d' => 'Y-m-d\T00:00:00.000P',
// Query::limit($limit), };
// Query::orderDesc('time'),
// ]);
// $stats[$metric] = []; foreach ($metrics as $metric) {
// foreach ($requestDocs as $requestDoc) { $usage[$metric] = [];
// $stats[$metric][] = [ $leap = time() - ($days['limit'] * $days['factor']);
// 'value' => $requestDoc->getAttribute('value'), while ($leap < time()) {
// 'date' => $requestDoc->getAttribute('time'), $leap += $days['factor'];
// ]; $formatDate = date($format, $leap);
// } $usage[$metric][] = [
'value' => $stats[$metric][$formatDate]['value'] ?? 0,
'date' => $formatDate,
];
}
$usage[$metric] = array_reverse($usage[$metric]);
}
// // backfill metrics with empty values for graphs $response->dynamic(new Document([
// $backfill = $limit - \count($requestDocs); 'range' => $range,
// while ($backfill > 0) { 'collectionsCount' => $usage[$metrics[0]],
// $last = $limit - $backfill - 1; // array index of last added metric 'documentsCount' => $usage[$metrics[1]],
// $diff = match ($period) { // convert period to seconds for unix timestamp math ]), Response::MODEL_USAGE_DATABASE);
// '1h' => 3600,
// '1d' => 86400,
// };
// $stats[$metric][] = [
// 'value' => 0,
// 'date' => DateTime::formatTz(DateTime::addSeconds(new \DateTime($stats[$metric][$last]['date'] ?? null), -1 * $diff)),
// ];
// $backfill--;
// }
// // TODO@kodumbeats explore performance if query is ordered by time ASC
// $stats[$metric] = array_reverse($stats[$metric]);
// }
// });
// $usage = new Document([
// 'range' => $range,
// 'collectionsCount' => $stats["collections.{$databaseId}.count.total"] ?? [],
// 'collectionsCreate' => $stats["collections.{$databaseId}.requests.create"] ?? [],
// 'collectionsRead' => $stats["collections.{$databaseId}.requests.read"] ?? [],
// 'collectionsUpdate' => $stats["collections.{$databaseId}.requests.update"] ?? [],
// 'collectionsDelete' => $stats["collections.{$databaseId}.requests.delete"] ?? [],
// 'documentsCount' => $stats["documents.{$databaseId}.count.total"] ?? [],
// 'documentsCreate' => $stats["documents.{$databaseId}.requests.create"] ?? [],
// 'documentsRead' => $stats["documents.{$databaseId}.requests.read"] ?? [],
// 'documentsUpdate' => $stats["documents.{$databaseId}.requests.update"] ?? [],
// 'documentsDelete' => $stats["documents.{$databaseId}.requests.delete"] ?? [],
// ]);
// }
// $response->dynamic($usage, Response::MODEL_USAGE_DATABASE);
}); });
App::get('/v1/databases/:databaseId/collections/:collectionId/usage') App::get('/v1/databases/:databaseId/collections/:collectionId/usage')
@ -2631,93 +2479,132 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/usage')
->inject('dbForProject') ->inject('dbForProject')
->action(function (string $databaseId, string $range, string $collectionId, Response $response, Database $dbForProject) { ->action(function (string $databaseId, string $range, string $collectionId, Response $response, Database $dbForProject) {
// $database = $dbForProject->getDocument('databases', $databaseId); $database = $dbForProject->getDocument('databases', $databaseId);
// $collectionDocument = $dbForProject->getDocument('database_' . $database->getInternalId(), $collectionId); $collectionDocument = $dbForProject->getDocument('database_' . $database->getInternalId(), $collectionId);
// $collection = $dbForProject->getCollection('database_' . $database->getInternalId() . '_collection_' . $collectionDocument->getInternalId()); $collection = $dbForProject->getCollection('database_' . $database->getInternalId() . '_collection_' . $collectionDocument->getInternalId());
// if ($collection->isEmpty()) { if ($collection->isEmpty()) {
// throw new Exception(Exception::COLLECTION_NOT_FOUND); throw new Exception(Exception::COLLECTION_NOT_FOUND);
// } }
// $usage = []; $periods = Config::getParam('usage', []);
// if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $stats = $usage = [];
// $periods = [ $days = $periods[$range];
// '24h' => [ $metrics = [
// 'period' => '1h', $collection->getId() . '.documents',
// 'limit' => 24, ];
// ],
// '7d' => [
// 'period' => '1d',
// 'limit' => 7,
// ],
// '30d' => [
// 'period' => '1d',
// 'limit' => 30,
// ],
// '90d' => [
// 'period' => '1d',
// 'limit' => 90,
// ],
// ];
// $metrics = [ Authorization::skip(function () use ($dbForProject, $days, $metrics, &$stats) {
// "documents.{$databaseId}/{$collectionId}.count.total", foreach ($metrics as $metric) {
// "documents.{$databaseId}/{$collectionId}.requests.create", $limit = $days['limit'];
// "documents.{$databaseId}/{$collectionId}.requests.read", $period = $days['period'];
// "documents.{$databaseId}/{$collectionId}.requests.update", $results = $dbForProject->find('stats', [
// "documents.{$databaseId}/{$collectionId}.requests.delete", Query::equal('period', [$period]),
// ]; Query::equal('metric', [$metric]),
Query::limit($limit),
Query::orderDesc('time'),
]);
$stats[$metric] = [];
foreach ($results as $result) {
$stats[$metric][$result->getAttribute('time')] = [
'value' => $result->getAttribute('value'),
];
}
}
});
// $stats = []; $format = match ($days['period']) {
'1h' => 'Y-m-d\TH:00:00.000P',
'1d' => 'Y-m-d\T00:00:00.000P',
};
// Authorization::skip(function () use ($dbForProject, $periods, $range, $metrics, &$stats) { foreach ($metrics as $metric) {
// foreach ($metrics as $metric) { $usage[$metric] = [];
// $limit = $periods[$range]['limit']; $leap = time() - ($days['limit'] * $days['factor']);
// $period = $periods[$range]['period']; while ($leap < time()) {
$leap += $days['factor'];
$formatDate = date($format, $leap);
$usage[$metric][] = [
'value' => $stats[$metric][$formatDate]['value'] ?? 0,
'date' => $formatDate,
];
}
$usage[$metric] = array_reverse($usage[$metric]);
}
// $requestDocs = $dbForProject->find('stats', [ $response->dynamic(new Document([
// Query::equal('period', [$period]), 'range' => $range,
// Query::equal('metric', [$metric]), 'documentsCount' => $usage[$metrics[0]],
// Query::limit($limit), ]), Response::MODEL_USAGE_COLLECTION);
// Query::orderDesc('time'), });
// ]);
App::get('/v1/databases/usage')
// $stats[$metric] = []; ->desc('Get usage stats for the database')
// foreach ($requestDocs as $requestDoc) { ->groups(['api', 'database'])
// $stats[$metric][] = [ ->label('scope', 'collections.read')
// 'value' => $requestDoc->getAttribute('value'), ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN])
// 'date' => $requestDoc->getAttribute('time'), ->label('sdk.namespace', 'databases')
// ]; ->label('sdk.method', 'getUsage')
// } ->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
// // backfill metrics with empty values for graphs ->label('sdk.response.model', Response::MODEL_USAGE_DATABASES)
// $backfill = $limit - \count($requestDocs); ->param('range', '30d', new WhiteList(['24h', '7d', '30d', '90d'], true), '`Date range.', true)
// while ($backfill > 0) { ->inject('response')
// $last = $limit - $backfill - 1; // array index of last added metric ->inject('dbForProject')
// $diff = match ($period) { // convert period to seconds for unix timestamp math ->action(function (string $range, Response $response, Database $dbForProject) {
// '1h' => 3600,
// '1d' => 86400, $periods = Config::getParam('usage', []);
// }; $stats = $usage = [];
// $stats[$metric][] = [ $days = $periods[$range];
// 'value' => 0, $metrics = [
// 'date' => DateTime::formatTz(DateTime::addSeconds(new \DateTime($stats[$metric][$last]['date'] ?? null), -1 * $diff)), 'databases',
// ]; 'collections',
// $backfill--; 'documents',
// } ];
// $stats[$metric] = array_reverse($stats[$metric]);
// } Authorization::skip(function () use ($dbForProject, $days, $metrics, &$stats) {
// }); foreach ($metrics as $metric) {
$limit = $days['limit'];
// $usage = new Document([ $period = $days['period'];
// 'range' => $range, $results = $dbForProject->find('stats', [
// 'documentsCount' => $stats["documents.{$databaseId}/{$collectionId}.count.total"] ?? [], Query::equal('period', [$period]),
// 'documentsCreate' => $stats["documents.{$databaseId}/{$collectionId}.requests.create"] ?? [], Query::equal('metric', [$metric]),
// 'documentsRead' => $stats["documents.{$databaseId}/{$collectionId}.requests.read"] ?? [], Query::limit($limit),
// 'documentsUpdate' => $stats["documents.{$databaseId}/{$collectionId}.requests.update"] ?? [], Query::orderDesc('time'),
// 'documentsDelete' => $stats["documents.{$databaseId}/{$collectionId}.requests.delete" ?? []] ]);
// ]); $stats[$metric] = [];
// } foreach ($results as $result) {
$stats[$metric][$result->getAttribute('time')] = [
// $response->dynamic($usage, Response::MODEL_USAGE_COLLECTION); 'value' => $result->getAttribute('value'),
];
}
}
});
$format = match ($days['period']) {
'1h' => 'Y-m-d\TH:00:00.000P',
'1d' => 'Y-m-d\T00:00:00.000P',
};
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,
];
}
$usage[$metric] = array_reverse($usage[$metric]);
}
$response->dynamic(new Document([
'range' => $range,
'databasesCount' => $usage[$metrics[0]],
'collectionsCount' => $usage[$metrics[1]],
'documentsCount' => $usage[$metrics[2]],
]), Response::MODEL_USAGE_DATABASES);
}); });

View file

@ -2,6 +2,7 @@
use Appwrite\Utopia\Response; use Appwrite\Utopia\Response;
use Utopia\App; use Utopia\App;
use Utopia\Config\Config;
use Utopia\Database\Database; use Utopia\Database\Database;
use Utopia\Database\DateTime; use Utopia\Database\DateTime;
use Utopia\Database\Document; use Utopia\Database\Document;
@ -23,90 +24,67 @@ App::get('/v1/project/usage')
->inject('response') ->inject('response')
->inject('dbForProject') ->inject('dbForProject')
->action(function (string $range, Response $response, Database $dbForProject) { ->action(function (string $range, Response $response, Database $dbForProject) {
// $usage = [];
// if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') {
// $periods = [
// '24h' => [
// 'period' => '1h',
// 'limit' => 24,
// ],
// '7d' => [
// 'period' => '1d',
// 'limit' => 7,
// ],
// '30d' => [
// 'period' => '1d',
// 'limit' => 30,
// ],
// '90d' => [
// 'period' => '1d',
// 'limit' => 90,
// ],
// ];
// $metrics = [ $periods = Config::getParam('usage', []);
// 'project.$all.network.requests', $stats = $usage = [];
// 'project.$all.network.bandwidth', $days = $periods[$range];
// 'project.$all.storage.size', $metrics = [
// 'users.$all.count.total', 'network.inbound',
// 'databases.$all.count.total', 'network.outbound',
// 'documents.$all.count.total', 'executions',
// 'executions.$all.compute.total', 'documents',
// 'buckets.$all.count.total' 'databases',
// ]; 'users',
'files.storage',
'buckets',
];
// $stats = []; 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'),
]);
$stats[$metric] = [];
foreach ($results as $result) {
$stats[$metric][$result->getAttribute('time')] = [
'value' => $result->getAttribute('value'),
];
}
}
});
// Authorization::skip(function () use ($dbForProject, $periods, $range, $metrics, &$stats) { $format = match ($days['period']) {
// foreach ($metrics as $metric) { '1h' => 'Y-m-d\TH:00:00.000P',
// $limit = $periods[$range]['limit']; '1d' => 'Y-m-d\T00:00:00.000P',
// $period = $periods[$range]['period']; };
// $requestDocs = $dbForProject->find('stats', [ foreach ($metrics as $metric) {
// Query::equal('period', [$period]), $usage[$metric] = [];
// Query::equal('metric', [$metric]), $leap = time() - ($days['limit'] * $days['factor']);
// Query::limit($limit), while ($leap < time()) {
// Query::orderDesc('time'), $leap += $days['factor'];
// ]); $formatDate = date($format, $leap);
$usage[$metric][] = [
'value' => $stats[$metric][$formatDate]['value'] ?? 0,
'date' => $formatDate,
];
}
$usage[$metric] = array_reverse($usage[$metric]);
}
// $stats[$metric] = []; $response->dynamic(new Document([
// foreach ($requestDocs as $requestDoc) { 'range' => $range,
// $stats[$metric][] = [ 'network' => ($usage[$metrics[0]] + $usage[$metrics[1]]),
// 'value' => $requestDoc->getAttribute('value'), 'executions' => $usage[$metrics[2]],
// 'date' => $requestDoc->getAttribute('time'), 'documents' => $usage[$metrics[3]],
// ]; 'databases' => $usage[$metrics[4]],
// } 'users' => $usage[$metrics[5]],
'storage' => $usage[$metrics[6]],
// // backfill metrics with empty values for graphs 'buckets' => $usage[$metrics[8]],
// $backfill = $limit - \count($requestDocs); ]), Response::MODEL_USAGE_PROJECT);
// while ($backfill > 0) {
// $last = $limit - $backfill - 1; // array index of last added metric
// $diff = match ($period) { // convert period to seconds for unix timestamp math
// '1h' => 3600,
// '1d' => 86400,
// };
// $stats[$metric][] = [
// 'value' => 0,
// 'date' => DateTime::formatTz(DateTime::addSeconds(new \DateTime($stats[$metric][$last]['date'] ?? null), -1 * $diff)),
// ];
// $backfill--;
// }
// $stats[$metric] = array_reverse($stats[$metric]);
// }
// });
// $usage = new Document([
// 'range' => $range,
// 'requests' => $stats[$metrics[0]] ?? [],
// 'network' => $stats[$metrics[1]] ?? [],
// 'storage' => $stats[$metrics[2]] ?? [],
// 'users' => $stats[$metrics[3]] ?? [],
// 'databases' => $stats[$metrics[4]] ?? [],
// 'documents' => $stats[$metrics[5]] ?? [],
// 'executions' => $stats[$metrics[6]] ?? [],
// 'buckets' => $stats[$metrics[7]] ?? [],
// ]);
// }
// $response->dynamic($usage, Response::MODEL_USAGE_PROJECT);
}); });

View file

@ -57,12 +57,12 @@ $databaseListener = function (string $event, Document $document, Document $proje
var_dump($document->getCollection()); var_dump($document->getCollection());
switch (true) { switch (true) {
case $document->getCollection() === 'users':
$queueForUsage->addMetric("users", $value); // per project
break;
case $document->getCollection() === 'teams': case $document->getCollection() === 'teams':
$queueForUsage->addMetric("teams", $value); // per project $queueForUsage->addMetric("teams", $value); // per project
break; break;
case $document->getCollection() === 'users':
$queueForUsage->addMetric("users", $value); // per project
break;
case $document->getCollection() === 'sessions': case $document->getCollection() === 'sessions':
$queueForUsage->addMetric("sessions", $value); // per project $queueForUsage->addMetric("sessions", $value); // per project
break; break;
@ -80,7 +80,6 @@ $databaseListener = function (string $event, Document $document, Document $proje
$dbCollections['value'] $dbCollections['value']
); );
} }
// Documents // Documents
$dbDocuments = $dbForProject->getDocument('stats', md5("_inf_" . "{$document->getId()}" . ".documents")); $dbDocuments = $dbForProject->getDocument('stats', md5("_inf_" . "{$document->getId()}" . ".documents"));
$projectDocuments = $dbForProject->getDocument('stats', md5("_inf_documents")); $projectDocuments = $dbForProject->getDocument('stats', md5("_inf_documents"));
@ -97,11 +96,24 @@ $databaseListener = function (string $event, Document $document, Document $proje
case str_starts_with($document->getCollection(), 'database_'): // collections case str_starts_with($document->getCollection(), 'database_'): // collections
$queueForUsage->addMetric("{$document['databaseId']}" . ".collections", $value); // per database $queueForUsage->addMetric("{$document['databaseId']}" . ".collections", $value); // per database
$queueForUsage->addMetric("collections", $value); // per project $queueForUsage->addMetric("collections", $value); // per project
if ($event === Database::EVENT_DOCUMENT_DELETE) {
// Documents
$dbDocuments = $dbForProject->getDocument('stats', md5("_inf_" . "{$document['databaseId']}" . ".documents"));
$projectDocuments = $dbForProject->getDocument('stats', md5("_inf_documents"));
if (!$dbDocuments->isEmpty()) {
$dbForProject->decreaseDocumentAttribute(
'stats',
$projectDocuments->getId(),
'value',
$dbDocuments['value']
);
}
}
break; break;
case $document->getCollection() === 'documents': case $document->getCollection() === 'documents':
$queueForUsage $queueForUsage
->addMetric("{$document['databaseId']}" . "." . "{$document['collectionId']}" . ".documents", $value) // per collection
->addMetric("{$document['databaseId']}" . ".documents", $value) // per database ->addMetric("{$document['databaseId']}" . ".documents", $value) // per database
->addMetric("{$document['databaseId']}" . "." . "{$document['collectionId']}" . ".documents", $value) // per collection
->addMetric("documents", $value) // per project ->addMetric("documents", $value) // per project
; ;
break; break;

23
composer.lock generated
View file

@ -3315,16 +3315,16 @@
}, },
{ {
"name": "matthiasmullie/minify", "name": "matthiasmullie/minify",
"version": "1.3.69", "version": "1.3.70",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/matthiasmullie/minify.git", "url": "https://github.com/matthiasmullie/minify.git",
"reference": "a61c949cccd086808063611ef9698eabe42ef22f" "reference": "2807d9f9bece6877577ad44acb5c801bb3ae536b"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/matthiasmullie/minify/zipball/a61c949cccd086808063611ef9698eabe42ef22f", "url": "https://api.github.com/repos/matthiasmullie/minify/zipball/2807d9f9bece6877577ad44acb5c801bb3ae536b",
"reference": "a61c949cccd086808063611ef9698eabe42ef22f", "reference": "2807d9f9bece6877577ad44acb5c801bb3ae536b",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -3333,9 +3333,10 @@
"php": ">=5.3.0" "php": ">=5.3.0"
}, },
"require-dev": { "require-dev": {
"friendsofphp/php-cs-fixer": "~2.0", "friendsofphp/php-cs-fixer": ">=2.0",
"matthiasmullie/scrapbook": "dev-master", "matthiasmullie/scrapbook": ">=1.3",
"phpunit/phpunit": ">=4.8" "phpunit/phpunit": ">=4.8",
"squizlabs/php_codesniffer": ">=3.0"
}, },
"suggest": { "suggest": {
"psr/cache-implementation": "Cache implementation to use with Minify::cache" "psr/cache-implementation": "Cache implementation to use with Minify::cache"
@ -3358,12 +3359,12 @@
{ {
"name": "Matthias Mullie", "name": "Matthias Mullie",
"email": "minify@mullie.eu", "email": "minify@mullie.eu",
"homepage": "http://www.mullie.eu", "homepage": "https://www.mullie.eu",
"role": "Developer" "role": "Developer"
} }
], ],
"description": "CSS & JavaScript minifier, in PHP. Removes whitespace, strips comments, combines files (incl. @import statements and small assets in CSS files), and optimizes/shortens a few common programming patterns.", "description": "CSS & JavaScript minifier, in PHP. Removes whitespace, strips comments, combines files (incl. @import statements and small assets in CSS files), and optimizes/shortens a few common programming patterns.",
"homepage": "http://www.minifier.org", "homepage": "https://github.com/matthiasmullie/minify",
"keywords": [ "keywords": [
"JS", "JS",
"css", "css",
@ -3373,7 +3374,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/matthiasmullie/minify/issues", "issues": "https://github.com/matthiasmullie/minify/issues",
"source": "https://github.com/matthiasmullie/minify/tree/1.3.69" "source": "https://github.com/matthiasmullie/minify/tree/1.3.70"
}, },
"funding": [ "funding": [
{ {
@ -3381,7 +3382,7 @@
"type": "github" "type": "github"
} }
], ],
"time": "2022-08-01T09:00:18+00:00" "time": "2022-12-09T12:56:44+00:00"
}, },
{ {
"name": "matthiasmullie/path-converter", "name": "matthiasmullie/path-converter",

View file

@ -23,34 +23,6 @@ class UsageCollection extends Model
'example' => [], 'example' => [],
'array' => true 'array' => true
]) ])
->addRule('documentsCreate', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for documents created.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('documentsRead', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for documents read.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('documentsUpdate', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for documents updated.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('documentsDelete', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for documents deleted.',
'default' => [],
'example' => [],
'array' => true
])
; ;
} }

View file

@ -16,13 +16,6 @@ class UsageDatabase extends Model
'default' => '', 'default' => '',
'example' => '30d', 'example' => '30d',
]) ])
->addRule('documentsCount', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for total number of documents.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('collectionsCount', [ ->addRule('collectionsCount', [
'type' => Response::MODEL_METRIC, 'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for total number of collections.', 'description' => 'Aggregated stats for total number of collections.',
@ -30,58 +23,9 @@ class UsageDatabase extends Model
'example' => [], 'example' => [],
'array' => true 'array' => true
]) ])
->addRule('documentsCreate', [ ->addRule('documentsCount', [
'type' => Response::MODEL_METRIC, 'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for documents created.', 'description' => 'Aggregated stats for total number of documents.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('documentsRead', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for documents read.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('documentsUpdate', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for documents updated.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('documentsDelete', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for documents deleted.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('collectionsCreate', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for collections created.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('collectionsRead', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for collections read.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('collectionsUpdate', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for collections updated.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('collectionsDelete', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for collections delete.',
'default' => [], 'default' => [],
'example' => [], 'example' => [],
'array' => true 'array' => true

View file

@ -23,13 +23,6 @@ class UsageDatabases extends Model
'example' => [], 'example' => [],
'array' => true 'array' => true
]) ])
->addRule('documentsCount', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for total number of documents.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('collectionsCount', [ ->addRule('collectionsCount', [
'type' => Response::MODEL_METRIC, 'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for total number of collections.', 'description' => 'Aggregated stats for total number of collections.',
@ -37,86 +30,9 @@ class UsageDatabases extends Model
'example' => [], 'example' => [],
'array' => true 'array' => true
]) ])
->addRule('databasesCreate', [ ->addRule('documentsCount', [
'type' => Response::MODEL_METRIC, 'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for documents created.', 'description' => 'Aggregated stats for total number of documents.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('databasesRead', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for documents read.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('databasesUpdate', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for documents updated.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('databasesDelete', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for total number of collections.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('documentsCreate', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for documents created.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('documentsRead', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for documents read.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('documentsUpdate', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for documents updated.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('documentsDelete', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for documents deleted.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('collectionsCreate', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for collections created.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('collectionsRead', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for collections read.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('collectionsUpdate', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for collections updated.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('collectionsDelete', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for collections delete.',
'default' => [], 'default' => [],
'example' => [], 'example' => [],
'array' => true 'array' => true

View file

@ -16,13 +16,6 @@ class UsageProject extends Model
'default' => '', 'default' => '',
'example' => '30d', 'example' => '30d',
]) ])
->addRule('requests', [
'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for number of requests.',
'default' => [],
'example' => [],
'array' => true
])
->addRule('network', [ ->addRule('network', [
'type' => Response::MODEL_METRIC, 'type' => Response::MODEL_METRIC,
'description' => 'Aggregated stats for consumed bandwidth.', 'description' => 'Aggregated stats for consumed bandwidth.',