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

117 lines
4.2 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-22 08:03:09 +13:00
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-22 08:03:09 +13:00
$projectId = $project->getInternalId();
$stats[$projectId]['database'] = $project->getAttribute('database');
2022-12-07 00:36:17 +13:00
2022-12-05 06:06:23 +13:00
foreach ($payload['metrics'] ?? [] as $metric) {
2022-12-22 08:03:09 +13:00
if (!isset($stats[$projectId]['keys'][$metric['key']])) {
$stats[$projectId]['keys'][$metric['key']] = $metric['value'];
2022-12-05 06:06:23 +13:00
continue;
}
2022-12-22 08:03:09 +13:00
$stats[$projectId]['keys'][$metric['key']] += $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) {
2022-12-22 08:03:09 +13:00
Timer::tick(30000, function () use ($register, $cache, $pools, $periods, &$stats) {
2022-12-22 08:03:09 +13:00
$offset = count($stats);
$projects = array_slice($stats, 0, $offset, true);
array_splice($stats, 0, $offset);
2022-12-22 08:03:09 +13:00
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;
2022-12-07 00:36:17 +13:00
}
2022-12-13 20:35:05 +13:00
2022-12-22 08:03:09 +13:00
foreach ($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
);
}
}
}
2022-12-07 00:36:17 +13:00
}
2022-12-22 08:03:09 +13:00
$dbForProject->createDocument('statsLogger', new Document([
'time' => DateTime::now(),
'metrics' => $project['keys'],
]));
} catch (\Exception $e) {
console::error($e->getMessage());
} finally {
$pools->reclaim();
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();