appwrite/src/Appwrite/Event/Usage.php

52 lines
1 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;
class Usage extends Event
{
protected array $metrics = [];
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
}
/**
2022-12-04 17:06:23 +00:00
* Add metric.
2022-11-24 07:53:52 +00:00
*
* @param string $key
2022-12-15 07:56:06 +00:00
* @param int|float $value
2022-11-24 07:53:52 +00:00
* @return self
*/
2022-12-15 07:56:06 +00:00
public function addMetric(string $key, int|float $value): self
2022-11-24 07:53:52 +00:00
{
2022-12-15 07:56:06 +00:00
if($key == 'executions.compute'){
var_dump($value);
}
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);
return $client->enqueue([
2022-12-06 11:36:17 +00:00
'project' => $this->getProject(),
2022-11-24 07:53:52 +00:00
'metrics' => $this->metrics,
]);
}
}