From eab2c5200b190f0a712d89729aaa9fc0555f372d Mon Sep 17 00:00:00 2001 From: Andreas Kristiansen <6977712+AndreasK79@users.noreply.github.com> Date: Sat, 7 Dec 2024 17:38:20 +0100 Subject: [PATCH 01/10] [Sat pass] Add date in the form --- application/controllers/Satellite.php | 15 ++++++++++++++- application/views/satellite/pass.php | 8 +++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/application/controllers/Satellite.php b/application/controllers/Satellite.php index 32d0979db..2f3306545 100644 --- a/application/controllers/Satellite.php +++ b/application/controllers/Satellite.php @@ -285,7 +285,7 @@ class Satellite extends CI_Controller { $tle = new Predict_TLE($sat_tle->satellite, $temp[0], $temp[1]); // Instantiate it $sat = new Predict_Sat($tle); // Load up the satellite data - $now = Predict_Time::get_current_daynum(); // get the current time as Julian Date (daynum) + $now = $this->get_daynum_from_date($this->security->xss_clean($this->input->post('date'))); // get the current time as Julian Date (daynum) // You can modify some preferences in Predict(), the defaults are below // @@ -316,4 +316,17 @@ class Satellite extends CI_Controller { $data['format'] = $format; $this->load->view('satellite/passtable', $data); } + + public static function get_daynum_from_date($date) { + // Convert a Y-m-d date to a day number + + // Convert date to Unix timestamp + $timestamp = strtotime($date); + if ($timestamp === false) { + throw new Exception("Invalid date format. Expected Y-m-d."); + } + + // Calculate the day number + return Predict_Time::unix2daynum($timestamp, 0); + } } diff --git a/application/views/satellite/pass.php b/application/views/satellite/pass.php index 83d087e6b..0d84fdd3c 100644 --- a/application/views/satellite/pass.php +++ b/application/views/satellite/pass.php @@ -469,12 +469,10 @@ - + +
+
From 4ee8e1d87a49faea9d19b303cf58db252f3df3a8 Mon Sep 17 00:00:00 2001 From: Andreas Kristiansen <6977712+AndreasK79@users.noreply.github.com> Date: Sat, 7 Dec 2024 17:52:38 +0100 Subject: [PATCH 03/10] Show if satellite has TLE in sat table --- application/models/Satellite_model.php | 3 ++- application/views/satellite/index.php | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/application/models/Satellite_model.php b/application/models/Satellite_model.php index 503391419..57defaaef 100644 --- a/application/models/Satellite_model.php +++ b/application/models/Satellite_model.php @@ -3,9 +3,10 @@ class Satellite_model extends CI_Model { function get_all_satellites() { - $sql = "select satellite.id, satellite.name as satname, group_concat(distinct satellitemode.name separator ', ') as modename, satellite.displayname, satellite.orbit, satellite.lotw as lotw + $sql = "select satellite.id, satellite.name as satname, group_concat(distinct satellitemode.name separator ', ') as modename, satellite.displayname, satellite.orbit, satellite.lotw as lotw, tle.updated from satellite left outer join satellitemode on satellite.id = satellitemode.satelliteid + left outer join tle on satellite.id = tle.satelliteid group by satellite.name, satellite.displayname, satellite.orbit, satellite.id"; return $this->db->query($sql)->result(); diff --git a/application/views/satellite/index.php b/application/views/satellite/index.php index 2e88f4965..75f68980c 100644 --- a/application/views/satellite/index.php +++ b/application/views/satellite/index.php @@ -23,6 +23,7 @@ + @@ -62,6 +63,15 @@ echo ''.__("Unknown").''; break; } + echo ''; + ?> + '; + if ($sat->updated != null) { + echo ''.__("Yes").''; + } else { + echo ''.__("No").''; + } + echo ''; ?> From 9db7610f457c25555a8ecd14ef7beaa78c96e729 Mon Sep 17 00:00:00 2001 From: Andreas Kristiansen <6977712+AndreasK79@users.noreply.github.com> Date: Sat, 7 Dec 2024 18:01:19 +0100 Subject: [PATCH 04/10] Added tooltip with info about last tle update --- application/controllers/Satellite.php | 10 ++++++++++ application/views/satellite/index.php | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/application/controllers/Satellite.php b/application/controllers/Satellite.php index 2f3306545..ab39acb7d 100644 --- a/application/controllers/Satellite.php +++ b/application/controllers/Satellite.php @@ -21,6 +21,16 @@ class Satellite extends CI_Controller { $pageData['satellites'] = $this->satellite_model->get_all_satellites(); + if($this->session->userdata('user_date_format')) { + // If Logged in and session exists + $custom_date_format = $this->session->userdata('user_date_format'); + } else { + // Get Default date format from /config/wavelog.php + $custom_date_format = $this->config->item('qso_date_format'); + } + + $pageData['custom_date_format'] = $custom_date_format; + $footerData = []; $footerData['scripts'] = [ 'assets/js/sections/satellite.js?' . filemtime(realpath(__DIR__ . "/../../assets/js/sections/satellite.js")), diff --git a/application/views/satellite/index.php b/application/views/satellite/index.php index 75f68980c..5040cc0fa 100644 --- a/application/views/satellite/index.php +++ b/application/views/satellite/index.php @@ -67,7 +67,7 @@ ?> '; if ($sat->updated != null) { - echo ''.__("Yes").''; + echo 'updated)) . '">'.__("Yes").''; } else { echo ''.__("No").''; } From 899eb67a1f0216c19fdb1cc7d1aadf78791aafc7 Mon Sep 17 00:00:00 2001 From: Andreas Kristiansen <6977712+AndreasK79@users.noreply.github.com> Date: Sat, 7 Dec 2024 20:04:28 +0100 Subject: [PATCH 05/10] Added cron job and button under debug for update TLE --- application/config/migration.php | 2 +- application/controllers/Debug.php | 3 +- application/controllers/Update.php | 57 ++----------------------- application/migrations/230_tle_cron.php | 33 ++++++++++++++ application/models/Update_model.php | 56 ++++++++++++++++++++++++ application/views/debug/index.php | 5 +++ 6 files changed, 101 insertions(+), 55 deletions(-) create mode 100644 application/migrations/230_tle_cron.php diff --git a/application/config/migration.php b/application/config/migration.php index f39a5837f..0e1107dd7 100644 --- a/application/config/migration.php +++ b/application/config/migration.php @@ -22,7 +22,7 @@ $config['migration_enabled'] = TRUE; | */ -$config['migration_version'] = 229; +$config['migration_version'] = 230; /* |-------------------------------------------------------------------------- diff --git a/application/controllers/Debug.php b/application/controllers/Debug.php index 427a8f0d5..57ec55a31 100644 --- a/application/controllers/Debug.php +++ b/application/controllers/Debug.php @@ -102,6 +102,7 @@ class Debug extends CI_Controller $data['scp_update'] = $this->cron_model->cron('update_update_clublog_scp')->row(); $data['sota_update'] = $this->cron_model->cron('update_update_sota')->row(); $data['wwff_update'] = $this->cron_model->cron('update_update_wwff')->row(); + $data['tle_update'] = $this->cron_model->cron('update_update_tle')->row(); $data['page_title'] = __("Debug"); @@ -224,7 +225,7 @@ class Debug extends CI_Controller // Show success message $this->session->set_flashdata('success', __("Wavelog was updated successfully!")); - + } catch (\Throwable $th) { log_message("Error","Error at selfupdating"); } diff --git a/application/controllers/Update.php b/application/controllers/Update.php index b3b3ef253..733f9a17e 100644 --- a/application/controllers/Update.php +++ b/application/controllers/Update.php @@ -390,65 +390,16 @@ class Update extends CI_Controller { } public function update_tle() { - $mtime = microtime(); - $mtime = explode(" ",$mtime); - $mtime = $mtime[1] + $mtime[0]; - $starttime = $mtime; - - $url = 'https://www.amsat.org/tle/dailytle.txt'; - $curl = curl_init($url); - - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); - - $response = curl_exec($curl); - - $count = 0; - - if ($response === false) { - echo 'Error: ' . curl_error($curl); - } else { - $this->db->empty_table("tle"); - // Split the response into an array of lines - $lines = explode("\n", $response); - - $satname = ''; - $tleline1 = ''; - $tleline2 = ''; - // Process each line - for ($i = 0; $i < count($lines); $i += 3) { - $count++; - // Check if there are at least three lines remaining - if (isset($lines[$i], $lines[$i + 1], $lines[$i + 2])) { - // Get the three lines - $satname = $lines[$i]; - $tleline1 = $lines[$i + 1]; - $tleline2 = $lines[$i + 2]; - $sql = "INSERT INTO tle (satelliteid, tle) select id, ? from satellite where name = ? or displayname = ?"; - $this->db->query($sql,array($tleline1."\n".$tleline2,$satname,$satname)); - } - } - } - - curl_close($curl); - - $mtime = microtime(); - $mtime = explode(" ",$mtime); - $mtime = $mtime[1] + $mtime[0]; - $endtime = $mtime; - $totaltime = ($endtime - $starttime); - echo "This page was created in ".$totaltime." seconds
"; - echo "Records inserted: " . $count . "
"; - $datetime = new DateTime("now", new DateTimeZone('UTC')); - $datetime = $datetime->format('Ymd h:i'); - $this->optionslib->update('tle_update', $datetime , 'no'); + $this->load->model('Update_model'); + $result = $this->Update_model->tle(); + echo $result; } function version_check() { // set the last run in cron table for the correct cron id $this->load->model('cron_model'); $this->cron_model->set_last_run($this->router->class . '_' . $this->router->method); - + $this->load->model('Update_model'); $this->Update_model->update_check(); } diff --git a/application/migrations/230_tle_cron.php b/application/migrations/230_tle_cron.php new file mode 100644 index 000000000..e41dfa121 --- /dev/null +++ b/application/migrations/230_tle_cron.php @@ -0,0 +1,33 @@ +chk4cron('update_update_tle') == 0) { + $data = array( + array( + 'id' => 'update_update_tle', + 'enabled' => '0', + 'status' => 'disabled', + 'description' => 'Update TLE for satellites', + 'function' => 'index.php/update/update_tle', + 'expression' => '45 4 * * *', + 'last_run' => null, + 'next_run' => null + )); + $this->db->insert_batch('cron', $data); + } + } + + public function down() { + if ($this->chk4cron('update_tle') > 0) { + $this->db->query("delete from cron where id='update_update_tle'"); + } + } + + function chk4cron($cronkey) { + $query = $this->db->query("select count(id) as cid from cron where id=?",$cronkey); + $row = $query->row(); + return $row->cid ?? 0; + } +} diff --git a/application/models/Update_model.php b/application/models/Update_model.php index 7b432932b..9763ff65c 100644 --- a/application/models/Update_model.php +++ b/application/models/Update_model.php @@ -306,4 +306,60 @@ class Update_model extends CI_Model { } } } + + function tle() { + // set the last run in cron table for the correct cron id + $this->load->model('cron_model'); + $this->cron_model->set_last_run($this->router->class . '_' . $this->router->method); + $mtime = microtime(); + $mtime = explode(" ",$mtime); + $mtime = $mtime[1] + $mtime[0]; + $starttime = $mtime; + + $url = 'https://www.amsat.org/tle/dailytle.txt'; + $curl = curl_init($url); + + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); + + $response = curl_exec($curl); + + $count = 0; + + if ($response === false) { + echo 'Error: ' . curl_error($curl); + } else { + $this->db->empty_table("tle"); + // Split the response into an array of lines + $lines = explode("\n", $response); + + $satname = ''; + $tleline1 = ''; + $tleline2 = ''; + // Process each line + for ($i = 0; $i < count($lines); $i += 3) { + $count++; + // Check if there are at least three lines remaining + if (isset($lines[$i], $lines[$i + 1], $lines[$i + 2])) { + // Get the three lines + $satname = $lines[$i]; + $tleline1 = $lines[$i + 1]; + $tleline2 = $lines[$i + 2]; + $sql = "INSERT INTO tle (satelliteid, tle) select id, ? from satellite where name = ? or displayname = ?"; + $this->db->query($sql,array($tleline1."\n".$tleline2,$satname,$satname)); + } + } + } + + curl_close($curl); + + $mtime = microtime(); + $mtime = explode(" ",$mtime); + $mtime = $mtime[1] + $mtime[0]; + $endtime = $mtime; + $totaltime = ($endtime - $starttime); + echo "This page was created in ".$totaltime." seconds
"; + echo "Records inserted: " . $count . "
"; + } + } diff --git a/application/views/debug/index.php b/application/views/debug/index.php index 6a318b590..3f3d17018 100644 --- a/application/views/debug/index.php +++ b/application/views/debug/index.php @@ -549,6 +549,11 @@ last_run ?? __("never"); ?> + + + + last_run ?? __("never"); ?> +
From d5f238f6d4a127fc803a89c5617535186388b7ac Mon Sep 17 00:00:00 2001 From: Andreas Kristiansen <6977712+AndreasK79@users.noreply.github.com> Date: Sat, 7 Dec 2024 21:36:45 +0100 Subject: [PATCH 06/10] Fix full_group_by --- application/models/Satellite_model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/models/Satellite_model.php b/application/models/Satellite_model.php index 57defaaef..7e2940d51 100644 --- a/application/models/Satellite_model.php +++ b/application/models/Satellite_model.php @@ -7,7 +7,7 @@ class Satellite_model extends CI_Model { from satellite left outer join satellitemode on satellite.id = satellitemode.satelliteid left outer join tle on satellite.id = tle.satelliteid - group by satellite.name, satellite.displayname, satellite.orbit, satellite.id"; + group by satellite.name, satellite.displayname, satellite.orbit, satellite.id, tle.updated"; return $this->db->query($sql)->result(); } From daf9ebb09aefc76ece59d8011ca32c78c0d33e64 Mon Sep 17 00:00:00 2001 From: Andreas Kristiansen <6977712+AndreasK79@users.noreply.github.com> Date: Sun, 8 Dec 2024 08:24:33 +0100 Subject: [PATCH 07/10] Update TLE if exists --- application/migrations/230_tle_cron.php | 11 +++++++++++ application/models/Update_model.php | 12 +++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/application/migrations/230_tle_cron.php b/application/migrations/230_tle_cron.php index e41dfa121..84e1c683d 100644 --- a/application/migrations/230_tle_cron.php +++ b/application/migrations/230_tle_cron.php @@ -17,12 +17,15 @@ class Migration_tle_cron extends CI_Migration { )); $this->db->insert_batch('cron', $data); } + + $this->dbtry("ALTER TABLE tle ADD CONSTRAINT tle_unique_satelliteid_FK FOREIGN KEY (satelliteid) REFERENCES satellite (id) ON DELETE CASCADE ON UPDATE RESTRICT;"); } public function down() { if ($this->chk4cron('update_tle') > 0) { $this->db->query("delete from cron where id='update_update_tle'"); } + $this->dbtry("alter table tle drop foreign key tle_unique_satelliteid_FK;"); } function chk4cron($cronkey) { @@ -30,4 +33,12 @@ class Migration_tle_cron extends CI_Migration { $row = $query->row(); return $row->cid ?? 0; } + + function dbtry($what) { + try { + $this->db->query($what); + } catch (Exception $e) { + log_message("error", "Something gone wrong while altering FKs: ".$e." // Executing: ".$this->db->last_query()); + } + } } diff --git a/application/models/Update_model.php b/application/models/Update_model.php index 9763ff65c..ad24d3335 100644 --- a/application/models/Update_model.php +++ b/application/models/Update_model.php @@ -329,7 +329,6 @@ class Update_model extends CI_Model { if ($response === false) { echo 'Error: ' . curl_error($curl); } else { - $this->db->empty_table("tle"); // Split the response into an array of lines $lines = explode("\n", $response); @@ -345,8 +344,15 @@ class Update_model extends CI_Model { $satname = $lines[$i]; $tleline1 = $lines[$i + 1]; $tleline2 = $lines[$i + 2]; - $sql = "INSERT INTO tle (satelliteid, tle) select id, ? from satellite where name = ? or displayname = ?"; - $this->db->query($sql,array($tleline1."\n".$tleline2,$satname,$satname)); + $sql = " + INSERT INTO tle (satelliteid, tle) + SELECT id, ? + FROM satellite + WHERE name = ? OR displayname = ? + ON DUPLICATE KEY UPDATE + tle = VALUES(tle), updated = now() + "; + $this->db->query($sql, array($tleline1 . "\n" . $tleline2, $satname, $satname)); } } } From 8608e8d85f23acc719bd1626b164c1ba16e94ed3 Mon Sep 17 00:00:00 2001 From: Andreas Kristiansen <6977712+AndreasK79@users.noreply.github.com> Date: Sun, 8 Dec 2024 08:46:49 +0100 Subject: [PATCH 08/10] Using only unique index --- application/migrations/230_tle_cron.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/application/migrations/230_tle_cron.php b/application/migrations/230_tle_cron.php index 84e1c683d..a68e702f8 100644 --- a/application/migrations/230_tle_cron.php +++ b/application/migrations/230_tle_cron.php @@ -18,14 +18,15 @@ class Migration_tle_cron extends CI_Migration { $this->db->insert_batch('cron', $data); } - $this->dbtry("ALTER TABLE tle ADD CONSTRAINT tle_unique_satelliteid_FK FOREIGN KEY (satelliteid) REFERENCES satellite (id) ON DELETE CASCADE ON UPDATE RESTRICT;"); + $this->dbtry("ALTER TABLE tle ADD CONSTRAINT tle_unique_satelliteid UNIQUE (satelliteid);"); + } public function down() { if ($this->chk4cron('update_tle') > 0) { $this->db->query("delete from cron where id='update_update_tle'"); } - $this->dbtry("alter table tle drop foreign key tle_unique_satelliteid_FK;"); + $this->dbtry("ALTER TABLE tle DROP INDEX tle_unique_satelliteid;"); } function chk4cron($cronkey) { From 49ad84d1f11734c3eb81f20d073e4c2d557224f8 Mon Sep 17 00:00:00 2001 From: int2001 Date: Mon, 9 Dec 2024 07:10:57 +0000 Subject: [PATCH 09/10] Recreate tle-table with FK --- application/migrations/230_tle_cron.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/application/migrations/230_tle_cron.php b/application/migrations/230_tle_cron.php index a68e702f8..171f72ccd 100644 --- a/application/migrations/230_tle_cron.php +++ b/application/migrations/230_tle_cron.php @@ -3,6 +3,11 @@ defined('BASEPATH') or exit('No direct script access allowed'); class Migration_tle_cron extends CI_Migration { public function up() { + $this->db->query("DROP TABLE tle"); + $this->db->query("CREATE TABLE `tle` (`id` int(6) unsigned NOT NULL AUTO_INCREMENT, `satelliteid` int(6) unsigned NOT NULL, + `tle` text DEFAULT NULL, `updated` timestamp NOT NULL DEFAULT current_timestamp(), + PRIMARY KEY (`satelliteid`), UNIQUE KEY `tle_unique_id` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); + $this->db->query("ALTER TABLE tle ADD CONSTRAINT tle_satellite_FK FOREIGN KEY (satelliteid) REFERENCES satellite(id) ON DELETE CASCADE"); if ($this->chk4cron('update_update_tle') == 0) { $data = array( array( @@ -18,15 +23,13 @@ class Migration_tle_cron extends CI_Migration { $this->db->insert_batch('cron', $data); } - $this->dbtry("ALTER TABLE tle ADD CONSTRAINT tle_unique_satelliteid UNIQUE (satelliteid);"); - } public function down() { if ($this->chk4cron('update_tle') > 0) { $this->db->query("delete from cron where id='update_update_tle'"); } - $this->dbtry("ALTER TABLE tle DROP INDEX tle_unique_satelliteid;"); + // No way back to tle-table } function chk4cron($cronkey) { From 4e649ec5da06411bf8c2977696f3f1cd8d48c765 Mon Sep 17 00:00:00 2001 From: int2001 Date: Mon, 9 Dec 2024 08:27:27 +0000 Subject: [PATCH 10/10] Make mig more failsafe --- application/migrations/230_tle_cron.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/application/migrations/230_tle_cron.php b/application/migrations/230_tle_cron.php index 171f72ccd..94d1e7b6d 100644 --- a/application/migrations/230_tle_cron.php +++ b/application/migrations/230_tle_cron.php @@ -3,11 +3,13 @@ defined('BASEPATH') or exit('No direct script access allowed'); class Migration_tle_cron extends CI_Migration { public function up() { - $this->db->query("DROP TABLE tle"); + if ($this->db->table_exists('tle')) { + $this->db->query("DROP TABLE tle"); + } $this->db->query("CREATE TABLE `tle` (`id` int(6) unsigned NOT NULL AUTO_INCREMENT, `satelliteid` int(6) unsigned NOT NULL, `tle` text DEFAULT NULL, `updated` timestamp NOT NULL DEFAULT current_timestamp(), PRIMARY KEY (`satelliteid`), UNIQUE KEY `tle_unique_id` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); - $this->db->query("ALTER TABLE tle ADD CONSTRAINT tle_satellite_FK FOREIGN KEY (satelliteid) REFERENCES satellite(id) ON DELETE CASCADE"); + $this->dbtry("ALTER TABLE tle ADD CONSTRAINT tle_satellite_FK FOREIGN KEY (satelliteid) REFERENCES satellite(id) ON DELETE CASCADE"); if ($this->chk4cron('update_update_tle') == 0) { $data = array( array(