Ejemplo n.º 1
0
    def process_event(self, event_edx):
        course_id = event_edx['context'].get('course_id', None)
        if course_id is None or course_id == '':
            try:
                # We need to do this because we receive a string instead than a dictionary
                event = json.loads(event_edx['event'])
                course_id = event['POST'].get('course_id', None)[0]
            except:
                pass  # No event data, just skip
        if course_id != '' and course_id in self.course_ids:
            try:
                # Sometimes we receive time as python datetime, sometimes as string...
                timepart = event_edx['time']
                timestamp = xutils.make_datetime_for_tincan(timepart)

                actor = None
                user_id = 0
                try:
                    user_id = event_edx['context'].get('user_id', 0)
                except:
                    user_id = json.loads(event_edx['context']).get('user_id', 0)
                try:
                    actor = self.get_actor(user_id)
                except (ValueError, UserSocialAuth.DoesNotExist) as e:
                    # Only logged ECO user need to be tracked
                    return

                verb, obj = self.to_xapi(event_edx, course_id)

                context = self.get_context(course_id, user_id)
                d = {"time_with_millis": timepart}
                context.extensions = Extensions(d)
                # verb = None means to not record the action
                if verb:
                    statement = Statement(
                        actor=actor,
                        verb=verb,
                        object=obj,
                        context=context,
                        timestamp=timestamp
                    )

                    tldat = TrackingLog(
                        dtcreated=timestamp,
                        user_id=user_id,
                        course_id=course_id,
                        statement=statement.to_json(),
                        original_event=json.dumps(event_edx, cls=DateTimeJSONEncoder)
                    )

                    # We don't need to add duplication event test, so we save directly
                    tldat.save()

            except Exception as e:  # pylint: disable=broad-except
                log.exception(e)
        else:
            if course_id != '':
                # print 'Course not activated', course_id  # Uncomment for debug
                pass
Ejemplo n.º 2
0
def process_data(x, lines):
    i = 0
    for row in lines:
        event = json.loads(row)
        try:
            dt = dateutil.parser.parse(event['time'])
        except ValueError:
            print 'Data error -> ', event['time']
            continue

        # event['context']['user_id'] = 6 # used only for local test, comment in the real environment
        user_id = event['context'].get('user_id')
        if user_id == '':
            continue
        # Search for events of same user in the same date (seconds precision)
        tls = TrackingLog.objects.filter(dtcreated=xutils.make_datetime_for_tincan(dt), user_id=user_id)
        if tls:
            differentMillis = True
            for t in tls:
                statement = Statement.from_json(t.statement)

                try:
                    t_event = statement.context.extensions['time_with_millis']
                    if t_event == event['time']:
                        differentMillis = False
                        break
                except:  # pylint: disable=bare-except
                    pass

            if differentMillis:
                i = i + 1
                x.process_event(event)
            else:
                # Skip duplicate events
                # print "Tracking event already exists for dt: %s and user_id : %s ", event['time'], user_id
                # print event
                pass

        else:
            i = i + 1
            x.process_event(event)

    print "%s events sent to backend", str(i)