2022-12-05 06:06:23 +13:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once __DIR__ . '/../worker.php';
|
|
|
|
|
2022-12-07 00:36:17 +13:00
|
|
|
use Swoole\Table;
|
2022-12-05 06:06:23 +13:00
|
|
|
use Swoole\Timer;
|
2022-12-07 03:17:56 +13:00
|
|
|
use Utopia\App;
|
2022-12-07 00:36:17 +13:00
|
|
|
use Utopia\Database\Database;
|
|
|
|
use Utopia\Database\Document;
|
|
|
|
use Utopia\Database\Validator\Authorization;
|
2022-12-05 06:06:23 +13:00
|
|
|
use Utopia\Queue\Message;
|
2022-12-07 00:36:17 +13:00
|
|
|
use Utopia\CLI\Console;
|
2022-12-05 06:06:23 +13:00
|
|
|
|
2022-12-07 00:36:17 +13:00
|
|
|
$stats = [];
|
|
|
|
|
|
|
|
$periods['1h'] = 'Y-m-d H:00';
|
|
|
|
$periods['1d'] = 'Y-m-d 00:00';
|
|
|
|
$periods['inf'] = 'Y-m-d 00:00';
|
|
|
|
|
|
|
|
//$stats = new Table(10000, 1);
|
|
|
|
//$stats->column('namespace', Table::TYPE_STRING, 64);
|
|
|
|
//$stats->column('key', Table::TYPE_STRING, 64);
|
|
|
|
//$stats->column('value', Table::TYPE_INT);
|
|
|
|
//$stats->create();
|
2022-12-05 06:06:23 +13:00
|
|
|
|
|
|
|
$server->job()
|
|
|
|
->inject('message')
|
2022-12-07 00:36:17 +13:00
|
|
|
->action(function (Message $message) use (&$stats) {
|
2022-12-05 06:06:23 +13:00
|
|
|
$payload = $message->getPayload() ?? [];
|
2022-12-07 00:36:17 +13:00
|
|
|
$project = new Document($payload['project'] ?? []);
|
|
|
|
|
2022-12-05 06:06:23 +13:00
|
|
|
foreach ($payload['metrics'] ?? [] as $metric) {
|
2022-12-07 00:36:17 +13:00
|
|
|
$uniq = md5($metric['namespace'] . $metric['key']);
|
|
|
|
|
|
|
|
// if ($stats->exists($uniq)) {
|
|
|
|
// $stats->incr($uniq, 'value', $metric['value']);
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// $stats->set($uniq, [
|
|
|
|
// 'projectInternalId' => $project->getInternalId(),
|
|
|
|
// 'database' => $project->getAttribute('database'),
|
|
|
|
// 'key' => $metric['key'],
|
|
|
|
// 'value' => $metric['value'],
|
|
|
|
// ]);
|
|
|
|
//
|
|
|
|
|
|
|
|
if (!isset($stats[$uniq])) {
|
|
|
|
$stats[$uniq] = [
|
|
|
|
'projectInternalId' => $project->getInternalId(),
|
|
|
|
'database' => $project->getAttribute('database'),
|
|
|
|
'key' => $metric['namespace'] . '.' . $metric['key'],
|
|
|
|
'value' => $metric['value']
|
|
|
|
];
|
|
|
|
|
2022-12-05 06:06:23 +13:00
|
|
|
continue;
|
|
|
|
}
|
2022-12-07 00:36:17 +13:00
|
|
|
|
|
|
|
$stats[$uniq]['value'] += $metric['value'];
|
2022-12-05 06:06:23 +13:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$server
|
|
|
|
->workerStart()
|
2022-12-07 00:36:17 +13:00
|
|
|
->inject('register')
|
|
|
|
->inject('cache')
|
|
|
|
->inject('pools')
|
|
|
|
->action(function ($register, $cache, $pools) use ($periods, &$stats) {
|
|
|
|
Timer::tick(30000, function () use ($register, $cache, $pools, $periods, &$stats) {
|
|
|
|
|
|
|
|
$slice = array_slice($stats, 0, count($stats));
|
|
|
|
array_splice($stats, 0, count($stats));
|
|
|
|
|
|
|
|
foreach ($slice as $metric) {
|
|
|
|
foreach ($periods as $period => $format) {
|
|
|
|
$time = date($format, time());
|
|
|
|
$id = \md5("{$time}_{$period}_{$metric['key']}");
|
|
|
|
|
|
|
|
$adapter = new Database(
|
|
|
|
$pools
|
|
|
|
->get($metric['database'])
|
|
|
|
->pop()
|
|
|
|
->getResource(),
|
|
|
|
$cache
|
|
|
|
);
|
2022-12-05 23:43:31 +13:00
|
|
|
|
2022-12-07 00:36:17 +13:00
|
|
|
$adapter->setNamespace('_' . $metric['projectInternalId']);
|
2022-12-05 06:06:23 +13:00
|
|
|
|
2022-12-07 00:36:17 +13:00
|
|
|
try {
|
|
|
|
$document = Authorization::skip(fn() =>$adapter->getDocument('stats', $id));
|
|
|
|
if ($document->isEmpty()) {
|
|
|
|
console::log("{$period}, {$time}, {$metric['key']}={$metric['value']}");
|
|
|
|
Authorization::skip(fn() => $adapter->createDocument('stats', new Document([
|
|
|
|
'$id' => $id,
|
|
|
|
'period' => $period,
|
|
|
|
'time' => $time,
|
|
|
|
'metric' => $metric['key'],
|
|
|
|
'value' => $metric['value'],
|
|
|
|
'type' => 0,
|
2022-12-07 03:17:56 +13:00
|
|
|
'region' => App::getEnv('_APP_REGION', 'default'),
|
2022-12-07 00:36:17 +13:00
|
|
|
])));
|
|
|
|
} else {
|
|
|
|
$value = $document->getAttribute('value') + $metric['value'];
|
|
|
|
console::info("{$document->getAttribute('period')}, {$document->getAttribute('time')}, {$document->getAttribute('metric')} = {$value}");
|
|
|
|
Authorization::skip(fn() => $adapter->updateDocument('stats', $document->getId(), $document->setAttribute('value', $value)));
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
console::error($e->getMessage());
|
|
|
|
}
|
|
|
|
$pools->reclaim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-12-05 06:06:23 +13:00
|
|
|
});
|
|
|
|
|
|
|
|
$server->start();
|