From 3d7258c1f5e4f10e52905cf5a192daa84e2ba2d4 Mon Sep 17 00:00:00 2001 From: Peter Goodhall Date: Sat, 9 Aug 2025 21:14:39 +0100 Subject: [PATCH] Optimize dashboard model loading and DXCC count Refactored Dashboard controller to load common models in the constructor and optimize dashboard options processing using associative arrays for faster lookups. Replaced DXCC entity counting logic with a new get_total_dxcc_count() method in the Dxcc model to avoid loading all records, improving performance. --- application/controllers/Dashboard.php | 95 +++++++++------------------ application/models/Dxcc.php | 6 ++ 2 files changed, 36 insertions(+), 65 deletions(-) diff --git a/application/controllers/Dashboard.php b/application/controllers/Dashboard.php index 67382972..b38540d5 100644 --- a/application/controllers/Dashboard.php +++ b/application/controllers/Dashboard.php @@ -3,6 +3,16 @@ class Dashboard extends CI_Controller { + public function __construct() + { + parent::__construct(); + + // Load common models that are used across multiple methods + $this->load->model('user_model'); + $this->load->model('logbook_model'); + $this->load->model('logbooks_model'); + } + public function index() { // If environment is set to development then show the debug toolbar @@ -13,10 +23,6 @@ class Dashboard extends CI_Controller // Load language files $this->lang->load('lotw'); - // Database connections - $this->load->model('logbook_model'); - $this->load->model('user_model'); - // LoTW infos $this->load->model('LotwCert'); @@ -31,7 +37,6 @@ class Dashboard extends CI_Controller redirect('user/login'); } - $this->load->model('logbooks_model'); $logbooks_locations_array = $this->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook')); /* @@ -108,53 +113,23 @@ class Dashboard extends CI_Controller $dashboard_options = $this->user_options_model->get_options('dashboard')->result(); + // Optimize options processing - convert to associative array for O(1) lookup + $options_map = array(); foreach ($dashboard_options as $item) { - $option_name = $item->option_name; - $option_key = $item->option_key; - $option_value = $item->option_value; - - if ($option_name == 'dashboard_upcoming_dx_card' && $option_key == 'enabled') { - if($option_value == 'true') { - $data['dashboard_upcoming_dx_card'] = true; - } else { - $data['dashboard_upcoming_dx_card'] = false; - } - } + $options_map[$item->option_name][$item->option_key] = $item->option_value; + } - if ($option_name == 'dashboard_qslcards_card' && $option_key == 'enabled') { - if($item->option_value == 'true') { - $data['dashboard_qslcard_card'] = true; - } else { - $data['dashboard_qslcard_card'] = false; - } - } + // Quick lookups instead of nested loops + $data['dashboard_upcoming_dx_card'] = isset($options_map['dashboard_upcoming_dx_card']['enabled']) && $options_map['dashboard_upcoming_dx_card']['enabled'] == 'true'; + $data['dashboard_qslcard_card'] = isset($options_map['dashboard_qslcards_card']['enabled']) && $options_map['dashboard_qslcards_card']['enabled'] == 'true'; + $data['dashboard_eqslcard_card'] = isset($options_map['dashboard_eqslcards_card']['enabled']) && $options_map['dashboard_eqslcards_card']['enabled'] == 'true'; + $data['dashboard_lotw_card'] = isset($options_map['dashboard_lotw_card']['enabled']) && $options_map['dashboard_lotw_card']['enabled'] == 'true'; + $data['dashboard_vuccgrids_card'] = isset($options_map['dashboard_vuccgrids_card']['enabled']) && $options_map['dashboard_vuccgrids_card']['enabled'] == 'true'; - if ($option_name == 'dashboard_eqslcards_card' && $option_key == 'enabled') { - if($item->option_value == 'true') { - $data['dashboard_eqslcard_card'] = true; - } else { - $data['dashboard_eqslcard_card'] = false; - } - } - - if ($option_name == 'dashboard_lotw_card' && $option_key == 'enabled') { - if($item->option_value == 'true') { - $data['dashboard_lotw_card'] = true; - } else { - $data['dashboard_lotw_card'] = false; - } - } - - if ($option_name == 'dashboard_vuccgrids_card' && $option_key == 'enabled') { - if($item->option_value == 'true') { - $data['dashboard_vuccgrids_card'] = true; - - $data['vucc'] = $this->vucc->fetchVuccSummary(); - $data['vuccSAT'] = $this->vucc->fetchVuccSummary('SAT'); - } else { - $data['dashboard_vuccgrids_card'] = false; - } - } + // Only load VUCC data if the card is actually enabled + if ($data['dashboard_vuccgrids_card']) { + $data['vucc'] = $this->vucc->fetchVuccSummary(); + $data['vuccSAT'] = $this->vucc->fetchVuccSummary('SAT'); } @@ -186,12 +161,11 @@ class Dashboard extends CI_Controller $data['page_title'] = "Dashboard"; + // Optimize DXCC calculation - get count directly instead of loading all records $this->load->model('dxcc'); - $dxcc = $this->dxcc->list_current(); - - $current = $this->logbook_model->total_countries_current($logbooks_locations_array); - - $data['total_countries_needed'] = count($dxcc->result()) - $current; + $total_dxcc_count = $this->dxcc->get_total_dxcc_count(); // We'll need to create this method + $current_countries = $this->logbook_model->total_countries_current($logbooks_locations_array); + $data['total_countries_needed'] = $total_dxcc_count - $current_countries; $this->load->view('interface_assets/header', $data); $this->load->view('dashboard/index'); @@ -200,30 +174,21 @@ class Dashboard extends CI_Controller } public function todays_qso_component() { - $this->load->model('user_model'); - if ($this->user_model->validate_session() == 0) { // User is not logged in - } else { - $this->load->model('logbook_model'); - $this->load->model('logbooks_model'); + return; } $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); $this->load->view('components/dashboard_todays_qsos', $data); - } public function logbook_display_component() { - $this->load->model('user_model'); - if ($this->user_model->validate_session() == 0) { // User is not logged in - } else { - $this->load->model('logbook_model'); - $this->load->model('logbooks_model'); + return; } // Get Logbook Locations diff --git a/application/models/Dxcc.php b/application/models/Dxcc.php index b02e940d..ef02fea5 100644 --- a/application/models/Dxcc.php +++ b/application/models/Dxcc.php @@ -58,6 +58,12 @@ class DXCC extends CI_Model { return $this->db->get('dxcc_entities'); } + /* Optimized method to get count of current DXCC entities without loading all records */ + function get_total_dxcc_count() { + $this->db->where('end', null); + return $this->db->count_all_results('dxcc_entities'); + } + function get_dxcc_array($dxccArray, $bands, $postdata) { $CI =& get_instance(); $CI->load->model('logbooks_model');