def test_FromJSONToJSON(self):
     json_str = '{"id":"016699c6-d600-48a7-96ab-86187498f16f", ' \
                '"actor": {"name":"test"}, ' \
                '"verb": {"id":"test"}, ' \
                '"object": {"object_type":"Agent", "name":"test"}, ' \
                '"authority":{ "name":"test"}, ' \
                '"context": {"registration":"016699c6-d600-48a7-96ab-86187498f16f"}, ' \
                '"attachments":[{"usage_type":"test"}]}'
     statement = Statement.from_json(json_str)
     self.assertEqual(statement.id, uuid.UUID('016699c6-d600-48a7-96ab-86187498f16f'))
     self.agentVerificationHelper(statement.actor)
     self.verbVerificationHelper(statement.verb)
     self.agentVerificationHelper(statement.object)
     self.agentVerificationHelper(statement.authority)
     self.contextVerificationHelper(statement.context)
     for k in statement.attachments:
         self.attachmentVerificationHelper(k)
     self.assertEqual(json.loads(statement.to_json()),
                      json.loads('{"verb": {"id": "test"}, '
                      '"attachments": [{"usageType": "test"}], '
                      '"object": {"name": "test", "objectType": "Agent"}, '
                      '"actor": {"name": "test", "objectType": "Agent"}, '
                      '"version": "1.0.3", '
                      '"authority": {"name": "test", "objectType": "Agent"}, '
                      '"context": {"registration": "016699c6-d600-48a7-96ab-86187498f16f"}, '
                                 '"id": "016699c6-d600-48a7-96ab-86187498f16f"}'))
 def test_FromJSONToJSON(self):
     json_str = '{"id":"016699c6-d600-48a7-96ab-86187498f16f", ' \
                '"actor": {"name":"test"}, ' \
                '"verb": {"id":"test"}, ' \
                '"object": {"object_type":"Agent", "name":"test"}, ' \
                '"authority":{ "name":"test"}, ' \
                '"context": {"registration":"016699c6-d600-48a7-96ab-86187498f16f"}, ' \
                '"attachments":[{"usage_type":"test"}]}'
     statement = Statement.from_json(json_str)
     self.assertEqual(statement.id, uuid.UUID('016699c6-d600-48a7-96ab-86187498f16f'))
     self.agentVerificationHelper(statement.actor)
     self.verbVerificationHelper(statement.verb)
     self.agentVerificationHelper(statement.object)
     self.agentVerificationHelper(statement.authority)
     self.contextVerificationHelper(statement.context)
     for k in statement.attachments:
         self.attachmentVerificationHelper(k)
     self.assertEqual(statement.to_json(),
                      '{"verb": {"id": "test"}, '
                      '"attachments": [{"usageType": "test"}], '
                      '"object": {"name": "test", "objectType": "Agent"}, '
                      '"actor": {"name": "test", "objectType": "Agent"}, '
                      '"version": "1.0.1", '
                      '"authority": {"name": "test", "objectType": "Agent"}, '
                      '"context": {"registration": "016699c6-d600-48a7-96ab-86187498f16f"}, '
                      '"id": "016699c6-d600-48a7-96ab-86187498f16f"}')
Example #3
0
 def thread_function(stack):
     for i in range(len(stack)):
         s = stack[i]
         statement = Statement.from_json(s)
         if debug_log_print:
             print("Tracing: Sending statement number {} from stack".format(i+1))
         if not _send_statement_lrs(statement):  # Send statement and receive HTTP response
             io.add_statement(statement)  # Backup the statement if it couldn't been sent
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)