Skip to content

Sphurthy - fix: Added analytics features#1917

Open
sphurthy wants to merge 3 commits into
developmentfrom
sphurthy-analytics-integration-backend
Open

Sphurthy - fix: Added analytics features#1917
sphurthy wants to merge 3 commits into
developmentfrom
sphurthy-analytics-integration-backend

Conversation

@sphurthy

@sphurthy sphurthy commented Nov 21, 2025

Copy link
Copy Markdown

Description

image

Related PRS (if any):

Frontend: OneCommunityGlobal/HighestGoodNetworkApp#4482

Main changes explained:

  • Created src/models/studentMetrics.js: added schema for per-student analytics metrics (averageScore, totalTimeSpentMinutes, engagementRate, completionRate, assessmentsTaken, lastUpdated).
  • Created src/services/analyticsService.js: logic to compute, cache, and retrieve analytics metrics from FormResponse data.
  • Created src/controllers/analyticsController.js: endpoints for GET /analytics/overview and GET /analytics/student/:studentId with optional ?force=true.
  • Created src/routes/analyticsRouter.js: registered analytics endpoints.
  • Created src/jobs/studentMetricsJob.js: scheduled daily job to refresh student metrics.
  • Updated src/startup/routes.js: registered analyticsRouter at /api/analytics.
  • Updated src/server.js: scheduled the student metrics job on startup.

How to test:

  1. Checkout branch.
  2. Run npm install.
  3. Start the server.
  4. Log in via POST /api/login and copy the JWT.
  5. Insert a FormResponse for a test student if needed.
  6. Get overview: GET /api/analytics/overview with Authorization.
  7. Get student metrics: GET /api/analytics/student/{studentId} with Authorization.
  8. Force refresh: GET /api/analytics/student/{studentId}?force=true with Authorization.
  9. Check MongoDB for studentMetrics collection and verify metrics are updated.

Screenshots or videos of changes:

image

@VijayAnirudh VijayAnirudh left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello!

  1. "Get overview: GET /api/analytics/overview with Authorization.
    Get student metrics: GET /api/analytics/student/{studentId} with Authorization.
    Force refresh: GET /api/analytics/student/{studentId}?force=true with Authorization."

These APIs are working fine!

  1. I couldn't understand what it meant to do: "Insert a FormResponse for a test student if needed." That you want me to add a student to metrics via POST? If so, please mention related API endpoint.

  2. As per "Check MongoDB for studentMetrics collection and verify metrics are updated," you did not mention how to update metrics. I can't verify the metrics without updating them. Please provide an API endpoint to update metrics.

image image

@sphurthy sphurthy changed the title fix: Added analytics features Sphurthy - fix: Added analytics features Apr 23, 2026
@one-community one-community added the High Priority - Please Review First This is an important PR we'd like to get merged as soon as possible label Jun 18, 2026

@kzou55 kzou55 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Spurthy, I ran the current branch locally.

I can verify the following API response works:

  • /api/analytics/overview

    Image

Issues:

  • Could you clarify why the two APIs: /api/analytics/student/{studentId} and /api/analytics/student/{studentId}?force=true add to the student metric collections in MongoDB? Aren’t these GET requests, not POST requests?

    • /api/analytics/student/{studentId}

      • Postman
        Image

      • Before
        Image

      • After (the total amount of data in the collection increases)
        Image

    • /api/analytics/student/{studentId}?force=true

      • Postman
        Image

      • Before
        Image

      • After (the total amount of data in the collection increases)
        Image

  • Also I wasn't sure what 5. Insert a FormResponse for a test student if needed, meant. Could you clarify and give the associated api call for that.

sphurthy and others added 2 commits July 6, 2026 19:26
…docs

Addresses PR review: GET /api/analytics/student/:studentId was growing the
studentMetrics collection on each call. Without a unique index the
findOneAndUpdate upsert could insert duplicate documents for the same
student. A unique index guarantees exactly one metrics doc per student, so
repeated GET requests update in place instead of adding rows.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…tics-integration-backend

# Conflicts:
#	package-lock.json
#	src/startup/routes.js
@sonarqubecloud

sonarqubecloud Bot commented Jul 7, 2026

Copy link
Copy Markdown

@HemanthNidamanuru HemanthNidamanuru left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Sphurthy,

I tested the analytics APIs locally.

The following APIs are working fine:

GET /api/analytics/overview
GET /api/analytics/student/{studentId}
GET /api/analytics/student/{studentId}?force=true

I was not clear about the step “Insert a FormResponse for a test student if needed.” If this needs to be done through an API, please provide the related endpoint and sample payload.

Also, for MongoDB verification, please mention how the metrics should be updated and which fields need to be checked in the studentMetrics collection.

Image Image Image

@DeepighaJ DeepighaJ left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested the API in postman. The below are working fine
GET /api/analytics/overview
GET /api/analytics/student/{studentId}
GET /api/analytics/student/{studentId}?force=true
I dint have more information on Insert a formresponse and metric updating verification in MongoDB so couldn't verify those two.

Image Image Image

@sphurthy

Copy link
Copy Markdown
Author

Thanks for testing @DeepighaJ (and @HemanthNidamanuru / @kzou55 / @VijayAnirudh, same answer for all of you 🙏). Clarifying the two confusing steps:

  1. There's no separate "update metrics" endpoint, by design. Metrics are computed on demand from FormResponse documents and cached in the studentMetrics collection. GET /api/analytics/student/{studentId} recomputes and upserts that cached doc, which is why the collection changes on a GET. Repeat calls now update the same doc instead of duplicating (fixed in d24634b, unique studentId).

  2. "Insert a FormResponse" just means seeding source data. The metrics read numeric answers from FormResponse, and our form API only accepts radio/checkbox/text answers, so for testing, insert directly into the formresponses collection:

db.formresponses.insertOne({
formID: "quiz-1",
submittedBy: "test-student-1",
responses: [
{ questionLabel: "Quiz score", answer: 85 },
{ questionLabel: "Time spent", answer: 12 }
],
submittedAt: new Date()
})
Numeric answers feed averageScore; a question labeled like "time" feeds totalTimeSpentMinutes.

To verify:

Insert one or more FormResponse docs for test-student-1 (use the same value in {studentId} below).
GET /api/analytics/student/test-student-1?force=true (force skips the 1‑hour cache).
Check the studentMetrics doc for that studentId, metrics.averageScore, assessmentsTaken, completionRate, engagementRate, totalTimeSpentMinutes should now be populated.
GET /api/analytics/overview aggregates those cached metrics across all students.

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

Labels

High Priority - Please Review First This is an important PR we'd like to get merged as soon as possible

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants