""" TranscodingOrder Model. This TranscodingOrder model uses sqlalchemy. It's used to store information about trancoding orders. """ from datetime import datetime from typing import Any from owsresponse import response from sentry_sdk import capture_exception from sqlalchemy import Column, String, func from sqlalchemy.dialects.mysql import INTEGER, TIMESTAMP from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.orm import Mapped from transcoding.connectors import mysql from transcoding.constants import error class TranscodingOrder(mysql.BaseModel): """Table definition for transcoding_order table.""" __tablename__ = "transcoding_order" transcoding_order_id = Column( INTEGER(unsigned=True), primary_key=True, autoincrement=True ) sns_topic = Column(String(1023), nullable=True) input_bucket = Column(String(255), nullable=False) input_key = Column(String(255), nullable=False) preset_id = Column(INTEGER(unsigned=True), nullable=True) created_timestamp: Mapped[datetime] = Column( TIMESTAMP, nullable=False, server_default=func.now() ) def as_dict(self) -> dict[str, Any]: """Return object as dict. Returns: dict: Dictionary representation of object. """ transcoding_order_dict = { "transcoding_order_id": self.transcoding_order_id, "sns_topic": self.sns_topic, "input_bucket": self.input_bucket, "input_key": self.input_key, "preset_id": self.preset_id, "created_timestamp": self.created_timestamp.isoformat(), } return transcoding_order_dict def create_transcoding_order( input_bucket: str, input_key: str, sns_topic: str | None = None, preset_id: int | None = None, ) -> response.Response: """Create new transcoding order item in the table. Args: input_bucket (str): Input bucket name. input_key (str): Input asset key. sns_topic (str): SNS topic for updates notification (optional). preset_id (int): Preset id for entries created by preset (optional). Returns: response.Response: Created transcoding order info or error. """ try: transcoding_order = TranscodingOrder( sns_topic=sns_topic, input_bucket=input_bucket, input_key=input_key, preset_id=preset_id, ) with mysql.db_session() as session: session.add(transcoding_order) session.flush() transcoding_order_dict = transcoding_order.as_dict() return response.Response(transcoding_order_dict) except SQLAlchemyError as e: capture_exception(e) return response.create_fatal_response(e.args) def get_transcoding_order(transcoding_order_id: int) -> response.Response: """Return transcoding order info by id. Args: transcoding_order_id (int): Transcoding order id. Returns: response.Response: TranscodingOrder.as_dict() in message attribute or error response. """ try: with mysql.db_session() as session: filters = [(TranscodingOrder.transcoding_order_id == transcoding_order_id)] entry = session.query(TranscodingOrder).filter(*filters).one_or_none() if entry is None: return response.create_not_found_response( message=error.ERROR_MESSAGE_TRANSCODING_ORDER_NOT_FOUND ) transcoding_order_dict = entry.as_dict() return response.Response(transcoding_order_dict) except SQLAlchemyError as e: capture_exception(e) return response.create_fatal_response(e.args)