Skip to content

Commit

Permalink
conflict server
Browse files Browse the repository at this point in the history
  • Loading branch information
dodoLaprovence committed Dec 12, 2024
1 parent 1119fcd commit a316f50
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
69 changes: 67 additions & 2 deletions server/controllers/statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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';

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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),
});
});
38 changes: 38 additions & 0 deletions server/stats/villageStats.ts
Original file line number Diff line number Diff line change
@@ -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);
};

0 comments on commit a316f50

Please sign in to comment.