Beispiel #1
0
def new_aggregate_event(request: Request, event_variant: GLib.Variant,
                        dbsession: DbSession) -> Optional[AggregateEvent]:
    event_id = str(UUID(bytes=get_bytes(event_variant.get_child_value(1))))

    if event_id in IGNORED_EVENTS:
        return None

    user_id = event_variant.get_child_value(0).get_uint32()
    count = event_variant.get_child_value(2).get_int64()
    event_relative_timestamp = event_variant.get_child_value(3).get_int64()
    payload = event_variant.get_child_value(4)

    event_date = get_event_datetime(request.absolute_timestamp,
                                    request.relative_timestamp,
                                    event_relative_timestamp)

    # We don't have any aggregate event yet, therefore it can only be unknown

    # Mypy complains here, even though this should be fine:
    # https://github.com/dropbox/sqlalchemy-stubs/issues/97
    event = UnknownAggregateEvent(
        request=request,
        user_id=user_id,  # type: ignore
        occured_at=event_date,
        count=count,
        event_id=event_id,
        payload=payload)
    dbsession.add(event)

    return event
Beispiel #2
0
def new_singular_event(request: Request, event_variant: GLib.Variant,
                       dbsession: DbSession) -> Optional[SingularEvent]:
    event_id = str(UUID(bytes=get_bytes(event_variant.get_child_value(1))))

    if event_id in IGNORED_EVENTS:
        return None

    user_id = event_variant.get_child_value(0).get_uint32()
    event_relative_timestamp = event_variant.get_child_value(2).get_int64()
    payload = event_variant.get_child_value(3)

    event_date = get_event_datetime(request.absolute_timestamp,
                                    request.relative_timestamp,
                                    event_relative_timestamp)

    try:
        event_model = SINGULAR_EVENT_MODELS[event_id]

    except KeyError:
        # Mypy complains here, even though this should be fine:
        # https://github.com/dropbox/sqlalchemy-stubs/issues/97
        event = UnknownSingularEvent(
            request=request,
            user_id=user_id,  # type: ignore
            occured_at=event_date,
            event_id=event_id,
            payload=payload)
        dbsession.add(event)
        return event

    try:
        # Mypy complains here, even though this should be fine:
        # https://github.com/dropbox/sqlalchemy-stubs/issues/97
        event = event_model(
            request=request,
            user_id=user_id,  # type: ignore
            occured_at=event_date,
            payload=payload)

    except Exception as e:
        if isinstance(e, EmptyPayloadError
                      ) and event_id in IGNORED_EMPTY_PAYLOAD_ERRORS:
            return None

        log.exception('An error occured while processing the event:')

        # Mypy complains here, even though this should be fine:
        # https://github.com/dropbox/sqlalchemy-stubs/issues/97
        event = InvalidSingularEvent(
            request=request,
            user_id=user_id,  # type: ignore
            occured_at=event_date,
            event_id=event_id,
            payload=payload,
            error=str(e))

    dbsession.add(event)

    return event
Beispiel #3
0
 def _get_fields_from_payload(payload: GLib.Variant) -> Dict[str, Any]:
     return {
         'the_int': payload.get_child_value(0).get_int32(),
         'the_str': payload.get_child_value(1).get_string(),
     }
Beispiel #4
0
def get_child_values(
        value: GLib.Variant) -> Generator[GLib.Variant, None, None]:
    return (value.get_child_value(i) for i in range(value.n_children()))
Beispiel #5
0
def new_sequence_event(
    request: Request, sequence_variant: GLib.Variant, dbsession: DbSession
) -> Optional[Union[SequenceEvent, InvalidSequence, UnknownSequence]]:
    event_id = str(UUID(bytes=get_bytes(sequence_variant.get_child_value(1))))

    if event_id in IGNORED_EVENTS:
        return None

    user_id = sequence_variant.get_child_value(0).get_uint32()
    events = sequence_variant.get_child_value(2)
    num_events = events.n_children()

    if num_events < 2:
        error = f'Sequence must have at least 2 elements, but only had {num_events}'

        # Mypy complains here, even though this should be fine:
        # https://github.com/dropbox/sqlalchemy-stubs/issues/97
        sequence = InvalidSequence(
            request=request,
            user_id=user_id,  # type: ignore
            event_id=event_id,
            payload=events,
            error=error)
        dbsession.add(sequence)

        return sequence

    start_variant, *_progress_variants, stop_variant = get_child_values(events)

    # For now, we ignore progress events entirely. We also assume the stop event always has a null
    # payload. This works for most sequence events we care about in priority.
    # TODO: Figure this out for the more complex events

    start_relative_timestamp = start_variant.get_child_value(0).get_int64()
    payload = start_variant.get_child_value(1)
    started_at = get_event_datetime(request.absolute_timestamp,
                                    request.relative_timestamp,
                                    start_relative_timestamp)

    stop_relative_timestamp = stop_variant.get_child_value(0).get_int64()
    stopped_at = get_event_datetime(request.absolute_timestamp,
                                    request.relative_timestamp,
                                    stop_relative_timestamp)

    try:
        event_model = SEQUENCE_EVENT_MODELS[event_id]

    except KeyError:
        # Mypy complains here, even though this should be fine:
        # https://github.com/dropbox/sqlalchemy-stubs/issues/97
        sequence = UnknownSequence(
            request=request,
            user_id=user_id,  # type: ignore
            event_id=event_id,
            payload=events)
        dbsession.add(sequence)
        return sequence

    try:
        # Mypy complains here, even though this should be fine:
        # https://github.com/dropbox/sqlalchemy-stubs/issues/97
        sequence = event_model(
            request=request,
            user_id=user_id,  # type: ignore
            started_at=started_at,
            stopped_at=stopped_at,
            payload=payload)

    except Exception as e:
        if isinstance(e, EmptyPayloadError
                      ) and event_id in IGNORED_EMPTY_PAYLOAD_ERRORS:
            return None

        log.exception('An error occured while processing the sequence:')

        # Mypy complains here, even though this should be fine:
        # https://github.com/dropbox/sqlalchemy-stubs/issues/97
        sequence = InvalidSequence(
            request=request,
            user_id=user_id,  # type: ignore
            event_id=event_id,
            payload=events,
            error=str(e))

    dbsession.add(sequence)

    return sequence