From 0d9dbb4cd238b4b0e4406ab5eb26e1149eb951cc Mon Sep 17 00:00:00 2001 From: Florian Graule Date: Tue, 16 Jul 2024 11:21:59 +0200 Subject: [PATCH] Clean code of orbitalDataGraph, restore strapi files, adding cronTask to the 3rd of each month. --- backend/config/functions/cronTask.js | 14 ++++- backend/config/server.js | 2 +- .../api/satellite/controllers/satellite.js | 15 +---- .../src/api/satellite/services/satellite.js | 63 +------------------ .../[satelliteSlug]/orbitDataGraph.tsx | 7 +-- .../app/satellites/[satelliteSlug]/page.tsx | 2 +- package.json | 1 + 7 files changed, 20 insertions(+), 84 deletions(-) diff --git a/backend/config/functions/cronTask.js b/backend/config/functions/cronTask.js index b39c59e..da4c945 100644 --- a/backend/config/functions/cronTask.js +++ b/backend/config/functions/cronTask.js @@ -5,6 +5,8 @@ */ 'use strict'; +const { fetchOrbitalData } = require('./satelliteUtils'); + module.exports = { updateAllSatellitesData: { task: async ({ strapi }) => { @@ -15,13 +17,21 @@ module.exports = { // Waiting for all promises to be resolved await Promise.all( satellites.map(async satellite => { - await strapi.service('api::satellite.satellite').fetchOrbitalData(satellite.id); + try { + setTimeout(async () => { + await fetchOrbitalData(strapi, satellite.id); + }, 10000); + } catch (error) { + console.error(error); + } }) ); } catch (error) { console.error(error); } }, - options: new Date(Date.now() + 10000), + options: { + rule: "0 0 0 3 * *", // Every month on the 3rd at midnight + }, }, }; diff --git a/backend/config/server.js b/backend/config/server.js index 2133f76..796c641 100644 --- a/backend/config/server.js +++ b/backend/config/server.js @@ -11,7 +11,7 @@ module.exports = ({ env }) => ({ }, cron: { // Enable or disable the cron tasks - enabled: env.bool("CRON_ENABLED", false), + enabled: env.bool("CRON_ENABLED", true), tasks: cronTask, }, }); diff --git a/backend/src/api/satellite/controllers/satellite.js b/backend/src/api/satellite/controllers/satellite.js index 58fba9d..0f4851d 100644 --- a/backend/src/api/satellite/controllers/satellite.js +++ b/backend/src/api/satellite/controllers/satellite.js @@ -6,17 +6,4 @@ const { createCoreController } = require('@strapi/strapi').factories; -module.exports = createCoreController('api::satellite.satellite', ({ strapi }) => ({ - async findOne(ctx) { - // Fetching data from Space-Track - const entity = await strapi.entityService.findOne('api::satellite.satellite', ctx.params.id); - const updatedSatellite = await strapi.service('api::satellite.satellite').fetchOrbitalData(ctx.params.id); - - if (!entity.historicalOrbitalData) { - return updatedSatellite; - } - - return entity; - } -}) -); +module.exports = createCoreController('api::satellite.satellite'); \ No newline at end of file diff --git a/backend/src/api/satellite/services/satellite.js b/backend/src/api/satellite/services/satellite.js index 87a7669..409d67a 100644 --- a/backend/src/api/satellite/services/satellite.js +++ b/backend/src/api/satellite/services/satellite.js @@ -7,67 +7,6 @@ const { createCoreService } = require('@strapi/strapi').factories; -const axios = require('axios'); - -// Function to fetch data from Space-Track such as Eccentricy, SMA, Inclination -async function fetchOrbitalData(contextId) { - try { - // Fetching the satellite - const satellite = await strapi.entityService.findOne('api::satellite.satellite', contextId); - const noradId = satellite.catalogNumberNORAD; - - // Authentification to Space-Track - const authResponse = await axios.post('https://www.space-track.org/ajaxauth/login', { - identity: 'grauleflorian@gmail.com', - password : 'Vm5JxTtD3-hYBdq' - }); - - 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`, { - headers: { - Cookie: authResponse.headers['set-cookie'] - } - }); - - if (satelliteResponse.status === 200) { - // Collecting data - const satelliteData = satelliteResponse.data; - // Parsing correctly the data with wanted data : Inclination, Eccentricity, SMA, and Epoch - const historicalOrbitalData = satelliteData.map(data => { - return { - epoch: data.EPOCH, - inclination: data.INCLINATION, - eccentricity: data.ECCENTRICITY, - semiMajorAxis: data.SEMIMAJOR_AXIS - } - }); - - if (satellite) { - // 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('Satellite not found while updating orbit data'); - } - - - } else { - throw new Error('Error while fetching data from Space-Track'); - } - } else { - throw new Error('Authentication failed'); - } - } catch (error) { - console.error('Error while fetching data to Space-Track: ', error); - } -} - module.exports = { - ...createCoreService('api::satellite.satellite'), - fetchOrbitalData, + ...createCoreService('api::satellite.satellite') }; diff --git a/frontend/src/app/satellites/[satelliteSlug]/orbitDataGraph.tsx b/frontend/src/app/satellites/[satelliteSlug]/orbitDataGraph.tsx index bf83305..a1fc501 100644 --- a/frontend/src/app/satellites/[satelliteSlug]/orbitDataGraph.tsx +++ b/frontend/src/app/satellites/[satelliteSlug]/orbitDataGraph.tsx @@ -7,7 +7,6 @@ import { LaunchDateCountDownProps } from './launchDateCountDown'; import ScrollBarThumb, { ScrollBarThumbProps } from './_orbitDataGraphComponents/ScrollBarThumb'; type OrbitDataProps = { - satNum : SatelliteNumber; launchDateString: LaunchDateCountDownProps['launchDate']; orbitalData: any; } @@ -19,7 +18,7 @@ type ChartData = { semiMajorAxis: number; } -const OrbitDataGraph : React.FC = ({ satNum, launchDateString, orbitalData }) => { +const OrbitDataGraph : React.FC = ({ launchDateString, orbitalData }) => { // href for the svg component, tracking the size of the container const svgContainer = useRef(null); @@ -129,7 +128,7 @@ const OrbitDataGraph : React.FC = ({ satNum, launchDateString, o return ( <> -
+ {orbitalData &&

Zoom :

{/* Scrollbar thumb represents the zoom period selected, in case it fits bad we don't display @@ -193,7 +192,7 @@ const OrbitDataGraph : React.FC = ({ satNum, launchDateString, o
-
+
} ) } diff --git a/frontend/src/app/satellites/[satelliteSlug]/page.tsx b/frontend/src/app/satellites/[satelliteSlug]/page.tsx index 8a6e10a..7b47630 100644 --- a/frontend/src/app/satellites/[satelliteSlug]/page.tsx +++ b/frontend/src/app/satellites/[satelliteSlug]/page.tsx @@ -160,7 +160,7 @@ export default async function SatelliteInfoPage({ {/*Pass the satNum and the launchDate as props to OrbitDataGraph*/} { noradId? ( satAttributes?.launchDate ? ( - + ) : null ) : null} diff --git a/package.json b/package.json index 4f7d6e4..ec92778 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "dependencies": { "@types/recharts": "^1.8.29", "concurrently": "^8.2.2", + "node-schedule": "^2.1.1", "openapi-typescript": "^6.7.4", "recharts": "^2.12.7", "three-glow-mesh": "^0.1.2"