From 278a4b384c9c068cba20217013d9127e425dac74 Mon Sep 17 00:00:00 2001 From: Peter Goodhall Date: Sat, 9 Aug 2025 21:10:04 +0100 Subject: [PATCH] Optimize QSO statistics retrieval with consolidated query Replaced multiple individual queries for QSO statistics in Dashboard controller with a single consolidated query in Logbook_model. This improves performance by reducing database calls when fetching today's, total, monthly, and yearly QSO counts. --- application/controllers/Dashboard.php | 11 +++--- application/models/Logbook_model.php | 57 +++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/application/controllers/Dashboard.php b/application/controllers/Dashboard.php index 4cdc91d2..67382972 100644 --- a/application/controllers/Dashboard.php +++ b/application/controllers/Dashboard.php @@ -85,11 +85,12 @@ class Dashboard extends CI_Controller $data['radio_status'] = $this->cat->recent_status(); - // Store info - $data['todays_qsos'] = $this->logbook_model->todays_qsos($logbooks_locations_array); - $data['total_qsos'] = $this->logbook_model->total_qsos($logbooks_locations_array); - $data['month_qsos'] = $this->logbook_model->month_qsos($logbooks_locations_array); - $data['year_qsos'] = $this->logbook_model->year_qsos($logbooks_locations_array); + // Store info - Use consolidated query for QSO statistics + $qso_stats = $this->logbook_model->get_qso_statistics_consolidated($logbooks_locations_array); + $data['todays_qsos'] = $qso_stats['todays_qsos']; + $data['total_qsos'] = $qso_stats['total_qsos']; + $data['month_qsos'] = $qso_stats['month_qsos']; + $data['year_qsos'] = $qso_stats['year_qsos']; // Load Countries Breakdown data into array $CountriesBreakdown = $this->logbook_model->total_countries_confirmed($logbooks_locations_array); diff --git a/application/models/Logbook_model.php b/application/models/Logbook_model.php index eb7ec02d..1b1e2ccc 100755 --- a/application/models/Logbook_model.php +++ b/application/models/Logbook_model.php @@ -4980,6 +4980,63 @@ class Logbook_model extends CI_Model // If all parsing fails, return the original input and let the database handle it return $date_input; } + + /* Consolidated QSO Statistics - Get all basic counts in a single query */ + function get_qso_statistics_consolidated($StationLocationsArray = null) + { + if ($StationLocationsArray == null) { + $this->load->model('logbooks_model'); + $logbooks_locations_array = $this->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook')); + } else { + $logbooks_locations_array = $StationLocationsArray; + } + + if (!$logbooks_locations_array) { + return array( + 'total_qsos' => 0, + 'todays_qsos' => 0, + 'month_qsos' => 0, + 'year_qsos' => 0 + ); + } + + // Calculate date ranges + $today_morning = date('Y-m-d 00:00:00'); + $today_night = date('Y-m-d 23:59:59'); + $month_morning = date('Y-m-01 00:00:00'); + $date = new DateTime('now'); + $date->modify('last day of this month'); + $month_night = $date->format('Y-m-d') . " 23:59:59"; + $year_morning = date('Y-01-01 00:00:00'); + $year_night = date('Y-12-31 23:59:59'); + + // Build the consolidated query + $this->db->select(" + COUNT(*) as total_qsos, + COUNT(CASE WHEN COL_TIME_ON >= '$today_morning' AND COL_TIME_ON <= '$today_night' THEN 1 END) as todays_qsos, + COUNT(CASE WHEN COL_TIME_ON >= '$month_morning' AND COL_TIME_ON <= '$month_night' THEN 1 END) as month_qsos, + COUNT(CASE WHEN COL_TIME_ON >= '$year_morning' AND COL_TIME_ON <= '$year_night' THEN 1 END) as year_qsos + ", FALSE); + $this->db->where_in('station_id', $logbooks_locations_array); + $query = $this->db->get($this->config->item('table_name')); + + if ($query->num_rows() > 0) { + $row = $query->row(); + return array( + 'total_qsos' => (int)$row->total_qsos, + 'todays_qsos' => (int)$row->todays_qsos, + 'month_qsos' => (int)$row->month_qsos, + 'year_qsos' => (int)$row->year_qsos + ); + } + + return array( + 'total_qsos' => 0, + 'todays_qsos' => 0, + 'month_qsos' => 0, + 'year_qsos' => 0 + ); + } } // Function to validate ADIF date format