Skip to content

Commit

Permalink
Adds filtering for active stations
Browse files Browse the repository at this point in the history
  • Loading branch information
eireland committed Jan 25, 2024
1 parent 677fe66 commit be803ff
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
9 changes: 5 additions & 4 deletions src/components/location-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import "./location-picker.scss";

export const LocationPicker = () => {
const {state, setState} = useStateContext();
const {units, location, weatherStation, weatherStationDistance} = state;
const {units, location, weatherStation, weatherStationDistance, startDate, endDate} = state;
const [showMapButton, setShowMapButton] = useState(false);
const [isEditing, setIsEditing] = useState(false);
const [locationPossibilities, setLocationPossibilities] = useState<IPlace[]>([]);
Expand Down Expand Up @@ -56,7 +56,6 @@ export const LocationPicker = () => {
useEffect(() => {
if (locationInputEl.current?.value === "") {
setShowSelectionList(false);
// setSelectedLocation(undefined);
}
}, [locationInputEl.current?.value]);

Expand All @@ -67,8 +66,10 @@ export const LocationPicker = () => {
}, [isEditing]);

useEffect(() => {
const _startDate = startDate ? startDate : new Date( -5364662060); // 1/1/1750
const _endDate = endDate ? endDate : new Date(Date.now());
if (location) {
findNearestActiveStations(location.latitude, location.longitude, 80926000, "present")
findNearestActiveStations(location.latitude, location.longitude, _startDate, _endDate)
.then((stationList: IStation[]) => {
if (stationList) {
setStationPossibilities(stationList);
Expand All @@ -81,7 +82,7 @@ export const LocationPicker = () => {
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
},[location]);
},[endDate, isEditing, location, startDate]);

useEffect(() => {
if (showStationSelectionList) {
Expand Down
38 changes: 28 additions & 10 deletions src/utils/getWeatherStations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import weatherStations from "../assets/data/weather-stations.json";
*/
export const adjustStationDataset = (dataset: IWeatherStation[]) => {
const datasetArr = Array.from(dataset);

let maxDate: dayjs.Dayjs | null = null;

if (dataset) {
Expand All @@ -32,26 +31,45 @@ export const adjustStationDataset = (dataset: IWeatherStation[]) => {
});
}
}
return dataset;
};

export const findNearestActiveStations = async(targetLat: number, targetLong: number, fromDate: number | string,
toDate: number | string) => {
// TODO: filter out weather stations that are active
// let nearestStation: IWeatherStation | null = null;
// let minDistance = Number.MAX_VALUE;
let nearestStations: IStation[] = [];
export const findNearestActiveStations = async(targetLat: number, targetLong: number, fromDate: Date,
toDate: Date) => {
const adjustedStationDataset = adjustStationDataset(weatherStations as IWeatherStation[]);
const fromSecs = fromDate.getTime() / 1000;
const toSecs = toDate.getTime() / 1000;
const nearestStations: IStation[] = [];
console.log(adjustedStationDataset);

Check warning on line 43 in src/utils/getWeatherStations.ts

View workflow job for this annotation

GitHub Actions / Build & Run Tests

Unexpected console statement

Check warning on line 43 in src/utils/getWeatherStations.ts

View workflow job for this annotation

GitHub Actions / S3 Deploy

Unexpected console statement

for (const station of weatherStations as IWeatherStation[]) {
const distance = calculateDistance(targetLat, targetLong, station.latitude, station.longitude);
const insertStation = (station: IWeatherStation, distance: number) => {
const newStation = {station, distance};

// Insert the new station into the sorted array at the correct position
const index = nearestStations.findIndex(s => s.distance > distance);
if (index === -1) {
nearestStations.push(newStation);
} else {
nearestStations.splice(index, 0, newStation);
}
};

for (const station of adjustedStationDataset) {
let shouldInsert = false;
if (station.maxdate === "present") { // If the station is still active (maxdate === "present") then we can use it
shouldInsert = true;
} else { // If the station is not active, we need to check if it has data in the date range
const stationMinSecs = new Date(station.mindate).getTime() / 1000;
const stationMaxSecs = new Date(station.maxdate).getTime() / 1000;
if (stationMinSecs <= toSecs && stationMaxSecs >= fromSecs) {
shouldInsert = true;
}
}

if (shouldInsert) {
const distance = calculateDistance(targetLat, targetLong, station.latitude, station.longitude);
const newStation = {station, distance};
insertStation(newStation.station, newStation.distance);
}
}

return nearestStations.slice(0, 5);
Expand Down

0 comments on commit be803ff

Please sign in to comment.