fix: get heatmap for past one year only

This commit is contained in:
sai80082 committed 2024-09-13 20:13:22 +05:30
1 parent 360c19699e
commit 5e41c88a89
1 file changed
+27 -23
+27 -23
View File
@@ -2,42 +2,42 @@
import { db } from "@/config/db"; import { db } from "@/config/db";
import { getCurrentUser } from "@/handlers/serverUtils/user.utils"; import { getCurrentUser } from "@/handlers/serverUtils/user.utils";
import { assets, exif } from "@/schema"; import { assets, exif } from "@/schema";
import { and, count, desc, eq, isNotNull, ne, sql, } from "drizzle-orm"; import { and, count, desc, eq, gte, isNotNull, ne, sql, } from "drizzle-orm";
import type { NextApiRequest, NextApiResponse } from "next"; import type { NextApiRequest, NextApiResponse } from "next";
// Helper function to format date as YYYY-MM-DD // Helper function to format date as YYYY-MM-DD
function formatDate(date: Date) { function formatDate(date: Date) {
return date.toISOString().split('T')[0]; return date.toISOString().split('T')[0];
} }
function fillMissingDates(data: any[]) { function fillMissingDates(data: any[]) {
const today = new Date(); const today = new Date();
// Set the start date as one year ago, excluding the current month in the previous year // Set the start date as one year ago, excluding the current month in the previous year
const oneYearAgo = new Date(today.getFullYear() - 1, today.getMonth() + 1, 1); // First day of the next month last year const oneYearAgo = new Date(today.getFullYear() - 1, today.getMonth() + 1, 1); // First day of the next month last year
const existingDates = new Set(data.map(item => item.date)); const existingDates = new Set(data.map(item => item.date));
const filledData = []; const filledData = [];
// Iterate from today to one year ago, but skip the current month from last year // Iterate from today to one year ago, but skip the current month from last year
for (let d = new Date(today); d >= oneYearAgo; d.setDate(d.getDate() - 1)) { for (let d = new Date(today); d >= oneYearAgo; d.setDate(d.getDate() - 1)) {
const dateStr = formatDate(d); const dateStr = formatDate(d);
// Skip dates in the current month of the previous year // Skip dates in the current month of the previous year
if (d.getFullYear() === today.getFullYear() - 1 && d.getMonth() === today.getMonth()) { if (d.getFullYear() === today.getFullYear() - 1 && d.getMonth() === today.getMonth()) {
continue; continue;
} }
if (existingDates.has(dateStr)) { if (existingDates.has(dateStr)) {
filledData.push(data.find(item => item.date === dateStr)); filledData.push(data.find(item => item.date === dateStr));
} else { } else {
filledData.push({ date: dateStr, count: 0 }); filledData.push({ date: dateStr, count: 0 });
} }
} }
// Sort the filled data in descending order // Sort the filled data in descending order
filledData.sort((a, b) => a.date.localeCompare(b.date)); filledData.sort((a, b) => a.date.localeCompare(b.date));
return filledData; return filledData;
} }
export default async function handler( export default async function handler(
@@ -52,7 +52,11 @@ export default async function handler(
count: count(), count: count(),
}) })
.from(assets) .from(assets)
.where(eq(assets.ownerId, currentUser.id)) .where(
and(
eq(assets.ownerId, currentUser.id),
gte(assets.fileCreatedAt, sql`CURRENT_DATE - INTERVAL '1 YEAR'`))
)
.groupBy(sql`DATE(${assets.fileCreatedAt})`) .groupBy(sql`DATE(${assets.fileCreatedAt})`)
.orderBy(sql`DATE(${assets.fileCreatedAt}) DESC`) .orderBy(sql`DATE(${assets.fileCreatedAt}) DESC`)
const updatedData = fillMissingDates(dataFromDB); const updatedData = fillMissingDates(dataFromDB);