"""Logic for transcoding order.""" from typing import Any from owsresponse import response from transcoding.constants import transcoding from transcoding.logic import transcoding_job from transcoding.models import ( transcoding_job as transcoding_job_model, transcoding_order as transcoding_order_model, ) def create( input_bucket: str, input_filename: str, output_bucket: str, transcoding_rules: list[dict[str, Any]], sns_topic_alias: str | None = None, preset_id: int | None = None, ) -> response.Response: """Create transcoding order with all transcoding jobs. Args: input_bucket (str): Input S3 bucket name. input_filename (str): Input filename. output_bucket (str): Output S3 bucket name. transcoding_rules (list): list of transcoding rules. sns_topic_alias (str): Alias of sns topic for order status reporting. preset_id (int): Id of transcoding preset (if exists). Returns: response.Response: Created transcoding order info or error """ order_response = transcoding_order_model.create_transcoding_order( input_bucket, input_filename, sns_topic_alias, preset_id ) if not order_response: return order_response order = order_response.message for transcoding_rule in transcoding_rules: job_response = transcoding_job.create(order, output_bucket, transcoding_rule) if not job_response: return job_response return response.Response( { "transcoding_order_id": order["transcoding_order_id"], "status": transcoding.REQUESTED_STATUS, } ) def get_status(transcoding_order_id: int) -> response.Response: """Get transcoding order status with all transcoding job ids. Args: transcoding_order_id (int): Id of transcoding order. Returns: response.Response: Transcoding order status with job ids or error message. """ transcoding_order_response = transcoding_order_model.get_transcoding_order( transcoding_order_id ) if not transcoding_order_response: return transcoding_order_response transcoding_order = transcoding_order_response.message transcoding_jobs_response = transcoding_job_model.get_transcoding_jobs_by_order_id( transcoding_order_id ) if not transcoding_jobs_response: return transcoding_jobs_response transcoding_jobs = transcoding_jobs_response.message prepared_jobs_with_status = _prepare_jobs_with_order_status(transcoding_jobs) return response.Response( { "transcoding_order_id": transcoding_order["transcoding_order_id"], **prepared_jobs_with_status, } ) def _prepare_jobs_with_order_status( transcoding_jobs: list[dict[str, Any]], ) -> dict[str, Any]: """Get transcoding order status with all transcoding job ids and statuses. Args: transcoding_jobs (list): List of transcoding jobs. Returns: dict: Dictionary contains transcoding_order status and list of jobs. E.g.: { 'status': 'completed', 'transcoding_jobs': [ { 'status': 'completed', 'transcoding_job_id': 1, 'description': '', 'duration': 1000, 'channels': 2, 'codec': 'pcm', 'sample_rate': 44100, 'bit_rate': 1411200, 'bit_depth': 16, }, { 'status': 'completed', 'transcoding_job_id': 2, 'description': '', 'duration': 2000, 'channels': 2, 'codec': 'pcm', 'sample_rate': 44100, 'bit_rate': 1411200, 'bit_depth': 16, }, ] } """ simplified_jobs = [] job_statuses = set() for job in transcoding_jobs: job_statuses.add(job["status"]) simplified_jobs.append( { "status": job["status"], "transcoding_job_id": job["transcoding_job_id"], "description": job["description"], "output_key": job["output_key"], "output_bucket": job["output_bucket"], "container": job["container"], "duration": job["duration"], "channels": job["channels"], "codec": job["codec"], "sample_rate": job["sample_rate"], "bit_rate": job["bit_rate"], "bit_depth": job["bit_depth"], } ) order_status = transcoding.PROCESSING_STATUS if len(job_statuses) == 1: order_status = job_statuses.pop() elif transcoding.ERROR_STATUS in job_statuses: order_status = transcoding.ERROR_STATUS return {"status": order_status, "transcoding_jobs": simplified_jobs}