import logging from dataclasses import dataclass from fansifter_common.adapters.db import BaseRepository from pydantic import TypeAdapter from app.connectors.database.base import ReportingDB from app.models import ( AdReportingCreativeThumbnailUrl, Creative, ) from .models import AdReportingAdDbt logger = logging.getLogger(__name__) CreativeList = TypeAdapter(list[Creative]) @dataclass(kw_only=True) class AdReportingRepository(BaseRepository[ReportingDB, AdReportingAdDbt]): db: ReportingDB meta_connection_table: str tiktok_connection_table: str google_connection_table: str max_fails_count: int def get_creatives(self) -> list[Creative]: template_name = "get_creatives.sql" query = self.db.query_from_template( template_name, context={ "meta_connection_table": self.meta_connection_table, "tiktok_connection_table": self.tiktok_connection_table, "google_connection_table": self.google_connection_table, "max_fails_count": self.max_fails_count, }, ) result = self.db.session.execute(query) return CreativeList.validate_python(result.mappings()) def store_ad_reporting_creative_thumbnail_urls( self, ad_reporting_creative_thumbnail_urls: list[AdReportingCreativeThumbnailUrl], ) -> None: if not ad_reporting_creative_thumbnail_urls: logger.info("Nothing to save to db") return None dumped_rows = tuple( tuple(row.model_dump().values()) for row in ad_reporting_creative_thumbnail_urls ) template_name = "update_thumbnail_url.sql" query = self.db.query_from_template( template_name, context={"values": dumped_rows}, ) self.db.session.execute(query) self.db.session.commit() logger.info("Results saved to db successfully")