diff --git a/src/Api/InfluxDB.php b/src/Api/InfluxDB.php new file mode 100644 index 0000000..ba5e16c --- /dev/null +++ b/src/Api/InfluxDB.php @@ -0,0 +1,46 @@ + + */ +class InfluxDB +{ + protected ?Client $client = null; + + public function __construct( + protected ?string $url, + protected ?string $token, + protected ?string $bucket, + protected ?string $org, + protected bool $debug = false + ) { + if (isset($this->url, $this->token, $this->bucket, $this->org)) { + $this->client = new Client([ + 'url' => $this->url, + 'token' => $this->token, + 'bucket' => $this->bucket, + 'org' => $this->org, + 'debug' => $this->debug, + 'precision' => WritePrecision::S, + 'timeout' => 1, + ]); + } + } + + public function isAvailable(): bool + { + return $this->getClient() !== null; + } + + public function getClient(): ?Client + { + return $this->client; + } +} diff --git a/src/EventListener/StatListener.php b/src/EventListener/StatListener.php new file mode 100644 index 0000000..999aa56 --- /dev/null +++ b/src/EventListener/StatListener.php @@ -0,0 +1,41 @@ + + */ +class StatListener +{ + public function __construct(protected InfluxDB $influxDB) + { + } + + public function onKernelRequest(RequestEvent $event) + { + if (!$this->influxDB->isAvailable()) { + return; + } + + $client = $this->influxDB->getClient(); + + $writeApi = $client->createWriteApi(['writeType' => WriteType::SYNCHRONOUS]); + $pageView = new Point('page_view'); + $pageView + ->addTag('request', 'view') + ->addField('value', 1) + ->time(time()) + ; + + $writeApi->write($pageView); + $writeApi->close(); + $client->close(); + } +}