fix: get heatmap for past one year only
This commit is contained in:
1 parent
360c19699e
commit
5e41c88a89
1 file changed
+18
-14
@@ -2,15 +2,15 @@
|
|||||||
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
|
||||||
@@ -22,18 +22,18 @@ function formatDate(date: Date) {
|
|||||||
|
|
||||||
// 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
|
||||||
@@ -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);
|
||||||
|
|||||||
Reference in new issue
Block a user