1
0
Fork 0
mirror of synced 2024-07-01 20:50:49 +12:00

refactor to use db pools

This commit is contained in:
Damodar Lohani 2022-10-31 07:31:31 +00:00
parent 982a449143
commit 546d8ba583
2 changed files with 31 additions and 12 deletions

View file

@ -3,13 +3,11 @@
global $cli, $register;
use Appwrite\Usage\Calculators\TimeSeries;
use InfluxDB\Database as InfluxDatabase;
use Utopia\App;
use Utopia\CLI\Console;
use Utopia\Database\Database as UtopiaDatabase;
use Utopia\Database\Document;
use Utopia\Database\Validator\Authorization;
use Utopia\Logger\Log;
use Utopia\Validator\WhiteList;
Authorization::disable();
Authorization::setDefaultStatus(false);
@ -51,15 +49,16 @@ $logError = function (Throwable $error, string $action = 'syncUsageStats') use (
$cli
->task('usage')
->desc('Schedules syncing data from influxdb to Appwrite console db')
->action(function () use ($logError) {
->action(function () use ($register, $logError) {
Console::title('Usage Aggregation V1');
Console::success(APP_NAME . ' usage aggregation process v1 has started');
$database = getConsoleDB();
$influxDB = getInfluxDB();
$getProjectDB = fn (Document $project) => getProjectDB($project);
$interval = (int) App::getEnv('_APP_USAGE_AGGREGATION_INTERVAL', '30'); // 30 seconds (by default)
$usage = new TimeSeries($database, $influxDB, $logError);
$usage = new TimeSeries($database, $influxDB, $getProjectDB, $register, $logError);
Console::loop(function () use ($interval, $usage) {
$now = date('d-m-Y H:i:s', time());

View file

@ -7,6 +7,7 @@ use Utopia\Database\Database;
use Utopia\Database\Document;
use InfluxDB\Database as InfluxDatabase;
use DateTime;
use Utopia\Registry\Registry;
class TimeSeries extends Calculator
{
@ -31,6 +32,20 @@ class TimeSeries extends Calculator
*/
protected $errorHandler;
/**
* Callback to get project DB
*
* @var callable
*/
protected mixed $getProjectDB;
/**
* Registry
*
* @var Registry
*/
protected Registry $register;
/**
* Latest times for metric that was synced to the database
*
@ -381,10 +396,12 @@ class TimeSeries extends Calculator
]
];
public function __construct(Database $database, InfluxDatabase $influxDB, callable $errorHandler = null)
public function __construct(Database $database, InfluxDatabase $influxDB, callable $getProjectDB, Registry $register, callable $errorHandler = null)
{
$this->database = $database;
$this->influxDB = $influxDB;
$this->getProjectDB = $getProjectDB;
$this->register = $register;
$this->errorHandler = $errorHandler;
}
@ -404,12 +421,13 @@ class TimeSeries extends Calculator
private function createOrUpdateMetric(string $projectId, string $time, string $period, string $metric, int $value, int $type): void
{
$id = \md5("{$time}_{$period}_{$metric}");
$this->database->setNamespace('_' . $projectId);
$project = $this->database->getDocument('projects', $projectId);
$database = call_user_func($this->getProjectDB, $project);
try {
$document = $this->database->getDocument('stats', $id);
$document = $database->getDocument('stats', $id);
if ($document->isEmpty()) {
$this->database->createDocument('stats', new Document([
$database->createDocument('stats', new Document([
'$id' => $id,
'period' => $period,
'time' => $time,
@ -418,7 +436,7 @@ class TimeSeries extends Calculator
'type' => $type,
]));
} else {
$this->database->updateDocument(
$database->updateDocument(
'stats',
$document->getId(),
$document->setAttribute('value', $value)
@ -431,6 +449,8 @@ class TimeSeries extends Calculator
throw $e;
}
}
$this->register->get('pools')->reclaim();
}
/**
@ -466,7 +486,7 @@ class TimeSeries extends Calculator
$query .= "WHERE \"time\" > '{$start}' ";
$query .= "AND \"time\" < '{$end}' ";
$query .= "AND \"metric_type\"='counter' {$filters} ";
$query .= "GROUP BY time({$period['key']}), \"projectId\", \"projectInternalId\" {$groupBy} ";
$query .= "GROUP BY time({$period['key']}), \"projectId\" {$groupBy} ";
$query .= "FILL(null)";
try {
@ -490,7 +510,7 @@ class TimeSeries extends Calculator
$value = (!empty($point['value'])) ? $point['value'] : 0;
$this->createOrUpdateMetric(
$point['projectInternalId'],
$point['projectId'],
$point['time'],
$period['key'],
$metricUpdated,