Repository: learningequality/kolibri

Issue:
Channel Name & Content Title is null in content_summary_logs.csv
<!--
Instructions:
 * Fill out the sections below, replace …'s with information about your issue
 * Use the 'preview' function above this text box to verify formatting before submitting
-->

## Observed behavior
<!--
Description of the behavior that was observed, including screenshots or other references when applicable
-->
Channel Name & Content title seems to be missing in kolibri v15.6 in content_summary_logs.csv report. For previous version
it seems to be there but not sure what happened with newer version?

## Errors and logs
<!--
Relevant logs from:
 * the command line
 * ~/.kolibri/logs/kolibri.txt
 * the browser console

Please wrap errors in triple backticks for clean formatting like this:

01:10 info: something happened
01:12 error: something bad happened
```
-->


## Expected behavior
<!--
Description of what behavior was expected but did not occur
-->
content_summary_logs.csv must have "Content title" so that admin can see the time spent by the user on any specific item.
…

## User-facing consequences
<!--
Implications and real-world consequences for learners, coaches, admins, and other users of the application
-->
learner

## Steps to reproduce
<!--
Precise steps that someone else can follow in order to see this behavior
-->
1. Generate "content_summary_logs.csv".
2. Check content_summary_logs.csv 
3. You will see Channel Name & Content Title is empty.

## Context
<!--
Tell us about your environment, including:
 * Kolibri version
 * Operating system
 * Browser
-->
Kolibri: v15.x+



Discussion:
Hi @hani647 - thanks for reporting this. We've had a similar report from a user on our community forums: https://community.learningequality.org/t/kolibri-session-log-files-empty-system-glitches/2513/8 but have so far been unable to replicate the error in our local testing.

I am wondering if there's anything specific to your context that we might be missing? What language are the Channel titles and Resource titles in? What script are they using? Is there anything else that might be relevant that we may be missing?
Also - do you happen to know what Python version is being used to run Kolibri?
Hey @rtibbles ,
Thanks for the prompt response, I'm using Python 2.7.18.
Content title & Channel name are in English. We're using below endpoint to generate content_summary_logs.csv on Kolibri server and that causing the issue that's mentioned in your forum issue too.

/api/tasks/tasks/startexportlogcsv/
@rtibbles its worth to mention here that same code works well with kolibri v13.x and that column empty issue only exist in v15.x.

Resolving patch:
diff --git a/kolibri/core/auth/csv_utils.py b/kolibri/core/auth/csv_utils.py
--- a/kolibri/core/auth/csv_utils.py
+++ b/kolibri/core/auth/csv_utils.py
@@ -97,9 +97,6 @@ def replace_multiple_classrooms(field, obj):
 )
 
 
-map_output = partial(output_mapper, labels=labels, output_mappings=output_mappings)
-
-
 input_fields = (
     "full_name",
     "username",
@@ -181,6 +178,14 @@ def csv_file_generator(facility, filepath, overwrite=True, demographic=False):
 
     csv_file = open_csv_for_writing(filepath)
 
+    mappings = {}
+
+    for key in output_mappings:
+        if demographic or key not in DEMO_FIELDS:
+            mappings[key] = output_mappings[key]
+
+    map_output = partial(output_mapper, labels=labels, output_mappings=mappings)
+
     with csv_file as f:
         writer = csv.DictWriter(f, header_labels)
         logger.info("Creating csv file {filename}".format(filename=filepath))
diff --git a/kolibri/core/utils/csv.py b/kolibri/core/utils/csv.py
--- a/kolibri/core/utils/csv.py
+++ b/kolibri/core/utils/csv.py
@@ -1,3 +1,5 @@
+from __future__ import unicode_literals
+
 import io
 import re
 import sys
@@ -35,12 +37,14 @@ def sanitize(value):
     return value
 
 
-def output_mapper(obj, labels=None, output_mappings=None):
+def output_mapper(obj, labels=None, output_mappings=None, exclude_fields=None):
+    if exclude_fields is None:
+        exclude_fields = set()
     mapped_obj = {}
     labels = labels or {}
     output_mappings = output_mappings or {}
     for header, label in labels.items():
-        if header in output_mappings and header in obj:
+        if header in output_mappings:
             mapped_obj[label] = sanitize(output_mappings[header](obj))
         elif header in obj:
             mapped_obj[label] = sanitize(obj[header])


Test patch:
diff --git a/kolibri/core/auth/test/test_api.py b/kolibri/core/auth/test/test_api.py
--- a/kolibri/core/auth/test/test_api.py
+++ b/kolibri/core/auth/test/test_api.py
@@ -5,6 +5,7 @@
 import base64
 import collections
 import sys
+import time
 import uuid
 from datetime import datetime
 from importlib import import_module
@@ -1104,6 +1105,7 @@ def test_session_update_last_active(self):
             format="json",
         )
         expire_date = self.client.session.get_expiry_date()
+        time.sleep(0.01)
         self.client.get(
             reverse("kolibri:core:session-detail", kwargs={"pk": "current"})
         )
diff --git a/kolibri/core/public/test/test_api.py b/kolibri/core/public/test/test_api.py
--- a/kolibri/core/public/test/test_api.py
+++ b/kolibri/core/public/test/test_api.py
@@ -771,6 +771,7 @@ def test_update_full_queue_should_queue(self):
             user_id=self.learner.id, instance_id=self.instance_id, keep_alive=10
         )
         old_updated = queue.updated
+        time.sleep(0.01)
         response = self.client.put(
             reverse("kolibri:core:syncqueue-detail", kwargs={"pk": queue.id}),
             data={"user": self.learner.id, "instance": self.instance_id},