Fhocutt has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/181770

Change subject: Add user names to json reports, add corresponding test
......................................................................

Add user names to json reports, add corresponding test

* Add a method to prepend a user's username to the identifying string
  in the json version of a report so that it's easy to recognize users
  by glancing through the data (when individual data is requested).
* Add test_report_result_json to the test suite. It checks the json
  report for the properly-formatted user data string.
* Fixed incorrect docstrings--the user_names dict is keyed by
  WikiUserKeys, not tuples.

Comments appreciated, not ready to merge. Need to (at least) get the
commented-out conditional working and take out print/debug statements.

Bug: T74747
Change-Id: I5eca5c942e27abfe3b44daf983a11a81f7916782
---
M tests/test_controllers/test_reports.py
M wikimetrics/controllers/reports.py
2 files changed, 86 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/analytics/wikimetrics 
refs/changes/70/181770/1

diff --git a/tests/test_controllers/test_reports.py 
b/tests/test_controllers/test_reports.py
index b50f8c7..2b905e8 100644
--- a/tests/test_controllers/test_reports.py
+++ b/tests/test_controllers/test_reports.py
@@ -474,6 +474,61 @@
         cohort_size = 'Cohort Size,{0}'.format(len(self.cohort))
         assert_true(response.data.find(cohort_size) >= 0)
 
+    def test_report_result_json(self):
+        desired_responses = [{
+            'name': 'Edits - test',
+            'cohort': {
+                'id': self.cohort.id,
+                'name': self.cohort.name,
+            },
+            'metric': {
+                'name': 'NamespaceEdits',
+                'timeseries': 'month',
+                'namespaces': [0, 1, 2],
+                'start_date': '2013-01-01 00:00:00',
+                'end_date': '2013-05-01 00:00:00',
+                'individualResults': True,
+                'aggregateResults': True,
+                'aggregateSum': False,
+                'aggregateAverage': True,
+                'aggregateStandardDeviation': False,
+            },
+        }]
+        json_to_post = json.dumps(desired_responses)
+
+        response = self.client.post('/reports/create/', data=dict(
+            responses=json_to_post
+        ))
+
+        # Wait a second for the task to get processed
+        time.sleep(1)
+
+        # Check that the task has been created
+        response = self.client.get('/reports/list/')
+        parsed = json.loads(response.data)
+        result_key = parsed['reports'][-1]['result_key']
+        task, report = get_celery_task(result_key)
+
+        response = 
self.client.get('/reports/result/{0}.json'.format(result_key))
+
+       # Check for presence of old user id string
+        assert_true(response.data.find(
+            '{0}|{1}|{2}'.format(
+                self.editors[0].user_id,
+                mediawiki_project, self.cohort.id)
+            ) >= 0)
+       # Check that user names are included
+        assert_true(response.data.find(
+            '{0}|{1}|{2}|{3}'.format(
+                self.editors[0].user_name, self.editors[0].user_id,
+                mediawiki_project, self.cohort.id)
+            ) >= 0)
+        assert_true(response.data.find(
+            '{0}|{1}|{2}|{3}'.format(
+                self.editors[1].user_name, self.editors[1].user_id,
+                mediawiki_project, self.cohort.id)
+            ) >= 0)
+
     @raises(InvalidCohort)
     def test_report_does_not_run_on_invalid_cohort(self):
 
diff --git a/wikimetrics/controllers/reports.py 
b/wikimetrics/controllers/reports.py
index 3ef7c6f..91e10d8 100644
--- a/wikimetrics/controllers/reports.py
+++ b/wikimetrics/controllers/reports.py
@@ -179,7 +179,7 @@
     Parameters
         task_result : the result dictionary from Celery
     Returns
-         user_names : dictionary of user names (keyed by (user_id, project))
+         user_names : dictionary of user names (keyed by WikiUserKey)
                       empty if results are not detailed by user
 
     TODO: this function should move outside the controller,
@@ -297,7 +297,7 @@
         task_result : the result dictionary from Celery
         pj          : a pointer to the permanent job
         parameters  : a dictionary of pj.parameters
-        user_names  : dictionary of user names (keyed by (user_id, project))
+        user_names  : dictionary of user names (keyed by WikiUserKey)
 
     Returns
         A StringIO instance representing simple CSV
@@ -379,11 +379,38 @@
     if celery_task.ready() and celery_task.successful():
         result = celery_task.get()
         json_result = pj.get_json_result(result)
-
-        return json_response(json_result)
+#        if Aggregation.IND in result[result_key]: #TODO: add check once the 
rest works
+        print(result) #TODO: take these out
+        user_names = get_usernames_for_task_result(result[result_key])
+        print(user_names)
+        json_result_with_names = add_user_names_to_json(json_result,
+                                                            user_names)
+        print(json_result_with_names)
+        return json_response(json_result_with_names)
+#        else:
+#            return json_response(json_result)
     else:
         return json_response(status=celery_task.status)
 
+def add_user_names_to_json(json_result, user_names):
+    """
+    Parameters
+        json_result : the result dictionary from pj.get_json_result
+        user_names  : dictionary of user names (keyed by WikiUserKey)
+    Returns
+        The result dict, with user names added to the WikiUserKey id strings
+    """
+    new_individual_ids = {}
+    for individual in json_result['result']['Individual Results']:
+        user_name = user_names[WikiUserKey.fromstr(individual)]
+        new_id_string = '{}|{}'.format(user_name, individual)
+        new_individual_ids[individual] = new_id_string
+
+    json_with_names = json_result.deepcopy()
+    json_with_names['result']['Individual Results'] = {new_individual_ids[key]:
+        value for (key, value)
+        in json_result['result']['Individual Results'].items()}
+    return json_with_names
 
 #@app.route('/reports/kill/<result_key>')
 #def report_kill(result_key):

-- 
To view, visit https://gerrit.wikimedia.org/r/181770
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5eca5c942e27abfe3b44daf983a11a81f7916782
Gerrit-PatchSet: 1
Gerrit-Project: analytics/wikimetrics
Gerrit-Branch: master
Gerrit-Owner: Fhocutt <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to