taskman_atf/stats.inc.php

57 lines
1.2 KiB
PHP
Raw Permalink Normal View History

2023-11-07 15:42:40 +03:00
<?php
namespace ATF;
use Exception;
use ClickHouseDB;
interface IStatsSender
{
function send(string $table, array $data);
}
class NullStatsSender implements IStatsSender
{
function send(string $table, array $data) {}
}
class ClickHouseStatsSender implements IStatsSender
{
private string $host;
private string $port;
private string $username;
private string $password;
private string $dbname;
function __construct(string $host, string $port, string $username, string $password, string $dbname)
{
$this->host = $host;
$this->port = $port;
$this->username = $username;
$this->password = $password;
$this->dbname = $dbname;
}
function getClient() : ClickHouseDB\Client
{
$config = [
'host' => $this->host,
'port' => $this->port,
'username' => $this->username,
'password' => $this->password,
];
$db = new ClickHouseDB\Client($config);
if($this->dbname)
$db->database($this->dbname);
$db->setTimeout(15);
$db->setConnectTimeOut(10);
return $db;
}
function send(string $table, array $data)
{
$this->getClient()->insert($table, array(array_values($data)), array_keys($data));
}
}