import asyncio from collections import defaultdict from typing import Any, cast from monday_com_orca_backend.api.routers.data.endpoints import get_label_meta_internal from monday_com_orca_backend.api.routers.data.models.responses import Label from monday_com_orca_backend.connectors.snowflake_db import Client as SnowflakeClient from monday_com_orca_backend.typings import LabelId from monday_com_orca_backend.utils.snowflake import get_snowflake_client from . import models, sql_queries, typings async def new_business_plan(label_ids: list[LabelId], *, fiscal_year: int) -> None: """ Create a new business plan with the given label IDs. Args: label_ids: List of label IDs to associate with the business plan. fiscal_year: The fiscal year for the business plan. Returns: dict[str, str]: A dictionary containing the ID of the created business plan. """ label_ids = list(set(label_ids)) # Ensure label IDs are unique _raw_data = await _fetch_raw_data(label_ids, fiscal_year=fiscal_year) _x = 0 async def _fetch_raw_data( label_ids: list[LabelId], *, fiscal_year: int ) -> typings.FetchDataResult: """Fetch the raw data needed for creating a new business plan.""" unique_label_ids = list(set(label_ids)) # Ensure label IDs are unique if not unique_label_ids: raise ValueError("No valid label IDs provided.") snowflake_client = get_snowflake_client() tasks = [ _fetch_label_meta(unique_label_ids), _fetch_financials(snowflake_client, unique_label_ids), _fetch_asset_counts( snowflake_client, unique_label_ids, fiscal_year=fiscal_year ), _fetch_label_salesforce_meta(snowflake_client, unique_label_ids), ] label_meta, financials, asset_counts, salesforce_meta = await asyncio.gather( *tasks, return_exceptions=False ) return typings.FetchDataResult( label_meta=cast(dict[LabelId, Label], label_meta), financials=cast(dict[LabelId, models.LabelFinancials], financials), asset_counts=cast(dict[LabelId, models.LabelAssets], asset_counts), salesforce_meta=cast(dict[LabelId, models.SalesforceMeta], salesforce_meta), ) async def _fetch_label_meta( label_ids: list[LabelId], ) -> dict[LabelId, Label]: """Fetch label metadata for the given label IDs.""" label_list = await get_label_meta_internal(label_ids) return {label.id: label for label in label_list} async def _fetch_financials( snowflake_client: SnowflakeClient, label_ids: list[LabelId], ) -> dict[LabelId, models.LabelFinancials]: """Fetch label metadata for the given label IDs.""" raw_data: list[dict[str, Any]] = cast( list[dict[str, Any]], await snowflake_client.afetch_all(sql_queries.get_label_financials(label_ids)), ) objects = [models.LabelFinancials(**row) for row in raw_data] # type: ignore return {obj.label_id: obj for obj in objects} async def _fetch_asset_counts( snowflake_client: SnowflakeClient, label_ids: list[LabelId], *, fiscal_year: int ) -> dict[LabelId, models.LabelAssets]: """Fetch asset counts for the given label IDs, returning LabelAssets models.""" raw_data: list[dict[str, Any]] = cast( list[dict[str, Any]], await snowflake_client.afetch_all( sql_queries.get_label_asset_counts(label_ids, fiscal_year=fiscal_year) ), ) grouped: defaultdict[LabelId, dict] = defaultdict(dict) for item in raw_data: label_id = int(item["LABELID"]) grouped[label_id][item["TYPE"]] = { "CNT": item["CNT"], "GROSS": item["GROSS"], } return { label_id: models.LabelAssets(**assets) for label_id, assets in grouped.items() } async def _fetch_label_salesforce_meta( snowflake_client: SnowflakeClient, label_ids: list[LabelId], ) -> dict[LabelId, models.SalesforceMeta]: """Fetch Salesforce metadata for the given label IDs.""" raw_data: list[dict[str, Any]] = cast( list[dict[str, Any]], await snowflake_client.afetch_all( sql_queries.get_label_salesforce_meta(label_ids) ), ) return {row["LABEL_ID"]: models.SalesforceMeta(**row) for row in raw_data}