appwrite/src/Appwrite/Event/Usage.php

65 lines
1.3 KiB
PHP
Raw Normal View History

2022-11-24 07:53:52 +00:00
<?php
namespace Appwrite\Event;
use Utopia\Queue\Client;
use Utopia\Queue\Connection;
use Utopia\Database\Document;
2022-11-24 07:53:52 +00:00
class Usage extends Event
{
protected array $metrics = [];
protected array $reduce = [];
2022-11-24 07:53:52 +00:00
public function __construct(protected Connection $connection)
{
2022-12-04 17:06:23 +00:00
parent::__construct(Event::USAGE_QUEUE_NAME, Event::USAGE_CLASS_NAME);
2022-11-24 07:53:52 +00:00
}
/**
* Add reduce.
*
* @param Document $document
* @return self
*/
public function addReduce(Document $document): self
{
$this->reduce[] = $document;
return $this;
}
2022-11-24 07:53:52 +00:00
/**
2022-12-04 17:06:23 +00:00
* Add metric.
2022-11-24 07:53:52 +00:00
*
* @param string $key
2022-12-15 09:45:43 +00:00
* @param int $value
2022-11-24 07:53:52 +00:00
* @return self
*/
2022-12-15 09:45:43 +00:00
public function addMetric(string $key, int $value): self
2022-11-24 07:53:52 +00:00
{
$this->metrics[] = [
'key' => $key,
'value' => $value,
];
return $this;
}
/**
2022-12-04 17:06:23 +00:00
* Sends metrics to the usage worker.
2022-11-24 07:53:52 +00:00
*
2022-12-04 17:06:23 +00:00
* @return string|bool
2022-11-24 07:53:52 +00:00
*/
public function trigger(): string|bool
{
$client = new Client($this->queue, $this->connection);
2022-12-15 09:45:43 +00:00
2022-11-24 07:53:52 +00:00
return $client->enqueue([
2022-12-06 11:36:17 +00:00
'project' => $this->getProject(),
'reduce' => $this->reduce,
2022-11-24 07:53:52 +00:00
'metrics' => $this->metrics,
]);
}
}