Skip to content

Commit

Permalink
Merge pull request #6113 from OCHA-DAP/bugfix/HDX-9238-mixpanel-time-…
Browse files Browse the repository at this point in the history
…not-tracked-qa-set-complete-not-sysadmin

HDX-9238 time not tracked on "qa set complete" MixPanel event, if triggering user is not sysadmin
  • Loading branch information
danmihaila authored Oct 13, 2023
2 parents f3ed89e + b048e5e commit 93f9379
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
5 changes: 3 additions & 2 deletions ckanext-hdx_package/ckanext/hdx_package/helpers/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,9 @@ def __init__(self, dataset_dict, metadata_modified, mark_as_set=True):
}
}

if g.userobj.sysadmin and mark_as_set:
self.analytics_dict['mixpanel_meta']['user id'] = g.userobj.id
if mark_as_set:
if g.userobj.sysadmin:
self.analytics_dict['mixpanel_meta']['user id'] = g.userobj.id

time_difference = datetime.datetime.utcnow() - metadata_modified
self.analytics_dict['mixpanel_meta']['minutes since modified'] = int(time_difference.total_seconds()) / 60
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import six
import mock
from datetime import datetime, timedelta

import ckan.tests.factories as factories
import ckan.model as model
Expand All @@ -11,6 +12,8 @@
from ckanext.hdx_org_group.helpers.static_lists import ORGANIZATION_TYPE_LIST
from ckanext.hdx_users.helpers.permissions import Permissions

from ckanext.hdx_package.helpers.analytics import QACompletedAnalyticsSender

config = tk.config
NotAuthorized = tk.NotAuthorized

Expand Down Expand Up @@ -177,6 +180,27 @@ def test_qa_completed_with_permission_via_hdx_mark_qa_completed(self, analytics_
assert "qa_completed" in package_dict.get('package') and package_dict.get('package').get(
"qa_completed") is False

@mock.patch('ckanext.hdx_package.helpers.analytics.g')
def test_qa_completed_tracking_analytics_dict(self, g_mock):
package_dict = self._get_action('package_show')({}, {'id': self.PACKAGE_ID})
yesterday = datetime.today() - timedelta(days=1)

g_mock.userobj.sysadmin = False
user_analytics_sender = QACompletedAnalyticsSender(package_dict, yesterday, mark_as_set=True)

user_mixpanel_meta = user_analytics_sender.analytics_dict.get('mixpanel_meta', {})

assert 'minutes since modified' in user_mixpanel_meta, "minutes since modified should be present"
assert 'user id' not in user_mixpanel_meta, "user id should not be present for non-sysadmin"

g_mock.userobj.sysadmin = True
sysadmin_analytics_sender = QACompletedAnalyticsSender(package_dict, yesterday, mark_as_set=True)

sysadmin_mixpanel_meta = sysadmin_analytics_sender.analytics_dict.get('mixpanel_meta', {})

assert 'minutes since modified' in sysadmin_mixpanel_meta, "minutes since modified should be present"
assert 'user id' in sysadmin_mixpanel_meta, "user id should be present for sysadmin"

def _package_patch_qa_completed_flag(self, package_id, qa_completed, user):
context = {
'model': model,
Expand Down

0 comments on commit 93f9379

Please sign in to comment.