Skip to content

KPI enrollment-conversion calculation issues N+1 queries per course #1534

Description

@RUKAYAT-CODER

Overview

KpiService.calculateEnrollmentConversionRate() in src/utils/masking/kpi.service.ts (around lines 113-129) loads every course and then, inside a for loop, issues two queries per course (an enrollment count and a course-view getCount). With N courses this is 2N+1 round-trips every run, and the method is triggered by an EVERY_5_MINUTES cron. This N+1 pattern scales poorly as the catalog grows.

Specifications

Features:

  • Enrollment-conversion metrics are computed with a bounded number of queries, independent of course count.

Tasks:

  • Replace the per-course loop with grouped aggregate queries: one GROUP BY courseId for enrollment counts and one for course-view counts, then compute the ratio in memory.
  • Preserve the existing gauge labels and values.

Impacted Files:

  • src/utils/masking/kpi.service.ts

Acceptance Criteria

  • The method issues a small constant number of queries regardless of the number of courses.
  • The reported conversion rates match the previous per-course computation.
  • All the CI passes
  • Star the repo

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendintermediateModerate difficulty; some context needednestjsbackendoptimizationPerformance or efficiency improvement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions