Skip to content

Commit

Permalink
Clean code of orbitalDataGraph, restore strapi files, adding cronTask…
Browse files Browse the repository at this point in the history
… to the 3rd of each month.
  • Loading branch information
Florian Graule committed Jul 16, 2024
1 parent 2dcdcf8 commit 0d9dbb4
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 84 deletions.
14 changes: 12 additions & 2 deletions backend/config/functions/cronTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
'use strict';

const { fetchOrbitalData } = require('./satelliteUtils');

module.exports = {
updateAllSatellitesData: {
task: async ({ strapi }) => {
Expand All @@ -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
},
},
};
2 changes: 1 addition & 1 deletion backend/config/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
15 changes: 1 addition & 14 deletions backend/src/api/satellite/controllers/satellite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
63 changes: 1 addition & 62 deletions backend/src/api/satellite/services/satellite.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { LaunchDateCountDownProps } from './launchDateCountDown';
import ScrollBarThumb, { ScrollBarThumbProps } from './_orbitDataGraphComponents/ScrollBarThumb';

type OrbitDataProps = {
satNum : SatelliteNumber;
launchDateString: LaunchDateCountDownProps['launchDate'];
orbitalData: any;
}
Expand All @@ -19,7 +18,7 @@ type ChartData = {
semiMajorAxis: number;
}

const OrbitDataGraph : React.FC<OrbitDataProps> = ({ satNum, launchDateString, orbitalData }) => {
const OrbitDataGraph : React.FC<OrbitDataProps> = ({ launchDateString, orbitalData }) => {

// href for the svg component, tracking the size of the container
const svgContainer = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -129,7 +128,7 @@ const OrbitDataGraph : React.FC<OrbitDataProps> = ({ satNum, launchDateString, o

return (
<>
<div ref={svgContainer} className="w-full flex flex-col">
{orbitalData && <div ref={svgContainer} className="w-full flex flex-col">
<div className="zoom-container flex items-center mb-5">
<h2 className="mx-5 text-grey-400 text-2xl">Zoom : </h2>
{/* Scrollbar thumb represents the zoom period selected, in case it fits bad we don't display
Expand Down Expand Up @@ -193,7 +192,7 @@ const OrbitDataGraph : React.FC<OrbitDataProps> = ({ satNum, launchDateString, o
</g>
</svg>
</div>
</div>
</div>}
</>
)
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/app/satellites/[satelliteSlug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export default async function SatelliteInfoPage({
{/*Pass the satNum and the launchDate as props to OrbitDataGraph*/}
{ noradId? (
satAttributes?.launchDate ? (
<OrbitDataGraph satNum={noradId} launchDateString={satAttributes?.launchDate} orbitalData={satAttributes?.historicalOrbitalData}/>
<OrbitDataGraph launchDateString={satAttributes?.launchDate} orbitalData={satAttributes?.historicalOrbitalData}/>
) : null

) : null}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 0d9dbb4

Please sign in to comment.