Optimize dashboard data retrieval with consolidated queries

Replaced multiple individual queries in Dashboard controller with consolidated methods for setup counts and country statistics. Added getAllSetupCounts to Setup_model and get_countries_statistics_consolidated to Logbook_model to improve performance and reduce database load.
这个提交包含在:
Peter Goodhall 2025-08-09 21:24:35 +01:00
父节点 ce7e9ee77b
当前提交 046dc4b4ef
共有 3 个文件被更改,包括 83 次插入13 次删除

查看文件

@ -68,9 +68,11 @@ class Dashboard extends CI_Controller
$this->load->model('stations'); $this->load->model('stations');
$this->load->model('setup_model'); $this->load->model('setup_model');
$data['countryCount'] = $this->setup_model->getCountryCount(); // Use consolidated setup counts instead of 3 separate queries
$data['logbookCount'] = $this->setup_model->getLogbookCount(); $setup_counts = $this->setup_model->getAllSetupCounts();
$data['locationCount'] = $this->setup_model->getLocationCount(); $data['countryCount'] = $setup_counts['country_count'];
$data['logbookCount'] = $setup_counts['logbook_count'];
$data['locationCount'] = $setup_counts['location_count'];
$data['current_active'] = $this->stations->find_active(); $data['current_active'] = $this->stations->find_active();
@ -97,13 +99,14 @@ class Dashboard extends CI_Controller
$data['month_qsos'] = $qso_stats['month_qsos']; $data['month_qsos'] = $qso_stats['month_qsos'];
$data['year_qsos'] = $qso_stats['year_qsos']; $data['year_qsos'] = $qso_stats['year_qsos'];
// Load Countries Breakdown data into array // Use consolidated countries statistics instead of separate queries
$CountriesBreakdown = $this->logbook_model->total_countries_confirmed($logbooks_locations_array); $countries_stats = $this->logbook_model->get_countries_statistics_consolidated($logbooks_locations_array);
$data['total_countries'] = $CountriesBreakdown['Countries_Worked']; $data['total_countries'] = $countries_stats['Countries_Worked'];
$data['total_countries_confirmed_paper'] = $CountriesBreakdown['Countries_Worked_QSL']; $data['total_countries_confirmed_paper'] = $countries_stats['Countries_Worked_QSL'];
$data['total_countries_confirmed_eqsl'] = $CountriesBreakdown['Countries_Worked_EQSL']; $data['total_countries_confirmed_eqsl'] = $countries_stats['Countries_Worked_EQSL'];
$data['total_countries_confirmed_lotw'] = $CountriesBreakdown['Countries_Worked_LOTW']; $data['total_countries_confirmed_lotw'] = $countries_stats['Countries_Worked_LOTW'];
$current_countries = $countries_stats['Countries_Current'];
$data['dashboard_upcoming_dx_card'] = false; $data['dashboard_upcoming_dx_card'] = false;
$data['dashboard_qslcard_card'] = false; $data['dashboard_qslcard_card'] = false;
@ -163,8 +166,7 @@ class Dashboard extends CI_Controller
// Optimize DXCC calculation - get count directly instead of loading all records // Optimize DXCC calculation - get count directly instead of loading all records
$this->load->model('dxcc'); $this->load->model('dxcc');
$total_dxcc_count = $this->dxcc->get_total_dxcc_count(); // We'll need to create this method $total_dxcc_count = $this->dxcc->get_total_dxcc_count();
$current_countries = $this->logbook_model->total_countries_current($logbooks_locations_array);
$data['total_countries_needed'] = $total_dxcc_count - $current_countries; $data['total_countries_needed'] = $total_dxcc_count - $current_countries;
$this->load->view('interface_assets/header', $data); $this->load->view('interface_assets/header', $data);
@ -181,7 +183,9 @@ class Dashboard extends CI_Controller
$logbooks_locations_array = $this->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook')); $logbooks_locations_array = $this->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook'));
$data['todays_qsos'] = $this->logbook_model->todays_qsos($logbooks_locations_array); // Use consolidated query instead of individual todays_qsos call
$qso_stats = $this->logbook_model->get_qso_statistics_consolidated($logbooks_locations_array);
$data['todays_qsos'] = $qso_stats['todays_qsos'];
$this->load->view('components/dashboard_todays_qsos', $data); $this->load->view('components/dashboard_todays_qsos', $data);
} }

查看文件

@ -2990,6 +2990,53 @@ class Logbook_model extends CI_Model
} }
} }
// Consolidated method to get all country statistics in one query
function get_countries_statistics_consolidated($StationLocationsArray = null) {
if ($StationLocationsArray == null) {
$CI = &get_instance();
$CI->load->model('logbooks_model');
$logbooks_locations_array = $CI->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook'));
} else {
$logbooks_locations_array = $StationLocationsArray;
}
if (!empty($logbooks_locations_array)) {
// Get both confirmed countries and current countries in one query
$this->db->select('
COUNT(DISTINCT COL_COUNTRY) as Countries_Worked,
COUNT(DISTINCT IF(COL_QSL_RCVD = "Y", COL_COUNTRY, NULL)) as Countries_Worked_QSL,
COUNT(DISTINCT IF(COL_EQSL_QSL_RCVD = "Y", COL_COUNTRY, NULL)) as Countries_Worked_EQSL,
COUNT(DISTINCT IF(COL_LOTW_QSL_RCVD = "Y", COL_COUNTRY, NULL)) as Countries_Worked_LOTW,
COUNT(DISTINCT IF(dxcc_entities.end IS NULL, COL_COUNTRY, NULL)) as Countries_Current
');
$this->db->join('dxcc_entities', 'dxcc_entities.adif = ' . $this->config->item('table_name') . '.col_dxcc', 'left');
$this->db->where_in('station_id', $logbooks_locations_array);
$this->db->where('COL_COUNTRY !=', 'Invalid');
$this->db->where('COL_DXCC >', '0');
if ($query = $this->db->get($this->config->item('table_name'))) {
if ($query->num_rows() > 0) {
$row = $query->row();
return array(
'Countries_Worked' => $row->Countries_Worked,
'Countries_Worked_QSL' => $row->Countries_Worked_QSL,
'Countries_Worked_EQSL' => $row->Countries_Worked_EQSL,
'Countries_Worked_LOTW' => $row->Countries_Worked_LOTW,
'Countries_Current' => $row->Countries_Current
);
}
}
}
return array(
'Countries_Worked' => 0,
'Countries_Worked_QSL' => 0,
'Countries_Worked_EQSL' => 0,
'Countries_Worked_LOTW' => 0,
'Countries_Current' => 0
);
}
/* Return total number of countries confirmed with paper QSL */ /* Return total number of countries confirmed with paper QSL */
function total_countries_confirmed_paper() function total_countries_confirmed_paper()
{ {

查看文件

@ -24,6 +24,25 @@ class Setup_model extends CI_Model {
return $query->row()->count; return $query->row()->count;
} }
// Consolidated method to get all setup counts in one query
function getAllSetupCounts() {
$userid = xss_clean($this->session->userdata('user_id'));
$sql = "SELECT
(SELECT COUNT(*) FROM dxcc_entities) as country_count,
(SELECT COUNT(*) FROM station_logbooks WHERE user_id = {$userid}) as logbook_count,
(SELECT COUNT(*) FROM station_profile WHERE user_id = {$userid}) as location_count";
$query = $this->db->query($sql);
$row = $query->row();
return array(
'country_count' => $row->country_count,
'logbook_count' => $row->logbook_count,
'location_count' => $row->location_count
);
}
} }
?> ?>