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 chartData;
|
||||||
let summary;
|
let summary;
|
||||||
if (completedProjects.length === 0) {
|
if (completedProjects.length === 0) {
|
||||||
chartData = [
|
// Generate continuous sample data from Jan 2024 to current month
|
||||||
{ month: 'Jan 2024', value: 50000, cumulative: 50000 },
|
const currentDate = new Date();
|
||||||
{ month: 'Feb 2024', value: 75000, cumulative: 125000 },
|
const startDate = new Date(2024, 0, 1); // Jan 2024
|
||||||
{ month: 'Mar 2024', value: 60000, cumulative: 185000 },
|
chartData = [];
|
||||||
{ month: 'Apr 2024', value: 80000, cumulative: 265000 },
|
let cumulative = 0;
|
||||||
{ month: 'May 2024', value: 95000, cumulative: 360000 },
|
|
||||||
{ month: 'Jun 2024', value: 70000, cumulative: 430000 },
|
let tempDate = new Date(startDate);
|
||||||
{ month: 'Jul 2024', value: 85000, cumulative: 515000 },
|
while (tempDate <= currentDate) {
|
||||||
{ month: 'Aug 2024', value: 90000, cumulative: 605000 },
|
const monthName = tempDate.toLocaleDateString('en-US', { year: 'numeric', month: 'short' });
|
||||||
{ month: 'Sep 2024', value: 78000, cumulative: 683000 },
|
let monthlyValue = 0;
|
||||||
{ month: 'Oct 2024', value: 92000, cumulative: 775000 },
|
|
||||||
{ month: 'Nov 2024', value: 88000, cumulative: 863000 },
|
// Add some sample values for certain months
|
||||||
{ month: 'Dec 2024', value: 95000, cumulative: 958000 }
|
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 = {
|
summary = {
|
||||||
total: {
|
total: {
|
||||||
realisedValue: 958000,
|
realisedValue: 958000,
|
||||||
@@ -123,24 +139,70 @@ export async function GET(request) {
|
|||||||
monthlyData[monthKey].value += projectValue;
|
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;
|
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 => {
|
sortedMonths.forEach(monthKey => {
|
||||||
cumulativeValue += monthlyData[monthKey].value;
|
cumulativeValue += allMonths[monthKey].value;
|
||||||
monthlyData[monthKey].cumulative = cumulativeValue;
|
allMonths[monthKey].cumulative = cumulativeValue;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Convert to array
|
// Convert to array
|
||||||
chartData = sortedMonths.map(monthKey => ({
|
chartData = sortedMonths.map(monthKey => ({
|
||||||
month: monthlyData[monthKey].month,
|
month: allMonths[monthKey].month,
|
||||||
value: Math.round(monthlyData[monthKey].value),
|
value: Math.round(allMonths[monthKey].value),
|
||||||
cumulative: Math.round(monthlyData[monthKey].cumulative)
|
cumulative: Math.round(allMonths[monthKey].cumulative)
|
||||||
}));
|
}));
|
||||||
summary = {
|
summary = {
|
||||||
|
total: {
|
||||||
realisedValue: Math.round(realisedValue),
|
realisedValue: Math.round(realisedValue),
|
||||||
unrealisedValue: Math.round(unrealisedValue)
|
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