Skip to content

Commit

Permalink
Split statistics helpers into separate files for separate features + …
Browse files Browse the repository at this point in the history
…add all by created by day endpoints for each statics feature
  • Loading branch information
bobalazek committed Jan 26, 2024
1 parent 0281290 commit 9a158a8
Show file tree
Hide file tree
Showing 20 changed files with 448 additions and 277 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,18 @@ export class CalendarStatisticsController {
data,
};
}

@UseGuards(AuthenticatedGuard)
@Get('events-created')
async eventsCreated(@Req() req: Request) {
const from = req.query.from ? new Date(req.query.from as string) : undefined;
const to = req.query.to ? new Date(req.query.to as string) : undefined;

const data = await calendarStatisticsManager.getEventsCreated(req.user, from, to);

return {
success: true,
data,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,18 @@ export class FocusStatisticsController {
data,
};
}

@UseGuards(AuthenticatedGuard)
@Get('focus-sessions-created')
async focusSessionsCreated(@Req() req: Request) {
const from = req.query.from ? new Date(req.query.from as string) : undefined;
const to = req.query.to ? new Date(req.query.to as string) : undefined;

const data = await focusStatisticsManager.getFocusSessionsCreated(req.user, from, to);

return {
success: true,
data,
};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Controller, Get, Req, UseGuards } from '@nestjs/common';
import { subDays } from 'date-fns';
import { Request } from 'express';

import { moodStatisticsManager } from '@moaitime/database-services';
Expand All @@ -20,12 +19,12 @@ export class MoodStatisticsController {
}

@UseGuards(AuthenticatedGuard)
@Get('daily')
async daily(@Req() req: Request) {
const to = new Date();
const from = subDays(to, 28);
@Get('mood-entries-created')
async moodEntriesCreated(@Req() req: Request) {
const from = req.query.from ? new Date(req.query.from as string) : undefined;
const to = req.query.to ? new Date(req.query.to as string) : undefined;

const data = await moodStatisticsManager.getDailyAverage(req.user, from, to);
const data = await moodStatisticsManager.getMoodEntriesCreated(req.user, from, to);

return {
success: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,18 @@ export class NotesStatisticsController {
data,
};
}

@UseGuards(AuthenticatedGuard)
@Get('notes-created')
async notesCreated(@Req() req: Request) {
const from = req.query.from ? new Date(req.query.from as string) : undefined;
const to = req.query.to ? new Date(req.query.to as string) : undefined;

const data = await notesStatisticsManager.getNotesCreated(req.user, from, to);

return {
success: true,
data,
};
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { format, startOfMonth, startOfWeek } from 'date-fns';
import { and, count, eq, gte, isNull, sql } from 'drizzle-orm';
import { and, between, count, eq, gte, isNull, lte, SQL, sql } from 'drizzle-orm';

import { events, getDatabase, User } from '@moaitime/database-core';
import { StatisticsCalendarBasicData } from '@moaitime/shared-common';
import {
padDataForRangeMap,
StatisticsCalendarBasicData,
StatisticsDateCountData,
} from '@moaitime/shared-common';

export class CalendarStatisticsManager {
async getBasics(user: User): Promise<StatisticsCalendarBasicData> {
Expand All @@ -16,36 +20,28 @@ export class CalendarStatisticsManager {
const startOfThisWeek = startOfWeek(today);
const startOfThisMonth = startOfMonth(today);

const createdAtDate = sql<Date>`DATE(${events.createdAt})`;
const rows = await getDatabase()
.select({ date: createdAtDate, count: count(events).mapWith(Number) })
.from(events)
.where(
and(
eq(events.userId, user.id),
isNull(events.deletedAt),
gte(events.createdAt, startOfThisMonth)
)
)
.groupBy(createdAtDate)
.execute();
const todayString = format(today, 'yyyy-MM-dd');
const yesterdayString = format(yesterday, 'yyyy-MM-dd');

for (const row of rows) {
const rowDateString = format(row.date, 'yyyy-MM-dd');
if (rowDateString === format(today, 'yyyy-MM-dd')) {
eventsCreatedTodayCount = row.count;
const rows = await this.getEventsCreated(user, startOfThisMonth);
for (const date in rows) {
const count = rows[date];
const dateObject = new Date(date);

if (date === todayString) {
eventsCreatedTodayCount = count;
}

if (rowDateString === format(yesterday, 'yyyy-MM-dd')) {
eventsCreatedYesterdayCount = row.count;
if (date === yesterdayString) {
eventsCreatedYesterdayCount = count;
}

if (row.date >= startOfThisWeek) {
eventsCreatedThisWeekCount += row.count;
if (dateObject >= startOfThisWeek) {
eventsCreatedThisWeekCount += count;
}

if (row.date >= startOfThisMonth) {
eventsCreatedThisMonthCount += row.count;
if (dateObject >= startOfThisMonth) {
eventsCreatedThisMonthCount += count;
}
}

Expand All @@ -56,6 +52,37 @@ export class CalendarStatisticsManager {
eventsCreatedThisMonthCount,
};
}

async getEventsCreated(user: User, from?: Date, to?: Date): Promise<StatisticsDateCountData> {
let where = and(eq(events.userId, user.id), isNull(events.deletedAt));

if (from && to) {
where = and(where, between(events.createdAt, from, to)) as SQL<unknown>;
} else if (from) {
where = and(where, gte(events.createdAt, from)) as SQL<unknown>;
} else if (to) {
where = and(where, lte(events.createdAt, to)) as SQL<unknown>;
}

const createdAtDate = sql<Date>`DATE(${events.createdAt})`;
const rows = await getDatabase()
.select({ date: createdAtDate, count: count(events).mapWith(Number) })
.from(events)
.where(where)
.groupBy(createdAtDate)
.execute();

const result: StatisticsDateCountData = {};
for (const row of rows) {
result[format(row.date, 'yyyy-MM-dd')] = row.count;
}

if (from && to) {
return padDataForRangeMap(result, from, to);
}

return result;
}
}

export const calendarStatisticsManager = new CalendarStatisticsManager();
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { format, startOfMonth, startOfWeek } from 'date-fns';
import { and, count, eq, gte, isNull, sql } from 'drizzle-orm';
import { and, between, count, eq, gte, isNull, lte, SQL, sql } from 'drizzle-orm';

import { focusSessions, getDatabase, User } from '@moaitime/database-core';
import { StatisticsFocusBasicData } from '@moaitime/shared-common';
import {
padDataForRangeMap,
StatisticsDateCountData,
StatisticsFocusBasicData,
} from '@moaitime/shared-common';

export class FocusStatisticsManager {
async getBasics(user: User): Promise<StatisticsFocusBasicData> {
Expand All @@ -16,36 +20,28 @@ export class FocusStatisticsManager {
const startOfThisWeek = startOfWeek(today);
const startOfThisMonth = startOfMonth(today);

const createdAtDate = sql<Date>`DATE(${focusSessions.createdAt})`;
const rows = await getDatabase()
.select({ date: createdAtDate, count: count(focusSessions).mapWith(Number) })
.from(focusSessions)
.where(
and(
eq(focusSessions.userId, user.id),
isNull(focusSessions.deletedAt),
gte(focusSessions.createdAt, startOfThisMonth)
)
)
.groupBy(createdAtDate)
.execute();
const todayString = format(today, 'yyyy-MM-dd');
const yesterdayString = format(yesterday, 'yyyy-MM-dd');

for (const row of rows) {
const rowDateString = format(row.date, 'yyyy-MM-dd');
if (rowDateString === format(today, 'yyyy-MM-dd')) {
focusSessionsCreatedTodayCount = row.count;
const rows = await this.getFocusSessionsCreated(user, startOfThisMonth);
for (const date in rows) {
const count = rows[date];
const dateObject = new Date(date);

if (date === todayString) {
focusSessionsCreatedTodayCount = count;
}

if (rowDateString === format(yesterday, 'yyyy-MM-dd')) {
focusSessionsCreatedYesterdayCount = row.count;
if (date === yesterdayString) {
focusSessionsCreatedYesterdayCount = count;
}

if (row.date >= startOfThisWeek) {
focusSessionsCreatedThisWeekCount += row.count;
if (dateObject >= startOfThisWeek) {
focusSessionsCreatedThisWeekCount += count;
}

if (row.date >= startOfThisMonth) {
focusSessionsCreatedThisMonthCount += row.count;
if (dateObject >= startOfThisMonth) {
focusSessionsCreatedThisMonthCount += count;
}
}

Expand All @@ -56,6 +52,41 @@ export class FocusStatisticsManager {
focusSessionsCreatedThisMonthCount,
};
}

async getFocusSessionsCreated(
user: User,
from?: Date,
to?: Date
): Promise<StatisticsDateCountData> {
let where = and(eq(focusSessions.userId, user.id), isNull(focusSessions.deletedAt));

if (from && to) {
where = and(where, between(focusSessions.createdAt, from, to)) as SQL<unknown>;
} else if (from) {
where = and(where, gte(focusSessions.createdAt, from)) as SQL<unknown>;
} else if (to) {
where = and(where, lte(focusSessions.createdAt, to)) as SQL<unknown>;
}

const createdAtDate = sql<Date>`DATE(${focusSessions.createdAt})`;
const rows = await getDatabase()
.select({ date: createdAtDate, count: count(focusSessions).mapWith(Number) })
.from(focusSessions)
.where(where)
.groupBy(createdAtDate)
.execute();

const result: StatisticsDateCountData = {};
for (const row of rows) {
result[format(row.date, 'yyyy-MM-dd')] = row.count;
}

if (from && to) {
return padDataForRangeMap(result, from, to);
}

return result;
}
}

export const focusStatisticsManager = new FocusStatisticsManager();
Loading

0 comments on commit 9a158a8

Please sign in to comment.