1
0
Fork 0
mirror of synced 2024-10-02 18:26:49 +13:00

usageHook tweaks

This commit is contained in:
shimon 2023-12-25 11:32:40 +02:00
parent b398338876
commit 4a837907d1

View file

@ -3,13 +3,12 @@
namespace Appwrite\Platform\Workers; namespace Appwrite\Platform\Workers;
use Utopia\App; use Utopia\App;
use Utopia\Database\Database;
use Utopia\Database\Document; use Utopia\Database\Document;
use Utopia\Database\Exception\Duplicate; use Utopia\Database\Exception\Duplicate;
use Utopia\Platform\Action; use Utopia\Platform\Action;
use Utopia\CLI\Console; use Utopia\CLI\Console;
use Swoole\Timer; use Swoole\Timer;
use Utopia\Registry\Registry; use Utopia\Database\DateTime;
class UsageHook extends Usage class UsageHook extends Usage
{ {
@ -39,55 +38,66 @@ class UsageHook extends Usage
public function action($register, $getProjectDB): void public function action($register, $getProjectDB): void
{ {
$interval = (int) App::getEnv('_APP_USAGE_AGGREGATION_INTERVAL', '60000'); $interval = (int) App::getEnv('_APP_USAGE_AGGREGATION_INTERVAL', '60000');
Timer::tick($interval, function () use ($register, $getProjectDB) { Timer::tick($interval, function () use ($register, $getProjectDB) {
$offset = count(self::$stats);
$projects = array_slice(self::$stats, 0, $offset, true);
array_splice(self::$stats, 0, $offset);
foreach ($projects as $data) {
try {
$dbForProject = $getProjectDB($data['project']);
foreach ($data['keys'] ?? [] as $key => $value) {
if ($value == 0) {
continue;
}
foreach ($this->periods as $period => $format) { $offset = count(self::$stats);
$time = 'inf' === $period ? null : date($format, time()); $projects = array_slice(self::$stats, 0, $offset, true);
$id = \md5("{$time}_{$period}_{$key}"); array_splice(self::$stats, 0, $offset);
foreach ($projects as $data) {
$numberOfKeys = !empty($data['keys']) ? count($data['keys']) : 0;
console::log(DateTime::now() . ' Iterating over ' . $numberOfKeys . ' keys');
try { if ($numberOfKeys === 0) {
$dbForProject->createDocument('stats_v2', new Document([ continue;
}
$projectInternalId = $data['project']->getInternalId();
try {
$dbForProject = $getProjectDB($data['project']);
foreach ($data['keys'] ?? [] as $key => $value) {
if ($value == 0) {
continue;
}
foreach ($this->periods as $period => $format) {
$time = 'inf' === $period ? null : date($format, time());
$id = \md5("{$time}_{$period}_{$key}");
try {
$dbForProject->createDocument('stats_v2', new Document([
'$id' => $id, '$id' => $id,
'period' => $period, 'period' => $period,
'time' => $time, 'time' => $time,
'metric' => $key, 'metric' => $key,
'value' => $value, 'value' => $value,
'region' => App::getEnv('_APP_REGION', 'default'), 'region' => App::getEnv('_APP_REGION', 'default'),
])); ]));
} catch (Duplicate $th) { } catch (Duplicate $th) {
if ($value < 0) { if ($value < 0) {
$dbForProject->decreaseDocumentAttribute( $dbForProject->decreaseDocumentAttribute(
'stats_v2', 'stats_v2',
$id, $id,
'value', 'value',
abs($value) abs($value)
); );
} else { } else {
$dbForProject->increaseDocumentAttribute( $dbForProject->increaseDocumentAttribute(
'stats_v2', 'stats_v2',
$id, $id,
'value', 'value',
$value $value
); );
}
} }
} }
} }
} catch (\Exception $e) {
console::error("[logger] " . " {DateTime::now()} " . " {$data['project']->getInternalId()} " . " {$e->getMessage()}");
} }
} catch (\Exception $e) {
console::error(DateTime::now() . ' ' . $projectInternalId . ' ' . $e->getMessage());
} }
}); }
});
} }
} }