Esempio n. 1
0
def activity_event_callback(msg):
    '''Callback fonction for asynchronous consumption of activity messages'''

    # Verify the message type
    assert(msg.topic == ActivityDetectionEventsConsumer.topic_activity_detection_events)

    # Deserialize the message from avro
    event = ActivityDetectionEvent.from_serialized(msg.value)
    print(str(event))

    # Update the user activity cache
    user_activity_cache[event.user_id] = event.activity_label, event.timestampBegin, event.timestampBackend
consumer = KafkaConsumer(args.topic,
                         group_id='my_group',
                         bootstrap_servers=[args.kafka + ':9092'])

print "Listening on topic:", args.topic

for message in consumer:
    # message value is raw byte string -- decode if necessary!
    # e.g., for unicode: `message.value.decode('utf-8')`

    if args.raw:
        value = message.value
    else:
        curTime = int(round(time.time() * 1000))

        activity = ActivityDetectionEvent.from_serialized(message.value)

        if startTime is None:
            startTime = activity.timestampBegin

        totalMsg += 1
        timeTaken = curTime - activity.timestampBegin
        halftimeTaken = activity.timestampBackend - activity.timestampBegin
        totalTimeTaken += timeTaken
        rightPredict = True if activity.activity_label == activity.actual_activity_label else False
        if rightPredict:
            totalCorrect += 1

        value = "user={0}, activity={1}, time-taken={2}ms, half-time={3}ms, correct={4}, total={5}, rate={6:.2f}/s, avg. speed={7:.2f}ms, prec={8:.2f}".format(
            activity.user_id,
            activity.activity_label,