Skip to content

Commit

Permalink
Merge pull request #439 from Open-Earth-Foundation/fix/show-populatio…
Browse files Browse the repository at this point in the history
…n-values

Fix/show population values
  • Loading branch information
cephaschapa authored Apr 15, 2024
2 parents 79c10ff + 952549c commit 8c9f6b6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/src/app/[lng]/[inventory]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export default function Home({ params: { lng } }: { params: { lng: string } }) {
{ cityId: inventory?.cityId!, year: inventory?.year! },
{ skip: !inventory?.cityId || !inventory?.year },
);

let totalProgress = 0,
thirdPartyProgress = 0,
uploadedProgress = 0;
Expand Down
17 changes: 13 additions & 4 deletions app/src/app/api/v0/city/[city]/population/[year]/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import UserService from "@/backend/UserService";
import { db } from "@/models";
import { apiHandler } from "@/util/api";
import { PopulationEntry, findClosestYearToInventory } from "@/util/helpers";
import { NextResponse } from "next/server";

export const GET = apiHandler(async (_req: Request, { session, params }) => {
const city = await UserService.findUserCity(params.city, session);
const population = await db.models.Population.findOne({
where: { cityId: city?.cityId, year: params.year },
const population = await db.models.Population.findAll({
where: { cityId: city?.cityId },
});
return NextResponse.json({ data: population });
});
const populations: PopulationEntry[] = population.map(({population, year})=>{
return {
year,
population: population!
}
})

const closestYearPopulationData = findClosestYearToInventory(populations, parseInt(params.year))
return NextResponse.json({ data: closestYearPopulationData });
});
35 changes: 35 additions & 0 deletions app/src/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,38 @@ export function findClosestYear(
null as PopulationEntry | null,
);
}

export function findClosestYearToInventory(
populationData: PopulationEntry[] | undefined,
year: number,
maxYearDifference: number = 10
): PopulationEntry | null {
if (!populationData || populationData.length === 0) {
return null;
}

let closestEntry = null;
let closestDistance = Infinity; // Initialize with a large number

populationData.forEach(entry => {
// Ensure the entry has a valid population value
if (entry.population !== null && entry.population !== undefined) {
const currentDistance = Math.abs(entry.year - year);
// Update closestEntry if the current entry is closer than the previously stored one
if (currentDistance < closestDistance) {
closestEntry = entry;
closestDistance = currentDistance;
}
}
});

// After identifying the closest entry, check if it's within the allowable range
if (closestEntry && closestDistance <= maxYearDifference) {
return closestEntry;
} else if (closestEntry) {
// If no entry is within the maxYearDifference, return the closest available entry
return closestEntry;
}

return null; // In case all entries are outside the maxYearDifference and no closest entry was found
}

0 comments on commit 8c9f6b6

Please sign in to comment.