From 9a158a824a77260c80cf796c819eb8a903360d56 Mon Sep 17 00:00:00 2001 From: Borut Balazek Date: Fri, 26 Jan 2024 09:33:12 +0100 Subject: [PATCH] Split statistics helpers into separate files for separate features + add all by created by day endpoints for each statics feature --- .../calendar-statistics.controller.ts | 14 ++ .../focus-statistics.controller.ts | 14 ++ .../controllers/mood-statistics.controller.ts | 11 +- .../notes-statistics.controller.ts | 14 ++ .../statistics/CalendarStatisticsManager.ts | 77 ++++++--- .../statistics/FocusStatisticsManager.ts | 81 +++++++--- .../statistics/MoodStatisticsManager.ts | 90 ++++++----- .../statistics/NotesStatisticsManager.ts | 77 ++++++--- .../tabs/StatisticsCalendarTabContent.tsx | 2 +- .../tabs/StatisticsFocusTabContent.tsx | 2 +- .../tabs/StatisticsMoodTabContent.tsx | 2 +- .../tabs/StatisticsNotesTabContent.tsx | 2 +- .../tasks/StatisticsTasksTabContentBasics.tsx | 2 +- .../StatisticsTasksTabContentDateCountMap.tsx | 2 +- .../hooks/StatisticsCalendarHooks.ts | 31 ++++ .../statistics/hooks/StatisticsFocusHooks.ts | 31 ++++ .../statistics/hooks/StatisticsHooks.ts | 148 +----------------- .../statistics/hooks/StatisticsMoodHooks.ts | 31 ++++ .../statistics/hooks/StatisticsNotesHooks.ts | 31 ++++ .../statistics/hooks/StatisticsTasksHooks.ts | 63 ++++++++ 20 files changed, 448 insertions(+), 277 deletions(-) create mode 100644 packages/web-core/src/features/statistics/hooks/StatisticsCalendarHooks.ts create mode 100644 packages/web-core/src/features/statistics/hooks/StatisticsFocusHooks.ts create mode 100644 packages/web-core/src/features/statistics/hooks/StatisticsMoodHooks.ts create mode 100644 packages/web-core/src/features/statistics/hooks/StatisticsNotesHooks.ts create mode 100644 packages/web-core/src/features/statistics/hooks/StatisticsTasksHooks.ts diff --git a/apps/api/src/features/statistics/controllers/calendar-statistics.controller.ts b/apps/api/src/features/statistics/controllers/calendar-statistics.controller.ts index c67b6caa..232efb28 100644 --- a/apps/api/src/features/statistics/controllers/calendar-statistics.controller.ts +++ b/apps/api/src/features/statistics/controllers/calendar-statistics.controller.ts @@ -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, + }; + } } diff --git a/apps/api/src/features/statistics/controllers/focus-statistics.controller.ts b/apps/api/src/features/statistics/controllers/focus-statistics.controller.ts index 3e52a0bc..ec457eb0 100644 --- a/apps/api/src/features/statistics/controllers/focus-statistics.controller.ts +++ b/apps/api/src/features/statistics/controllers/focus-statistics.controller.ts @@ -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, + }; + } } diff --git a/apps/api/src/features/statistics/controllers/mood-statistics.controller.ts b/apps/api/src/features/statistics/controllers/mood-statistics.controller.ts index 03f428d0..3d81663d 100644 --- a/apps/api/src/features/statistics/controllers/mood-statistics.controller.ts +++ b/apps/api/src/features/statistics/controllers/mood-statistics.controller.ts @@ -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'; @@ -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, diff --git a/apps/api/src/features/statistics/controllers/notes-statistics.controller.ts b/apps/api/src/features/statistics/controllers/notes-statistics.controller.ts index 217bb857..a1e7fb93 100644 --- a/apps/api/src/features/statistics/controllers/notes-statistics.controller.ts +++ b/apps/api/src/features/statistics/controllers/notes-statistics.controller.ts @@ -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, + }; + } } diff --git a/packages/database-services/src/features/statistics/CalendarStatisticsManager.ts b/packages/database-services/src/features/statistics/CalendarStatisticsManager.ts index c26e27a7..ad449db3 100644 --- a/packages/database-services/src/features/statistics/CalendarStatisticsManager.ts +++ b/packages/database-services/src/features/statistics/CalendarStatisticsManager.ts @@ -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 { @@ -16,36 +20,28 @@ export class CalendarStatisticsManager { const startOfThisWeek = startOfWeek(today); const startOfThisMonth = startOfMonth(today); - const createdAtDate = sql`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; } } @@ -56,6 +52,37 @@ export class CalendarStatisticsManager { eventsCreatedThisMonthCount, }; } + + async getEventsCreated(user: User, from?: Date, to?: Date): Promise { + let where = and(eq(events.userId, user.id), isNull(events.deletedAt)); + + if (from && to) { + where = and(where, between(events.createdAt, from, to)) as SQL; + } else if (from) { + where = and(where, gte(events.createdAt, from)) as SQL; + } else if (to) { + where = and(where, lte(events.createdAt, to)) as SQL; + } + + const createdAtDate = sql`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(); diff --git a/packages/database-services/src/features/statistics/FocusStatisticsManager.ts b/packages/database-services/src/features/statistics/FocusStatisticsManager.ts index dc45ed4c..16e68651 100644 --- a/packages/database-services/src/features/statistics/FocusStatisticsManager.ts +++ b/packages/database-services/src/features/statistics/FocusStatisticsManager.ts @@ -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 { @@ -16,36 +20,28 @@ export class FocusStatisticsManager { const startOfThisWeek = startOfWeek(today); const startOfThisMonth = startOfMonth(today); - const createdAtDate = sql`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; } } @@ -56,6 +52,41 @@ export class FocusStatisticsManager { focusSessionsCreatedThisMonthCount, }; } + + async getFocusSessionsCreated( + user: User, + from?: Date, + to?: Date + ): Promise { + let where = and(eq(focusSessions.userId, user.id), isNull(focusSessions.deletedAt)); + + if (from && to) { + where = and(where, between(focusSessions.createdAt, from, to)) as SQL; + } else if (from) { + where = and(where, gte(focusSessions.createdAt, from)) as SQL; + } else if (to) { + where = and(where, lte(focusSessions.createdAt, to)) as SQL; + } + + const createdAtDate = sql`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(); diff --git a/packages/database-services/src/features/statistics/MoodStatisticsManager.ts b/packages/database-services/src/features/statistics/MoodStatisticsManager.ts index ca73e9cc..c608fc8b 100644 --- a/packages/database-services/src/features/statistics/MoodStatisticsManager.ts +++ b/packages/database-services/src/features/statistics/MoodStatisticsManager.ts @@ -1,8 +1,12 @@ import { format, startOfMonth, startOfWeek } from 'date-fns'; -import { and, avg, count, eq, gte, isNull, lte, sql } from 'drizzle-orm'; +import { and, between, count, eq, gte, isNull, lte, SQL, sql } from 'drizzle-orm'; import { getDatabase, moodEntries, User } from '@moaitime/database-core'; -import { StatisticsMoodBasicData } from '@moaitime/shared-common'; +import { + padDataForRangeMap, + StatisticsDateCountData, + StatisticsMoodBasicData, +} from '@moaitime/shared-common'; export class MoodStatisticsManager { async getBasics(user: User): Promise { @@ -16,36 +20,28 @@ export class MoodStatisticsManager { const startOfThisWeek = startOfWeek(today); const startOfThisMonth = startOfMonth(today); - const createdAtDate = sql`DATE(${moodEntries.createdAt})`; - const rows = await getDatabase() - .select({ date: createdAtDate, count: count(moodEntries).mapWith(Number) }) - .from(moodEntries) - .where( - and( - eq(moodEntries.userId, user.id), - isNull(moodEntries.deletedAt), - gte(moodEntries.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')) { - moodEntriesCreatedTodayCount = row.count; + const rows = await this.getMoodEntriesCreated(user, startOfThisMonth); + for (const date in rows) { + const count = rows[date]; + const dateObject = new Date(date); + + if (date === todayString) { + moodEntriesCreatedTodayCount = count; } - if (rowDateString === format(yesterday, 'yyyy-MM-dd')) { - moodEntriesCreatedYesterdayCount = row.count; + if (date === yesterdayString) { + moodEntriesCreatedYesterdayCount = count; } - if (row.date >= startOfThisWeek) { - moodEntriesCreatedThisWeekCount += row.count; + if (dateObject >= startOfThisWeek) { + moodEntriesCreatedThisWeekCount += count; } - if (row.date >= startOfThisMonth) { - moodEntriesCreatedThisMonthCount += row.count; + if (dateObject >= startOfThisMonth) { + moodEntriesCreatedThisMonthCount += count; } } @@ -57,31 +53,39 @@ export class MoodStatisticsManager { }; } - async getDailyAverage( + async getMoodEntriesCreated( user: User, - from: Date, - to: Date - ): Promise<{ date: string; score: number }[]> { - const loggedAtDate = sql`DATE(${moodEntries.loggedAt})`; - - const where = and( - eq(moodEntries.userId, user.id), - gte(moodEntries.loggedAt, from.toISOString()), - lte(moodEntries.loggedAt, to.toISOString()), - isNull(moodEntries.deletedAt) - ); + from?: Date, + to?: Date + ): Promise { + let where = and(eq(moodEntries.userId, user.id), isNull(moodEntries.deletedAt)); + + if (from && to) { + where = and(where, between(moodEntries.createdAt, from, to)) as SQL; + } else if (from) { + where = and(where, gte(moodEntries.createdAt, from)) as SQL; + } else if (to) { + where = and(where, lte(moodEntries.createdAt, to)) as SQL; + } + const createdAtDate = sql`DATE(${moodEntries.createdAt})`; const rows = await getDatabase() - .select({ date: loggedAtDate, score: avg(moodEntries.happinessScore).mapWith(Number) }) + .select({ date: createdAtDate, count: count(moodEntries).mapWith(Number) }) .from(moodEntries) .where(where) - .groupBy(loggedAtDate) + .groupBy(createdAtDate) .execute(); - return rows.map((row) => ({ - date: format(row.date, 'yyyy-MM-dd'), - score: row.score ?? 0, - })); + 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; } } diff --git a/packages/database-services/src/features/statistics/NotesStatisticsManager.ts b/packages/database-services/src/features/statistics/NotesStatisticsManager.ts index 13d7adf8..14f8806e 100644 --- a/packages/database-services/src/features/statistics/NotesStatisticsManager.ts +++ b/packages/database-services/src/features/statistics/NotesStatisticsManager.ts @@ -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 { getDatabase, notes, User } from '@moaitime/database-core'; -import { StatisticsNotesBasicData } from '@moaitime/shared-common'; +import { + padDataForRangeMap, + StatisticsDateCountData, + StatisticsNotesBasicData, +} from '@moaitime/shared-common'; export class NotesStatisticsManager { async getBasics(user: User): Promise { @@ -16,36 +20,28 @@ export class NotesStatisticsManager { const startOfThisWeek = startOfWeek(today); const startOfThisMonth = startOfMonth(today); - const createdAtDate = sql`DATE(${notes.createdAt})`; - const rows = await getDatabase() - .select({ date: createdAtDate, count: count(notes).mapWith(Number) }) - .from(notes) - .where( - and( - eq(notes.userId, user.id), - isNull(notes.deletedAt), - gte(notes.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')) { - notesCreatedTodayCount = row.count; + const rows = await this.getNotesCreated(user, startOfThisMonth); + for (const date in rows) { + const count = rows[date]; + const dateObject = new Date(date); + + if (date === todayString) { + notesCreatedTodayCount = count; } - if (rowDateString === format(yesterday, 'yyyy-MM-dd')) { - notesCreatedYesterdayCount = row.count; + if (date === yesterdayString) { + notesCreatedYesterdayCount = count; } - if (row.date >= startOfThisWeek) { - notesCreatedThisWeekCount += row.count; + if (dateObject >= startOfThisWeek) { + notesCreatedThisWeekCount += count; } - if (row.date >= startOfThisMonth) { - notesCreatedThisMonthCount += row.count; + if (dateObject >= startOfThisMonth) { + notesCreatedThisMonthCount += count; } } @@ -56,6 +52,37 @@ export class NotesStatisticsManager { notesCreatedThisMonthCount, }; } + + async getNotesCreated(user: User, from?: Date, to?: Date): Promise { + let where = and(eq(notes.userId, user.id), isNull(notes.deletedAt)); + + if (from && to) { + where = and(where, between(notes.createdAt, from, to)) as SQL; + } else if (from) { + where = and(where, gte(notes.createdAt, from)) as SQL; + } else if (to) { + where = and(where, lte(notes.createdAt, to)) as SQL; + } + + const createdAtDate = sql`DATE(${notes.createdAt})`; + const rows = await getDatabase() + .select({ date: createdAtDate, count: count(notes).mapWith(Number) }) + .from(notes) + .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 notesStatisticsManager = new NotesStatisticsManager(); diff --git a/packages/web-core/src/features/statistics/components/tabs/StatisticsCalendarTabContent.tsx b/packages/web-core/src/features/statistics/components/tabs/StatisticsCalendarTabContent.tsx index 40ce8503..1a4e4308 100644 --- a/packages/web-core/src/features/statistics/components/tabs/StatisticsCalendarTabContent.tsx +++ b/packages/web-core/src/features/statistics/components/tabs/StatisticsCalendarTabContent.tsx @@ -1,6 +1,6 @@ import { ErrorAlert } from '../../../core/components/ErrorAlert'; import { Loader } from '../../../core/components/Loader'; -import { useCalendarStatisticsQuery } from '../../hooks/StatisticsHooks'; +import { useCalendarStatisticsQuery } from '../../hooks/StatisticsCalendarHooks'; import StatisticsCard from '../statistics-card/StatisticsCard'; const StatisticsCalendarTabContent = () => { diff --git a/packages/web-core/src/features/statistics/components/tabs/StatisticsFocusTabContent.tsx b/packages/web-core/src/features/statistics/components/tabs/StatisticsFocusTabContent.tsx index 49ab8f8a..a99de883 100644 --- a/packages/web-core/src/features/statistics/components/tabs/StatisticsFocusTabContent.tsx +++ b/packages/web-core/src/features/statistics/components/tabs/StatisticsFocusTabContent.tsx @@ -1,6 +1,6 @@ import { ErrorAlert } from '../../../core/components/ErrorAlert'; import { Loader } from '../../../core/components/Loader'; -import { useFocusStatisticsQuery } from '../../hooks/StatisticsHooks'; +import { useFocusStatisticsQuery } from '../../hooks/StatisticsFocusHooks'; import StatisticsCard from '../statistics-card/StatisticsCard'; const StatisticsFocusTabContent = () => { diff --git a/packages/web-core/src/features/statistics/components/tabs/StatisticsMoodTabContent.tsx b/packages/web-core/src/features/statistics/components/tabs/StatisticsMoodTabContent.tsx index e42a43da..f8d2f945 100644 --- a/packages/web-core/src/features/statistics/components/tabs/StatisticsMoodTabContent.tsx +++ b/packages/web-core/src/features/statistics/components/tabs/StatisticsMoodTabContent.tsx @@ -1,6 +1,6 @@ import { ErrorAlert } from '../../../core/components/ErrorAlert'; import { Loader } from '../../../core/components/Loader'; -import { useMoodStatisticsQuery } from '../../hooks/StatisticsHooks'; +import { useMoodStatisticsQuery } from '../../hooks/StatisticsMoodHooks'; import StatisticsCard from '../statistics-card/StatisticsCard'; const StatisticsMoodTabContent = () => { diff --git a/packages/web-core/src/features/statistics/components/tabs/StatisticsNotesTabContent.tsx b/packages/web-core/src/features/statistics/components/tabs/StatisticsNotesTabContent.tsx index e288d7df..daadb498 100644 --- a/packages/web-core/src/features/statistics/components/tabs/StatisticsNotesTabContent.tsx +++ b/packages/web-core/src/features/statistics/components/tabs/StatisticsNotesTabContent.tsx @@ -1,6 +1,6 @@ import { ErrorAlert } from '../../../core/components/ErrorAlert'; import { Loader } from '../../../core/components/Loader'; -import { useNotesStatisticsQuery } from '../../hooks/StatisticsHooks'; +import { useNotesStatisticsQuery } from '../../hooks/StatisticsNotesHooks'; import StatisticsCard from '../statistics-card/StatisticsCard'; const StatisticsNotesTabContent = () => { diff --git a/packages/web-core/src/features/statistics/components/tabs/tasks/StatisticsTasksTabContentBasics.tsx b/packages/web-core/src/features/statistics/components/tabs/tasks/StatisticsTasksTabContentBasics.tsx index 5bf882ea..27f08a66 100644 --- a/packages/web-core/src/features/statistics/components/tabs/tasks/StatisticsTasksTabContentBasics.tsx +++ b/packages/web-core/src/features/statistics/components/tabs/tasks/StatisticsTasksTabContentBasics.tsx @@ -1,6 +1,6 @@ import { ErrorAlert } from '../../../../core/components/ErrorAlert'; import { Loader } from '../../../../core/components/Loader'; -import { useTasksStatisticsQuery } from '../../../hooks/StatisticsHooks'; +import { useTasksStatisticsQuery } from '../../../hooks/StatisticsTasksHooks'; import StatisticsCard from '../../statistics-card/StatisticsCard'; export default function StatisticsTasksTabContentBasics() { diff --git a/packages/web-core/src/features/statistics/components/tabs/tasks/StatisticsTasksTabContentDateCountMap.tsx b/packages/web-core/src/features/statistics/components/tabs/tasks/StatisticsTasksTabContentDateCountMap.tsx index b35ce39e..e217057e 100644 --- a/packages/web-core/src/features/statistics/components/tabs/tasks/StatisticsTasksTabContentDateCountMap.tsx +++ b/packages/web-core/src/features/statistics/components/tabs/tasks/StatisticsTasksTabContentDateCountMap.tsx @@ -2,7 +2,7 @@ import Chart from 'react-apexcharts'; import { ErrorAlert } from '../../../../core/components/ErrorAlert'; import { Loader } from '../../../../core/components/Loader'; -import { useTasksStatisticsTasksCreatedQuery } from '../../../hooks/StatisticsHooks'; +import { useTasksStatisticsTasksCreatedQuery } from '../../../hooks/StatisticsTasksHooks'; export default function StatisticsTasksTabContentDateCountMap({ from, diff --git a/packages/web-core/src/features/statistics/hooks/StatisticsCalendarHooks.ts b/packages/web-core/src/features/statistics/hooks/StatisticsCalendarHooks.ts new file mode 100644 index 00000000..e3586f79 --- /dev/null +++ b/packages/web-core/src/features/statistics/hooks/StatisticsCalendarHooks.ts @@ -0,0 +1,31 @@ +// Need to have this, else typescript won't work... +// https://github.com/microsoft/TypeScript/issues/47663#issuecomment-1519138189 +// eslint-disable-next-line @typescript-eslint/no-unused-vars +import type { UseQueryResult } from '@tanstack/react-query'; + +import { useQuery } from '@tanstack/react-query'; + +import { API_URL, ResponseInterface, StatisticsCalendarBasicData } from '@moaitime/shared-common'; + +import { fetchJson } from '../../core/utils/FetchHelpers'; + +// Calendar +export const STATISTICS_CALENDAR_KEY = 'statistics:calendar'; + +export const getCalendarStatistics = async () => { + const response = await fetchJson>( + `${API_URL}/api/v1/calendar-statistics`, + { + method: 'GET', + } + ); + + return response.data as StatisticsCalendarBasicData; +}; + +export const useCalendarStatisticsQuery = () => { + return useQuery({ + queryKey: [STATISTICS_CALENDAR_KEY], + queryFn: getCalendarStatistics, + }); +}; diff --git a/packages/web-core/src/features/statistics/hooks/StatisticsFocusHooks.ts b/packages/web-core/src/features/statistics/hooks/StatisticsFocusHooks.ts new file mode 100644 index 00000000..f6545a5c --- /dev/null +++ b/packages/web-core/src/features/statistics/hooks/StatisticsFocusHooks.ts @@ -0,0 +1,31 @@ +// Need to have this, else typescript won't work... +// https://github.com/microsoft/TypeScript/issues/47663#issuecomment-1519138189 +// eslint-disable-next-line @typescript-eslint/no-unused-vars +import type { UseQueryResult } from '@tanstack/react-query'; + +import { useQuery } from '@tanstack/react-query'; + +import { API_URL, ResponseInterface, StatisticsFocusBasicData } from '@moaitime/shared-common'; + +import { fetchJson } from '../../core/utils/FetchHelpers'; + +// Focus +export const STATISTICS_FOCUS_KEY = 'statistics:focus'; + +export const getFocusStatistics = async () => { + const response = await fetchJson>( + `${API_URL}/api/v1/focus-statistics`, + { + method: 'GET', + } + ); + + return response.data as StatisticsFocusBasicData; +}; + +export const useFocusStatisticsQuery = () => { + return useQuery({ + queryKey: [STATISTICS_FOCUS_KEY], + queryFn: getFocusStatistics, + }); +}; diff --git a/packages/web-core/src/features/statistics/hooks/StatisticsHooks.ts b/packages/web-core/src/features/statistics/hooks/StatisticsHooks.ts index 5d252660..6d2ac4b7 100644 --- a/packages/web-core/src/features/statistics/hooks/StatisticsHooks.ts +++ b/packages/web-core/src/features/statistics/hooks/StatisticsHooks.ts @@ -5,16 +5,7 @@ import type { UseQueryResult } from '@tanstack/react-query'; import { useQuery } from '@tanstack/react-query'; -import { - API_URL, - ResponseInterface, - StatisticsCalendarBasicData, - StatisticsFocusBasicData, - StatisticsGeneralBasicData, - StatisticsMoodBasicData, - StatisticsNotesBasicData, - StatisticsTasksBasicData, -} from '@moaitime/shared-common'; +import { API_URL, ResponseInterface, StatisticsGeneralBasicData } from '@moaitime/shared-common'; import { fetchJson } from '../../core/utils/FetchHelpers'; @@ -38,140 +29,3 @@ export const useGeneralStatisticsQuery = () => { queryFn: getGeneralStatistics, }); }; - -// Calendar -export const STATISTICS_CALENDAR_KEY = 'statistics:calendar'; - -export const getCalendarStatistics = async () => { - const response = await fetchJson>( - `${API_URL}/api/v1/calendar-statistics`, - { - method: 'GET', - } - ); - - return response.data as StatisticsCalendarBasicData; -}; - -export const useCalendarStatisticsQuery = () => { - return useQuery({ - queryKey: [STATISTICS_CALENDAR_KEY], - queryFn: getCalendarStatistics, - }); -}; - -// Tasks -// Tasks - Basics -export const STATISTICS_TASKS_KEY = 'statistics:tasks'; - -export const getTasksStatistics = async () => { - const response = await fetchJson>( - `${API_URL}/api/v1/tasks-statistics`, - { - method: 'GET', - } - ); - - return response.data as StatisticsTasksBasicData; -}; - -export const useTasksStatisticsQuery = () => { - return useQuery({ - queryKey: [STATISTICS_TASKS_KEY], - queryFn: getTasksStatistics, - }); -}; - -// Tasks - Tasks Created Map -export const STATISTICS_TASKS_DATE_COUNT_MAP_KEY = 'statistics:tasks:tasks-created'; - -export const getTasksStatisticsTasksCreated = async (from?: Date, to?: Date) => { - const url = new URL(`${API_URL}/api/v1/tasks-statistics/tasks-created`); - - if (from) { - url.searchParams.append('from', from.toISOString()); - } - - if (to) { - url.searchParams.append('to', to.toISOString()); - } - - const response = await fetchJson>(url.toString(), { - method: 'GET', - }); - - return response.data as StatisticsTasksBasicData; -}; - -export const useTasksStatisticsTasksCreatedQuery = ({ from, to }: { from?: Date; to?: Date }) => { - const queryKey = [STATISTICS_TASKS_DATE_COUNT_MAP_KEY, from, to]; - const queryFn = () => getTasksStatisticsTasksCreated(from, to); - - return useQuery({ - queryKey, - queryFn, - }); -}; - -// Notes -export const STATISTICS_NOTES_KEY = 'statistics:notes'; - -export const getNotesStatistics = async () => { - const response = await fetchJson>( - `${API_URL}/api/v1/notes-statistics`, - { - method: 'GET', - } - ); - - return response.data as StatisticsNotesBasicData; -}; - -export const useNotesStatisticsQuery = () => { - return useQuery({ - queryKey: [STATISTICS_NOTES_KEY], - queryFn: getNotesStatistics, - }); -}; - -// Mood -export const STATISTICS_MOOD_KEY = 'statistics:mood'; - -export const getMoodStatistics = async () => { - const response = await fetchJson>( - `${API_URL}/api/v1/mood-statistics`, - { - method: 'GET', - } - ); - - return response.data as StatisticsMoodBasicData; -}; - -export const useMoodStatisticsQuery = () => { - return useQuery({ - queryKey: [STATISTICS_MOOD_KEY], - queryFn: getMoodStatistics, - }); -}; - -// Focus -export const STATISTICS_FOCUS_KEY = 'statistics:focus'; - -export const getFocusStatistics = async () => { - const response = await fetchJson>( - `${API_URL}/api/v1/focus-statistics`, - { - method: 'GET', - } - ); - - return response.data as StatisticsFocusBasicData; -}; - -export const useFocusStatisticsQuery = () => { - return useQuery({ - queryKey: [STATISTICS_FOCUS_KEY], - queryFn: getFocusStatistics, - }); -}; diff --git a/packages/web-core/src/features/statistics/hooks/StatisticsMoodHooks.ts b/packages/web-core/src/features/statistics/hooks/StatisticsMoodHooks.ts new file mode 100644 index 00000000..44243fbb --- /dev/null +++ b/packages/web-core/src/features/statistics/hooks/StatisticsMoodHooks.ts @@ -0,0 +1,31 @@ +// Need to have this, else typescript won't work... +// https://github.com/microsoft/TypeScript/issues/47663#issuecomment-1519138189 +// eslint-disable-next-line @typescript-eslint/no-unused-vars +import type { UseQueryResult } from '@tanstack/react-query'; + +import { useQuery } from '@tanstack/react-query'; + +import { API_URL, ResponseInterface, StatisticsMoodBasicData } from '@moaitime/shared-common'; + +import { fetchJson } from '../../core/utils/FetchHelpers'; + +// Mood +export const STATISTICS_MOOD_KEY = 'statistics:mood'; + +export const getMoodStatistics = async () => { + const response = await fetchJson>( + `${API_URL}/api/v1/mood-statistics`, + { + method: 'GET', + } + ); + + return response.data as StatisticsMoodBasicData; +}; + +export const useMoodStatisticsQuery = () => { + return useQuery({ + queryKey: [STATISTICS_MOOD_KEY], + queryFn: getMoodStatistics, + }); +}; diff --git a/packages/web-core/src/features/statistics/hooks/StatisticsNotesHooks.ts b/packages/web-core/src/features/statistics/hooks/StatisticsNotesHooks.ts new file mode 100644 index 00000000..e49b1e58 --- /dev/null +++ b/packages/web-core/src/features/statistics/hooks/StatisticsNotesHooks.ts @@ -0,0 +1,31 @@ +// Need to have this, else typescript won't work... +// https://github.com/microsoft/TypeScript/issues/47663#issuecomment-1519138189 +// eslint-disable-next-line @typescript-eslint/no-unused-vars +import type { UseQueryResult } from '@tanstack/react-query'; + +import { useQuery } from '@tanstack/react-query'; + +import { API_URL, ResponseInterface, StatisticsNotesBasicData } from '@moaitime/shared-common'; + +import { fetchJson } from '../../core/utils/FetchHelpers'; + +// Notes +export const STATISTICS_NOTES_KEY = 'statistics:notes'; + +export const getNotesStatistics = async () => { + const response = await fetchJson>( + `${API_URL}/api/v1/notes-statistics`, + { + method: 'GET', + } + ); + + return response.data as StatisticsNotesBasicData; +}; + +export const useNotesStatisticsQuery = () => { + return useQuery({ + queryKey: [STATISTICS_NOTES_KEY], + queryFn: getNotesStatistics, + }); +}; diff --git a/packages/web-core/src/features/statistics/hooks/StatisticsTasksHooks.ts b/packages/web-core/src/features/statistics/hooks/StatisticsTasksHooks.ts new file mode 100644 index 00000000..5786fffa --- /dev/null +++ b/packages/web-core/src/features/statistics/hooks/StatisticsTasksHooks.ts @@ -0,0 +1,63 @@ +// Need to have this, else typescript won't work... +// https://github.com/microsoft/TypeScript/issues/47663#issuecomment-1519138189 +// eslint-disable-next-line @typescript-eslint/no-unused-vars +import type { UseQueryResult } from '@tanstack/react-query'; + +import { useQuery } from '@tanstack/react-query'; + +import { API_URL, ResponseInterface, StatisticsTasksBasicData } from '@moaitime/shared-common'; + +import { fetchJson } from '../../core/utils/FetchHelpers'; + +// Tasks +// Tasks - Basics +export const STATISTICS_TASKS_KEY = 'statistics:tasks'; + +export const getTasksStatistics = async () => { + const response = await fetchJson>( + `${API_URL}/api/v1/tasks-statistics`, + { + method: 'GET', + } + ); + + return response.data as StatisticsTasksBasicData; +}; + +export const useTasksStatisticsQuery = () => { + return useQuery({ + queryKey: [STATISTICS_TASKS_KEY], + queryFn: getTasksStatistics, + }); +}; + +// Tasks - Tasks Created Map +export const STATISTICS_TASKS_DATE_COUNT_MAP_KEY = 'statistics:tasks:tasks-created'; + +export const getTasksStatisticsTasksCreated = async (from?: Date, to?: Date) => { + const url = new URL(`${API_URL}/api/v1/tasks-statistics/tasks-created`); + + if (from) { + url.searchParams.append('from', from.toISOString()); + } + + if (to) { + url.searchParams.append('to', to.toISOString()); + } + + const response = await fetchJson>(url.toString(), { + method: 'GET', + }); + + return response.data as StatisticsTasksBasicData; +}; + +export const useTasksStatisticsTasksCreatedQuery = ({ from, to }: { from?: Date; to?: Date }) => { + const queryKey = [STATISTICS_TASKS_DATE_COUNT_MAP_KEY, from, to]; + const queryFn = () => getTasksStatisticsTasksCreated(from, to); + + return useQuery({ + queryKey, + queryFn, + }); +};