users addition
This commit is contained in:
parent
785c00ae5c
commit
89f0146c45
|
@ -1 +1 @@
|
|||
Subproject commit 9174d8f8cb584744dd7a53f69d324f490ee82ee3
|
||||
Subproject commit b981302dee30eab33e155af79f0088822b29a2b6
|
|
@ -274,8 +274,5 @@ $worker
|
|||
Console::error('[Error] Line: ' . $error->getLine());
|
||||
});
|
||||
|
||||
$worker->workerStart()
|
||||
->action(function () use ($workerName) {
|
||||
Console::info("Worker $workerName started");
|
||||
});
|
||||
|
||||
$worker->start();
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
#!/bin/sh
|
||||
|
||||
QUEUE=v1-usage php /usr/src/code/app/workers/usage.php $@
|
||||
php /usr/src/code/app/worker.php usage $@
|
5216
composer.lock
generated
5216
composer.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -636,6 +636,8 @@ services:
|
|||
volumes:
|
||||
- ./app:/usr/src/code/app
|
||||
- ./src:/usr/src/code/src
|
||||
- ./vendor/utopia-php/platform:/usr/src/code/vendor/utopia-php/platform
|
||||
|
||||
depends_on:
|
||||
- redis
|
||||
- mariadb
|
||||
|
|
|
@ -13,6 +13,7 @@ use Appwrite\Platform\Workers\Functions;
|
|||
use Appwrite\Platform\Workers\Builds;
|
||||
use Appwrite\Platform\Workers\Deletes;
|
||||
use Appwrite\Platform\Workers\Usage;
|
||||
use Appwrite\Platform\Workers\UsageHook;
|
||||
|
||||
class Workers extends Service
|
||||
{
|
||||
|
@ -29,7 +30,9 @@ class Workers extends Service
|
|||
->addAction(Functions::getName(), new Functions())
|
||||
->addAction(Builds::getName(), new Builds())
|
||||
->addAction(Deletes::getName(), new Deletes())
|
||||
//->addAction(Usage::getName(), new Usage())
|
||||
->addAction(UsageHook::getName(), new UsageHook())
|
||||
->addAction(Usage::getName(), new Usage())
|
||||
|
||||
;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,17 +11,15 @@ use Utopia\Queue\Message;
|
|||
|
||||
class Usage extends Action
|
||||
{
|
||||
private $stats = [];
|
||||
private array $periods = [
|
||||
'1h' => 'Y-m-d H:00',
|
||||
'1d' => 'Y-m-d 00:00',
|
||||
'inf' => '0000-00-00 00:00'
|
||||
protected static array $stats = [];
|
||||
protected array $periods = [
|
||||
'1h' => 'Y-m-d H:00',
|
||||
'1d' => 'Y-m-d 00:00',
|
||||
'inf' => '0000-00-00 00:00'
|
||||
];
|
||||
|
||||
|
||||
const INFINITY_PERIOD = '_inf_';
|
||||
|
||||
|
||||
public static function getName(): string
|
||||
{
|
||||
return 'usage';
|
||||
|
@ -32,12 +30,13 @@ class Usage extends Action
|
|||
*/
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
$this
|
||||
->desc('Usage worker')
|
||||
->inject('message')
|
||||
->inject('pools')
|
||||
->inject('cache')
|
||||
->callback(function ($message, $pools, $cache) use (&$stats) {
|
||||
->callback(function ($message, $pools, $cache) {
|
||||
$this->action($message, $pools, $cache);
|
||||
});
|
||||
}
|
||||
|
@ -71,13 +70,13 @@ class Usage extends Action
|
|||
);
|
||||
}
|
||||
|
||||
$stats[$projectId]['database'] = $project->getAttribute('database');
|
||||
self::$stats[$projectId]['database'] = $project->getAttribute('database');
|
||||
foreach ($payload['metrics'] ?? [] as $metric) {
|
||||
if (!isset($stats[$projectId]['keys'][$metric['key']])) {
|
||||
$stats[$projectId]['keys'][$metric['key']] = $metric['value'];
|
||||
if (!isset(self::$stats[$projectId]['keys'][$metric['key']])) {
|
||||
self::$stats[$projectId]['keys'][$metric['key']] = $metric['value'];
|
||||
continue;
|
||||
}
|
||||
$stats[$projectId]['keys'][$metric['key']] += $metric['value'];
|
||||
self::$stats[$projectId]['keys'][$metric['key']] += $metric['value'];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
106
src/Appwrite/Platform/Workers/UsageHook.php
Normal file
106
src/Appwrite/Platform/Workers/UsageHook.php
Normal file
|
@ -0,0 +1,106 @@
|
|||
<?php
|
||||
|
||||
namespace Appwrite\Platform\Workers;
|
||||
|
||||
use Utopia\App;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\DateTime;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Exception\Duplicate;
|
||||
use Utopia\Platform\Action;
|
||||
use Utopia\CLI\Console;
|
||||
use Swoole\Timer;
|
||||
|
||||
class UsageHook extends Usage
|
||||
{
|
||||
public static function getName(): string
|
||||
{
|
||||
return 'usageHook';
|
||||
}
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
$this
|
||||
->setType(Action::TYPE_WORKER_START)
|
||||
->inject('register')
|
||||
->inject('cache')
|
||||
->inject('pools')
|
||||
->callback(function ($register, $cache, $pools) {
|
||||
$this->action($register, $cache, $pools);
|
||||
})
|
||||
;
|
||||
}
|
||||
|
||||
public function action($register, $cache, $pools): void
|
||||
{
|
||||
Timer::tick(30000, function () use ($register, $cache, $pools) {
|
||||
|
||||
$offset = count(self::$stats);
|
||||
$projects = array_slice(self::$stats, 0, $offset, true);
|
||||
array_splice(self::$stats, 0, $offset);
|
||||
|
||||
foreach ($projects as $projectInternalId => $project) {
|
||||
try {
|
||||
$dbForProject = new Database(
|
||||
$pools
|
||||
->get($project['database'])
|
||||
->pop()
|
||||
->getResource(),
|
||||
$cache
|
||||
);
|
||||
|
||||
$dbForProject->setNamespace('_' . $projectInternalId);
|
||||
|
||||
foreach ($project['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', new Document([
|
||||
'$id' => $id,
|
||||
'period' => $period,
|
||||
'time' => $time,
|
||||
'metric' => $key,
|
||||
'value' => $value,
|
||||
'region' => App::getEnv('_APP_REGION', 'default'),
|
||||
]));
|
||||
} catch (Duplicate $th) {
|
||||
if ($value < 0) {
|
||||
$dbForProject->decreaseDocumentAttribute(
|
||||
'stats',
|
||||
$id,
|
||||
'value',
|
||||
abs($value)
|
||||
);
|
||||
} else {
|
||||
$dbForProject->increaseDocumentAttribute(
|
||||
'stats',
|
||||
$id,
|
||||
'value',
|
||||
$value
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!empty($project['keys'])) {
|
||||
$dbForProject->createDocument('statsLogger', new Document([
|
||||
'time' => DateTime::now(),
|
||||
'metrics' => $project['keys'],
|
||||
]));
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
console::error("[logger] " . " {DateTime::now()} " . " {$projectInternalId} " . " {$e->getMessage()}");
|
||||
} finally {
|
||||
$pools->reclaim();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue