示例#1
0
async def safe_insert_order(conn: SAConn, in_tx: Tx, out_tx: Tx, order: Order):
    async with conn.begin("SERIALIZABLE") as transaction:
        try:
            _in_res = await insert_tx(conn, in_tx)
            _out_res = await insert_tx(conn, out_tx)
            _order_res = await insert_order(conn, order)
            assert _in_res
            assert _out_res
            assert _order_res
            return True
        except Exception as ex:
            return False
示例#2
0
async def safe_insert_order(conn: SAConn, in_tx: Tx, out_tx: Tx, order: Order):
    async with conn.begin("SERIALIZABLE") as transaction:
        try:
            _in_res = await insert_tx(conn, in_tx)
            _out_res = await insert_tx(conn, out_tx)
            _order_res = await insert_order(conn, order)
            assert _in_res
            assert _out_res
            assert _order_res
            return True
        except Exception as ex:
            log.info(f"Serialization inserting order {order.id} fail: {ex}")
            return False
示例#3
0
async def safe_update_order(conn: SAConn, order: OrderDTO):
    async with conn.begin("SERIALIZABLE") as transaction:
        cursor = await conn.execute(
            select([Order]).where(Order.id == order.order_id).as_scalar())
        _db_order_instance = await cursor.fetchone()

        _txs_to_update = [
            tx for tx in (order.in_tx, order.out_tx) if tx is not None
        ]

        for tx in _txs_to_update:

            if tx == order.in_tx:
                tx_uuid = _db_order_instance.in_tx
            elif tx == order.out_tx:
                tx_uuid = _db_order_instance.out_tx
            else:
                raise

            _tx = Tx(id=tx_uuid, **dataclasses.asdict(tx))

            await update_tx(conn, _tx)
        return True
示例#4
0
async def save_history_chunk(*, songs, conn: asa.SAConnection):
    """In charge of saving a chunck of continuous songs."""
    # {'__v': 0,
    #  '_id': '583bf4a9d9abb248008a698a',
    #  '_song': {
    #      '__v': 0,
    #      '_id': '5637c2cf7d7d3f2200b05659',
    #      'created': '2015-11-02T20:08:47.588Z',
    #      'fkid': 'eOwwLhMPRUE',
    #      'images': {
    #          'thumbnail': 'https://i.ytimg.com/vi/eOwwLhMPRUE/hqdefault.jpg',
    #          'youtube': {
    #              'default': {
    #                  'height': 90,
    #                  'url': 'https://i.ytimg.com/vi/eOwwLhMPRUE/default.jpg',
    #                  'width': 120
    #              },
    #              'high': {
    #                  'height': 360,
    #                  'url': 'https://i.ytimg.com/vi/eOwwLhMPRUE/hqdefault.jpg',
    #                  'width': 480
    #              },
    #              'maxres': {
    #                  'height': 720,
    #                  'url': 'https://i.ytimg.com/vi/eOwwLhMPRUE/maxresdefault.jpg',
    #                  'width': 1280
    #              },
    #              'medium': {
    #                  'height': 180,
    #                  'url': 'https://i.ytimg.com/vi/eOwwLhMPRUE/mqdefault.jpg',
    #                  'width': 320
    #              },
    #              'standard': {
    #                  'height': 480,
    #                  'url': 'https://i.ytimg.com/vi/eOwwLhMPRUE/sddefault.jpg',
    #                  'width': 640
    #              }
    #          }
    #      },
    #      'name': 'Craig Armstrong - Dream Violin',
    #      'songLength': 204000,
    #      'type': 'youtube'
    #  },
    #  '_user': {
    #      '__v': 0,
    #      '_id': '57595c7a16c34f3d00b5ea8d',
    #      'created': 1465474170519,
    #      'dubs': 0,
    #      'profileImage': {
    #          'bytes': 72094,
    #          'etag': 'fdcdd43edcaaec225a6dcd9701e62be1',
    #          'format': 'png',
    #          'height': 500,
    #          'public_id': 'user/57595c7a16c34f3d00b5ea8d',
    #          'resource_type': 'image',
    #          'secure_url':
    #              'https://res.cloudinary.com/hhberclba/image/upload/v1465474392/user'
    #              '/57595c7a16c34f3d00b5ea8d.png',
    #          'tags': [],
    #          'type': 'upload',
    #          'url': 'http://res.cloudinary.com/hhberclba/image/upload/v1465474392/user'
    #                 '/57595c7a16c34f3d00b5ea8d.png',
    #          'version': 1465474392,
    #          'width': 500
    #      },
    #      'roleid': 1,
    #      'status': 1,
    #      'username': '******'
    #  },
    #  'created': 1480324264803,
    #  'downdubs': 0,
    #  'isActive': True,
    #  'isPlayed': True,
    #  'order': 243,
    #  'played': 1480464322618,
    #  'roomid': '561b1e59c90a9c0e00df610b',
    #  'skipped': False,
    #  'songLength': 204000,
    #  'songid': '5637c2cf7d7d3f2200b05659',
    #  'updubs': 1,
    #  'userid': '57595c7a16c34f3d00b5ea8d'
    #  }
    song_played = None
    previous_song, previous_playback_id = {}, None
    async with conn.begin():
        for song in songs:
            # Generate Action skip for the previous Playback entry
            song_played = datetime.datetime.utcfromtimestamp(song['played'] / 1000)
            if previous_song.get('skipped'):
                await history_import_skip_action(
                    previous_playback_id=previous_playback_id,
                    song_played=song_played,
                    conn=conn,
                )

            # Query or create the User for the Playback entry
            user = await get_or_create_user(song=song, conn=conn)
            user_id = user['id']

            # Query or create the Track entry for this Playback entry
            track = await get_or_create_track(song=song, conn=conn)
            track_id = track['id']

            # Query or create the Playback entry
            playback_id = await get_or_create_playback(
                song_played=song_played,
                track_id=track_id,
                user_id=user_id,
                conn=conn
            )

            # Query or create the UserAction<upvote> UserAction<downvote> entries
            await update_user_actions(
                song=song,
                song_played=song_played,
                playback_id=playback_id,
                conn=conn,
            )

            previous_song, previous_playback_id = song, playback_id
        logger.info(f'Saved songs up to {song_played}')
    await conn.close()