10000 Update usage-dump payload by shimonewman · Pull Request #9085 · appwrite/appwrite · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Update usage-dump payload #9085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Appwrite/Platform/Workers/Usage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ class Usage extends Action
{
private array $stats = [];
private int $lastTriggeredTime = 0;
private int $aggregationInterval = 20;
private int $keys = 0;
private const INFINITY_PERIOD = '_inf_';
private const KEYS_THRESHOLD = 10000;
private const KEYS_THRESHOLD = 20000;

public static function getName(): string
{
Expand All @@ -39,6 +40,7 @@ public function __construct()
$this->action($message, $getProjectDB, $queueForUsageDump);
});

$this->aggregationInterval = (int) System::getEnv('_APP_USAGE_AGGREGATION_INTERVAL', '20');
$this->lastTriggeredTime = time();
}

Expand All @@ -56,9 +58,7 @@ public function action(Message $message, callable $getProjectDB, UsageDump $queu
if (empty($payload)) {
throw new Exception('Missing payload');
}
//Todo Figure out way to preserve keys when the container is being recreated @shimonewman

$aggregationInterval = (int) System::getEnv('_APP_USAGE_AGGREGATION_INTERVAL', '20');
$project = new Document($payload['project'] ?? []);
$projectId = $project->getInternalId();
foreach ($payload['reduce'] ?? [] as $document) {
Expand All @@ -74,7 +74,11 @@ public function action(Message $message, callable $getProjectDB, UsageDump $queu
);
}

$this->stats[$projectId]['project'] = $project;
$this->stats[$projectId]['project'] = [
'$uid' => $project->getId(),
'$internalId' => $project->getInternalId(),
'database' => $project->getAttribute('database'),
];
$this->stats[$projectId]['receivedAt'] = DateTime::now();
foreach ($payload['metrics'] ?? [] as $metric) {
$this->keys++;
Expand All @@ -89,7 +93,7 @@ public function action(Message $message, callable $getProjectDB, UsageDump $queu
// If keys crossed threshold or X time passed since the last send and there are some keys in the array ($this->stats)
if (
$this->keys >= self::KEYS_THRESHOLD ||
(time() - $this->lastTriggeredTime > $aggregationInterval && $this->keys > 0)
(time() - $this->lastTriggeredTime > $this->aggregationInterval && $this->keys > 0)
5011 ) {
Console::warning('[' . DateTime::now() . '] Aggregated ' . $this->keys . ' keys');

Expand Down
Loading
0