Skip to content

portfolio_utils

Portfolio utils are used to handle common actions related to handling our portfolio.

load_mpid_to_asset_name_map(platform_conn)

Loads a mapping of MPIDs to asset common names from cis_prod in the Django platform database.

Queries the structure_cis_prod table to retrieve all rows where both mpid and common_name are not null. Returns a dictionary mapping each mpid to its corresponding common_name.

Parameters:

Name Type Description Default
platform_conn AzureSqlConnection

A connection wrapper with an SQLAlchemy engine for the Django platform database.

required

Returns:

Type Description
Dict[str, str]

Dict[str, str]: A dictionary where the keys are MPIDs and the values are asset common names.

Example
from physical_operations_utils.sql_utils import load_mpid_to_asset_name_map
from physical_operations_utils.azure_utils.azure_sql_connection import get_db_connection
from physical_operations_utils.azure_utils.environment_utils import setup_environment

environment = setup_environment()
platform_conn = get_db_connection(f"physical_trading_platform_django_{environment}")
mpid_map = load_mpid_to_asset_name_map(platform_conn)
print(mpid_map["1234567890"])  # Output: "Wind Farm A"
Source code in physical_operations_utils/portfolio_utils.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def load_mpid_to_asset_name_map(platform_conn: AzureSqlConnection) -> Dict[str, str]:
    """
    Loads a mapping of MPIDs to asset common names from cis_prod in the Django platform database.

    Queries the `structure_cis_prod` table to retrieve all rows where both `mpid` and
    `common_name` are not null. Returns a dictionary mapping each `mpid` to its corresponding
    `common_name`.

    Args:
        platform_conn (AzureSqlConnection): A connection wrapper with an SQLAlchemy engine for the Django platform database.

    Returns:
        Dict[str, str]: A dictionary where the keys are MPIDs and the values are asset common names.

    Example:
        ```python
        from physical_operations_utils.sql_utils import load_mpid_to_asset_name_map
        from physical_operations_utils.azure_utils.azure_sql_connection import get_db_connection
        from physical_operations_utils.azure_utils.environment_utils import setup_environment

        environment = setup_environment()
        platform_conn = get_db_connection(f"physical_trading_platform_django_{environment}")
        mpid_map = load_mpid_to_asset_name_map(platform_conn)
        print(mpid_map["1234567890"])  # Output: "Wind Farm A"
        ```
    """
    query = """
    SELECT
        mpid,
        common_name
    FROM
        structure_cis_prod
    WHERE mpid IS NOT NULL AND common_name IS NOT NULL
    """
    df = pandas_read_sql_with_retry(db_engine=platform_conn.engine, query=query)
    mpid_to_asset_name_map = dict(zip(df["mpid"], df["common_name"]))
    return mpid_to_asset_name_map