Files
backend/app/Services/Commands/SendKeypressStatsService.php

71 lines
2.7 KiB
PHP

<?php
namespace App\Services\Commands;
use Exception;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class SendKeypressStatsService
{
private string $url;
private string $channelName;
private string $provinceId;
private string $provinceName;
public function __construct()
{
$this->url = config('ProvinceInfo.center_ip');
$this->channelName = 'send_keypress_stats_report';
$this->provinceId = config('ProvinceInfo.id');
$this->provinceName = config('ProvinceInfo.name');
}
/**
* @throws ConnectionException
* @throws RequestException
*/
public function execute(): void
{
try
{
$data = $this->prepare();
Http::post($this->url, $data)->throw();
Log::channel($this->channelName)->info(SendKeypressStatsService::class, [$this->provinceName, array_sum($data), 'successful']);
}
catch (Exception $e)
{
Log::channel($this->channelName)->error(SendKeypressStatsService::class, [$e->getMessage()]);
throw $e;
}
}
private function prepare(): array
{
$query = "SELECT
{$this->provinceId} AS province_id,
'{$this->provinceName}' AS province_name,
DATE(cdate) AS date,
COUNT(CASE WHEN (ctype=-1 AND channel=0) THEN 1 END) AS total,
COUNT(CASE WHEN (ctype=1 AND channel=0) THEN 1 END) AS key1,
COUNT(CASE WHEN (ctype=2 AND channel=0) THEN 1 END) AS key2,
COUNT(CASE WHEN (ctype=3 AND channel=0) THEN 1 END) AS key3,
COUNT(CASE WHEN (ctype=4 AND channel=0) THEN 1 END) AS key4,
COUNT(CASE WHEN (ctype=5 AND channel=0) THEN 1 END) AS key5,
COUNT(CASE WHEN (ctype=6 AND channel=0) THEN 1 END) AS key6,
COUNT(CASE WHEN (ctype=9 AND channel=0) THEN 1 END) AS key9,
COUNT(CASE WHEN (ctype=1 AND channel=4) THEN 1 END) AS key4_1,
COUNT(CASE WHEN (ctype=2 AND channel=4) THEN 1 END) AS key4_2,
COUNT(CASE WHEN (ctype=1 AND channel=5) THEN 1 END) AS key5_1,
COUNT(CASE WHEN (ctype=2 AND channel=5) THEN 1 END) AS key5_2,
COUNT(CASE WHEN (ctype=1 AND channel=2) THEN 1 END) AS key2_1,
COUNT(CASE WHEN (ctype=2 AND channel=2) THEN 1 END) AS key2_2,
COUNT(CASE WHEN (ctype=3 AND channel=2) THEN 1 END) AS key2_3
FROM report WHERE DATE(cdate)> DATE(NOW()- INTERVAL 3 day) GROUP BY date ORDER BY date DESC";
return DB::select($query);
}
}