Skip to content

Commit

Permalink
Merge pull request #178 from jsilva74/feat/fse-api-hits-counter
Browse files Browse the repository at this point in the history
FSE API Hits Counter
  • Loading branch information
piero-la-lune authored Dec 9, 2023
2 parents 2085106 + 93c9e5a commit 48392ce
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"leaflet": "^1.9.4",
"leaflet-polylinedecorator": "^1.6.0",
"lodash": "^4.17.21",
"luxon": "^3.4.4",
"lz-string": "^1.5.0",
"maplibre-gl": "^2.4.0",
"match-sorter": "^6.3.1",
Expand Down
4 changes: 4 additions & 0 deletions src/MapLayers/CustomLayers/CustomLayerPopup.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import RadioGroup from '@mui/material/RadioGroup';
import { HexColorPicker, HexColorInput } from "react-colorful";

import Storage from '../../Storage.js';
import { apiHits } from "../../util/utility";
import CommunityPopup from './CommunityPopup.js';
import AreaPicker from './Components/AreaPicker.js';
import SizePicker from './Components/SizePicker.js';
Expand Down Expand Up @@ -303,6 +304,9 @@ function CustomLayerPopup(props) {
.catch(function(error) {
alert(error);
setLoading(false);
})
.finally(() => {
apiHits()
});
}

Expand Down
22 changes: 21 additions & 1 deletion src/Popups/Update.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import pointInPolygon from 'point-in-polygon';
import CustomAreaPopup from './Components/CustomArea.js';
import Storage from '../Storage.js';
import log from '../util/logger.js';
import { hideAirport, wrapNb } from "../util/utility.js";
import { apiHits, hideAirport, wrapNb } from "../util/utility.js";

import aircrafts from "../data/aircraft.json";

Expand Down Expand Up @@ -397,6 +397,8 @@ function UpdatePopup(props) {

const areas = React.useState(() => getAreas(props.icaodata, props.icaos))[0];

const [hits, setHits] = React.useState(0)

// Update custom area when map center is updated
React.useEffect(() => {
const wrapZone = latlngs => {
Expand Down Expand Up @@ -443,6 +445,8 @@ function UpdatePopup(props) {
const layers = l.filter(elm => jl.includes(elm.id));
setLayersOptions(l);
setJobsLayers(layers);
apiHits(false); // remove old entries from counter on init
setHits((storage.get('apiHits', [])).length);
}, [props.open, props.icaos, props.icaodata, props.settings.display.sim]);

// Update the number of request for loading jobs each time one input changes
Expand Down Expand Up @@ -501,6 +505,9 @@ function UpdatePopup(props) {
log.error("Error while updating Jobs", error);
updateAlert(error);
setLoading(false);
})
.finally(() => {
apiHits();
});
}
// Save jobs
Expand Down Expand Up @@ -592,6 +599,9 @@ function UpdatePopup(props) {
log.error("Error while updating Rentable Planes", error);
updateAlert(error);
setLoading(false);
})
.finally(() => {
apiHits();
});
}
const updateOwnedPlanesRequest = (usernames, planes, callback) => {
Expand Down Expand Up @@ -621,6 +631,9 @@ function UpdatePopup(props) {
log.error("Error while updating User Planes", error);
updateAlert(error);
setLoading(false);
})
.finally(() => {
apiHits();
});
}
// Planes Update button clicked
Expand Down Expand Up @@ -710,6 +723,9 @@ function UpdatePopup(props) {
log.error("Error while updating assignments", error);
updateAlert(error);
setLoading(false);
})
.finally(() => {
apiHits();
});
}

Expand Down Expand Up @@ -777,6 +793,10 @@ function UpdatePopup(props) {

<Alert severity="warning" sx={{ mb: 2 }}>You are limited to 40 requests every 6 hours (~1 request every 10 minutes).</Alert>

{!!hits && (
<Alert severity={hits > 34 ? 'error' : hits > 24 ? 'warning' : 'info'} sx={{ mb: 2 }}><span style={{fontWeight: 600}}>{hits}</span> request{hits !== 1 ? 's' : ''} in the past 6 hours</Alert>
)}

<Box>
<Accordion expanded={expanded === 'panel1'} onChange={panelChange('panel1')} data-tour="Step4">
<AccordionSummary expandIcon={<ExpandMoreIcon />}>
Expand Down
15 changes: 14 additions & 1 deletion src/util/utility.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {DateTime} from "luxon";
import icaodata from "../data/icaodata.json";
import aircrafts from "../data/aircraft.json";

import pointInPolygon from 'point-in-polygon';
import { getDistance, getRhumbLineBearing, convertDistance } from "geolib";
import Storage from "../Storage";

export function hideAirport(icao, s, sim) {
return (
Expand Down Expand Up @@ -266,7 +268,18 @@ export function toLatLngs(str) {
return null;
}

// Transform a latitute and longitude into a text GPS coordinates
// Transform a latitude and longitude into a text GPS coordinates
export function formatGPSCoord(lat, lng) {
return Math.abs(lat)+(lat >= 0 ? 'N' : 'S')+' '+Math.abs(lng)+(lng >= 0 ? 'E' : 'W');
}

// increments fse api hits counter
export function apiHits(increment = true){
const storage= new Storage();
const hits = storage.get('apiHits', [])?.filter(hit => DateTime.now().diff(DateTime.fromMillis(hit), 'hours').hours < 7);
if (increment) {
storage.set('apiHits', [...hits, DateTime.now().valueOf()]);
return;
}
storage.set('apiHits', [...hits]);
}

0 comments on commit 48392ce

Please sign in to comment.