diff --git a/server/controllers/statistics.ts b/server/controllers/statistics.ts index 3b493f5ca..7286c89dd 100644 --- a/server/controllers/statistics.ts +++ b/server/controllers/statistics.ts @@ -2,12 +2,20 @@ import type { Request } from 'express'; import { Classroom } from '../entities/classroom'; import { - getClassroomsInfos, getConnectedClassroomsCount, - getContributedClassroomsCount, getRegisteredClassroomsCount, + getChildrenCodesCountForClassroom, + getConnectedFamiliesCountForClassroom, + getFamiliesWithoutAccountForClassroom, normalizeForCountry, } from '../stats/classroomStats'; +import { + getChildrenCodesCountForGlobal, + getConnectedFamiliesCountForGlobal, + getFamiliesWithoutAccountForGlobal, + getFamilyAccountsCountForGlobal, + getFloatingAccountsForGlobal, +} from '../stats/globalStats'; import { getAverageConnections, getAverageDuration, @@ -19,6 +27,13 @@ import { getMinDuration, getUserConnectionsList, } from '../stats/sessionStats'; +import { + getChildrenCodesCountForVillage, + getConnectedFamiliesCountForVillage, + getFamiliesWithoutAccountForVillage, + getFamilyAccountsCountForVillage, + getFloatingAccountsForVillage, +} from '../stats/villageStats'; import { AppDataSource } from '../utils/data-source'; import { Controller } from './controller'; @@ -66,6 +81,24 @@ statisticsController.get({ path: '/sessions' }, async (req: Request, res) => { } }); +statisticsController.get({ path: '/sessions/:phase' }, async (req: Request, res) => { + // const phase = req.params.phase ? parseInt(req.params.phase) : null; + + res.sendJSON({ + minDuration: await getMinDuration(), // TODO - add phase + maxDuration: await getMaxDuration(), // TODO - add phase + averageDuration: await getAverageDuration(), // TODO - add phase + medianDuration: await getMedianDuration(), // TODO - add phase + minConnections: await getMinConnections(), // TODO - add phase + maxConnections: await getMaxConnections(), // TODO - add phase + averageConnections: await getAverageConnections(), // TODO - add phase + medianConnections: await getMedianConnections(), // TODO - add phase + registeredClassroomsCount: await getRegisteredClassroomsCount(), + connectedClassroomsCount: await getConnectedClassroomsCount(), // TODO - add phase + // contributedClassroomsCount: await getContributedClassroomsCount(phase), + }); +}); + statisticsController.get({ path: '/classrooms' }, async (req, res) => { const villageId = req.query.villageId ? parseInt(req.query.villageId as string) : null; const countryCode = req.query.countryCode ? (req.query.countryCode as string) : null; @@ -180,3 +213,35 @@ statisticsController.get({ path: '/classrooms' }, async (req, res) => { // classrooms: await getClassroomsInfos(), // }); // }); + +statisticsController.get({ path: '/onevillage' }, async (_req, res) => { + res.sendJSON({ + familyAccountsCount: await getFamilyAccountsCountForGlobal(), + childrenCodesCount: await getChildrenCodesCountForGlobal(), + connectedFamiliesCount: await getConnectedFamiliesCountForGlobal(), + familiesWithoutAccount: await getFamiliesWithoutAccountForGlobal(), + floatingAccounts: await getFloatingAccountsForGlobal(), + }); +}); + +statisticsController.get({ path: '/villages/:villageId' }, async (_req, res) => { + const villageId = parseInt(_req.params.villageId); + const phase = _req.query.phase as unknown as number; + res.sendJSON({ + familyAccountsCount: await getFamilyAccountsCountForVillage(villageId, phase), + childrenCodesCount: await getChildrenCodesCountForVillage(villageId, phase), + connectedFamiliesCount: await getConnectedFamiliesCountForVillage(villageId, phase), + familiesWithoutAccount: await getFamiliesWithoutAccountForVillage(villageId), + floatingAccounts: await getFloatingAccountsForVillage(villageId), + }); +}); + +statisticsController.get({ path: '/classrooms/:classroomId' }, async (_req, res) => { + const classroomId = parseInt(_req.params.classroomId); + const phase = _req.query.phase as unknown as number; + res.sendJSON({ + childrenCodesCount: await getChildrenCodesCountForClassroom(classroomId, phase), + connectedFamiliesCount: await getConnectedFamiliesCountForClassroom(classroomId, phase), + familiesWithoutAccount: await getFamiliesWithoutAccountForClassroom(classroomId), + }); +}); diff --git a/server/stats/villageStats.ts b/server/stats/villageStats.ts index e69de29bb..8c80d12da 100644 --- a/server/stats/villageStats.ts +++ b/server/stats/villageStats.ts @@ -0,0 +1,38 @@ +import type { VillagePhase } from '../entities/village'; +import { + generateEmptyFilterParams, + getChildrenCodesCount, + getConnectedFamiliesCount, + getFamiliesWithoutAccount, + getFamilyAccountsCount, + getFloatingAccounts, +} from './queryStatsByFilters'; + +export const getChildrenCodesCountForVillage = async (villageId: number, phase: VillagePhase) => { + let filterParams = generateEmptyFilterParams(); + filterParams = { ...filterParams, villageId, phase }; + const whereClause = { clause: 'classroom.villageId = :villageId', value: { villageId } }; + return await getChildrenCodesCount(filterParams, whereClause); +}; + +export const getFamilyAccountsCountForVillage = async (villageId: number, phase: number) => { + let filterParams = generateEmptyFilterParams(); + filterParams = { ...filterParams, villageId, phase }; + return await getFamilyAccountsCount(filterParams); +}; + +export const getConnectedFamiliesCountForVillage = async (villageId: number, phase: number) => { + let filterParams = generateEmptyFilterParams(); + filterParams = { ...filterParams, villageId, phase }; + return await getConnectedFamiliesCount(filterParams); +}; + +export const getFamiliesWithoutAccountForVillage = async (villageId: number) => { + return getFamiliesWithoutAccount('classroom.villageId = :villageId', { villageId }); +}; + +export const getFloatingAccountsForVillage = async (villageId: number) => { + let filterParams = generateEmptyFilterParams(); + filterParams = { ...filterParams, villageId }; + return await getFloatingAccounts(filterParams); +};