Skip to content

Commit

Permalink
finished year filter for kitchen outcome network averages
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesjin123 committed Jan 9, 2025
1 parent f5fc00c commit 5d7c7fe
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 32 deletions.
30 changes: 26 additions & 4 deletions client/src/Visualizations/KitchenOutcomeViz.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Tab,
MenuItem,
Popper,
Slider,
} from '@mui/material';
import { Doughnut } from 'react-chartjs-2';
import { Chart as ChartJS, ArcElement, Tooltip, Legend } from 'chart.js';
Expand Down Expand Up @@ -137,6 +138,7 @@ function KitchenOutcomesVisualization() {
name: string;
id: string;
};
const currentYear = new Date().getFullYear();
const [orgList, setOrgList] = useState<OrgVal[] | null>(null);
const [yearList, setYearList] = useState<number[]>([]);
const [searchTerm, setSearchTerm] = useState('');
Expand All @@ -145,6 +147,7 @@ function KitchenOutcomesVisualization() {
const [year, setYear] = useState<number | ''>('');
const [mealType, setMealType] = useState<string>('All');
const [mealRange, setMealRange] = useState<string>('All');
const [yearRange, setYearRange] = useState<number[]>([2017, currentYear]); // Default range

const tabNames = [
'Hunger Relief',
Expand Down Expand Up @@ -182,6 +185,11 @@ function KitchenOutcomesVisualization() {
setMealRange(event.target.value);
};

const handleYearRangeChange = (event: Event, newValue: number | number[]) => {
setYearRange(newValue as number[]);
// Trigger network average calculation with new filter
};

useEffect(() => {
const fetchOrgList = async () => {
try {
Expand Down Expand Up @@ -764,6 +772,7 @@ function KitchenOutcomesVisualization() {

const fetchAllNetworkAverages = async (
selectedYear: number,
yearRangeFilter: number[],
mealTypeFilter: string,
mealRangeFilter: string,
) => {
Expand Down Expand Up @@ -792,8 +801,9 @@ function KitchenOutcomesVisualization() {
console.log(
`trying to get network avg route with ${field} ${selectedYear} ${mealTypeFilter} ${mealRangeFilter}`,
);
const [startYear, endYear] = yearRangeFilter;
const response = await getData(
`kitchen_outcomes/network-average/${field}/${selectedYear}/${mealTypeFilter}/${mealRangeFilter}`,
`kitchen_outcomes/network-average/${field}/${startYear}/${endYear}/${mealTypeFilter}/${mealRangeFilter}`,
);
averages[field] = response.data.average;
} catch (error) {
Expand All @@ -805,8 +815,9 @@ function KitchenOutcomesVisualization() {

try {
console.log('trying to call route with year: ', selectedYear);
const [startYear, endYear] = yearRangeFilter;
const response2 = await getData(
`kitchen_outcomes/distri/${selectedYear}/${mealTypeFilter}/${mealRangeFilter}`,
`kitchen_outcomes/distri/${startYear}/${endYear}/${mealTypeFilter}/${mealRangeFilter}`,
);
console.log('response data: ', response2.data);
const ageRaceData = response2.data;
Expand All @@ -825,9 +836,9 @@ function KitchenOutcomesVisualization() {

useEffect(() => {
if (year) {
fetchAllNetworkAverages(Number(year), mealType, mealRange);
fetchAllNetworkAverages(Number(year), yearRange, mealType, mealRange);
}
}, [year, mealType, mealRange]);
}, [year, yearRange, mealType, mealRange]);

return (
<Container maxWidth="lg">
Expand Down Expand Up @@ -919,6 +930,17 @@ function KitchenOutcomesVisualization() {
))}
</TextField>
</Grid>
<Grid item xs={12}>
<Typography gutterBottom>Year Range</Typography>
<Slider
value={yearRange}
onChange={handleYearRangeChange}
valueLabelDisplay="auto"
min={2017}
max={currentYear}
sx={{ color: 'black' }}
/>
</Grid>
</Grid>

{/* Tabs - now left justified */}
Expand Down
65 changes: 48 additions & 17 deletions server/src/controllers/kitchen.outcomes.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,54 @@ const distriController = async (
res: express.Response,
next: express.NextFunction,
) => {
const { year, mealType, mealRange } = req.params;
const { startYear, endYear, mealType, mealRange } = req.params;

if (!year) {
next(ApiError.missingFields(['year']));
if (!startYear) {
next(ApiError.missingFields(['startYear']));
return;
}

if (!endYear) {
next(ApiError.missingFields(['endYear']));
return;
}

try {
const yearNum = parseInt(year, 10);
if (isNaN(yearNum)) {
const startYearNum = parseInt(startYear, 10);
if (isNaN(startYearNum)) {
next(ApiError.badRequest('Invalid year format'));
return;
}

const endYearNum = parseInt(endYear, 10);
if (isNaN(endYearNum)) {
next(ApiError.badRequest('Invalid year format'));
return;
}

console.log(
'calculating age and race distribution controller for year:',
year,
'calculating age and race distribution controller for startYear:',
startYear,
'endYear:',
endYear,
);

const ageDistribution = await calculateAgeAndRaceDistributions(
yearNum,
startYearNum,
endYearNum,
mealType,
mealRange,
);

res.status(StatusCode.OK).json({
year: yearNum,
startYear: startYearNum,
endYear: endYearNum,
ageDistribution,
});
} catch (error) {
next(
ApiError.internal(
`Unable to calculate age distribution for year ${year}`,
`Unable to calculate age distribution for year ${startYear} to ${endYear}`,
),
);
}
Expand All @@ -65,30 +80,46 @@ const getNetworkAverageController = async (
res: express.Response,
next: express.NextFunction,
) => {
const { field, year, mealType, mealRange } = req.params;
const { field, startYear, endYear, mealType, mealRange } = req.params;

if (!field || !year || !mealType || !mealRange) {
next(ApiError.missingFields(['field', 'year', 'mealType', 'mealRange']));
if (!field || !startYear || !endYear || !mealType || !mealRange) {
next(
ApiError.missingFields([
'field',
'startYear',
'endYear',
'mealType',
'mealRange',
]),
);
return;
}

try {
const yearNum = parseInt(year, 10);
if (isNaN(yearNum)) {
const startYearNum = parseInt(startYear, 10);
if (isNaN(startYearNum)) {
next(ApiError.badRequest('Invalid year format'));
return;
}

const endYearNum = parseInt(endYear, 10);
if (isNaN(endYearNum)) {
next(ApiError.badRequest('Invalid year format'));
return;
}

const average = await getNetworkAverage(
field,
yearNum,
startYearNum,
endYearNum,
mealType,
mealRange,
);

res.status(StatusCode.OK).json({
field,
year: yearNum,
startYear: startYearNum,
endYear: endYearNum,
average: average ?? null,
});
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions server/src/routes/kitchen.outcomes.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
const router = express.Router();

router.get(
'/distri/:year/:mealType/:mealRange',
'/distri/:startYear/:endYear/:mealType/:mealRange',
isAuthenticated,
distriController,
);
Expand All @@ -43,7 +43,7 @@ router.delete('/delete/:id', isAdmin, deleteKitchenOutcomeByIdController);
router.post('/add/', isAuthenticated, addKitchenOutcomesController);

router.get(
'/network-average/:field/:year/:mealType/:mealRange',
'/network-average/:field/:startYear/:endYear/:mealType/:mealRange',
isAuthenticated,
getNetworkAverageController,
);
Expand Down
23 changes: 14 additions & 9 deletions server/src/services/kitchen.outcomes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import {
} from '../models/kitchen.outcomes.model.ts';

const calculateAgeAndRaceDistributions = async (
year: number,
startYear: number,
endYear: number,
mealType: string,
mealRange: string,
) => {
try {
console.log(
'calculating age and race distribution service for year:',
year,
'calculating age and race distribution service for start year:',
startYear,
'end year:',
endYear,
);
const startDate = new Date(Date.UTC(year, 0, 1));
const endDate = new Date(Date.UTC(year + 1, 0, 1));
const startDate = new Date(Date.UTC(startYear, 0, 1));
const endDate = new Date(Date.UTC(endYear + 1, 0, 1));

const matchConditions: any = {
year: { $gte: startDate, $lt: endDate },
Expand Down Expand Up @@ -146,17 +149,19 @@ export { calculateAgeAndRaceDistributions };

const getNetworkAverage = async (
field: string,
year: number,
startYear: number,
endYear: number,
mealType: string,
mealRange: string,
): Promise<number | null> => {
const startDate = new Date(Date.UTC(year, 0, 1));
const endDate = new Date(Date.UTC(year + 1, 0, 1));
const startDate = new Date(Date.UTC(startYear, 0, 1));
const endDate = new Date(Date.UTC(endYear + 1, 0, 1));

try {
console.log('Service - Getting network average for:', {
field,
year,
startYear,
endYear,
startDate,
endDate,
mealType,
Expand Down

0 comments on commit 5d7c7fe

Please sign in to comment.