Skip to content

Commit

Permalink
fix: include current score in all graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
FreekBes committed Nov 15, 2024
1 parent 6bbf3a1 commit 3a349a2
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/routes/charts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const setupChartRoutes = function(app: Express, prisma: PrismaClient): vo
for (let i = 0; i <= 60; i++) {
dates.push(new Date(monthAgo.getTime() + i * 12 * 60 * 60 * 1000));
}
dates.push(new Date()); // always add the current score

// Get the scores for each coalition
const coalitionDataPoints: { [key: number]: CoalitionScore[] } = {};
Expand All @@ -40,7 +41,7 @@ export const setupChartRoutes = function(app: Express, prisma: PrismaClient): vo
const chartJSData: ChartConfiguration = {
type: 'line',
data: {
labels: dates.map((date) => `${date.toLocaleDateString()} ${date.getHours()}:00`),
labels: dates.map((date) => `${date.toLocaleDateString()} ${date.getHours()}:${String(date.getMinutes()).padStart(2, '0')}`),
datasets: [],
},
options: {
Expand Down Expand Up @@ -110,12 +111,16 @@ export const setupChartRoutes = function(app: Express, prisma: PrismaClient): vo
const date = new Date(monthAgo.getTime() + i * 12 * 60 * 60 * 1000);
dataPoints[date.getTime()] = await getCoalitionScore(prisma, coalitionId, date);
}
dataPoints[Date.now()] = await getCoalitionScore(prisma, coalitionId, new Date()); // always add the current score

// Compose the returnable data (in a format Chart.js can understand)
const chartJSData: ChartConfiguration = {
type: 'line',
data: {
labels: Object.keys(dataPoints).map((timestamp) => new Date(parseInt(timestamp)).toLocaleDateString()),
labels: Object.keys(dataPoints).map((timestamp) => {
const date = new Date(parseInt(timestamp));
return `${date.toLocaleDateString()} ${date.getHours()}:${String(date.getMinutes()).padStart(2, '0')}`;
}),
datasets: [
{
label: 'Score',
Expand Down Expand Up @@ -227,6 +232,7 @@ export const setupChartRoutes = function(app: Express, prisma: PrismaClient): vo
for (let i = 0; i <= 60; i++) {
dates.push(new Date(monthAgo.getTime() + i * 12 * 60 * 60 * 1000));
}
dates.push(new Date()); // always add the current score

// Get all scores for this user for the past 30 days
const scoreSumsPerDate: { [key: number]: number } = {};
Expand All @@ -253,7 +259,7 @@ export const setupChartRoutes = function(app: Express, prisma: PrismaClient): vo
const chartJSData: ChartConfiguration = {
type: 'line',
data: {
labels: dates.map((date) => `${date.toLocaleDateString()} ${date.getHours()}:00`),
labels: dates.map((date) => `${date.toLocaleDateString()} ${date.getHours()}:${String(date.getMinutes()).padStart(2, '0')}`),
datasets: [{
label: 'Total points',
data: dates.map(date => scoreSumsPerDate[date.getTime()] || 0),
Expand Down Expand Up @@ -333,6 +339,7 @@ export const setupChartRoutes = function(app: Express, prisma: PrismaClient): vo
for (let i = 0; i <= 60; i++) {
dates.push(new Date(monthAgo.getTime() + i * 12 * 60 * 60 * 1000));
}
dates.push(new Date()); // always add the current score

// Get all fixed point types
const fixedPointTypes = await prisma.codamCoalitionFixedType.findMany({
Expand Down Expand Up @@ -374,7 +381,7 @@ export const setupChartRoutes = function(app: Express, prisma: PrismaClient): vo
const chartJSData: ChartConfiguration = {
type: 'line',
data: {
labels: dates.map((date) => `${date.toLocaleDateString()} ${date.getHours()}:00`),
labels: dates.map((date) => `${date.toLocaleDateString()} ${date.getHours()}:${String(date.getMinutes()).padStart(2, '0')}`),
datasets: fixedPointTypes.map((fixedPointType) => {
return {
label: fixedPointType.type || 'custom',
Expand Down

0 comments on commit 3a349a2

Please sign in to comment.