Skip to content

Commit

Permalink
Merge pull request #1895 from onaio/1891_analytics
Browse files Browse the repository at this point in the history
Update submission metrics collection
  • Loading branch information
DavisRayM authored Dec 14, 2020
2 parents 62a238e + e93f3b4 commit 62c22d7
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 133 deletions.
20 changes: 17 additions & 3 deletions onadata/libs/serializers/data_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def validate(self, attrs):
properties={
'submitted_by': 'user',
'xform_id': 'xform__pk',
'project_id': 'xform__project__pk',
'organization': 'xform__user__profile__organization'},
additional_context={'from': 'XML Submissions'}
)
Expand Down Expand Up @@ -318,6 +319,7 @@ def validate(self, attrs):
properties={
'submitted_by': 'user',
'xform_id': 'xform__pk',
'project_id': 'xform__project__pk',
'organization': 'xform__user__profile__organization'},
additional_context={'from': 'JSON Submission'}
)
Expand Down Expand Up @@ -347,7 +349,11 @@ class RapidProSubmissionSerializer(BaseRapidProSubmissionSerializer):
"""
@track_object_event(
user_field='xform__user',
properties={'submitted_by': 'user', 'xform_id': 'xform__pk'},
properties={
'submitted_by': 'user',
'xform_id': 'xform__pk',
'project_id': 'xform__project__pk',
},
additional_context={'from': 'RapidPro'}
)
def create(self, validated_data):
Expand All @@ -369,7 +375,11 @@ class RapidProJSONSubmissionSerializer(BaseRapidProSubmissionSerializer):
"""
@track_object_event(
user_field='xform__user',
properties={'submitted_by': 'user', 'xform_id': 'xform__pk'},
properties={
'submitted_by': 'user',
'xform_id': 'xform__pk',
'project_id': 'xform__project__pk',
},
additional_context={'from': 'RapidPro(JSON)'}
)
def create(self, validated_data):
Expand All @@ -392,7 +402,11 @@ class FLOIPListSerializer(serializers.ListSerializer):
"""
@track_object_event(
user_field='xform__user',
properties={'submitted_by': 'user', 'xform_id': 'xform__pk'},
properties={
'submitted_by': 'user',
'xform_id': 'xform__pk',
'project_id': 'xform__project__pk',
},
additional_context={'from': 'FLOIP'}
)
def create(self, validated_data):
Expand Down
8 changes: 8 additions & 0 deletions onadata/libs/serializers/project_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
DataViewMinimalSerializer
from onadata.libs.serializers.fields.json_field import JsonField
from onadata.libs.serializers.tag_list_serializer import TagListSerializer
from onadata.libs.utils.analytics import track_object_event
from onadata.libs.utils.cache_tools import (
PROJ_BASE_FORMS_CACHE, PROJ_FORMS_CACHE, PROJ_NUM_DATASET_CACHE,
PROJ_PERM_CACHE, PROJ_SUB_DATE_CACHE, PROJ_TEAM_USERS_CACHE,
Expand Down Expand Up @@ -438,6 +439,13 @@ def update(self, instance, validated_data):

return instance

@track_object_event(
user_field='created_by',
properties={
'created_by': 'created_by',
'project_id': 'pk',
'project_name': 'name'}
)
def create(self, validated_data):
metadata = validated_data.get('metadata', dict())
if metadata is None:
Expand Down
6 changes: 6 additions & 0 deletions onadata/libs/serializers/user_profile_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from onadata.libs.permissions import CAN_VIEW_PROFILE, is_organization
from onadata.libs.serializers.fields.json_field import JsonField
from onadata.libs.utils.cache_tools import IS_ORG
from onadata.libs.utils.analytics import track_object_event
from onadata.libs.utils.email import (
get_verification_url, get_verification_email_data
)
Expand Down Expand Up @@ -229,6 +230,11 @@ def update(self, instance, validated_data):

return super(UserProfileSerializer, self).update(instance, params)

@track_object_event(
user_field='user',
properties={
'name': 'name',
'country': 'country'})
def create(self, validated_data):
params = validated_data
request = self.context.get('request')
Expand Down
106 changes: 44 additions & 62 deletions onadata/libs/tests/utils/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@

from django.contrib.auth.models import User, AnonymousUser
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.conf import settings
from django.test import override_settings

import onadata.libs.utils.analytics
from onadata.apps.api.tests.viewsets.test_abstract_viewset import \
TestAbstractViewSet
from onadata.apps.api.viewsets.xform_submission_viewset import \
XFormSubmissionViewSet
from onadata.libs.utils.analytics import get_user_id, sanitize_metric_values
from onadata.libs.utils.analytics import get_user_id


class TestAnalytics(TestAbstractViewSet):
Expand All @@ -31,7 +32,7 @@ def test_get_user_id(self):
self.assertTrue(len(user2.email) > 0)
self.assertEqual(get_user_id(user2), user2.email)

@override_settings(SEGMENT_WRITE_KEY='123', HOSTNAME='test-server')
@override_settings(SEGMENT_WRITE_KEY='123')
def test_track(self):
"""Test analytics.track() function.
"""
Expand All @@ -46,55 +47,39 @@ def test_track(self):
user1.username,
'testing track function',
{'value': 1},
{'source': 'test-server'})

def test_sanitize_metric_values(self):
"""Test sanitize_metric_values()"""
expected_output = {
'action_from': 'XML_Submissions',
'event_by': 'bob',
'ip': '18.203.134.101',
'path': '/enketo/1814/submission',
'source': 'onadata-ona-stage',
'url': 'https://stage-api.ona.io/enketo/1814/submission',
'userId': 1,
'xform_id': 1}
context = {
'action_from': 'XML Submissions',
'event_by': 'bob',
'ip': '18.203.134.101',
'library': {
'name': 'analytics-python',
'version': '1.2.9'
},
'organization': '',
'path': '/enketo/1814/submission',
'source': 'onadata-ona-stage',
'url': 'https://stage-api.ona.io/enketo/1814/submission',
'userId': 1,
'xform_id': 1}

self.assertEqual(sanitize_metric_values(context), expected_output)
{'page': {}, 'campaign': {}, 'active': True})

@override_settings(
SEGMENT_WRITE_KEY='123', HOSTNAME='test-server',
APPOPTICS_API_TOKEN='123')
SEGMENT_WRITE_KEY='123')
def test_submission_tracking(self):
"""Test that submissions are tracked"""
segment_mock = MagicMock()
appoptics_mock = MagicMock()
onadata.libs.utils.analytics.segment_analytics = segment_mock
onadata.libs.utils.analytics.init_analytics()
self.assertEqual(segment_mock.write_key, '123')

# Test out that the track_object_event decorator
# Tracks created submissions
# Tracks created submissions, XForms and Projects
view = XFormSubmissionViewSet.as_view({
'post': 'create',
'head': 'create'
})
self._publish_xls_form_to_project()
onadata.libs.utils.analytics.appoptics_api = appoptics_mock
segment_mock.track.assert_called_with(
'[email protected]',
'XForm created',
{
'created_by': self.xform.user,
'xform_id': self.xform.pk,
'xform_name': self.xform.title,
'from': 'Publish XLS Form',
'value': 1
},
{
'page': {},
'campaign': {},
'active': True
})
s = self.surveys[0]
media_file = "1335783522563.jpg"
path = os.path.join(self.main_directory, 'fixtures',
Expand All @@ -110,6 +95,13 @@ def test_submission_tracking(self):
data = {'xml_submission_file': sf, 'media_file': f}
request = self.factory.post(request_path, data)
request.user = AnonymousUser()
request.META['HTTP_DATE'] = '2020-09-10T11:56:32.424726+00:00'
request.META['HTTP_REFERER'] = settings.HOSTNAME +\
':8000'
request.META['HTTP_USER_AGENT'] =\
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit'\
'/537.36 (KHTML, like Gecko) Chrome'\
'/83.0.4103.61 Safari/537.36'
response = view(request, username=self.user.username)
self.assertContains(response, 'Successful submission',
status_code=201)
Expand All @@ -128,35 +120,25 @@ def test_submission_tracking(self):
'Submission created',
{
'xform_id': self.xform.pk,
'project_id': self.xform.project.pk,
'organization': 'Bob Inc.',
'from': 'XML Submissions',
'from': 'Submission collected from Enketo',
'label': f'form-{form_id}-owned-by-{username}',
'value': 1,
'event_by': 'anonymous'
},
{
'source': 'test-server',
'organization': 'Bob Inc.',
'event_by': 'anonymous',
'action_from': 'XML Submissions',
'xform_id': self.xform.pk,
'path': f'/{username}/submission',
'url': f'http://testserver/{username}/submission',
'ip': '127.0.0.1',
'userId': self.user.id
})

appoptics_mock.submit_measurement.assert_called_with(
'Submission created',
1,
tags={
'source': 'test-server',
'event_by': 'anonymous',
'organization': 'Bob_Inc.',
'action_from': 'XML_Submissions',
'xform_id': self.xform.pk,
'path': f'/{username}/submission',
'url': f'http://testserver/{username}/submission',
{'page': {
'path': '/bob/submission',
'referrer': settings.HOSTNAME + ':8000',
'url': 'http://testserver/bob/submission'
},
'campaign': {
'source': settings.HOSTNAME},
'active': True,
'ip': '127.0.0.1',
'userId': self.user.id
})
'userId': self.xform.user.pk,
'receivedAt': '2020-09-10T11:56:32.424726+00:00',
'userAgent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit'
'/537.36 (KHTML, like Gecko) Chrome'
'/83.0.4103.61 Safari/537.36'}
)
Loading

0 comments on commit 62c22d7

Please sign in to comment.