def test_get_latest_play(db_mock):
    """Tests that the latest play is returned"""
    date1 = datetime(2020, 10, 4, 10, 35, 0)
    date2 = datetime(2020, 10, 1, 10, 10, 0)
    date3 = datetime(2020, 9, 20, 8, 1, 0)

    with db_mock.scoped_session() as session:
        Play.__table__.create(db_mock._engine)
        session.add(Play(user_id=1, play_item_id=1, created_at=date1))
        session.add(Play(user_id=2, play_item_id=1, created_at=date2))
        session.add(Play(user_id=3, play_item_id=2, created_at=date3))

    latest_play = get_latest_play()
    assert latest_play == date1
示例#2
0
def play_check():
    """
    max_drift: maximum duration in seconds between `now` and the
     latest recorded play record to be considered healthy
    """
    max_drift = request.args.get("max_drift", type=int)

    latest_play = get_latest_play()
    drift = (datetime.now() - latest_play).total_seconds()

    # Error if max drift was provided and the drift is greater than max_drift
    error = max_drift and drift > max_drift

    return success_response(latest_play,
                            500 if error else 200,
                            sign_response=False)
示例#3
0
def get_play_health_info(
        redis: Redis, plays_count_max_drift: Optional[int]) -> PlayHealthInfo:
    if redis is None:
        raise Exception("Invalid arguments for get_play_health_info")

    current_time_utc = datetime.utcnow()
    # Fetch plays info from Solana
    sol_play_info = get_sol_play_health_info(redis, current_time_utc)

    # If play count max drift provided, perform comparison
    is_unhealthy_sol_plays = bool(
        plays_count_max_drift
        and plays_count_max_drift < sol_play_info["time_diff"])

    # If unhealthy sol plays, this will be overwritten
    time_diff_general = sol_play_info["time_diff"]

    if is_unhealthy_sol_plays or not plays_count_max_drift:
        # Calculate time diff from now to latest play
        latest_db_play = redis_get_or_restore(redis, latest_legacy_play_db_key)
        if not latest_db_play:
            # Query and cache latest db play if found
            latest_db_play = get_latest_play()
            if latest_db_play:
                redis_set_and_dump(redis, latest_legacy_play_db_key,
                                   latest_db_play.timestamp())
        else:
            # Decode bytes into float for latest timestamp
            latest_db_play = float(latest_db_play.decode())
            latest_db_play = datetime.utcfromtimestamp(latest_db_play)

        oldest_unarchived_play = redis_get_or_restore(
            redis, oldest_unarchived_play_key)
        if not oldest_unarchived_play:
            # Query and cache oldest unarchived play
            oldest_unarchived_play = get_oldest_unarchived_play()
            if oldest_unarchived_play:
                redis_set_and_dump(
                    redis,
                    oldest_unarchived_play_key,
                    oldest_unarchived_play.timestamp(),
                )
        else:
            # Decode bytes into float for latest timestamp
            oldest_unarchived_play = float(oldest_unarchived_play.decode())
            oldest_unarchived_play = datetime.utcfromtimestamp(
                oldest_unarchived_play)

        time_diff_general = ((current_time_utc -
                              latest_db_play).total_seconds()
                             if latest_db_play else time_diff_general)

    is_unhealthy_plays = bool(plays_count_max_drift and
                              (is_unhealthy_sol_plays and
                               (plays_count_max_drift < time_diff_general)))

    return {
        "is_unhealthy": is_unhealthy_plays,
        "tx_info": sol_play_info,
        "time_diff_general": time_diff_general,
        "oldest_unarchived_play_created_at": oldest_unarchived_play,
    }