From 9d17fd26ea0a28e8d0812ecbe20506e2b1212caf Mon Sep 17 00:00:00 2001 From: Graulitard <127958528+Graulitard@users.noreply.github.com> Date: Mon, 12 Aug 2024 14:56:35 +0200 Subject: [PATCH] Fix cron jobs issue and update footer information (#420) * Fetching data from Space-Track, fixing some bugs, adding cron tasks for updating database with tool functions * Orbital chart done, using backend data without refetching from the backend server * Clean code of orbitalDataGraph, restore strapi files, adding cronTask to the 3rd of each month. * Adding package*.json files * ESLint fixed * Updating strapi env * Reseting frontend/package.json * Prettier checked * Updating for ESlint check * Adding backend env file * Revert "Adding backend env file" This reverts commit 88a46fb157ab99a06248e064b1c228fdde788dd0. * Try fixing cron job, error caused by Strapi reloading after updating db * Fix cron jobs issue and update Footer info --- backend/config/functions/cronTask.js | 38 ++++++++++++++-------- backend/config/functions/satelliteUtils.js | 23 +++---------- frontend/src/components/layout/Footer.tsx | 14 +++----- 3 files changed, 33 insertions(+), 42 deletions(-) diff --git a/backend/config/functions/cronTask.js b/backend/config/functions/cronTask.js index da4c945..55d1b98 100644 --- a/backend/config/functions/cronTask.js +++ b/backend/config/functions/cronTask.js @@ -11,23 +11,33 @@ module.exports = { updateAllSatellitesData: { task: async ({ strapi }) => { try { - // Fetching all satellites - const satellites = await strapi.entityService.findMany('api::satellite.satellite'); - - // Waiting for all promises to be resolved + // Fetch all satellites + const satellites = await strapi.entityService.findMany('api::satellite.satellite', { + fields: ['id', 'catalogNumberNORAD'], + filters: { + catalogNumberNORAD: { $ne: null }, + } + }); await Promise.all( - satellites.map(async satellite => { - try { - setTimeout(async () => { - await fetchOrbitalData(strapi, satellite.id); - }, 10000); - } catch (error) { - console.error(error); - } - }) - ); + satellites.map(async (satellite) => { + const fetchedData = await fetchOrbitalData(satellite.catalogNumberNORAD); + return { id: satellite.id, historicalOrbitalData: fetchedData }; + })).then(async (historicalOrbitalData) => { + await strapi.db.transaction(async (trx) => { + // Fetch data for each satellite + historicalOrbitalData.map(async (satellite) => { + // Update the database with the new data + const updatedSat = await strapi.entityService.update('api::satellite.satellite', satellite.id, { + data: { + historicalOrbitalData: satellite.historicalOrbitalData, + }, + }, { trx }); + }) + }); + }) } catch (error) { console.error(error); + return; } }, options: { diff --git a/backend/config/functions/satelliteUtils.js b/backend/config/functions/satelliteUtils.js index bc9abdc..c98c0b9 100644 --- a/backend/config/functions/satelliteUtils.js +++ b/backend/config/functions/satelliteUtils.js @@ -1,12 +1,8 @@ // backend/utils/satelliteUtils.js const axios = require('axios'); -async function fetchOrbitalData(strapi, contextId) { +async function fetchOrbitalData(noradId) { try { - // Fetching the satellite - const satellite = await strapi.entityService.findOne('api::satellite.satellite', contextId); - const noradId = satellite.catalogNumberNORAD; - // Authentication to Space-Track const authResponse = await axios.post('https://www.space-track.org/ajaxauth/login', { identity: 'floridg@stud.ntnu.no', @@ -15,7 +11,9 @@ async function fetchOrbitalData(strapi, contextId) { if (authResponse.status === 200) { // Fetching data from Space-Track - const satelliteResponse = await axios.get(`https://www.space-track.org/basicspacedata/query/class/gp_history/NORAD_CAT_ID/${noradId}/orderby/TLE_LINE1%20ASC/EPOCH/1950-07-02--2024-07-02/format/json`, { + + const today = new Date(); + const satelliteResponse = await axios.get(`https://www.space-track.org/basicspacedata/query/class/gp_history/NORAD_CAT_ID/${noradId}/orderby/TLE_LINE1%20ASC/orderby/TLE_LINE1%20ASC/format/json`, { headers: { Cookie: authResponse.headers['set-cookie'] } @@ -30,19 +28,8 @@ async function fetchOrbitalData(strapi, contextId) { eccentricity: data.ECCENTRICITY, semiMajorAxis: data.SEMIMAJOR_AXIS })); - - // Updating the satellite with the new data - const updatedSatellite = await strapi.entityService.update('api::satellite.satellite', contextId, { - data: { - historicalOrbitalData: historicalOrbitalData, - }, - }); - return updatedSatellite; - } else { - throw new Error('Error while fetching data from Space-Track'); + return historicalOrbitalData; } - } else { - throw new Error('Authentication failed'); } } catch (error) { console.error('Error while fetching data to Space-Track: ', error); diff --git a/frontend/src/components/layout/Footer.tsx b/frontend/src/components/layout/Footer.tsx index 05759fe..d91613b 100644 --- a/frontend/src/components/layout/Footer.tsx +++ b/frontend/src/components/layout/Footer.tsx @@ -18,28 +18,22 @@ export default function Footer() {
- Twitter - - Facebook Instagram - Youtube + LinkedIn