feat: enhance dashboard API to generate continuous sample data and calculate cumulative values for project timelines
This commit is contained in:
@@ -65,20 +65,36 @@ export async function GET(request) {
|
||||
let chartData;
|
||||
let summary;
|
||||
if (completedProjects.length === 0) {
|
||||
chartData = [
|
||||
{ month: 'Jan 2024', value: 50000, cumulative: 50000 },
|
||||
{ month: 'Feb 2024', value: 75000, cumulative: 125000 },
|
||||
{ month: 'Mar 2024', value: 60000, cumulative: 185000 },
|
||||
{ month: 'Apr 2024', value: 80000, cumulative: 265000 },
|
||||
{ month: 'May 2024', value: 95000, cumulative: 360000 },
|
||||
{ month: 'Jun 2024', value: 70000, cumulative: 430000 },
|
||||
{ month: 'Jul 2024', value: 85000, cumulative: 515000 },
|
||||
{ month: 'Aug 2024', value: 90000, cumulative: 605000 },
|
||||
{ month: 'Sep 2024', value: 78000, cumulative: 683000 },
|
||||
{ month: 'Oct 2024', value: 92000, cumulative: 775000 },
|
||||
{ month: 'Nov 2024', value: 88000, cumulative: 863000 },
|
||||
{ month: 'Dec 2024', value: 95000, cumulative: 958000 }
|
||||
];
|
||||
// Generate continuous sample data from Jan 2024 to current month
|
||||
const currentDate = new Date();
|
||||
const startDate = new Date(2024, 0, 1); // Jan 2024
|
||||
chartData = [];
|
||||
let cumulative = 0;
|
||||
|
||||
let tempDate = new Date(startDate);
|
||||
while (tempDate <= currentDate) {
|
||||
const monthName = tempDate.toLocaleDateString('en-US', { year: 'numeric', month: 'short' });
|
||||
let monthlyValue = 0;
|
||||
|
||||
// Add some sample values for certain months
|
||||
if (tempDate.getMonth() === 0 && tempDate.getFullYear() === 2024) monthlyValue = 50000; // Jan 2024
|
||||
else if (tempDate.getMonth() === 1 && tempDate.getFullYear() === 2024) monthlyValue = 75000; // Feb 2024
|
||||
else if (tempDate.getMonth() === 2 && tempDate.getFullYear() === 2024) monthlyValue = 60000; // Mar 2024
|
||||
else if (tempDate.getMonth() === 7 && tempDate.getFullYear() === 2024) monthlyValue = 10841; // Aug 2024 (real data)
|
||||
else if (tempDate.getMonth() === 8 && tempDate.getFullYear() === 2024) monthlyValue = 18942; // Sep 2024
|
||||
else if (tempDate.getMonth() === 9 && tempDate.getFullYear() === 2024) monthlyValue = 13945; // Oct 2024
|
||||
else if (tempDate.getMonth() === 10 && tempDate.getFullYear() === 2024) monthlyValue = 12542; // Nov 2024
|
||||
|
||||
cumulative += monthlyValue;
|
||||
chartData.push({
|
||||
month: monthName,
|
||||
value: monthlyValue,
|
||||
cumulative: cumulative
|
||||
});
|
||||
|
||||
tempDate.setMonth(tempDate.getMonth() + 1);
|
||||
}
|
||||
|
||||
summary = {
|
||||
total: {
|
||||
realisedValue: 958000,
|
||||
@@ -123,24 +139,70 @@ export async function GET(request) {
|
||||
monthlyData[monthKey].value += projectValue;
|
||||
});
|
||||
|
||||
// Second pass: calculate cumulative values
|
||||
// Generate continuous timeline from earliest completion to current date
|
||||
let startDate = new Date();
|
||||
let endDate = new Date();
|
||||
|
||||
if (completedProjects.length > 0) {
|
||||
// Find earliest completion date
|
||||
const earliestCompletion = completedProjects.reduce((earliest, project) => {
|
||||
const projectDate = new Date(project.completion_date);
|
||||
return projectDate < earliest ? projectDate : earliest;
|
||||
}, new Date());
|
||||
|
||||
startDate = new Date(earliestCompletion.getFullYear(), earliestCompletion.getMonth(), 1);
|
||||
} else {
|
||||
// If no completed projects, start from 6 months ago
|
||||
startDate = new Date();
|
||||
startDate.setMonth(startDate.getMonth() - 6);
|
||||
startDate = new Date(startDate.getFullYear(), startDate.getMonth(), 1);
|
||||
}
|
||||
|
||||
// Generate all months from start to current
|
||||
const allMonths = {};
|
||||
let currentDate = new Date(startDate);
|
||||
|
||||
while (currentDate <= endDate) {
|
||||
const monthKey = `${currentDate.getFullYear()}-${String(currentDate.getMonth() + 1).padStart(2, '0')}`;
|
||||
const monthName = currentDate.toLocaleDateString('en-US', { year: 'numeric', month: 'short' });
|
||||
|
||||
allMonths[monthKey] = {
|
||||
month: monthName,
|
||||
value: monthlyData[monthKey]?.value || 0
|
||||
};
|
||||
|
||||
currentDate.setMonth(currentDate.getMonth() + 1);
|
||||
}
|
||||
|
||||
// Calculate cumulative values
|
||||
let cumulativeValue = 0;
|
||||
const sortedMonths = Object.keys(monthlyData).sort((a, b) => a.localeCompare(b));
|
||||
const sortedMonths = Object.keys(allMonths).sort((a, b) => a.localeCompare(b));
|
||||
|
||||
sortedMonths.forEach(monthKey => {
|
||||
cumulativeValue += monthlyData[monthKey].value;
|
||||
monthlyData[monthKey].cumulative = cumulativeValue;
|
||||
cumulativeValue += allMonths[monthKey].value;
|
||||
allMonths[monthKey].cumulative = cumulativeValue;
|
||||
});
|
||||
|
||||
// Convert to array
|
||||
chartData = sortedMonths.map(monthKey => ({
|
||||
month: monthlyData[monthKey].month,
|
||||
value: Math.round(monthlyData[monthKey].value),
|
||||
cumulative: Math.round(monthlyData[monthKey].cumulative)
|
||||
month: allMonths[monthKey].month,
|
||||
value: Math.round(allMonths[monthKey].value),
|
||||
cumulative: Math.round(allMonths[monthKey].cumulative)
|
||||
}));
|
||||
summary = {
|
||||
total: {
|
||||
realisedValue: Math.round(realisedValue),
|
||||
unrealisedValue: Math.round(unrealisedValue)
|
||||
},
|
||||
byType: Object.fromEntries(
|
||||
Object.entries(typeSummary).map(([type, data]) => [
|
||||
type,
|
||||
{
|
||||
realisedValue: Math.round(data.realisedValue),
|
||||
unrealisedValue: Math.round(data.unrealisedValue)
|
||||
}
|
||||
])
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user