-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add most popular courses statistics (#257)
* feat: add most popular courses statistics * feat: apply feedback, renaming of functions
- Loading branch information
1 parent
2c37a79
commit 121a8fc
Showing
13 changed files
with
2,296 additions
and
33 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Controller, Get, UseGuards } from "@nestjs/common"; | ||
import { Validate } from "nestjs-typebox"; | ||
|
||
import { baseResponse, BaseResponse, UUIDType } from "src/common"; | ||
import { Roles } from "src/common/decorators/roles.decorator"; | ||
import { CurrentUser } from "src/common/decorators/user.decorator"; | ||
import { RolesGuard } from "src/common/guards/roles.guard"; | ||
import { USER_ROLES } from "src/users/schemas/user-roles"; | ||
|
||
import { TeacherStatsSchema, UserStatsSchema } from "./schemas/userStats.schema"; | ||
import { StatisticsService } from "./statistics.service"; | ||
|
||
import type { TeacherStats, UserStats } from "./schemas/userStats.schema"; | ||
|
||
@UseGuards(RolesGuard) | ||
@Controller("statistics") | ||
export class StatisticsController { | ||
constructor(private statisticsService: StatisticsService) {} | ||
|
||
@Get("user-stats") | ||
@Validate({ | ||
response: baseResponse(UserStatsSchema), | ||
}) | ||
async getUserStatistics( | ||
@CurrentUser("userId") currentUserId: UUIDType, | ||
): Promise<BaseResponse<UserStats>> { | ||
return new BaseResponse(await this.statisticsService.getUserStats(currentUserId)); | ||
} | ||
|
||
@Get("teacher-stats") | ||
@Roles(USER_ROLES.admin, USER_ROLES.tutor) | ||
@Validate({ | ||
response: baseResponse(TeacherStatsSchema), | ||
}) | ||
async getTeacherStats( | ||
@CurrentUser("userId") currentUserId: UUIDType, | ||
): Promise<BaseResponse<TeacherStats>> { | ||
return new BaseResponse(await this.statisticsService.getTeacherStats(currentUserId)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...pi/src/storage/migrations/0012_create_materialized_view_course_statistics_per_teacher.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
ALTER TABLE "student_lessons_progress" ADD COLUMN "completed_as_freemium" boolean DEFAULT false NOT NULL; | ||
|
||
CREATE MATERIALIZED VIEW | ||
"course_statistics_per_teacher" AS | ||
SELECT | ||
c.author_id AS teacher_id, | ||
c.id AS course_id, | ||
COUNT(sc.id) AS student_count, | ||
SUM( | ||
CASE | ||
WHEN sc.state = 'completed' THEN 1 | ||
ELSE 0 | ||
END | ||
) AS completed_student_count, | ||
COUNT( | ||
DISTINCT CASE | ||
WHEN slp.completed_as_freemium = true THEN sc.student_id | ||
ELSE NULL | ||
END | ||
) AS purchased_after_freemium_count | ||
FROM | ||
courses c | ||
LEFT JOIN student_courses sc ON c.id = sc.course_id | ||
LEFT JOIN student_lessons_progress slp ON c.id = slp.course_id | ||
AND slp.completed_at IS NOT NULL | ||
GROUP BY | ||
c.author_id, | ||
c.id; |
Oops, something went wrong.