""" Asset Model. This Asset model uses sqlalchemy. This is to fetch count of distinct bits_per_sample values for assets based on provided UPC. """ from oto import response from sqlalchemy import BigInteger from sqlalchemy import Column from sqlalchemy import Integer from sqlalchemy import distinct from sqlalchemy.exc import SQLAlchemyError from assets.connectors import mysql from assets.connectors.sentry import sentry_client from assets.constants import asset_legacy from assets.constants import error from assets.models.legacy import asset_location from assets.models.legacy import asset_location_detail from assets.models.legacy import asset_type from assets.models.legacy import storage from assets.models.legacy import storage_drive from assets.models.legacy import storage_drive_asset_folder class Asset(mysql.DdModel): """Table defination for asset table.""" __tablename__ = 'asset' asset_id = Column(Integer, primary_key=True) upc = Column(BigInteger) asset_type_id = Column(Integer) cd = Column(Integer) track_id = Column(Integer) original_physical_location = Column(Integer) def get_bits_per_sample_data_for_tracks(upc, location_id, asset_type_list): """Get bits_per_sample data for tracks for provided UPC. Args: upc (int): reference to the product location_id (int): original physical location of asset to check. asset_type_list (list): asset type list to check. Returns: message: bits_per_sample data for tracks based on provided UPC """ if not upc: return response.create_error_response( message=error.ERROR_UPC_NOT_FOUND, code=400) try: filters = [ (Asset.asset_id == asset_location.AssetLocation.asset_id), (asset_location.AssetLocation.asset_location_id == asset_location_detail.AssetLocationDetail.asset_location_id), (storage_drive.StorageDrive.storage_drive_id == asset_location.AssetLocation.storage_drive_id), (storage.Storage.storage_id == storage_drive.StorageDrive.storage_id), (Asset.upc == upc), (storage.Storage.physical_location_id == location_id), (Asset.asset_type_id.in_(asset_type_list))] with mysql.dd_db_session() as session: result = session.query( Asset.asset_type_id, asset_location_detail.AssetLocationDetail.filename, asset_location_detail.AssetLocationDetail.bits_per_sample ).filter(*filters).all() if result: return response.Response( message=[u._asdict() for u in result]) return response.create_not_found_response( message=error.NO_BITS_PER_SAMPLE_FOUND) except SQLAlchemyError as ex: sentry_client.captureException() return response.create_fatal_response(str(ex)) def get_number_distinct_bitrates_for_upc(upc, location_id, asset_type_id): """Get number of distinct bitrates by provided UPC. Args: upc (int): reference to the product location_id (int): original physical location of asset to check. asset_type_id (int): asset type to check. Returns: message: count of bits_per_sample for tracks based on provided UPC """ if not upc: return response.create_error_response( message=error.ERROR_UPC_NOT_FOUND, code=400) try: filters = [ (Asset.asset_id == asset_location.AssetLocation.asset_id), (asset_location.AssetLocation.asset_location_id == asset_location_detail.AssetLocationDetail.asset_location_id), (storage_drive.StorageDrive.storage_drive_id == asset_location.AssetLocation.storage_drive_id), (storage.Storage.storage_id == storage_drive.StorageDrive.storage_id), (Asset.upc == upc), (storage.Storage.physical_location_id == location_id), (Asset.asset_type_id == asset_type_id)] with mysql.dd_db_session() as session: result = session.query(distinct( asset_location_detail.AssetLocationDetail.bits_per_sample)).\ filter(*filters) if result.count(): return response.Response( message=int(result.count()) ) else: return response.create_not_found_response( message=error.NO_BITS_PER_SAMPLE_FOUND ) except SQLAlchemyError as ex: sentry_client.captureException() return response.create_fatal_response(str(ex)) def get_bitrates_for_upc(upc, location_id, asset_type_id): """Get bitrates for provided UPC. Args: upc (int): reference to the product. location_id (int): original physical location of asset to check. asset_type_id (int): asset type to check. Returns: message: list of bitrates. """ if not upc: return response.create_error_response( message=error.ERROR_UPC_NOT_FOUND, code=400) try: filters = [ (Asset.asset_id == asset_location.AssetLocation.asset_id), (asset_location.AssetLocation.asset_location_id == asset_location_detail.AssetLocationDetail.asset_location_id), (storage_drive.StorageDrive.storage_drive_id == asset_location.AssetLocation.storage_drive_id), (storage.Storage.storage_id == storage_drive.StorageDrive.storage_id), (Asset.upc == upc), (storage.Storage.physical_location_id == location_id), (Asset.asset_type_id == asset_type_id)] with mysql.dd_db_session() as session: result = session.query(distinct( asset_location_detail.AssetLocationDetail.bits_per_sample)).\ filter(*filters).all() if result: bitrates_list = \ [asset_details[0] for asset_details in result] return response.Response( message=bitrates_list ) else: return response.create_not_found_response( message=error.NO_BITS_PER_SAMPLE_FOUND ) except SQLAlchemyError as ex: sentry_client.captureException() return response.create_fatal_response(str(ex)) def get_streaming_info( upc, asset_type_id, physical_location_id, search_condition, strict=False): """Get asset information required for asset streaming. Args: upc (int): Asset product upc. asset_type_id (int): Asset type id. physical_location_id (int): Asset physical location id. search_condition (str): Filename search condition. strict (bool): Flag to mark strict search. Returns: response.Response: Response with asset info for streaming or error """ try: filters = [ (Asset.asset_id == asset_location.AssetLocation.asset_id), (asset_location.AssetLocation.storage_drive_id == storage_drive.StorageDrive.storage_drive_id), (asset_location.AssetLocation.asset_location_id == asset_location_detail.AssetLocationDetail.asset_location_id), (storage_drive.StorageDrive.storage_id == storage.Storage.storage_id), (storage_drive.StorageDrive.storage_drive_id == storage_drive_asset_folder.StorageDriveAssetFolder. storage_drive_id), (Asset.asset_type_id == storage_drive_asset_folder.StorageDriveAssetFolder. asset_type_id), (Asset.asset_type_id == asset_type.AssetType.asset_type_id), (Asset.upc == upc), (asset_type.AssetType.asset_type_id == asset_type_id), (storage.Storage.physical_location_id == physical_location_id), (asset_location.AssetLocation.status.in_([ asset_legacy.LOCATION_STATUS_Y, asset_legacy.LOCATION_STATUS_L])) ] if strict: filename_filter = (asset_location_detail.AssetLocationDetail. filename == search_condition) else: filename_filter = (asset_location_detail.AssetLocationDetail. filename.like(search_condition)) filters.append(filename_filter) with mysql.dd_db_session() as session: query_result = session.query( Asset.upc, Asset.cd, Asset.track_id, storage.Storage.local_ip, storage.Storage.storage_id, asset_type.AssetType.extension, asset_type.AssetType.folder_structure, asset_type.AssetType.file_type, storage_drive_asset_folder. StorageDriveAssetFolder.initial_folder, asset_location_detail.AssetLocationDetail.filename, asset_location_detail.AssetLocationDetail.duration).\ filter(*filters).first() if not query_result: return response.create_not_found_response( message=error.ERROR_ASSET_NOT_FOUND) result = { 'upc': query_result[0], 'cd': query_result[1], 'track_id': query_result[2], 'local_ip': query_result[3], 'storage_id': query_result[4], 'extension': query_result[5], 'folder_structure': query_result[6], 'path_structure_type': query_result[7], 'initial_folder': query_result[8], 'filename': query_result[9], 'duration': float(query_result[10]) } return response.Response(result) except SQLAlchemyError as ex: sentry_client.captureException() return response.create_fatal_response(str(ex))