kolibri icon indicating copy to clipboard operation
kolibri copied to clipboard

Conditions on to-many relations repeat rows in log CSV exports and `usercontentnode` `next_steps`

Open kruxarth opened this issue 2 weeks ago • 4 comments

🙂 Looking for an issue? Welcome! This issue is open for contribution. If this is the first time you’re requesting an issue, please:

  • Read Contributing guidelines carefully. Pay extra attention to Using generative AI. Pull requests and comments that don’t follow the guidelines won’t be answered.
  • Confirm that you’ve read the guidelines in your comment.

Target branch: develop

Observed behavior

In the session and summary log CSV exports, every log of a user who coaches a class appears more than once. A coach of one class gets each row twice, a coach of two classes three times. The repeated rows are identical.

GET /api/content/usercontentnode/?next_steps=true returns the right sibling of a completed resource once for each prerequisite that sibling has. A sibling with two prerequisites, neither completed, appears twice in the response.

Errors and logs

No errors. With a coach of two classes and a single summary log in the database, the export has three data rows:

Facility name,Username,User type,Channel id,Channel name,Content id,...
Rock N' Roll High School #0,user0,Class coach,6199dde6...,testing,2823ab31...,...
Rock N' Roll High School #0,user0,Class coach,6199dde6...,testing,2823ab31...,...
Rock N' Roll High School #0,user0,Class coach,6199dde6...,testing,2823ab31...,...

Expected behavior

One row per log.

next_steps=true lists a node with several prerequisites once, whether it qualifies as a sibling, through a completed prerequisite, or both.

User-facing consequences

Anyone adding up time spent or counting interactions from the export gets inflated numbers for coaches.

None today for next_steps: ContentNodeResource.fetchNextSteps has no caller. #15385 brings next_steps into use.

Steps to reproduce

CSV exports:

  1. Create a class and assign a coach to it.
  2. Make sure that coach has at least one content log (I created a ContentSummaryLog and a ContentSessionLog for the coach in a test).
  3. Export the session or summary logs, from Facility > Data or with kolibri manage exportlogs --log-type summary --start_date 2020-01-01 --end_date 2030-01-01.
  4. Count the rows for the coach.

Learners, and facility admins who coach no class, are not affected. An admin who coaches one class gets each row three times. It looks like the "User type" column added in #13624 is the cause: the When(user__roles__...) conditions in user_type_annotations (kolibri/core/logger/csv_export.py) join the roles table, so each log is repeated once per role. A class coach has a facility-level role plus one role per class.

next_steps:

  1. In one channel, create resources A and B as consecutive children of a topic, and resources C and D anywhere else in it.
  2. Add C and D to B's has_prerequisite.
  3. As a learner, create a ContentSummaryLog for A with progress=1.
  4. As that learner, GET /api/content/usercontentnode/?next_steps=true.

B is listed twice. filter_by_next_steps matches prerequisites and siblings in one OR, and the prerequisite half joins the prerequisite table: user.py#L93-L103.

Context

develop at fe298f5, SQLite, Python 3.12, Linux.

next_steps: develop at 758605b764, SQLite.

AI usage

I used Claude Code to read through the log export code, and it found this and drafted the issue. I ran the reproduction myself (a test with a coach of two classes that exports both log types) and saw the repeated rows, and I edited the text.

The next_steps site was added by Claude Code (rtibblesbot), which reproduced it with an API test on develop while working on #15043.

kruxarth avatar Nov 16 '26 15:11 kruxarth

🔍 Thanks for opening this issue! @rtibblesbot will check it against the code and comment with its findings.

What happens next:

  • The community-issue-review label stays on until the review comment is posted.
  • The comment identifies the files, lines, or commits examined so you can follow the reasoning and respond if you disagree.
  • If the check confirms the issue, @rtibblesbot may also correct the title and description. GitHub preserves the edit history, so the original text remains available.
  • @rtibblesbot's comments are LLM-generated, so please evaluate them critically.

A maintainer decides what happens next. This check does not close the issue.

learning-equality-bot avatar Nov 16 '26 15:11 learning-equality-bot

I've read the contributing guidelines, including the section on using generative AI. I'd like to work on this. Could it be assigned to me?

kruxarth avatar Nov 16 '26 15:11 kruxarth

Verdict: claim holds Evidence: kolibri/core/logger/csv_export.py#L215-L223 at fe298f5449, added in 5f52c780ec (#13624); reproduced with exportlogs on develop

  • The user_type Case filters on user__roles__isnull and user__roles__kind, which LEFT JOINs the role table, so each log row repeats once per role the user holds.
  • Reproduced with one summary log and one session log per user. Both exports gave the same counts:
User Roles Rows per log
Learner none 1
Admin admin 1
Coach of 1 class facility classroom assignable coach + 1 class coach 2
Coach of 2 classes facility classroom assignable coach + 2 class coach 3
Admin who coaches 1 class admin + facility classroom assignable coach + 1 class coach 3
  • One correction: admins who coach a class are affected too. The issue says facility admins are not.

Changes

  • Steps to reproduce: "Learners and facility admins are not affected" → only admins who coach no class are unaffected; an admin coaching one class holds three roles (admin, classroom assignable coach, class coach) and got three rows per log in the reproduction.

Written by rtibblesbot, an LLM-based coding agent.

rtibblesbot avatar Nov 16 '26 15:11 rtibblesbot

A second site of this class: GET /api/content/usercontentnode/?next_steps=true lists a node once per prerequisite it has.

  • filter_by_next_steps ORs has_prerequisite__in with the sibling match, which joins the prerequisite table: user.py#L93-L103.
  • The right sibling of a completed resource, with two prerequisites and neither completed, is returned twice.
  • No user-facing consequence today: ContentNodeResource.fetchNextSteps has no caller. #15385 brings next_steps into use.
  • #15922 fixes the CSV exports only, and closes this issue on merge.

Reproduced with an API test on develop at 758605b764, SQLite:

  1. In one channel, create resources A and B as consecutive children of a topic, and resources C and D anywhere else in it.
  2. Add C and D to B's has_prerequisite.
  3. As a learner, create a ContentSummaryLog for A with progress=1.
  4. As that learner, GET /api/content/usercontentnode/?next_steps=true.

B is listed twice. A test for it has to compare the returned ids as a list: a set hides the repeat.


Written by Claude Code (rtibblesbot), which found this while working on #15043.


Written by rtibblesbot, an LLM-based coding agent.

rtibblesbot avatar Nov 27 '26 23:11 rtibblesbot