From 928af0eaf370131890003915e74cc30ce5fc4c39 Mon Sep 17 00:00:00 2001 From: Patrick Burns Date: Wed, 29 May 2024 15:38:45 -0500 Subject: [PATCH 1/3] add a new API route to return the most recent QSO's --- application/controllers/Api.php | 11 ++++++ application/models/Logbook_model.php | 54 ++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/application/controllers/Api.php b/application/controllers/Api.php index 7bb18b5ac..4a98415c2 100644 --- a/application/controllers/Api.php +++ b/application/controllers/Api.php @@ -484,6 +484,17 @@ function statistics($key = null) { } + function recent_qsos($key = null) { + header('Content-type: application/json'); + $this->load->model('logbook_model'); + + $data['qsos'] = $this->logbook_model->recent_qsos(null, $key); + + http_response_code(201); + echo json_encode($data['qsos']); + + } + function lookup() { // start benchmarking $this->output->enable_profiler(TRUE); diff --git a/application/models/Logbook_model.php b/application/models/Logbook_model.php index 570f5063e..1c728127b 100755 --- a/application/models/Logbook_model.php +++ b/application/models/Logbook_model.php @@ -2238,6 +2238,60 @@ function todays_qsos($StationLocationsArray = null, $api_key = null) } } + + /* Return 10 most recent QSOs */ + function recent_qsos($StationLocationsArray = null, $api_key = null) + { + $CI = &get_instance(); + $detailed = $this->input->get('detailed') === 'true' ? true : false; + $limit = $this->input->get('limit'); + $limit = is_numeric($limit) ? $limit : 10; // Default to 10 if limit is not a number + + // Throw an error if limit is greater than 100 + if ($limit > 100) { + show_error('The limit cannot be greater than 100.', 400); + return; + } + + + if ($StationLocationsArray == null) { + $CI->load->model('logbooks_model'); + if ($api_key != null) { + $CI->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 = $CI->logbooks_model->find_active_station_logbook_from_userid($user_id); + $logbooks_locations_array = $CI->logbooks_model->list_logbook_relationships($active_station_logbook); + } else { + $logbooks_locations_array = []; + } + } else { + $logbooks_locations_array = $CI->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook')); + } + } else { + $logbooks_locations_array = $StationLocationsArray; + } + + if ($logbooks_locations_array) { + if ($detailed) { + $this->db->select('*'); + } else { + $this->db->select('COL_CALL, COL_BAND, COL_FREQ, COL_MODE, COL_SUBMODE, COL_NAME, COL_MY_GRIDSQUARE, COL_COUNTRY, COL_DXCC, COL_CONTEST_ID, COL_FREQ_RX, COL_MY_CITY, COL_MY_CNTY, COL_MY_COUNTRY, COL_COMMENT, COL_DISTANCE, COL_NAME, COL_OPERATOR, COL_RST_RCVD, COL_RST_SENT, COL_STATION_CALLSIGN, COL_TIME_OFF, COL_TIME_ON, COL_TX_PWR'); + } + $this->db->where_in('station_id', $logbooks_locations_array); + $this->db->order_by('COL_TIME_ON', 'DESC'); + $this->db->limit($limit); + $query = $this->db->get($this->config->item('table_name')); + + if ($query->num_rows() > 0) { + return $query->result(); + } + } else { + return null; + } + } + /* Return QSOs over a period of days */ function map_week_qsos($start, $end) { From ff97d96c339689c4dc06903f5db00d9eff7d75ef Mon Sep 17 00:00:00 2001 From: Patrick Burns Date: Wed, 29 May 2024 15:50:49 -0500 Subject: [PATCH 2/3] add some comments and self review --- application/controllers/Api.php | 1 - application/models/Logbook_model.php | 97 +++++++++++++++++----------- 2 files changed, 58 insertions(+), 40 deletions(-) diff --git a/application/controllers/Api.php b/application/controllers/Api.php index 4a98415c2..f45fa5284 100644 --- a/application/controllers/Api.php +++ b/application/controllers/Api.php @@ -481,7 +481,6 @@ function statistics($key = null) { http_response_code(201); echo json_encode(['Today' => $data['todays_qsos'], 'total_qsos' => $data['total_qsos'], 'month_qsos' => $data['month_qsos'], 'year_qsos' => $data['year_qsos']]); - } function recent_qsos($key = null) { diff --git a/application/models/Logbook_model.php b/application/models/Logbook_model.php index 1c728127b..2703944b8 100755 --- a/application/models/Logbook_model.php +++ b/application/models/Logbook_model.php @@ -2239,58 +2239,77 @@ function todays_qsos($StationLocationsArray = null, $api_key = null) } - /* Return 10 most recent QSOs */ + input->get('detailed') === 'true' ? true : false; - $limit = $this->input->get('limit'); - $limit = is_numeric($limit) ? $limit : 10; // Default to 10 if limit is not a number + // Get a reference to the CodeIgniter instance + $CI = &get_instance(); - // Throw an error if limit is greater than 100 - if ($limit > 100) { - show_error('The limit cannot be greater than 100.', 400); - return; - } + // Check if 'detailed' parameter is set to 'true' in the URL + $detailed = $this->input->get('detailed') === 'true' ? true : false; + // Get 'limit' parameter from the URL, default to 10 if it's not a number + $limit = $this->input->get('limit'); + $limit = is_numeric($limit) ? $limit : 10; - if ($StationLocationsArray == null) { - $CI->load->model('logbooks_model'); - if ($api_key != null) { - $CI->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 = $CI->logbooks_model->find_active_station_logbook_from_userid($user_id); - $logbooks_locations_array = $CI->logbooks_model->list_logbook_relationships($active_station_logbook); + // Throw an error if limit is greater than 100 + if ($limit > 100) { + show_error('The limit cannot be greater than 100.', 400); + return; + } + + // If no station locations array is provided, get it from the logbooks model + if ($StationLocationsArray == null) { + $CI->load->model('logbooks_model'); + + // If an API key is provided, get the logbooks locations array for the user associated with the API key + if ($api_key != null) { + $CI->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 = $CI->logbooks_model->find_active_station_logbook_from_userid($user_id); + $logbooks_locations_array = $CI->logbooks_model->list_logbook_relationships($active_station_logbook); + } else { + $logbooks_locations_array = []; + } } else { - $logbooks_locations_array = []; + // If no API key is provided, get the logbooks locations array for the current session's active station logbook + $logbooks_locations_array = $CI->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook')); } } else { - $logbooks_locations_array = $CI->logbooks_model->list_logbook_relationships($this->session->userdata('active_station_logbook')); + $logbooks_locations_array = $StationLocationsArray; } - } else { - $logbooks_locations_array = $StationLocationsArray; - } - if ($logbooks_locations_array) { - if ($detailed) { - $this->db->select('*'); - } else { - $this->db->select('COL_CALL, COL_BAND, COL_FREQ, COL_MODE, COL_SUBMODE, COL_NAME, COL_MY_GRIDSQUARE, COL_COUNTRY, COL_DXCC, COL_CONTEST_ID, COL_FREQ_RX, COL_MY_CITY, COL_MY_CNTY, COL_MY_COUNTRY, COL_COMMENT, COL_DISTANCE, COL_NAME, COL_OPERATOR, COL_RST_RCVD, COL_RST_SENT, COL_STATION_CALLSIGN, COL_TIME_OFF, COL_TIME_ON, COL_TX_PWR'); - } - $this->db->where_in('station_id', $logbooks_locations_array); - $this->db->order_by('COL_TIME_ON', 'DESC'); - $this->db->limit($limit); - $query = $this->db->get($this->config->item('table_name')); + // If there are any logbooks locations, get the recent QSOs + if ($logbooks_locations_array) { + // If 'detailed' is true, select all columns, otherwise select specific columns + if ($detailed) { + $this->db->select('*'); + } else { + $this->db->select('COL_CALL, COL_BAND, COL_FREQ, COL_MODE, COL_SUBMODE, COL_NAME, COL_MY_GRIDSQUARE, COL_COUNTRY, COL_DXCC, COL_CONTEST_ID, COL_FREQ_RX, COL_MY_CITY, COL_MY_CNTY, COL_MY_COUNTRY, COL_COMMENT, COL_DISTANCE, COL_NAME, COL_OPERATOR, COL_RST_RCVD, COL_RST_SENT, COL_STATION_CALLSIGN, COL_TIME_OFF, COL_TIME_ON, COL_TX_PWR'); + } - if ($query->num_rows() > 0) { - return $query->result(); + // Filter by station ID and order by time on, descending + $this->db->where_in('station_id', $logbooks_locations_array); + $this->db->order_by('COL_TIME_ON', 'DESC'); + + // Limit the number of results + $this->db->limit($limit); + + // Execute the query + $query = $this->db->get($this->config->item('table_name')); + + // If there are any results, return them, otherwise return null + if ($query->num_rows() > 0) { + return $query->result(); + } + } else { + return null; } - } else { - return null; - } } + ?> /* Return QSOs over a period of days */ function map_week_qsos($start, $end) From e4d49e9676373b027f3888d6a249869304893785 Mon Sep 17 00:00:00 2001 From: Patrick Burns Date: Wed, 29 May 2024 16:31:11 -0500 Subject: [PATCH 3/3] remove an errant /* Return QSOs over a period of days */ function map_week_qsos($start, $end)