improve tracking informations on id and add tracking for search 00.00.18
authorymh <ymh.work@gmail.com>
Wed, 27 Jul 2016 15:26:23 +0200
changeset 112 2ec47574a371
parent 111 7e5704d95f41
child 113 323dfd414f9a
improve tracking informations on id and add tracking for search
server/src/assets/js/mtdc-tracking-worker.js
server/src/assets/js/mtdc-tracking.js
server/src/metaeducation/__init__.py
server/src/metaeducation/settings/__init__.py
server/src/metaeducation/static/metaeducation/js/mtdc-tracking-worker.js
server/src/metaeducation/static/metaeducation/js/mtdc-tracking.js
server/src/metaeducation/templates/renkan_edit.html
server/src/metaeducation/templates/renkan_view.html
server/src/metaeducation/tracking/messages.py
server/src/metaeducation/views/renkan.py
server/src/requirement_iri.txt
--- a/server/src/assets/js/mtdc-tracking-worker.js	Mon Jul 25 18:24:58 2016 +0200
+++ b/server/src/assets/js/mtdc-tracking-worker.js	Wed Jul 27 15:26:23 2016 +0200
@@ -12,7 +12,17 @@
     var Mtdc = root.Mtdc;
 
 
-    Mtdc.TrackingWorker = function(renkan, trackingUrl, trackingCloseUrl, registration, debounceDelay = 1000) {
+    Mtdc.TrackingWorker = function(renkan, opts){
+
+        var options = _.defaults(opts, {
+            debounceDelay: 1000,
+            trackingUriTemplates: {
+                'renkan': 'urn:mtdc:renkan:renkan:${renkan_id}',
+                'node': 'urn:mtdc:renkan:node:${renkan_id}:${node_id}',
+                'edge': 'urn:mtdc:renkan:edge:${renkan_id}:${edge_id}',
+                'view': 'urn:mtdc:renkan:view:${renkan_id}:${view_id}'
+            }
+        });
 
         function _sendTrackingInfo() {
             var trackingMessages = this.trackingMessages;
@@ -37,8 +47,8 @@
             trackingMessages: [],
             currentUser: renkan.current_user,
             renkan: renkan,
-            trackingUrl: trackingUrl,
-            trackingCloseUrl: trackingCloseUrl,
+            trackingUrl: options.trackingUrl,
+            trackingCloseUrl: options.trackingCloseUrl,
             getUUID4 : function() {
                 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
                     var r = Math.random() * 16 | 0,
@@ -47,9 +57,10 @@
                 });
             },
             _init: function() {
-                this.sendTrackingInfo = _.debounce(_.bind(_sendTrackingInfo, this), debounceDelay);
+                this.sendTrackingInfo = _.debounce(_.bind(_sendTrackingInfo, this), options.debounceDelay);
                 this.csrftoken = Cookies.get('csrftoken');
-                this.registration = registration ? registration : this.getUUID4();
+                this.registration = options.registration ? options.registration : this.getUUID4();
+                this.trackingUriTemplates = _.mapValues(options.trackingUriTemplates, _.template);
                 return this;
             },
             _pushTrackingMessage: function(msg) {
@@ -81,7 +92,12 @@
                         close: {
                             id: 'http://activitystrea.ms/schema/1.0/close',
                             display: { 'fr-FR': 'a fermé'}
+                        },
+                        search: {
+                            id: 'http://activitystrea.ms/schema/1.0/search',
+                            display: { 'fr-FR': 'a cherché'}
                         }
+
                     }[verb];
                 return {
                     actor: {
@@ -105,8 +121,11 @@
                     timestamp: timestamp.toISOString()
                 };
             },
-            _getObjectUrn: function(objectType, objectId) {
-                return 'urn:mtdc:renkan:'+ objectType + ':' + objectId;
+            _getObjectUri: function(objectType, objectId) {
+                var template = this.trackingUriTemplates[objectType];
+                var templateParams = {'renkan_id': this.renkan.project.get('id')};
+                templateParams[objectType+'_id'] = objectId;
+                return template(templateParams);
             },
             _sendNodeMsg: function(verb, nodeData, changedData, previousData) {
                 this._sendObjectMsg('node', 'http://www.w3.org/ns/activitystreams#Node', verb, nodeData, changedData, previousData);
@@ -122,7 +141,7 @@
                 var msg = this._getBaseMsg(verb);
 
                 msg.object = _.merge(msg.object, {
-                    id: this._getObjectUrn(objectType, objData._id), //TODO full url ???
+                    id: this._getObjectUri(objectType, objData._id), //TODO full url ???
                     definition: {
                         name: {
                             'fr-FR': objData.title
@@ -182,6 +201,23 @@
             updateView: function(viewData, changedData, previousData) {
                 this._sendViewMsg('update', viewData, changedData, previousData);
             },
+            searchProject: function(searchData) {
+                var msg = this._getBaseMsg('search');
+
+                msg.object = _.merge(msg.object, {
+                    id: this._getObjectUri('renkan', this.renkan.project.get('id')),
+                    definition: {
+                        name: {
+                            'fr-FR': this.renkan.project.get('title')
+                        },
+                        type: 'http://www.w3.org/ns/activitystreams#Renkan',
+                        extensions: {
+                            'http://www.w3.org/ns/activitystreams#Data': searchData
+                        }
+                    }
+                });
+                this._pushTrackingMessage(msg);
+            },
             closeProject: function() {
                 this.flushTrackingInfo();
                 $.ajax({
--- a/server/src/assets/js/mtdc-tracking.js	Mon Jul 25 18:24:58 2016 +0200
+++ b/server/src/assets/js/mtdc-tracking.js	Wed Jul 27 15:26:23 2016 +0200
@@ -92,4 +92,15 @@
 
     });
 
+    Rkns.$('.Rk-GraphSearch-Field').on('input', Rkns._.debounce(function() {
+        var val = Rkns.$('.Rk-GraphSearch-Field').val();
+        if(val && val.length >= 2) {
+            _trackingWorker.searchProject();
+        }
+    }, 1000));
+
+    Rkns.$(window).on('beforeunload', function() {
+        _trackingWorker.closeProject();
+    });
+
 };
--- a/server/src/metaeducation/__init__.py	Mon Jul 25 18:24:58 2016 +0200
+++ b/server/src/metaeducation/__init__.py	Wed Jul 27 15:26:23 2016 +0200
@@ -1,4 +1,4 @@
-VERSION = (0, 0, 17, "final", 0)
+VERSION = (0, 0, 18, "final", 0)
 
 VERSION_STR = ".".join(map(lambda i:"%02d" % (i,), VERSION[:2]))
 
--- a/server/src/metaeducation/settings/__init__.py	Mon Jul 25 18:24:58 2016 +0200
+++ b/server/src/metaeducation/settings/__init__.py	Wed Jul 27 15:26:23 2016 +0200
@@ -168,3 +168,10 @@
 LRS_TRACKING_SERVICE_URL = "" # URL of the LRS to which we send tracking data
 LRS_MTDC_RENKAN_USERNAME = "" # Username of Renkan app for LRS auth
 LRS_MTDC_RENKAN_PASSWORD = "" # Password of Renkan app for LRS auth
+
+TRACKING_URI_TEMPLATES = {
+    'renkan': 'urn:mtdc:renkan:renkan:${renkan_id}',
+    'node': 'urn:mtdc:renkan:node:${renkan_id}:${node_id}',
+    'edge': 'urn:mtdc:renkan:edge:${renkan_id}:${edge_id}',
+    'view': 'urn:mtdc:renkan:view:${renkan_id}:${view_id}'
+}
--- a/server/src/metaeducation/static/metaeducation/js/mtdc-tracking-worker.js	Mon Jul 25 18:24:58 2016 +0200
+++ b/server/src/metaeducation/static/metaeducation/js/mtdc-tracking-worker.js	Wed Jul 27 15:26:23 2016 +0200
@@ -10,8 +10,17 @@
 
     var Mtdc = root.Mtdc;
 
-    Mtdc.TrackingWorker = function (renkan, trackingUrl, trackingCloseUrl, registration) {
-        var debounceDelay = arguments.length <= 4 || arguments[4] === undefined ? 1000 : arguments[4];
+    Mtdc.TrackingWorker = function (renkan, opts) {
+
+        var options = _.defaults(opts, {
+            debounceDelay: 1000,
+            trackingUriTemplates: {
+                'renkan': 'urn:mtdc:renkan:renkan:${renkan_id}',
+                'node': 'urn:mtdc:renkan:node:${renkan_id}:${node_id}',
+                'edge': 'urn:mtdc:renkan:edge:${renkan_id}:${edge_id}',
+                'view': 'urn:mtdc:renkan:view:${renkan_id}:${view_id}'
+            }
+        });
 
         function _sendTrackingInfo() {
             var trackingMessages = this.trackingMessages;
@@ -36,8 +45,8 @@
             trackingMessages: [],
             currentUser: renkan.current_user,
             renkan: renkan,
-            trackingUrl: trackingUrl,
-            trackingCloseUrl: trackingCloseUrl,
+            trackingUrl: options.trackingUrl,
+            trackingCloseUrl: options.trackingCloseUrl,
             getUUID4: function getUUID4() {
                 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
                     var r = Math.random() * 16 | 0,
@@ -46,9 +55,10 @@
                 });
             },
             _init: function _init() {
-                this.sendTrackingInfo = _.debounce(_.bind(_sendTrackingInfo, this), debounceDelay);
+                this.sendTrackingInfo = _.debounce(_.bind(_sendTrackingInfo, this), options.debounceDelay);
                 this.csrftoken = Cookies.get('csrftoken');
-                this.registration = registration ? registration : this.getUUID4();
+                this.registration = options.registration ? options.registration : this.getUUID4();
+                this.trackingUriTemplates = _.mapValues(options.trackingUriTemplates, _.template);
                 return this;
             },
             _pushTrackingMessage: function _pushTrackingMessage(msg) {
@@ -80,7 +90,12 @@
                     close: {
                         id: 'http://activitystrea.ms/schema/1.0/close',
                         display: { 'fr-FR': 'a fermé' }
+                    },
+                    search: {
+                        id: 'http://activitystrea.ms/schema/1.0/search',
+                        display: { 'fr-FR': 'a cherché' }
                     }
+
                 })[verb];
                 return {
                     actor: {
@@ -104,8 +119,11 @@
                     timestamp: timestamp.toISOString()
                 };
             },
-            _getObjectUrn: function _getObjectUrn(objectType, objectId) {
-                return 'urn:mtdc:renkan:' + objectType + ':' + objectId;
+            _getObjectUri: function _getObjectUri(objectType, objectId) {
+                var template = this.trackingUriTemplates[objectType];
+                var templateParams = { 'renkan_id': this.renkan.project.get('id') };
+                templateParams[objectType + '_id'] = objectId;
+                return template(templateParams);
             },
             _sendNodeMsg: function _sendNodeMsg(verb, nodeData, changedData, previousData) {
                 this._sendObjectMsg('node', 'http://www.w3.org/ns/activitystreams#Node', verb, nodeData, changedData, previousData);
@@ -121,7 +139,7 @@
                 var msg = this._getBaseMsg(verb);
 
                 msg.object = _.merge(msg.object, {
-                    id: this._getObjectUrn(objectType, objData._id), //TODO full url ???
+                    id: this._getObjectUri(objectType, objData._id), //TODO full url ???
                     definition: {
                         name: {
                             'fr-FR': objData.title
@@ -180,6 +198,23 @@
             updateView: function updateView(viewData, changedData, previousData) {
                 this._sendViewMsg('update', viewData, changedData, previousData);
             },
+            searchProject: function searchProject(searchData) {
+                var msg = this._getBaseMsg('search');
+
+                msg.object = _.merge(msg.object, {
+                    id: this._getObjectUri('renkan', this.renkan.project.get('id')),
+                    definition: {
+                        name: {
+                            'fr-FR': this.renkan.project.get('title')
+                        },
+                        type: 'http://www.w3.org/ns/activitystreams#Renkan',
+                        extensions: {
+                            'http://www.w3.org/ns/activitystreams#Data': searchData
+                        }
+                    }
+                });
+                this._pushTrackingMessage(msg);
+            },
             closeProject: function closeProject() {
                 this.flushTrackingInfo();
                 $.ajax({
--- a/server/src/metaeducation/static/metaeducation/js/mtdc-tracking.js	Mon Jul 25 18:24:58 2016 +0200
+++ b/server/src/metaeducation/static/metaeducation/js/mtdc-tracking.js	Wed Jul 27 15:26:23 2016 +0200
@@ -97,4 +97,15 @@
             registerView(_view);
         });
     });
+
+    Rkns.$('.Rk-GraphSearch-Field').on('input', Rkns._.debounce(function () {
+        var val = Rkns.$('.Rk-GraphSearch-Field').val();
+        if (val && val.length >= 2) {
+            _trackingWorker.searchProject();
+        }
+    }, 1000));
+
+    Rkns.$(window).on('beforeunload', function () {
+        _trackingWorker.closeProject();
+    });
 };
\ No newline at end of file
--- a/server/src/metaeducation/templates/renkan_edit.html	Mon Jul 25 18:24:58 2016 +0200
+++ b/server/src/metaeducation/templates/renkan_edit.html	Wed Jul 27 15:26:23 2016 +0200
@@ -50,7 +50,7 @@
           });
           _renkan.setCurrentUser('{{ user.external_id }}', '{{ user.username }}');
 
-          var trackingWorker = Mtdc.TrackingWorker(_renkan, "{% url 'tracking_view' %}", "{% url 'tracking_view_close' %}", "{{ registration }}");
+          var trackingWorker = Mtdc.TrackingWorker(_renkan, {trackingUrl: "{% url 'tracking_view' %}", trackingCloseUrl: "{% url 'tracking_view_close' %}", registration: "{{ registration }}", trackingUriTemplates: {{ tracking_uri_templates|safe }}});
 
           Rkns.mtdcTracking(_renkan, trackingWorker);
           Rkns.mtdcJsonIO(_renkan, {
@@ -58,9 +58,6 @@
               user_id: '{{ user.external_id }}',
               user_name: '{{ user.username }}'
           });
-          $(window).on('beforeunload', function(e) {
-              trackingWorker.closeProject();
-          });
       };
   </script>
 {% endblock js_import %}
--- a/server/src/metaeducation/templates/renkan_view.html	Mon Jul 25 18:24:58 2016 +0200
+++ b/server/src/metaeducation/templates/renkan_view.html	Wed Jul 27 15:26:23 2016 +0200
@@ -17,6 +17,7 @@
   <script src="{% static 'renkanmanager/lib/requirejs/require.js' %}"></script>
   <script src="{% static 'renkanmanager/lib/renkan/js/renkan.js' %}"></script>
   <script src="{% static 'metaeducation/js/mtdc-save.js' %}"></script>
+  <script src="{% static 'metaeducation/js/mtdc-tracking.js' %}"></script>
   <script src="{% static 'metaeducation/js/mtdc-tracking-worker.js' %}"></script>
   <script type="text/javascript">
 
@@ -29,7 +30,7 @@
               user_authenticated: true,
               user_id: "{{ user.id }}",
               user_name: "{{ user.username }}",
-              url_parameters: false,
+              url_parameters: true,
               update_url: false,
               user_color_editable: false,
               show_user_color: false,
@@ -46,16 +47,17 @@
               show_bookmarklet: false,
               show_fullscreen_button: false,
               home_button_url: false,
-              popup_editor: false,
+              popup_editor: true,
+              show_node_tooltip_creator: false,
+              show_edge_tooltip_creator: false
           });
+          _renkan.setCurrentUser('{{ user.external_id }}', '{{ user.username }}');
+
+          var trackingWorker = Mtdc.TrackingWorker(_renkan, {trackingUrl: "{% url 'tracking_view' %}", trackingCloseUrl: "{% url 'tracking_view_close' %}", registration: "{{ registration }}", trackingUriTemplates: {{ tracking_uri_templates|safe }}});
+          Rkns.mtdcTracking(_renkan, trackingWorker);
           Rkns.mtdcJsonIO(_renkan, {
               url: "{% url 'v1.0:renkan_detail' renkan_guid=renkan_guid %}?content_only=true"
           });
-          var trackingWorker = Mtdc.TrackingWorker(_renkan, "{% url 'tracking_view' %}", "{% url 'tracking_view_close' %}", "{{ registration }}");
-
-          $(window).on('beforeunload', function(e) {
-              trackingWorker.closeProject();
-          });
 
       };
   </script>
--- a/server/src/metaeducation/tracking/messages.py	Mon Jul 25 18:24:58 2016 +0200
+++ b/server/src/metaeducation/tracking/messages.py	Wed Jul 27 15:26:23 2016 +0200
@@ -2,7 +2,9 @@
 import json
 import logging
 import pytz
+import string
 
+from django.conf import settings
 from django.utils import six
 
 from .tasks import send_tracking_data
@@ -70,7 +72,7 @@
         'verb': verbNode,
         'object': {
             'objectType': 'Activity',
-            'id': get_renkan_urn(renkan_id)
+            'id': get_renkan_uri(renkan_id)
         },
         'context': {
             'extensions': {
@@ -84,8 +86,9 @@
 
     return msg
 
-def get_renkan_urn(renkan_id):
-    return 'urn:mtdc:renkan:renkan:%s' % renkan_id;
+def get_renkan_uri(renkan_id):
+    template = string.Template(settings.TRACKING_URI_TEMPLATES.get('renkan', 'urn:mtdc:renkan:renkan:${renkan_id}'))
+    return template.safe_substitute({'renkan_id': renkan_id})
 
 
 def send_close_renkan(renkan, current_user, registration):
--- a/server/src/metaeducation/views/renkan.py	Mon Jul 25 18:24:58 2016 +0200
+++ b/server/src/metaeducation/views/renkan.py	Wed Jul 27 15:26:23 2016 +0200
@@ -50,6 +50,7 @@
     def get_context_data(self, **kwargs):
         context = super(ViewRenkanView, self).get_context_data(**kwargs)
         context['registration'] = str(uuid.uuid4())
+        context['tracking_uri_templates'] = json.dumps(settings.TRACKING_URI_TEMPLATES)
         return context
 
     def get(self, request, *args, **kwargs):
@@ -65,6 +66,7 @@
     def get_context_data(self, **kwargs):
         context = super(EditRenkanView, self).get_context_data(**kwargs)
         context['registration'] = str(uuid.uuid4())
+        context['tracking_uri_templates'] = json.dumps(settings.TRACKING_URI_TEMPLATES)
         return context
 
     def get(self, request, *args, **kwargs):
--- a/server/src/requirement_iri.txt	Mon Jul 25 18:24:58 2016 +0200
+++ b/server/src/requirement_iri.txt	Wed Jul 27 15:26:23 2016 +0200
@@ -1,1 +1,1 @@
-renkanmanager (==0.12.20)
+renkanmanager (==0.12.22)