From 0eab18c03abf12914dafe775e7cd06982d7a90c0 Mon Sep 17 00:00:00 2001 From: Andreas Kristiansen <6977712+AndreasK79@users.noreply.github.com> Date: Wed, 4 Feb 2026 16:13:50 +0100 Subject: [PATCH] [Accumulated Statistics] Changed query to use window functions for speed --- application/models/Accumulate_model.php | 1205 ++++++----------------- 1 file changed, 301 insertions(+), 904 deletions(-) diff --git a/application/models/Accumulate_model.php b/application/models/Accumulate_model.php index 83e792483..86125db04 100644 --- a/application/models/Accumulate_model.php +++ b/application/models/Accumulate_model.php @@ -37,714 +37,231 @@ class Accumulate_model extends CI_Model return $result; } - function get_accumulated_dxcc($band, $mode, $propmode, $period, $location_list) { - $binding=[]; - if ($period == "year") { - $sql = "select year(thcv.col_time_on) year"; - } else if ($period == "month") { - $sql = "select date_format(thcv.col_time_on, '%Y-%m') year"; - } + function get_accumulated_by_column($band, $mode, $propmode, $period, $location_list, $column, $where_condition = '', $where_check = '', $outer_where_condition = '') { + // Use modern CTE/window function approach for MySQL 8.0+ + $dbversion = $this->db->version(); + $dbversion = explode('.', $dbversion); - $sql .= ", coalesce(y.tot, 0) tot - from " . $this->config->item('table_name') . " thcv - left outer join ( - select count(col_dxcc) as tot, year - from (select distinct "; + if ($dbversion[0] <= "8") { + return $this->get_accumulated_by_column_fast($band, $mode, $propmode, $period, $location_list, $column, $where_condition, $outer_where_condition); + } else { + return $this->get_accumulated_by_column_slow($band, $mode, $propmode, $period, $location_list, $column, $where_condition, $where_check, $outer_where_condition); + } + } - if ($period == "year") { - $sql .= "year(col_time_on)"; - } else if ($period == "month") { - $sql .= "date_format(col_time_on, '%Y-%m')"; - } + function get_accumulated_by_column_fast($band, $mode, $propmode, $period, $location_list, $column, $where_condition = '', $outer_where_condition = '') { + $binding = []; - $sql .= " year, col_dxcc - from " . $this->config->item('table_name') . - " where col_dxcc > 0 and station_id in (" . $location_list . ")"; + // Build the filter conditions used in both CTEs + $filter_conditions = $this->build_filter_conditions($band, $mode, $propmode); - if ($band == 'SAT') { // Left for compatibility reasons - $sql .= " and col_prop_mode = ?"; - $binding[] = $band; - } else { // Not SAT - if ($band != 'All') { // Band set? Take care of it - $sql .= " and col_band = ?"; - $binding[] = $band; - } - if ( $propmode == 'NoSAT' ) { // All without SAT - $sql .= " and col_prop_mode !='SAT'"; - } elseif ($propmode == 'None') { // Empty Propmode - $sql .= " and (trim(col_prop_mode)='' or col_prop_mode is null)"; - } elseif ($propmode == 'All') { // Dont care for propmode - ; // No Prop-Filter - } else { // Propmode set, take care of it - $sql .= " and col_prop_mode = ?"; - $binding[] = $propmode; - } - } + // Build WHERE clause for inner queries + $where_clause = "station_id in (" . $location_list . ")"; + if ($where_condition) { + $where_clause .= " and " . $where_condition; + } + // Build the year/month selection + if ($period == "year") { + $year_expr = "YEAR(col_time_on)"; + } else if ($period == "month") { + $year_expr = "DATE_FORMAT(col_time_on, '%Y-%m')"; + } - if ($mode != 'All') { - $sql .= " and (col_mode = ? or col_submode = ?)"; - $binding[] = $mode; - $binding[] = $mode; - } + // For DXCC, we need an outer WHERE condition in the all_periods CTE + $all_periods_where = $where_clause . " " . $filter_conditions['sql']; + if ($outer_where_condition) { + $all_periods_where .= " and " . $outer_where_condition; + } - $sql .= " order by year + $sql = "WITH all_periods AS ( + SELECT DISTINCT " . $year_expr . " AS year + FROM " . $this->config->item('table_name') . " + WHERE " . $all_periods_where . " + ), + new_items_per_period AS ( + SELECT + year, + COUNT(*) AS tot + FROM ( + SELECT + " . $column . ", + " . $year_expr . " AS year, + ROW_NUMBER() OVER (PARTITION BY " . $column . " ORDER BY " . $year_expr . ") AS rn + FROM " . $this->config->item('table_name') . " + WHERE " . $where_clause . " + " . $filter_conditions['sql'] . " + ) ranked + WHERE rn = 1 + GROUP BY year + ) + SELECT + ap.year, + COALESCE(nipp.tot, 0) AS tot + FROM all_periods ap + LEFT JOIN new_items_per_period nipp ON ap.year = nipp.year + ORDER BY ap.year"; + + // Duplicate bindings for both CTEs (same filters used twice) + $final_bindings = array_merge($filter_conditions['bindings'], $filter_conditions['bindings']); + + $query = $this->db->query($sql, $final_bindings); + return $this->count_and_add_accumulated_total($query->result()); + } + + function build_filter_conditions($band, $mode, $propmode) { + $sql = ""; + $binding = []; + + // Normal band/propmode handling + if ($band == 'SAT') { + $sql .= "and col_prop_mode = ?"; + $binding[] = $band; + } else { + if ($band != 'All') { + $sql .= "and col_band = ?"; + $binding[] = $band; + } + if ($propmode == 'NoSAT') { + $sql .= "and col_prop_mode !='SAT'"; + } elseif ($propmode == 'None') { + $sql .= "and (trim(col_prop_mode)='' or col_prop_mode is null)"; + } elseif ($propmode == 'All') { + // No Prop-Filter + } else { + $sql .= "and col_prop_mode = ?"; + $binding[] = $propmode; + } + } + + if ($mode != 'All') { + $sql .= "and (col_mode = ? or col_submode = ?)"; + $binding[] = $mode; + $binding[] = $mode; + } + + return ['sql' => $sql, 'bindings' => $binding]; + } + + function get_accumulated_by_column_slow($band, $mode, $propmode, $period, $location_list, $column, $where_condition = '', $where_check = '', $outer_where_condition = '') { + $filter_conditions = $this->build_filter_conditions($band, $mode, $propmode); + $binding = $filter_conditions['bindings']; + $filter_sql = $filter_conditions['sql']; + + if ($period == "year") { + $sql = "select year(thcv.col_time_on) year"; + } else if ($period == "month") { + $sql = "select date_format(col_time_on, '%Y-%m') year"; + } + + $sql .= ", coalesce(y.tot, 0) tot + from " . $this->config->item('table_name') . " thcv + left outer join ( + select count(" . $column . ") as tot, year + from (select distinct "; + + if ($period == "year") { + $sql .= "year(col_time_on)"; + } else if ($period == "month") { + $sql .= "date_format(col_time_on, '%Y-%m')"; + } + + $sql .= " year, " . $column . " + from " . $this->config->item('table_name') . + " where station_id in (" . $location_list . ")"; + + if ($where_condition) { + $sql .= " and " . $where_condition; + } + + $sql .= " " . $filter_sql; + + $sql .= " order by year ) x where not exists (select 1 from " . $this->config->item('table_name') . " where"; - if ($period == "year") { - $sql .= " year(col_time_on) < year";; - } else if ($period == "month") { - $sql .= " date_format(col_time_on, '%Y-%m') < year";; - } + if ($period == "year") { + $sql .= " year(col_time_on) < year";; + } else if ($period == "month") { + $sql .= " date_format(col_time_on, '%Y-%m') < year";; + } - $sql .= " and col_dxcc = x.col_dxcc"; + $sql .= " and " . $column . " = x." . $column; - if ($band == 'SAT') { // Left for compatibility reasons - $sql .= " and col_prop_mode = ?"; - $binding[] = $band; - } else { // Not SAT - if ($band != 'All') { // Band set? Take care of it - $sql .= " and col_band = ?"; - $binding[] = $band; - } - if ( $propmode == 'NoSAT' ) { // All without SAT - $sql .= " and col_prop_mode !='SAT'"; - } elseif ($propmode == 'None') { // Empty Propmode - $sql .= " and (trim(col_prop_mode)='' or col_prop_mode is null)"; - } elseif ($propmode == 'All') { // Dont care for propmode - ; // No Prop-Filter - } else { // Propmode set, take care of it - $sql .= " and col_prop_mode = ?"; - $binding[] = $propmode; - } - } + $sql .= " " . $filter_sql; - if ($mode != 'All') { - $sql .= " and (col_mode = ? or col_submode = ?)"; - $binding[] = $mode; - $binding[] = $mode; - } + if ($where_check) { + $sql .= " and " . $where_check; + } - $sql .= " and station_id in (" . $location_list . ")) - group by year - order by year"; + $sql .= " and station_id in (" . $location_list . ")) + group by year + order by year"; - if ($period == "year") { - $sql .= " ) y on year(thcv.col_time_on) = y.year"; - } else if ($period == "month") { - $sql .= " ) y on date_format(col_time_on, '%Y-%m') = y.year"; - } + if ($period == "year") { + $sql .= " ) y on year(thcv.col_time_on) = y.year"; + } else if ($period == "month") { + $sql .= " ) y on date_format(col_time_on, '%Y-%m') = y.year"; + } - $sql .= " where thcv.col_dxcc > 0"; + $sql .= " where station_id in (" . $location_list . ")"; - if ($band != 'All') { - if ($band == 'SAT') { - $sql .= " and col_prop_mode = ?"; - $binding[] = $band; - } else { - $sql .= " and col_prop_mode !='SAT'"; - $sql .= " and col_band = ?"; - $binding[] = $band; - } - } + if ($outer_where_condition) { + $sql .= " and " . $outer_where_condition; + } - if ($mode != 'All') { - $sql .= " and (col_mode = ? or col_submode = ?)"; - $binding[] = $mode; - $binding[] = $mode; - } + $sql .= " " . $filter_sql; - $sql .= " and station_id in (" . $location_list . ")"; + if ($period == "year") { + $sql .= " group by year(thcv.col_time_on), y.tot + order by year(thcv.col_time_on)"; + } else if ($period == "month") { + $sql .= " group by date_format(col_time_on, '%Y-%m'), y.tot + order by date_format(col_time_on, '%Y-%m')"; + } - if ($period == "year") { - $sql .= " group by year(thcv.col_time_on), y.tot - order by year(thcv.col_time_on)"; - } else if ($period == "month") { - $sql .= " group by date_format(col_time_on, '%Y-%m'), y.tot - order by date_format(col_time_on, '%Y-%m')"; - } + // Triplicate bindings for the three filter locations + $final_bindings = array_merge($binding, $binding, $binding); - $query = $this->db->query($sql,$binding); + $query = $this->db->query($sql, $final_bindings); - return $this->count_and_add_accumulated_total($query->result()); + return $this->count_and_add_accumulated_total($query->result()); } - function count_and_add_accumulated_total($array) { - $counter = 0; - for ($i = 0; $i < count($array); $i++) { - $array[$i]->total = $array[$i]->tot + $counter; - $counter = $array[$i]->total; - } - return $array; - } - - function get_accumulated_waja($band, $mode, $propmode, $period, $location_list) { - $binding=[]; - if ($period == "year") { - $sql = "select year(thcv.col_time_on) year"; - } else if ($period == "month") { - $sql = "select date_format(col_time_on, '%Y-%m') year"; - } - - $sql .= ", coalesce(y.tot, 0) tot - from " . $this->config->item('table_name') . " thcv - left outer join ( - select count(col_state) as tot, year - from (select distinct "; - - if ($period == "year") { - $sql .= "year(col_time_on)"; - } else if ($period == "month") { - $sql .= "date_format(col_time_on, '%Y-%m')"; - } - - $sql .= " year, col_state - from " . $this->config->item('table_name') . - " where station_id in (" . $location_list . ")"; - - if ($band == 'SAT') { // Left for compatibility reasons - $sql .= " and col_prop_mode = ?"; - $binding[] = $band; - } else { // Not SAT - if ($band != 'All') { // Band set? Take care of it - $sql .= " and col_band = ?"; - $binding[] = $band; - } - if ( $propmode == 'NoSAT' ) { // All without SAT - $sql .= " and col_prop_mode !='SAT'"; - } elseif ($propmode == 'None') { // Empty Propmode - $sql .= " and (trim(col_prop_mode)='' or col_prop_mode is null)"; - } elseif ($propmode == 'All') { // Dont care for propmode - ; // No Prop-Filter - } else { // Propmode set, take care of it - $sql .= " and col_prop_mode = ?"; - $binding[] = $propmode; - } - } - - if ($mode != 'All') { - $sql .= " and (col_mode = ? or col_submode = ?)"; - $binding[] = $mode; - $binding[] = $mode; - } - - $sql .= " and COL_DXCC in ('339') and trim(coalesce(col_state,'')) != ''"; - - $sql .= " order by year - ) x - where not exists (select 1 from " . $this->config->item('table_name') . " where"; - - if ($period == "year") { - $sql .= " year(col_time_on) < year";; - } else if ($period == "month") { - $sql .= " date_format(col_time_on, '%Y-%m') < year";; - } - - $sql .= " and col_state = x.col_state"; - - if ($band == 'SAT') { // Left for compatibility reasons - $sql .= " and col_prop_mode = ?"; - $binding[] = $band; - } else { // Not SAT - if ($band != 'All') { // Band set? Take care of it - $sql .= " and col_band = ?"; - $binding[] = $band; - } - if ( $propmode == 'NoSAT' ) { // All without SAT - $sql .= " and col_prop_mode !='SAT'"; - } elseif ($propmode == 'None') { // Empty Propmode - $sql .= " and (trim(col_prop_mode)='' or col_prop_mode is null)"; - } elseif ($propmode == 'All') { // Dont care for propmode - ; // No Prop-Filter - } else { // Propmode set, take care of it - $sql .= " and col_prop_mode = ?"; - $binding[] = $propmode; - } - } - - if ($mode != 'All') { - $sql .= " and (col_mode = ? or col_submode = ?)"; - $binding[] = $mode; - $binding[] = $mode; - } - - $sql .= " and COL_DXCC in ('339')"; - - $sql .= " and station_id in (" . $location_list . ")) - group by year - order by year"; - - if ($period == "year") { - $sql .= " ) y on year(thcv.col_time_on) = y.year"; - } else if ($period == "month") { - $sql .= " ) y on date_format(col_time_on, '%Y-%m') = y.year"; - } - - $sql .= " where station_id in (" . $location_list . ")"; - - if ($band == 'SAT') { // Left for compatibility reasons - $sql .= " and col_prop_mode = ?"; - $binding[] = $band; - } else { // Not SAT - if ($band != 'All') { // Band set? Take care of it - $sql .= " and col_band = ?"; - $binding[] = $band; - } - if ( $propmode == 'NoSAT' ) { // All without SAT - $sql .= " and col_prop_mode !='SAT'"; - } elseif ($propmode == 'None') { // Empty Propmode - $sql .= " and (trim(col_prop_mode)='' or col_prop_mode is null)"; - } elseif ($propmode == 'All') { // Dont care for propmode - ; // No Prop-Filter - } else { // Propmode set, take care of it - $sql .= " and col_prop_mode = ?"; - $binding[] = $propmode; - } - } - - if ($mode != 'All') { - $sql .= " and (col_mode = ? or col_submode = ?)"; - $binding[] = $mode; - $binding[] = $mode; - } - - if ($period == "year") { - $sql .= " group by year(thcv.col_time_on), y.tot - order by year(thcv.col_time_on)"; - } else if ($period == "month") { - $sql .= " group by date_format(col_time_on, '%Y-%m'), y.tot - order by date_format(col_time_on, '%Y-%m')"; - } - - $query = $this->db->query($sql, $binding); - - return $this->count_and_add_accumulated_total($query->result()); - } - - function get_accumulated_was($band, $mode, $propmode, $period, $location_list) { - $binding=[]; - if ($period == "year") { - $sql = "select year(thcv.col_time_on) year"; - } else if ($period == "month") { - $sql = "select date_format(col_time_on, '%Y-%m') year"; - } - - $sql .= ", coalesce(y.tot, 0) tot - from " . $this->config->item('table_name') . " thcv - left outer join ( - select count(col_state) as tot, year - from (select distinct "; - - if ($period == "year") { - $sql .= "year(col_time_on)"; - } else if ($period == "month") { - $sql .= "date_format(col_time_on, '%Y-%m')"; - } - - $sql .= " year, col_state - from " . $this->config->item('table_name') . - " where station_id in (" . $location_list . ")"; - - - if ($band == 'SAT') { // Left for compatibility reasons - $sql .= " and col_prop_mode = ?"; - $binding[] = $band; - } else { // Not SAT - if ($band != 'All') { // Band set? Take care of it - $sql .= " and col_band = ?"; - $binding[] = $band; - } - if ( $propmode == 'NoSAT' ) { // All without SAT - $sql .= " and col_prop_mode !='SAT'"; - } elseif ($propmode == 'None') { // Empty Propmode - $sql .= " and (trim(col_prop_mode)='' or col_prop_mode is null)"; - } elseif ($propmode == 'All') { // Dont care for propmode - ; // No Prop-Filter - } else { // Propmode set, take care of it - $sql .= " and col_prop_mode = ?"; - $binding[] = $propmode; - } - } - - if ($mode != 'All') { - $sql .= " and (col_mode = ? or col_submode = ?)"; - $binding[] = $mode; - $binding[] = $mode; - } - - $sql .= " and COL_DXCC in ('291', '6', '110')"; - $sql .= " and COL_STATE in ('AK','AL','AR','AZ','CA','CO','CT','DE','FL','GA','HI','IA','ID','IL','IN','KS','KY','LA','MA','MD','ME','MI','MN','MO','MS','MT','NC','ND','NE','NH','NJ','NM','NV','NY','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VA','VT','WA','WI','WV','WY')"; - - $sql .= " order by year - ) x - where not exists (select 1 from " . $this->config->item('table_name') . " where"; - - if ($period == "year") { - $sql .= " year(col_time_on) < year";; - } else if ($period == "month") { - $sql .= " date_format(col_time_on, '%Y-%m') < year";; - } - - $sql .= " and col_state = x.col_state"; - - if ($band == 'SAT') { // Left for compatibility reasons - $sql .= " and col_prop_mode = ?"; - $binding[] = $band; - } else { // Not SAT - if ($band != 'All') { // Band set? Take care of it - $sql .= " and col_band = ?"; - $binding[] = $band; - } - if ( $propmode == 'NoSAT' ) { // All without SAT - $sql .= " and col_prop_mode !='SAT'"; - } elseif ($propmode == 'None') { // Empty Propmode - $sql .= " and (trim(col_prop_mode)='' or col_prop_mode is null)"; - } elseif ($propmode == 'All') { // Dont care for propmode - ; // No Prop-Filter - } else { // Propmode set, take care of it - $sql .= " and col_prop_mode = ?"; - $binding[] = $propmode; - } - } - - if ($mode != 'All') { - $sql .= " and (col_mode = ? or col_submode = ?)"; - $binding[] = $mode; - $binding[] = $mode; - } - - $sql .= " and COL_DXCC in ('291', '6', '110')"; - $sql .= " and COL_STATE in ('AK','AL','AR','AZ','CA','CO','CT','DE','FL','GA','HI','IA','ID','IL','IN','KS','KY','LA','MA','MD','ME','MI','MN','MO','MS','MT','NC','ND','NE','NH','NJ','NM','NV','NY','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VA','VT','WA','WI','WV','WY')"; - - $sql .= " and station_id in (" . $location_list . ")) - group by year - order by year"; - - if ($period == "year") { - $sql .= " ) y on year(thcv.col_time_on) = y.year"; - } else if ($period == "month") { - $sql .= " ) y on date_format(col_time_on, '%Y-%m') = y.year"; - } - - $sql .= " where station_id in (" . $location_list . ")"; - - if ($band == 'SAT') { // Left for compatibility reasons - $sql .= " and col_prop_mode = ?"; - $binding[] = $band; - } else { // Not SAT - if ($band != 'All') { // Band set? Take care of it - $sql .= " and col_band = ?"; - $binding[] = $band; - } - if ( $propmode == 'NoSAT' ) { // All without SAT - $sql .= " and col_prop_mode !='SAT'"; - } elseif ($propmode == 'None') { // Empty Propmode - $sql .= " and (trim(col_prop_mode)='' or col_prop_mode is null)"; - } elseif ($propmode == 'All') { // Dont care for propmode - ; // No Prop-Filter - } else { // Propmode set, take care of it - $sql .= " and col_prop_mode = ?"; - $binding[] = $propmode; - } - } - - if ($mode != 'All') { - $sql .= " and (col_mode = ? or col_submode = ?)"; - $binding[] = $mode; - $binding[] = $mode; - } - - if ($period == "year") { - $sql .= " group by year(thcv.col_time_on), y.tot - order by year(thcv.col_time_on)"; - } else if ($period == "month") { - $sql .= " group by date_format(col_time_on, '%Y-%m'), y.tot - order by date_format(col_time_on, '%Y-%m')"; - } - - $query = $this->db->query($sql, $binding); - - return $this->count_and_add_accumulated_total($query->result()); - } - - function get_accumulated_iota($band, $mode, $propmode, $period, $location_list) { - $binding = []; - if ($period == "year") { - $sql = "select year(thcv.col_time_on) year"; - } else if ($period == "month") { - $sql = "select date_format(col_time_on, '%Y-%m') year"; - } - - $sql .= ", coalesce(y.tot, 0) tot - from " . $this->config->item('table_name') . " thcv - left outer join ( - select count(col_iota) as tot, year - from (select distinct "; - - if ($period == "year") { - $sql .= "year(col_time_on)"; - } else if ($period == "month") { - $sql .= "date_format(col_time_on, '%Y-%m')"; - } - - $sql .= " year, col_iota - from " . $this->config->item('table_name') . - " where station_id in (" . $location_list . ")"; - - if ($band == 'SAT') { // Left for compatibility reasons - $sql .= " and col_prop_mode = ?"; - $binding[] = $band; - } else { // Not SAT - if ($band != 'All') { // Band set? Take care of it - $sql .= " and col_band = ?"; - $binding[] = $band; - } - if ( $propmode == 'NoSAT' ) { // All without SAT - $sql .= " and col_prop_mode !='SAT'"; - } elseif ($propmode == 'None') { // Empty Propmode - $sql .= " and (trim(col_prop_mode)='' or col_prop_mode is null)"; - } elseif ($propmode == 'All') { // Dont care for propmode - ; // No Prop-Filter - } else { // Propmode set, take care of it - $sql .= " and col_prop_mode = ?"; - $binding[] = $propmode; - } - } - - if ($mode != 'All') { - $sql .= " and (col_mode = ? or col_submode = ?)"; - $binding[] = $mode; - $binding[] = $mode; - } - - $sql .= " order by year - ) x - where not exists (select 1 from " . $this->config->item('table_name') . " where"; - - if ($period == "year") { - $sql .= " year(col_time_on) < year";; - } else if ($period == "month") { - $sql .= " date_format(col_time_on, '%Y-%m') < year";; - } - - $sql .= " and col_iota = x.col_iota"; - - if ($band == 'SAT') { // Left for compatibility reasons - $sql .= " and col_prop_mode = ?"; - $binding[] = $band; - } else { // Not SAT - if ($band != 'All') { // Band set? Take care of it - $sql .= " and col_band = ?"; - $binding[] = $band; - } - if ( $propmode == 'NoSAT' ) { // All without SAT - $sql .= " and col_prop_mode !='SAT'"; - } elseif ($propmode == 'None') { // Empty Propmode - $sql .= " and (trim(col_prop_mode)='' or col_prop_mode is null)"; - } elseif ($propmode == 'All') { // Dont care for propmode - ; // No Prop-Filter - } else { // Propmode set, take care of it - $sql .= " and col_prop_mode = ?"; - $binding[] = $propmode; - } - } - - if ($mode != 'All') { - $sql .= " and (col_mode = ? or col_submode = ?)"; - $binding[] = $mode; - $binding[] = $mode; - } - - $sql .= " and station_id in (" . $location_list . ")) - group by year - order by year"; - - if ($period == "year") { - $sql .= " ) y on year(thcv.col_time_on) = y.year"; - } else if ($period == "month") { - $sql .= " ) y on date_format(col_time_on, '%Y-%m') = y.year"; - } - - $sql .= " where station_id in (" . $location_list . ")"; - - if ($band == 'SAT') { // Left for compatibility reasons - $sql .= " and col_prop_mode = ?"; - $binding[] = $band; - } else { // Not SAT - if ($band != 'All') { // Band set? Take care of it - $sql .= " and col_band = ?"; - $binding[] = $band; - } - if ( $propmode == 'NoSAT' ) { // All without SAT - $sql .= " and col_prop_mode !='SAT'"; - } elseif ($propmode == 'None') { // Empty Propmode - $sql .= " and (trim(col_prop_mode)='' or col_prop_mode is null)"; - } elseif ($propmode == 'All') { // Dont care for propmode - ; // No Prop-Filter - } else { // Propmode set, take care of it - $sql .= " and col_prop_mode = ?"; - $binding[] = $propmode; - } - } - - if ($mode != 'All') { - $sql .= " and (col_mode = ? or col_submode = ?)"; - $binding[] = $mode; - $binding[] = $mode; - } - - if ($period == "year") { - $sql .= " group by year(thcv.col_time_on), y.tot - order by year(thcv.col_time_on)"; - } else if ($period == "month") { - $sql .= " group by date_format(col_time_on, '%Y-%m'), y.tot - order by date_format(col_time_on, '%Y-%m')"; - } - - $query = $this->db->query($sql, $binding); - - return $this->count_and_add_accumulated_total($query->result()); - } - - function get_accumulated_waz($band, $mode, $propmode, $period, $location_list) { - $binding=[]; - if ($period == "year") { - $sql = "select year(thcv.col_time_on) year"; - } else if ($period == "month") { - $sql = "select date_format(col_time_on, '%Y-%m') year"; - } - - $sql .= ", coalesce(y.tot, 0) tot - from " . $this->config->item('table_name') . " thcv - left outer join ( - select count(col_cqz) as tot, year - from (select distinct "; - - if ($period == "year") { - $sql .= "year(col_time_on)"; - } else if ($period == "month") { - $sql .= "date_format(col_time_on, '%Y-%m')"; - } - - $sql .= " year, col_cqz - from " . $this->config->item('table_name') . - " where station_id in (" . $location_list . ")"; - - if ($band == 'SAT') { // Left for compatibility reasons - $sql .= " and col_prop_mode = ?"; - $binding[] = $band; - } else { // Not SAT - if ($band != 'All') { // Band set? Take care of it - $sql .= " and col_band = ?"; - $binding[] = $band; - } - if ( $propmode == 'NoSAT' ) { // All without SAT - $sql .= " and col_prop_mode !='SAT'"; - } elseif ($propmode == 'None') { // Empty Propmode - $sql .= " and (trim(col_prop_mode)='' or col_prop_mode is null)"; - } elseif ($propmode == 'All') { // Dont care for propmode - ; // No Prop-Filter - } else { // Propmode set, take care of it - $sql .= " and col_prop_mode = ?"; - $binding[] = $propmode; - } - } - - if ($mode != 'All') { - $sql .= " and (col_mode = ? or col_submode = ?)"; - $binding[] = $mode; - $binding[] = $mode; - } - - $sql .= " order by year - ) x - where not exists (select 1 from " . $this->config->item('table_name') . " where"; - - if ($period == "year") { - $sql .= " year(col_time_on) < year";; - } else if ($period == "month") { - $sql .= " date_format(col_time_on, '%Y-%m') < year";; - } - - $sql .= " and col_cqz = x.col_cqz"; - - if ($band == 'SAT') { // Left for compatibility reasons - $sql .= " and col_prop_mode = ?"; - $binding[] = $band; - } else { // Not SAT - if ($band != 'All') { // Band set? Take care of it - $sql .= " and col_band = ?"; - $binding[] = $band; - } - if ( $propmode == 'NoSAT' ) { // All without SAT - $sql .= " and col_prop_mode !='SAT'"; - } elseif ($propmode == 'None') { // Empty Propmode - $sql .= " and (trim(col_prop_mode)='' or col_prop_mode is null)"; - } elseif ($propmode == 'All') { // Dont care for propmode - ; // No Prop-Filter - } else { // Propmode set, take care of it - $sql .= " and col_prop_mode = ?"; - $binding[] = $propmode; - } - } - - if ($mode != 'All') { - $sql .= " and (col_mode = ? or col_submode = ?)"; - $binding[] = $mode; - $binding[] = $mode; - } - - $sql .= " and station_id in (" . $location_list . ")) - group by year - order by year"; - - if ($period == "year") { - $sql .= " ) y on year(thcv.col_time_on) = y.year"; - } else if ($period == "month") { - $sql .= " ) y on date_format(col_time_on, '%Y-%m') = y.year"; - } - - $sql .= " where station_id in (" . $location_list . ")"; - - if ($band == 'SAT') { // Left for compatibility reasons - $sql .= " and col_prop_mode = ?"; - $binding[] = $band; - } else { // Not SAT - if ($band != 'All') { // Band set? Take care of it - $sql .= " and col_band = ?"; - $binding[] = $band; - } - if ( $propmode == 'NoSAT' ) { // All without SAT - $sql .= " and col_prop_mode !='SAT'"; - } elseif ($propmode == 'None') { // Empty Propmode - $sql .= " and (trim(col_prop_mode)='' or col_prop_mode is null)"; - } elseif ($propmode == 'All') { // Dont care for propmode - ; // No Prop-Filter - } else { // Propmode set, take care of it - $sql .= " and col_prop_mode = ?"; - $binding[] = $propmode; - } - } - - if ($mode != 'All') { - $sql .= " and (col_mode = ? or col_submode = ?)"; - $binding[] = $mode; - $binding[] = $mode; - } - - if ($period == "year") { - $sql .= " group by year(thcv.col_time_on), y.tot - order by year(thcv.col_time_on)"; - } else if ($period == "month") { - $sql .= " group by date_format(col_time_on, '%Y-%m'), y.tot - order by date_format(col_time_on, '%Y-%m')"; - } - - $query = $this->db->query($sql,$binding); - - return $this->count_and_add_accumulated_total($query->result()); - } + function count_and_add_accumulated_total($array) { + $counter = 0; + for ($i = 0; $i < count($array); $i++) { + $array[$i]->total = $array[$i]->tot + $counter; + $counter = $array[$i]->total; + } + return $array; + } + + function get_accumulated_dxcc($band, $mode, $propmode, $period, $location_list) { + $where_condition = "COL_DXCC > 0"; + $where_check = "COL_DXCC > 0"; + return $this->get_accumulated_by_column($band, $mode, $propmode, $period, $location_list, 'col_dxcc', $where_condition, $where_check, $where_condition, true); + } + + function get_accumulated_waja($band, $mode, $propmode, $period, $location_list) { + $where_condition = "COL_DXCC in ('339') and trim(coalesce(col_state,'')) != ''"; + $where_check = "COL_DXCC in ('339')"; + return $this->get_accumulated_by_column($band, $mode, $propmode, $period, $location_list, 'col_state', $where_condition, $where_check); + } + + function get_accumulated_was($band, $mode, $propmode, $period, $location_list) { + $where_condition = "COL_DXCC in ('291', '6', '110') and COL_STATE in ('AK','AL','AR','AZ','CA','CO','CT','DE','FL','GA','HI','IA','ID','IL','IN','KS','KY','LA','MA','MD','ME','MI','MN','MO','MS','MT','NC','ND','NE','NH','NJ','NM','NV','NY','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VA','VT','WA','WI','WV','WY')"; + return $this->get_accumulated_by_column($band, $mode, $propmode, $period, $location_list, 'col_state', $where_condition, $where_condition); + } + + function get_accumulated_iota($band, $mode, $propmode, $period, $location_list) { + $where_condition = "COL_IOTA > ''"; + return $this->get_accumulated_by_column($band, $mode, $propmode, $period, $location_list, 'col_iota', $where_condition, $where_condition); + } + + function get_accumulated_waz($band, $mode, $propmode, $period, $location_list) { + return $this->get_accumulated_by_column($band, $mode, $propmode, $period, $location_list, 'col_cqz', 'col_cqz between 1 and 40', 'col_cqz between 1 and 40'); + } function get_accumulated_vucc($band, $mode, $propmode, $period, $location_list) { $dbversion = $this->db->version(); @@ -752,256 +269,136 @@ class Accumulate_model extends CI_Model $sql = ""; if ($dbversion[0] >= "8") { - $query = $this->fastquery($band, $mode, $propmode, $period, $location_list); + $query = $this->fastVuccQuery($band, $mode, $propmode, $period, $location_list); return $query->result(); } else { - $query = $this->slowquery($band, $mode, $propmode, $period, $location_list); + $query = $this->slowVuccQuery($band, $mode, $propmode, $period, $location_list); return $this->count_and_add_accumulated_total($query->result()); } } - function fastquery($band, $mode, $propmode, $period, $location_list) { - $binding=[]; - $sql = "WITH firstseen AS ( - SELECT substr(col_gridsquare,1,4) as grid, "; + function fastVuccQuery($band, $mode, $propmode, $period, $location_list) { + $filter_conditions = $this->build_filter_conditions($band, $mode, $propmode); + $binding = $filter_conditions['bindings']; + $filter_sql = $filter_conditions['sql']; - if ($period == "year") { - $sql .= "MIN(year(col_time_on)) year"; - } else if ($period == "month") { - $sql .= "MIN(date_format(col_time_on, '%Y-%m')) year"; - } + $sql = "WITH firstseen AS ( + SELECT substr(col_gridsquare,1,4) as grid, "; - $sql .= " from " . $this->config->item('table_name') . " thcv - where coalesce(col_gridsquare, '') <> '' - and station_id in (" . $location_list . ")"; + if ($period == "year") { + $sql .= "MIN(year(col_time_on)) year"; + } else if ($period == "month") { + $sql .= "MIN(date_format(col_time_on, '%Y-%m')) year"; + } + $sql .= " from " . $this->config->item('table_name') . " thcv + where coalesce(col_gridsquare, '') <> '' + and station_id in (" . $location_list . ") + " . $filter_sql . " + GROUP BY 1 + union all + select substr(grid, 1,4) as grid, year + from ( + select TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(COL_VUCC_GRIDS, ',', x.x), ',',-1)) as grid, "; + if ($period == "year") { + $sql .= "MIN(year(col_time_on)) year"; + } else if ($period == "month") { + $sql .= "MIN(date_format(col_time_on, '%Y-%m')) year"; + } - if ($band == 'SAT') { // Left for compatibility reasons - $sql .= " and col_prop_mode = ?"; - $binding[] = $band; - } else { // Not SAT - if ($band != 'All') { // Band set? Take care of it - $sql .= " and col_band = ?"; - $binding[] = $band; - } - if ( $propmode == 'NoSAT' ) { // All without SAT - $sql .= " and col_prop_mode !='SAT'"; - } elseif ($propmode == 'None') { // Empty Propmode - $sql .= " and (trim(col_prop_mode)='' or col_prop_mode is null)"; - } elseif ($propmode == 'All') { // Dont care for propmode - ; // No Prop-Filter - } else { // Propmode set, take care of it - $sql .= " and col_prop_mode = ?"; - $binding[] = $propmode; - } - } - - if ($mode != 'All') { - $sql .= " and (col_mode = ? or col_submode = ?)"; - $binding[] = $mode; - $binding[] = $mode; - } - - $sql .= " GROUP BY 1 - union all - select substr(grid, 1,4) as grid, year - from ( - select TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(COL_VUCC_GRIDS, ',', x.x), ',',-1)) as grid, "; - if ($period == "year") { - $sql .= "MIN(year(col_time_on)) year"; - } else if ($period == "month") { - $sql .= "MIN(date_format(col_time_on, '%Y-%m')) year"; - } - - $sql .= " from " . $this->config->item('table_name') . " thcv - cross join ( - select 1 as x - union all - select 2 - union all - select 3 - union all - select 4) x - where - x.x <= length(COL_VUCC_GRIDS)-length(replace(COL_VUCC_GRIDS, ',', ''))+ 1 - and coalesce(COL_VUCC_GRIDS, '') <> '' - and station_id in (" . $location_list . ")"; - - if ($band == 'SAT') { // Left for compatibility reasons - $sql .= " and col_prop_mode = ?"; - $binding[] = $band; - } else { // Not SAT - if ($band != 'All') { // Band set? Take care of it - $sql .= " and col_band = ?"; - $binding[] = $band; - } - if ( $propmode == 'NoSAT' ) { // All without SAT - $sql .= " and col_prop_mode !='SAT'"; - } elseif ($propmode == 'None') { // Empty Propmode - $sql .= " and (trim(col_prop_mode)='' or col_prop_mode is null)"; - } elseif ($propmode == 'All') { // Dont care for propmode - ; // No Prop-Filter - } else { // Propmode set, take care of it - $sql .= " and col_prop_mode = ?"; - $binding[] = $propmode; - } - } - - if ($mode != 'All') { - $sql .= " and (col_mode = ? or col_submode = ?)"; - $binding[] = $mode; - $binding[] = $mode; - } - - $sql .= " GROUP BY 1) as z + $sql .= " from " . $this->config->item('table_name') . " thcv + cross join ( + select 1 as x + union all + select 2 + union all + select 3 + union all + select 4) x + where + x.x <= length(COL_VUCC_GRIDS)-length(replace(COL_VUCC_GRIDS, ',', ''))+ 1 + and coalesce(COL_VUCC_GRIDS, '') <> '' + and station_id in (" . $location_list . ") + " . $filter_sql . " + GROUP BY 1) as z ) , z as ( SELECT grid, row_number() OVER (partition by grid ORDER BY grid asc, year asc) as rn, year FROM firstseen ) select DISTINCT COUNT(grid) OVER (ORDER BY year) as total, year from z where rn = 1 -"; + "; - $query = $this->db->query($sql, $binding); - return $query; - } + // Duplicate bindings for both parts of the UNION + $final_bindings = array_merge($binding, $binding); - function slowquery($band, $mode, $propmode, $period, $location_list) { - $binding=[]; - $sql = ""; - if ($period == "year") { - $sql = "select year(thcv.col_time_on) year"; - } else if ($period == "month") { - $sql = "select date_format(col_time_on, '%Y-%m') year"; - } + $query = $this->db->query($sql, $final_bindings); + return $query; + } - $sql .= ", coalesce(y.tot, 0) tot - from " . $this->config->item('table_name') . " thcv - left outer join ( - select count(substr(col_gridsquare,1,4)) as tot, year - from (select distinct "; + function slowVuccQuery($band, $mode, $propmode, $period, $location_list) { + $filter_conditions = $this->build_filter_conditions($band, $mode, $propmode); + $binding = $filter_conditions['bindings']; + $filter_sql = $filter_conditions['sql']; - if ($period == "year") { - $sql .= "year(col_time_on)"; - } else if ($period == "month") { - $sql .= "date_format(col_time_on, '%Y-%m')"; - } + if ($period == "year") { + $sql = "select year(thcv.col_time_on) year"; + } else if ($period == "month") { + $sql = "select date_format(col_time_on, '%Y-%m') year"; + } - $sql .= " year, substr(col_gridsquare,1,4) as col_gridsquare - from " . $this->config->item('table_name') . - " where station_id in (" . $location_list . ")"; + $sql .= ", coalesce(y.tot, 0) tot + from " . $this->config->item('table_name') . " thcv + left outer join ( + select count(substr(col_gridsquare,1,4)) as tot, year + from (select distinct "; - if ($band == 'SAT') { // Left for compatibility reasons - $sql .= " and col_prop_mode = ?"; - $binding[] = $band; - } else { // Not SAT - if ($band != 'All') { // Band set? Take care of it - $sql .= " and col_band = ?"; - $binding[] = $band; - } - if ( $propmode == 'NoSAT' ) { // All without SAT - $sql .= " and col_prop_mode !='SAT'"; - } elseif ($propmode == 'None') { // Empty Propmode - $sql .= " and (trim(col_prop_mode)='' or col_prop_mode is null)"; - } elseif ($propmode == 'All') { // Dont care for propmode - ; // No Prop-Filter - } else { // Propmode set, take care of it - $sql .= " and col_prop_mode = ?"; - $binding[] = $propmode; - } - } + if ($period == "year") { + $sql .= "year(col_time_on)"; + } else if ($period == "month") { + $sql .= "date_format(col_time_on, '%Y-%m')"; + } - if ($mode != 'All') { - $sql .= " and (col_mode = ? or col_submode = ?)"; - $binding[]=$mode; - $binding[]=$mode; - } - - $sql .= " order by year + $sql .= " year, substr(col_gridsquare,1,4) as col_gridsquare + from " . $this->config->item('table_name') . + " where station_id in (" . $location_list . ")" + . $filter_sql . " + order by year ) x where not exists (select 1 from " . $this->config->item('table_name') . " where"; - if ($period == "year") { - $sql .= " year(col_time_on) < year";; - } else if ($period == "month") { - $sql .= " date_format(col_time_on, '%Y-%m') < year";; - } + if ($period == "year") { + $sql .= " year(col_time_on) < year";; + } else if ($period == "month") { + $sql .= " date_format(col_time_on, '%Y-%m') < year";; + } - $sql .= " and substr(col_gridsquare,1,4) = substr(x.col_gridsquare,1,4)"; + $sql .= " and substr(col_gridsquare,1,4) = substr(x.col_gridsquare,1,4)" + . $filter_sql . " + and station_id in (" . $location_list . ")) + group by year + order by year"; - if ($band == 'SAT') { // Left for compatibility reasons - $sql .= " and col_prop_mode = ?"; - $binding[] = $band; - } else { // Not SAT - if ($band != 'All') { // Band set? Take care of it - $sql .= " and col_band = ?"; - $binding[] = $band; - } - if ( $propmode == 'NoSAT' ) { // All without SAT - $sql .= " and col_prop_mode !='SAT'"; - } elseif ($propmode == 'None') { // Empty Propmode - $sql .= " and (trim(col_prop_mode)='' or col_prop_mode is null)"; - } elseif ($propmode == 'All') { // Dont care for propmode - ; // No Prop-Filter - } else { // Propmode set, take care of it - $sql .= " and col_prop_mode = ?"; - $binding[] = $propmode; - } - } + if ($period == "year") { + $sql .= " ) y on year(thcv.col_time_on) = y.year"; + } else if ($period == "month") { + $sql .= " ) y on date_format(col_time_on, '%Y-%m') = y.year"; + } - if ($mode != 'All') { - $sql .= " and (col_mode = ? or col_submode = ?)"; - $binding[]=$mode; - $binding[]=$mode; - } + $sql .= " where station_id in (" . $location_list . ")" . $filter_sql; - $sql .= " and station_id in (" . $location_list . ")) - group by year - order by year"; + if ($period == "year") { + $sql .= " group by year(thcv.col_time_on), y.tot + order by year(thcv.col_time_on)"; + } else if ($period == "month") { + $sql .= " group by date_format(col_time_on, '%Y-%m'), y.tot + order by date_format(col_time_on, '%Y-%m')"; + } - if ($period == "year") { - $sql .= " ) y on year(thcv.col_time_on) = y.year"; - } else if ($period == "month") { - $sql .= " ) y on date_format(col_time_on, '%Y-%m') = y.year"; - } + // Triplicate bindings for the three filter locations + $final_bindings = array_merge($binding, $binding, $binding); - $sql .= " where station_id in (" . $location_list . ")"; - - if ($band == 'SAT') { // Left for compatibility reasons - $sql .= " and col_prop_mode = ?"; - $binding[] = $band; - } else { // Not SAT - if ($band != 'All') { // Band set? Take care of it - $sql .= " and col_band = ?"; - $binding[] = $band; - } - if ( $propmode == 'NoSAT' ) { // All without SAT - $sql .= " and col_prop_mode !='SAT'"; - } elseif ($propmode == 'None') { // Empty Propmode - $sql .= " and (trim(col_prop_mode)='' or col_prop_mode is null)"; - } elseif ($propmode == 'All') { // Dont care for propmode - ; // No Prop-Filter - } else { // Propmode set, take care of it - $sql .= " and col_prop_mode = ?"; - $binding[] = $propmode; - } - } - - if ($mode != 'All') { - $sql .= " and (col_mode = ? or col_submode = ?)"; - $binding[]=$mode; - $binding[]=$mode; - } - - if ($period == "year") { - $sql .= " group by year(thcv.col_time_on), y.tot - order by year(thcv.col_time_on)"; - } else if ($period == "month") { - $sql .= " group by date_format(col_time_on, '%Y-%m'), y.tot - order by date_format(col_time_on, '%Y-%m')"; - } - - $query = $this->db->query($sql, $binding); - return $query; - } + $query = $this->db->query($sql, $final_bindings); + return $query; + } }