1
0
Fork 0
mirror of synced 2024-06-14 00:34:51 +12:00

fix formatting

This commit is contained in:
Damodar Lohani 2022-06-13 15:49:27 +00:00
parent ae9bed4ab7
commit 05152da195
3 changed files with 33 additions and 29 deletions

View file

@ -14,7 +14,7 @@ use Utopia\Database\Database;
use Utopia\Database\Validator\Authorization;
use Utopia\Registry\Registry;
function getDatabase(Registry&$register, string $namespace): Database
function getDatabase(Registry &$register, string $namespace): Database
{
$attempts = 0;
@ -46,7 +46,7 @@ function getDatabase(Registry&$register, string $namespace): Database
return $database;
}
function getInfluxDB(Registry&$register): InfluxDatabase
function getInfluxDB(Registry &$register): InfluxDatabase
{
/** @var InfluxDB\Client $client */
$client = $register->get('influxdb');
@ -72,7 +72,7 @@ function getInfluxDB(Registry&$register): InfluxDatabase
return $database;
}
$logError = function($message, $stackTrace) {
$logError = function ($message, $stackTrace) {
Console::warning("Failed: {$message}");
Console::warning($stackTrace);
};

View file

@ -7,7 +7,8 @@ use Utopia\Database\Document;
use InfluxDB\Database as InfluxDatabase;
use DateTime;
class Usage {
class Usage
{
protected InfluxDatabase $influxDB;
protected Database $database;
protected $errorHandler;
@ -166,14 +167,14 @@ class Usage {
/**
* Create or Update Mertic
* Create or update each metric in the stats collection for the given project
*
*
* @param string $projectId
* @param int $time
* @param string $period
* @param string $metric
* @param int $value
* @param int $type
*
*
* @return void
*/
private function createOrUpdateMetric(string $projectId, int $time, string $period, string $metric, int $value, int $type): void
@ -200,7 +201,7 @@ class Usage {
}
$this->latestTime[$metric][$period['key']] = $time;
} catch (\Exception $e) { // if projects are deleted this might fail
if(is_callable($this->errorHandler)) {
if (is_callable($this->errorHandler)) {
call_user_func($this->errorHandler, "Unable to save data for project {$projectId} and metric {$metric}: {$e->getMessage()}", $e->getTraceAsString());
} else {
throw $e;
@ -211,11 +212,11 @@ class Usage {
/**
* Sync From InfluxDB
* Sync stats from influxDB to stats collection in the Appwrite database
*
*
* @param string $metric
* @param array $options
* @param array $period
*
*
* @return void
*/
private function syncFromInfluxDB(string $metric, array $options, array $period): void
@ -272,7 +273,7 @@ class Usage {
/**
* Collect Stats
* Collect all the stats from Influd DB to Database
*
*
* @return void
*/
public function collect(): void
@ -282,7 +283,7 @@ class Usage {
try {
$this->syncFromInfluxDB($metric, $options, $period);
} catch (\Exception $e) {
if(is_callable($this->errorHandler)) {
if (is_callable($this->errorHandler)) {
call_user_func($this->errorHandler, $e->getMessage(), $e->getTraceAsString());
} else {
throw $e;
@ -291,4 +292,4 @@ class Usage {
}
}
}
}
}

View file

@ -46,12 +46,12 @@ class UsageDB extends Usage
$document->getId(),
$document->setAttribute('value', $value)
);
}
}
} catch (\Exception$e) { // if projects are deleted this might fail
if (is_callable($this->errorHandler)) {
call_user_func($this->errorHandler, "Unable to save data for project {$projectId} and metric {$metric}: {$e->getMessage()}", $e->getTraceAsString());
} else {
throw $e;
throw $e;
}
}
}
@ -60,12 +60,12 @@ class UsageDB extends Usage
/**
* Foreach Document
* Call provided callback for each document in the collection
*
*
* @param string $projectId
* @param string $collection
* @param array $queries
* @param callable $callback
*
*
* @return void
*/
private function foreachDocument(string $projectId, string $collection, array $queries, callable $callback): void
@ -78,6 +78,9 @@ class UsageDB extends Usage
while ($sum === $limit) {
$results = $this->database->find($collection, $queries, $limit, cursor:$latestDocument);
if (empty($results)) {
return;
}
$sum = count($results);
@ -93,12 +96,12 @@ class UsageDB extends Usage
/**
* Sum
* Calculate sum of a attribute of documents in collection
*
*
* @param string $projectId
* @param string $collection
* @param string $attribute
* @param string $metric
*
*
* @return int
*/
private function sum(string $projectId, string $collection, string $attribute, string $metric): int
@ -112,11 +115,11 @@ class UsageDB extends Usage
/**
* Count
* Count number of documents in collection
*
*
* @param string $projectId
* @param string $collection
* @param string $metric
*
*
* @return int
*/
private function count(string $projectId, string $collection, string $metric): int
@ -131,9 +134,9 @@ class UsageDB extends Usage
/**
* Deployments Total
* Total sum of storage used by deployments
*
*
* @param string $projectId
*
*
* @return int
*/
private function deploymentsTotal(string $projectId): int
@ -158,9 +161,9 @@ class UsageDB extends Usage
* Storage Stats
* Metrics: storage.total, storage.files.total, storage.buckets.{bucketId}.files.total,
* storage.buckets.count, storage.files.count, storage.buckets.{bucketId}.files.count
*
*
* @param string $projectId
*
*
* @return void
*/
private function storageStats(string $projectId): void
@ -173,7 +176,7 @@ class UsageDB extends Usage
$metric = 'storage.buckets.count';
$this->count($projectId, 'buckets', $metric);
$this->foreachDocument($projectId, 'buckets', [], function ($bucket) use (&$projectFilesCount, &$projectFilesTotal, $projectId, ) {
$this->foreachDocument($projectId, 'buckets', [], function ($bucket) use (&$projectFilesCount, &$projectFilesTotal, $projectId,) {
$metric = "storage.buckets.{$bucket->getId()}.files.count";
$count = $this->count($projectId, 'buckets_' . $bucket->getInternalId(), $metric);
@ -195,9 +198,9 @@ class UsageDB extends Usage
* Collect all database stats
* Metrics: database.collections.count, database.collections.{collectionId}.documents.count,
* database.documents.count
*
*
* @param string $projectId
*
*
* @return void
*/
private function databaseStats(string $projectId): void
@ -207,7 +210,7 @@ class UsageDB extends Usage
$metric = 'database.collections.count';
$this->count($projectId, 'collections', $metric);
$this->foreachDocument($projectId, 'collections', [], function ($collection) use (&$projectDocumentsCount, $projectId, ) {
$this->foreachDocument($projectId, 'collections', [], function ($collection) use (&$projectDocumentsCount, $projectId,) {
$metric = "database.collections.{$collection->getId()}.documents.count";
$count = $this->count($projectId, 'collection_' . $collection->getInternalId(), $metric);
@ -220,7 +223,7 @@ class UsageDB extends Usage
/**
* Collect Stats
* Collect all database related stats
*
*
* @return void
*/
public function collect(): void