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

106 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;
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-09 02:55:18 +13:00
Authorization::disable();
Authorization::setDefaultStatus(false);
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';
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-07 00:36:17 +13:00
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(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());
2022-12-09 02:55:18 +13:00
$timestamp = $period === 'inf' ? 0 : $time;
$id = \md5("{$timestamp}_{$period}_{$metric['key']}");
var_dump("{$timestamp}_{$period}_{$metric['key']}");
var_dump($id);
2022-12-07 00:36:17 +13:00
$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 {
2022-12-09 02:55:18 +13:00
$document = $adapter->getDocument('stats', $id);
2022-12-07 00:36:17 +13:00
if ($document->isEmpty()) {
2022-12-09 02:55:18 +13:00
//console::log("{$period}, {$time}, {$metric['key']}={$metric['value']}");
$adapter->createDocument('stats', new Document([
2022-12-07 00:36:17 +13:00
'$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-09 02:55:18 +13:00
]));
2022-12-07 00:36:17 +13:00
} else {
$value = $document->getAttribute('value') + $metric['value'];
2022-12-09 02:55:18 +13:00
//console::info("{$document->getAttribute('period')}, {$document->getAttribute('time')}, {$document->getAttribute('metric')} = {$value}");
$adapter->updateDocument(
'stats',
$document->getId(),
$document->setAttribute('value', $document->getAttribute('value') + $metric['value'])
);
2022-12-07 00:36:17 +13:00
}
} catch (\Exception $e) {
console::error($e->getMessage());
}
$pools->reclaim();
}
}
});
2022-12-05 06:06:23 +13:00
});
$server->start();