Optimize Yr/Mon/Day-Stat-SQL

This commit is contained in:
int2001
2026-02-04 05:34:33 +00:00
parent b6eb34c370
commit 2a2c6d7abe
4 changed files with 90 additions and 14 deletions

View File

@@ -837,10 +837,11 @@ class API extends CI_Controller {
$this->load->model('api_model');
if ((($key ?? '') != '') && ($this->api_model->authorize($key) != 0)) {
$this->load->model('logbook_model');
$data['todays_qsos'] = $this->logbook_model->todays_qsos(null, $key);
$data['total_qsos'] = $this->logbook_model->total_qsos(null, $key);
$data['month_qsos'] = $this->logbook_model->month_qsos(null, $key);
$data['year_qsos'] = $this->logbook_model->year_qsos(null, $key);
$qso_counts = $this->logbook_model->get_qso_counts(null, $key);
$data['todays_qsos'] = $qso_counts['today'];
$data['total_qsos'] = $qso_counts['total'];
$data['month_qsos'] = $qso_counts['month'];
$data['year_qsos'] = $qso_counts['year'];
} else { # for Downcompat
$data['todays_qsos'] = 0;
$data['total_qsos'] = 0;

View File

@@ -4,6 +4,7 @@ class Dashboard extends CI_Controller {
public function index()
{
$this->output->enable_profiler(TRUE);
// Check if users logged in
$this->load->model('user_model');
if ($this->user_model->validate_session() == 0) {
@@ -97,11 +98,11 @@ 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);
$qso_counts = $this->logbook_model->get_qso_counts($logbooks_locations_array);
$data['todays_qsos'] = $qso_counts['today'];
$data['total_qsos'] = $qso_counts['total'];
$data['month_qsos'] = $qso_counts['month'];
$data['year_qsos'] = $qso_counts['year'];
$rawstreak=$this->dayswithqso_model->getAlmostCurrentStreak();
if (is_array($rawstreak)) {

View File

@@ -97,11 +97,11 @@ class Visitor extends CI_Controller {
$this->pagination->initialize($config);
// 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);
$qso_counts = $this->logbook_model->get_qso_counts($logbooks_locations_array);
$data['todays_qsos'] = $qso_counts['today'];
$data['total_qsos'] = $qso_counts['total'];
$data['month_qsos'] = $qso_counts['month'];
$data['year_qsos'] = $qso_counts['year'];
$data['user_map_custom'] = $this->optionslib->get_map_custom(true,$public_slug);

View File

@@ -3684,6 +3684,80 @@ class Logbook_model extends CI_Model {
}
}
/*
* Combined function to get all QSO counts (today, month, year, total) in a single query
* This reduces 4 separate queries to 1, improving performance
*/
function get_qso_counts($StationLocationsArray = null, $api_key = null) {
if ($StationLocationsArray == null) {
$this->load->model('logbooks_model');
if ($api_key != null) {
$this->load->model('api_model');
if (strpos($this->api_model->access($api_key), 'r') !== false) {
$this->api_model->update_last_used($api_key);
$user_id = $this->api_model->key_userid($api_key);
$active_station_logbook = $this->logbooks_model->find_active_station_logbook_from_userid($user_id);
$logbooks_locations_array = $this->logbooks_model->list_logbook_relationships($active_station_logbook);
} else {
$logbooks_locations_array = [];
}
} else {
$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 [
'total' => 0,
'today' => 0,
'month' => 0,
'year' => 0
];
}
// Calculate date boundaries once
$todayStart = date('Y-m-d 00:00:00');
$todayEnd = date('Y-m-d 23:59:59');
$monthStart = date('Y-m-01 00:00:00');
$date = new DateTime('now');
$date->modify('last day of this month');
$monthEnd = $date->format('Y-m-d') . ' 23:59:59';
$yearStart = date('Y-01-01 00:00:00');
$yearEnd = date('Y-12-31 23:59:59');
// Single query with conditional aggregation
$sql = "SELECT
COUNT(*) as total,
SUM(CASE WHEN COL_TIME_ON >= ? AND COL_TIME_ON <= ? THEN 1 ELSE 0 END) as today,
SUM(CASE WHEN COL_TIME_ON >= ? AND COL_TIME_ON <= ? THEN 1 ELSE 0 END) as month,
SUM(CASE WHEN COL_TIME_ON >= ? AND COL_TIME_ON <= ? THEN 1 ELSE 0 END) as year
FROM " . $this->config->item('table_name') . "
WHERE station_id IN ('" . implode("','", $logbooks_locations_array) . "')";
$query = $this->db->query($sql, [$todayStart, $todayEnd, $monthStart, $monthEnd, $yearStart, $yearEnd]);
if ($query->num_rows() > 0) {
$row = $query->row();
return [
'total' => (int)$row->total,
'today' => (int)$row->today,
'month' => (int)$row->month,
'year' => (int)$row->year
];
}
return [
'total' => 0,
'today' => 0,
'month' => 0,
'year' => 0
];
}
private function where_date_range($dateFrom, $dateTo) {
if (!empty($dateFrom)) {
$this->db->where('COL_TIME_ON >=', $dateFrom . ' 00:00:00');