from collections.abc import Sequence import sqlalchemy as sa from pydantic import TypeAdapter from dmp.ad_reporting.dtos import AdReportingCampaignBenchmarkByAccountArtistCriteria from dmp.ad_reporting.models import AdReportingCampaignBenchmarkByAccountArtistDbt from dmp.adapters.db import ReportingRepository AdReportingCampaignBenchmarkByAccountArtistList = TypeAdapter( list[AdReportingCampaignBenchmarkByAccountArtistDbt] ) class AdReportingCampaignBenchmarkByAccountArtistRepository( ReportingRepository[AdReportingCampaignBenchmarkByAccountArtistDbt] ): def find_by_account_and_artist_id( self, vendor_id: int, subaccount_id: int, global_participant_id: str, ) -> Sequence[AdReportingCampaignBenchmarkByAccountArtistDbt]: query = sa.select(AdReportingCampaignBenchmarkByAccountArtistDbt).where( AdReportingCampaignBenchmarkByAccountArtistDbt.vendor_id == vendor_id, AdReportingCampaignBenchmarkByAccountArtistDbt.subaccount_id == subaccount_id, AdReportingCampaignBenchmarkByAccountArtistDbt.global_participant_id == global_participant_id, ) return self.db.session.execute(query).scalars().all() def find_unique_by_criteria( self, criteria: AdReportingCampaignBenchmarkByAccountArtistCriteria ) -> Sequence[AdReportingCampaignBenchmarkByAccountArtistDbt]: if criteria.countries: template_name = "ad-reporting/benchmark/find-unique-campaign-benchmarks-by-account-artist-country-criteria.sql" else: template_name = "ad-reporting/benchmark/find-unique-campaign-benchmarks-by-account-artist-criteria.sql" query = self.db.query_from_template( template_name, context={"criteria": criteria}, ) result = self.db.session.execute( sa.select(AdReportingCampaignBenchmarkByAccountArtistDbt).from_statement( query ) ) return result.scalars().all()