Exemplo n.º 1
0
def get_recording_recommendation_events_for_feed(
        user_ids: List[int], min_ts: int, max_ts: int,
        count: int) -> List[UserTimelineEvent]:
    """ Gets a list of recording_recommendation events for specified users.

    user_ids is a tuple of user row IDs.
    """
    with db.engine.connect() as connection:
        result = connection.execute(
            sqlalchemy.text("""
            SELECT id, user_id, event_type, metadata, created
              FROM user_timeline_event
             WHERE user_id IN :user_ids
               AND created > :min_ts
               AND created < :max_ts
               AND event_type = :event_type
          ORDER BY created DESC
             LIMIT :count
        """), {
                "user_ids": tuple(user_ids),
                "min_ts": datetime.utcfromtimestamp(min_ts),
                "max_ts": datetime.utcfromtimestamp(max_ts),
                "count": count,
                "event_type":
                UserTimelineEventType.RECORDING_RECOMMENDATION.value,
            })

        return [UserTimelineEvent(**row) for row in result.fetchall()]
Exemplo n.º 2
0
def get_user_timeline_events(user_id: int,
                             event_type: UserTimelineEventType,
                             count: int = 50) -> List[UserTimelineEvent]:
    """ Gets user timeline events of the specified type associated with the specified user.

    The optional `count` parameter can be used to control the number of events being returned.
    """
    with db.engine.connect() as connection:
        result = connection.execute(
            sqlalchemy.text("""
            SELECT id, user_id, event_type, metadata, created
              FROM user_timeline_event
             WHERE user_id = :user_id
               AND event_type = :event_type
          ORDER BY created
             LIMIT :count
        """), {
                'user_id': user_id,
                'event_type': event_type.value,
                'count': count,
            })

        return [UserTimelineEvent(**row) for row in result.fetchall()]
Exemplo n.º 3
0
def create_user_timeline_event(
    user_id: int,
    event_type: UserTimelineEventType,
    metadata: UserTimelineEventMetadata,
) -> UserTimelineEvent:
    """ Creates a user timeline event in the database and returns the event.
    """
    try:
        with db.engine.connect() as connection:
            result = connection.execute(
                sqlalchemy.text("""
                INSERT INTO user_timeline_event (user_id, event_type, metadata)
                    VALUES (:user_id, :event_type, :metadata)
                RETURNING id, user_id, event_type, metadata, created
                """), {
                    'user_id': user_id,
                    'event_type': event_type.value,
                    'metadata': ujson.dumps(metadata.dict()),
                })

            r = dict(result.fetchone())
            return UserTimelineEvent(**r)
    except Exception as e:
        raise DatabaseException(str(e))