Skip to content

Commit 760e314

Browse files
authored
Merge pull request #479 from CodingFactory-Repos/g7/feature/statistique
G7/feature/statistique
2 parents 0f44b86 + 472e6cf commit 760e314

10 files changed

Lines changed: 429 additions & 2 deletions

File tree

back-end/package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

back-end/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"@nestjs/websockets": "^9.4.2",
3636
"argon2": "^0.30.3",
3737
"axios": "^1.4.0",
38+
"chart.js": "^4.3.0",
3839
"class-transformer": "^0.5.1",
3940
"class-validator": "^0.14.0",
4041
"cookie-parser": "^1.4.6",

back-end/src/base/articles/articles.controller.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ export class ArticlesController {
5353
return res.status(201).json(article);
5454
}
5555

56+
@Get('/stats/participant')
57+
async getArticleWithMostParticipants(@Req() req: Request, @Res() res: Response) {
58+
const articleId = await this.articlesService.getArticleWithMostParticipants();
59+
return res.status(201).json(articleId);
60+
}
61+
62+
@Get('/stats/topcreateur')
63+
async getTopCreateur(@Req() req: Request, @Res() res: Response) {
64+
const articleId = await this.articlesService.getTopCreateur();
65+
return res.status(201).json(articleId);
66+
}
67+
68+
@Get('/stats/topparticipant')
69+
async getTopParticipant(@Req() req: Request, @Res() res: Response) {
70+
const articleId = await this.articlesService.getTopParticipant();
71+
return res.status(201).json(articleId);
72+
}
73+
5674
// remove participant from the array of participants in article in the database
5775
@Put('/removeParticipant/:id')
5876
async removeParticipant(@Req() req: Request, @Res() res: Response) {

back-end/src/base/articles/articles.service.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,85 @@ export class ArticlesService {
7575
return await this.articlesRepository.updateOneArticle({ _id: new ObjectId(id) }, update);
7676
}
7777

78+
async getArticleWithMostParticipants() {
79+
const res = await this.articlesRepository.articles
80+
.aggregate([
81+
{
82+
$match: {
83+
participants: { $exists: true, $ne: [] },
84+
},
85+
},
86+
{
87+
$project: {
88+
_id: 1,
89+
title: 1,
90+
participants: 1,
91+
numParticipants: { $size: '$participants' },
92+
},
93+
},
94+
{
95+
$sort: { numParticipants: -1 },
96+
},
97+
{
98+
$limit: 5,
99+
},
100+
])
101+
.toArray();
102+
return res;
103+
}
104+
async getTopCreateur() {
105+
const res = await this.articlesRepository.articles
106+
.aggregate([
107+
{
108+
$unwind: '$owner',
109+
},
110+
{
111+
$group: {
112+
_id: '$owner',
113+
firstName: { $first: '$owner.firstName' },
114+
lastName: { $first: '$owner.lastName' },
115+
count: { $sum: 1 },
116+
},
117+
},
118+
{
119+
$sort: { count: -1 },
120+
},
121+
{
122+
$limit: 10,
123+
},
124+
])
125+
.toArray();
126+
return res;
127+
}
128+
129+
async getTopParticipant() {
130+
const res = await this.articlesRepository.articles
131+
.aggregate([
132+
{
133+
$match: { type: 'Evenement' },
134+
},
135+
{
136+
$unwind: '$participants',
137+
},
138+
{
139+
$group: {
140+
_id: '$participants._id',
141+
firstName: { $first: '$participants.firstName' },
142+
lastName: { $first: '$participants.lastName' },
143+
count: { $sum: 1 },
144+
},
145+
},
146+
{
147+
$sort: { count: -1 },
148+
},
149+
{
150+
$limit: 10,
151+
},
152+
])
153+
.toArray();
154+
return res;
155+
}
156+
78157
// remove participant from the array of participants in article in the database
79158
async removeParticipant(id, queryParticipant) {
80159
queryParticipant._id = new ObjectId(queryParticipant._id);

0 commit comments

Comments
 (0)