Skip to content

Commit

Permalink
Refactor stats code
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonioVdlC committed Jul 29, 2023
1 parent e850ea5 commit 92999d7
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 96 deletions.
8 changes: 8 additions & 0 deletions app/utils/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function getDateComponents(date: Date) {
return {
year: date.getUTCFullYear(),
month: date.getUTCMonth() + 1,
day: date.getUTCDate(),
hour: date.getUTCHours(),
};
}
173 changes: 77 additions & 96 deletions app/utils/stats.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Website } from "@prisma/client";
import { db } from "./db.server";
import { getDateComponents } from "./date";

export async function getPageViewsLastHour({
website,
Expand All @@ -8,48 +9,43 @@ export async function getPageViewsLastHour({
website: Website;
currentDate: Date;
}) {
const year = currentDate.getUTCFullYear();
const month = currentDate.getUTCMonth() + 1;
const day = currentDate.getUTCDate();
const hour = currentDate.getUTCHours();

const value = { current: 0, previous: 0 };
const { year, month, day, hour } = getDateComponents(currentDate);

// Current
const eventsCurrent = await db.event.findMany({
where: {
websiteId: website.id,
period: { year, month, day, hour },
},
});

value.current = eventsCurrent.reduce((sum, event) => (sum += event.count), 0);
const current =
(await db.event.count({
where: {
websiteId: website.id,
period: { year, month, day, hour },
},
})) || 0;

// Previous
const previousDate = new Date(currentDate.getTime() - 60 * 60 * 1000);
const previousYear = previousDate.getUTCFullYear();
const previousMonth = previousDate.getUTCMonth() + 1;
const previousDay = previousDate.getUTCDate();
const previousHour = previousDate.getUTCHours();

const events = await db.event.findMany({
where: {
websiteId: website.id,
period: {
year: previousYear,
month: previousMonth,
day: previousDay,
hour: previousHour,
const {
year: previousYear,
month: previousMonth,
day: previousDay,
hour: previousHour,
} = getDateComponents(previousDate);

const previous =
(await db.event.count({
where: {
websiteId: website.id,
period: {
year: previousYear,
month: previousMonth,
day: previousDay,
hour: previousHour,
},
},
},
});

value.previous = events.reduce((sum, event) => (sum += event.count), 0);
})) || 0;

// Change
const change = value.current - value.previous;
const change = current - previous;

return { value, change };
return { value: { current, previous }, change };
}

export async function getPageViewsLastDay({
Expand All @@ -59,48 +55,41 @@ export async function getPageViewsLastDay({
website: Website;
currentDate: Date;
}) {
const year = currentDate.getUTCFullYear();
const month = currentDate.getUTCMonth() + 1;
const day = currentDate.getUTCDate();

const value = { current: 0, previous: 0 };
const { year, month, day } = getDateComponents(currentDate);

// Current
const eventsCurrent = await db.event.findMany({
where: {
websiteId: website.id,
period: { year, month, day },
},
});

value.current = eventsCurrent.reduce((sum, event) => (sum += event.count), 0);
const current =
(await db.event.count({
where: {
websiteId: website.id,
period: { year, month, day },
},
})) || 0;

// Previous
const previousDate = new Date(currentDate.getTime() - 24 * 60 * 60 * 1000);
const previousYear = previousDate.getUTCFullYear();
const previousMonth = previousDate.getUTCMonth() + 1;
const previousDay = previousDate.getUTCDate();

const events = await db.event.findMany({
where: {
websiteId: website.id,
period: {
year: previousYear,
month: previousMonth,
day: previousDay,
const {
year: previousYear,
month: previousMonth,
day: previousDay,
} = getDateComponents(previousDate);

const previous =
(await db.event.count({
where: {
websiteId: website.id,
period: {
year: previousYear,
month: previousMonth,
day: previousDay,
},
},
},
});

value.previous = events.reduce((sum, event) => (sum += event.count), 0);
})) || 0;

// Change
const change = value.current - value.previous;
const change = current - previous;

return {
value,
change,
};
return { value: { current, previous }, change };
}

export async function getPageViewsLastMonth({
Expand All @@ -110,45 +99,37 @@ export async function getPageViewsLastMonth({
website: Website;
currentDate: Date;
}) {
const year = currentDate.getUTCFullYear();
const month = currentDate.getUTCMonth() + 1;

const value = { current: 0, previous: 0 };
const { year, month } = getDateComponents(currentDate);

// Current
const eventsCurrent = await db.event.findMany({
where: {
websiteId: website.id,
period: { year, month },
},
});

value.current = eventsCurrent.reduce((sum, event) => (sum += event.count), 0);
const current =
(await db.event.count({
where: {
websiteId: website.id,
period: { year, month },
},
})) || 0;

// Previous
const previousDate = new Date(
currentDate.getTime() - 30 * 24 * 60 * 60 * 1000
);
const previousYear = previousDate.getUTCFullYear();
const previousMonth = previousDate.getUTCMonth() + 1;

const events = await db.event.findMany({
where: {
websiteId: website.id,
period: {
year: previousYear,
month: previousMonth,
const { year: previousYear, month: previousMonth } =
getDateComponents(previousDate);

const previous =
(await db.event.count({
where: {
websiteId: website.id,
period: {
year: previousYear,
month: previousMonth,
},
},
},
});

value.previous = events.reduce((sum, event) => (sum += event.count), 0);
})) || 0;

// Change
const change = value.current - value.previous;
const change = current - previous;

return {
value,
change,
};
return { value: { current, previous }, change };
}

0 comments on commit 92999d7

Please sign in to comment.