1
0
Fork 0
mirror of synced 2024-09-07 21:31:38 +12:00
appwrite/app/workers/usage.php

128 lines
4.5 KiB
PHP
Raw Normal View History

2022-12-05 06:06:23 +13:00
<?php
require_once __DIR__ . '/../worker.php';
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;
2022-12-13 20:35:05 +13:00
use Utopia\Database\DateTime;
2022-12-07 00:36:17 +13:00
use Utopia\Database\Document;
2022-12-13 20:35:05 +13:00
use Utopia\Database\Exception\Duplicate;
2022-12-07 00:36:17 +13:00
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-09 02:55:18 +13:00
Authorization::disable();
Authorization::setDefaultStatus(false);
2022-12-07 00:36:17 +13:00
$stats = [];
2022-12-09 08:06:55 +13:00
$periods['1h'] = 'Y-m-d H:00';
$periods['1d'] = 'Y-m-d 00:00';
2022-12-16 06:00:05 +13:00
//$periods['1m'] = 'Y-m-1 00:00';
2022-12-09 08:06:55 +13:00
$periods['inf'] = '0000-00-00 00:00';
2022-12-07 00:36:17 +13:00
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-08 06:56:38 +13:00
$uniq = md5($metric['key']);
2022-12-07 00:36:17 +13:00
if (!isset($stats[$uniq])) {
$stats[$uniq] = [
'projectInternalId' => $project->getInternalId(),
'database' => $project->getAttribute('database'),
2022-12-08 06:56:38 +13:00
'key' => $metric['key'],
2022-12-07 00:36:17 +13:00
'value' => $metric['value']
];
2022-12-05 06:06:23 +13:00
continue;
}
2022-12-08 06:56:38 +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(3000, function () use ($register, $cache, $pools, $periods, &$stats) {
2022-12-12 22:37:10 +13:00
$slice = array_slice($stats, 0, count($stats));
array_splice($stats, 0, count($stats));
2022-12-19 21:25:49 +13:00
//$log = [];
2022-12-12 22:37:10 +13:00
foreach ($slice as $metric) {
if ($metric['value'] == 0) {
continue;
}
2022-12-13 20:35:05 +13:00
$dbForProject = new Database(
$pools
->get($metric['database'])
->pop()
->getResource(),
$cache
);
2022-12-13 20:35:05 +13:00
$dbForProject->setNamespace('_' . $metric['projectInternalId']);
2022-12-12 22:37:10 +13:00
foreach ($periods as $period => $format) {
$time = 'inf' === $period ? null : date($format, time());
$id = \md5("{$time}_{$period}_{$metric['key']}");
try {
2022-12-13 20:35:05 +13:00
try {
2022-12-12 22:37:10 +13:00
$dbForProject->createDocument('stats', new Document([
'$id' => $id,
2022-12-12 22:37:10 +13:00
'period' => $period,
'time' => $time,
2022-12-12 22:37:10 +13:00
'metric' => $metric['key'],
'value' => $metric['value'],
2022-12-12 22:37:10 +13:00
'region' => App::getEnv('_APP_REGION', 'default'),
]));
2022-12-13 20:35:05 +13:00
} catch (Duplicate $th) {
if ($metric['value'] < 0) {
$dbForProject->decreaseDocumentAttribute(
'stats',
$id,
'value',
abs($metric['value'])
);
} else {
$dbForProject->increaseDocumentAttribute(
'stats',
$id,
'value',
$metric['value']
);
}
2022-12-07 00:36:17 +13:00
}
2022-12-13 20:35:05 +13:00
2022-12-19 21:25:49 +13:00
// $log[] = [
// 'id' => $id,
// 'period' => $period,
// 'time' => $time,
// 'metric' => $metric['key'],
// 'value' => $metric['value'],
// 'region' => App::getEnv('_APP_REGION', 'default'),
// ];
2022-12-12 22:37:10 +13:00
} catch (\Exception $e) {
console::error($e->getMessage());
} finally {
$pools->reclaim();
2022-12-07 00:36:17 +13:00
}
2022-12-12 22:37:10 +13:00
}
2022-12-13 20:35:05 +13:00
2022-12-19 21:25:49 +13:00
// if (!empty($log)) {
// $dbForProject->createDocument('statsLogger', new Document([
// 'time' => DateTime::now(),
// 'metrics' => $log,
// ]));
// }
2022-12-12 22:37:10 +13:00
}
});
2022-12-05 06:06:23 +13:00
});
2022-12-13 20:35:05 +13:00
$server->start();