From 23587ae958cba87fae238e28cf1286ac71bfd1c5 Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Sun, 3 Aug 2025 11:08:14 +0330 Subject: [PATCH] create service and command for sending reports --- .../Commands/SendCallStatsReportCommand.php | 32 +++++++ .../SendPeopleMessageStatsCommand.php | 32 +++++++ .../SendSystemMessageStatsCommand.php | 32 +++++++ .../Commands/SendCallStatsReportService.php | 91 +++++++++++++++++++ .../Commands/SendKeypressStatsService.php | 59 ++++++++++++ .../SendPeopleMessageStatsService.php | 64 +++++++++++++ .../SendSystemMessageStatsService.php | 59 ++++++++++++ config/ProvinceInfo.php | 8 ++ config/logging.php | 23 +++++ 9 files changed, 400 insertions(+) create mode 100644 app/Console/Commands/SendCallStatsReportCommand.php create mode 100644 app/Console/Commands/SendPeopleMessageStatsCommand.php create mode 100644 app/Console/Commands/SendSystemMessageStatsCommand.php create mode 100644 app/Services/Commands/SendCallStatsReportService.php create mode 100644 app/Services/Commands/SendKeypressStatsService.php create mode 100644 app/Services/Commands/SendPeopleMessageStatsService.php create mode 100644 app/Services/Commands/SendSystemMessageStatsService.php create mode 100644 config/ProvinceInfo.php diff --git a/app/Console/Commands/SendCallStatsReportCommand.php b/app/Console/Commands/SendCallStatsReportCommand.php new file mode 100644 index 0000000..ac88284 --- /dev/null +++ b/app/Console/Commands/SendCallStatsReportCommand.php @@ -0,0 +1,32 @@ +execute(); + $this->info('Done!'); + } +} diff --git a/app/Console/Commands/SendPeopleMessageStatsCommand.php b/app/Console/Commands/SendPeopleMessageStatsCommand.php new file mode 100644 index 0000000..1c940ef --- /dev/null +++ b/app/Console/Commands/SendPeopleMessageStatsCommand.php @@ -0,0 +1,32 @@ +execute(); + $this->info('Done!'); + } +} diff --git a/app/Console/Commands/SendSystemMessageStatsCommand.php b/app/Console/Commands/SendSystemMessageStatsCommand.php new file mode 100644 index 0000000..93201b4 --- /dev/null +++ b/app/Console/Commands/SendSystemMessageStatsCommand.php @@ -0,0 +1,32 @@ +execute(); + $this->info('Done!'); + } +} diff --git a/app/Services/Commands/SendCallStatsReportService.php b/app/Services/Commands/SendCallStatsReportService.php new file mode 100644 index 0000000..5ebe9ef --- /dev/null +++ b/app/Services/Commands/SendCallStatsReportService.php @@ -0,0 +1,91 @@ +url = config('ProvinceInfo.center_ip'); + $this->channelName = 'send_call_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(SendCallStatsReportService::class, [$this->provinceName, array_sum($data), 'successful']); + } + catch (Exception $e) + { + Log::channel($this->channelName)->error(SendCallStatsReportService::class, [$e->getMessage()]); + throw $e; + } + } + + private function prepare(): array + { + $query = "SELECT + {$this->provinceId} AS province_id, + '{$this->provinceName}' AS province_name, + date, + total, + route_to_teh4, + route_to_teh5, + route_to_os4, + route_to_os5, + answered, + missed, + answered_over10, + dnd, + transferred, + talked_to_operator, + c_agency, + c_agency_driver, + average_calls_time, + (talked_to_operator - (answered + missed) + c_agency + c_agency_driver) AS fails + FROM ( + SELECT + DATE(calldate) AS date, + COUNT(CASE WHEN recordingfile = '' OR recordingfile LIKE 'q-%' THEN 1 END) AS total, + COUNT(CASE WHEN lastdata = 'IAX2/serverpeer/100' THEN 1 END) AS route_to_teh4, + COUNT(CASE WHEN lastdata = 'IAX2/serverpeer/200' THEN 1 END) AS route_to_teh5, + COUNT(CASE WHEN dst = '500' THEN 1 END) AS route_to_os4, + COUNT(CASE WHEN dst = '600' THEN 1 END) AS route_to_os5, + COUNT(CASE WHEN dcontext = 'from-internal' AND disposition = 'ANSWERED' AND dst BETWEEN 101 AND 104 THEN 1 END) AS answered, + COUNT(CASE WHEN dcontext = 'from-internal' AND disposition = 'NO ANSWER' AND dst BETWEEN 101 AND 104 AND duration > 14 THEN 1 END) AS missed, + COUNT(CASE WHEN dcontext = 'from-internal' AND disposition = 'ANSWERED' AND dst BETWEEN 101 AND 104 AND billsec > 9 THEN 1 END) AS answered_over10, + COUNT(CASE WHEN lastapp = 'Busy' THEN 1 END) AS dnd, + COUNT(CASE WHEN dst BETWEEN 502 AND 599 THEN 1 END) AS transferred, + COUNT(CASE WHEN disposition <> 'BUSY' AND dst BETWEEN 101 AND 105 THEN 1 END) AS talked_to_operator, + COUNT(CASE WHEN lastdata LIKE '%custom/agency%' THEN 1 END) AS c_agency, + COUNT(CASE WHEN lastdata LIKE '%custom/agency-driver%' THEN 1 END) AS c_agency_driver, + AVG(CASE WHEN dcontext = 'from-internal' AND disposition = 'ANSWERED' THEN billsec END) AS average_calls_time + FROM asteriskcdrdb.cdr + WHERE calldate >= NOW() - INTERVAL 3 DAY + GROUP BY date + ) + WHERE date >= DATE(NOW() - INTERVAL 3 DAY)"; + + return DB::select($query); + } +} diff --git a/app/Services/Commands/SendKeypressStatsService.php b/app/Services/Commands/SendKeypressStatsService.php new file mode 100644 index 0000000..9f13190 --- /dev/null +++ b/app/Services/Commands/SendKeypressStatsService.php @@ -0,0 +1,59 @@ +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, + COUNT(CASE WHEN message_quality=1 THEN 1 END) AS message_quality_ok, + COUNT(CASE WHEN message_quality=0 THEN 1 END) AS message_quality_nok, + COUNT(CASE WHEN format_quality=1 THEN 1 END) AS message_format_ok, + COUNT(CASE WHEN format_quality=0 THEN 1 END) AS message_format_nok, + FROM system_messages GROUP BY date ORDER BY date DESC LIMIT 5"; + + return DB::select($query); + } +} diff --git a/app/Services/Commands/SendPeopleMessageStatsService.php b/app/Services/Commands/SendPeopleMessageStatsService.php new file mode 100644 index 0000000..811fa76 --- /dev/null +++ b/app/Services/Commands/SendPeopleMessageStatsService.php @@ -0,0 +1,64 @@ +url = config('ProvinceInfo.center_ip'); + $this->channelName = 'send_people_message_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(SendPeopleMessageStatsService::class, [$this->provinceName, array_sum($data), 'successful']); + } + catch (Exception $e) + { + Log::channel($this->channelName)->error(SendPeopleMessageStatsService::class, [$e->getMessage()]); + throw $e; + } + } + + private function prepare(): array + { + $query = "SELECT + {$this->provinceId} AS province_id, + '{$this->provinceName}' AS province_name, + date, + COUNT(CASE WHEN action_id=1 THEN 1 END) AS action1, + COUNT(CASE WHEN action_id=2 THEN 1 END) AS action2, + COUNT(CASE WHEN action_id=3 THEN 1 END) AS action3, + COUNT(CASE WHEN action_id=4 THEN 1 END) AS action4, + COUNT(CASE WHEN action_id=5 THEN 1 END) AS action5, + COUNT(CASE WHEN is_listen=1 THEN 1 END) AS listens, + COUNT(CASE WHEN is_listen=0 THEN 1 END) AS not_listens, + COUNT(*) AS total, + AVG(review_duration) as review_average + FROM system_messages GROUP BY date ORDER BY date DESC LIMIT 3"; + + return DB::select($query); + } +} diff --git a/app/Services/Commands/SendSystemMessageStatsService.php b/app/Services/Commands/SendSystemMessageStatsService.php new file mode 100644 index 0000000..812d50b --- /dev/null +++ b/app/Services/Commands/SendSystemMessageStatsService.php @@ -0,0 +1,59 @@ +url = config('ProvinceInfo.center_ip'); + $this->channelName = 'send_system_message_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(SendSystemMessageStatsService::class, [$this->provinceName, array_sum($data), 'successful']); + } + catch (Exception $e) + { + Log::channel($this->channelName)->error(SendSystemMessageStatsService::class, [$e->getMessage()]); + throw $e; + } + } + + private function prepare(): array + { + $query = "SELECT + {$this->provinceId} AS province_id, + '{$this->provinceName}' AS province_name, + date, + COUNT(CASE WHEN message_quality=1 THEN 1 END) AS message_quality_ok, + COUNT(CASE WHEN message_quality=0 THEN 1 END) AS message_quality_nok, + COUNT(CASE WHEN format_quality=1 THEN 1 END) AS message_format_ok, + COUNT(CASE WHEN format_quality=0 THEN 1 END) AS message_format_nok, + FROM system_messages GROUP BY date ORDER BY date DESC LIMIT 5"; + + return DB::select($query); + } +} diff --git a/config/ProvinceInfo.php b/config/ProvinceInfo.php new file mode 100644 index 0000000..fa9d3ff --- /dev/null +++ b/config/ProvinceInfo.php @@ -0,0 +1,8 @@ + env('PROVINCE_INFO_IP'), + 'name' => env('PROVINCE_INFO_NAME'), + 'id' => env('PROVINCE_INFO_ID'), + 'center_ip' => env('PROVINCE_INFO_CENTER_IP'), +]; diff --git a/config/logging.php b/config/logging.php index 1345f6f..ca78f2e 100644 --- a/config/logging.php +++ b/config/logging.php @@ -127,6 +127,29 @@ return [ 'path' => storage_path('logs/laravel.log'), ], + 'send_call_stats_report' => [ + 'driver' => 'single', + 'path' => storage_path('logs/send_call_stats_report.log'), + 'level' => 'debug', + ], + + 'send_keypress_stats_report' => [ + 'driver' => 'single', + 'path' => storage_path('logs/send_keypress_stats_report.log'), + 'level' => 'debug', + ], + + 'send_people_message_stats_report' => [ + 'driver' => 'single', + 'path' => storage_path('logs/send_people_message_stats_report.log'), + 'level' => 'debug', + ], + + 'send_system_message_stats_report' => [ + 'driver' => 'single', + 'path' => storage_path('logs/send_system_message_stats_report.log'), + 'level' => 'debug', + ], ], ];