# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/02_catalog.ipynb.

# %% auto #0
__all__ = ['GPUCatalog']

# %% ../nbs/02_catalog.ipynb #82b62621-7175-4ae2-a472-d373d761c3ef
from loguru import logger
from tabulate import tabulate


# %% ../nbs/02_catalog.ipynb #edd58877-acd6-47ae-b904-622a45646aad
class GPUCatalog():
    def __init__(self, catalog_dict:dict, region:str, subsidiary:str='FR'):
        """
        OVH Project class, storing project information
    
        Parameters
        ----------
        catalog_dict : dict, required
            OVH Catalog dictionary
        region : str, required
            OVH Catalog region
        subsidiary : str, required
            OVH Catalog subsidiary/country, default set to FR (France)
        consumption_mode: str, required
            OVH Ressource consumption mode, default set to consumption. Possible values ['consumption', 'monthly.postpaid']
        """
        self.region = region
        self.subsidiary = subsidiary
        self.catalog_dict = catalog_dict
        self.consumption_mode = None
        self.gpu_families =  ['a10', 'rtx5000', 't1', 't2', 'l4', 'h100']
        self.ressource_type = 'instance'
        self._project_plans = []
        self._instances_addons = []
        self._gpu_addons = []
        self.gpu_instances_data = []

    def _list_project_plans(self):
        """
        Lists OVH Catalog Plans
        """ 
        self._project_plans = [p for p in self.catalog_dict['plans'] if (p['planCode']=='project' and p['invoiceName']=='Public Cloud Project')]

    def _list_addons(self):
        """
        Lists OVH Plans addons
        """ 
        self._instances_addons = [p for p in self._project_plans[0]['addonFamilies'] if p['name']==self.ressource_type]

    def _list_gpu_addons(self, consumption_mode:str='consumption'):
        """
        Lists OVH GPU Addons
        """ 
        dict_consumption_mode = {
            'consumption': 'consumption',
            'monthly.postpaid': 'monthly'
        }
        self._gpu_addons = [i for i in self._instances_addons[0]['addons'] 
                           if (i.split('.')[0].split('-')[0] in self.gpu_families) 
                           and (dict_consumption_mode.get(consumption_mode) in i.split('.')[1])]

    def list_gpu_instances(self, consumption_mode:str='consumption'):
        """
        Lists available gpu instances for the OVH Catalog. It displays a table containing:
        - Name
        - Price (hourly or monthly depending on consumption mode)
        - GPU Model
        - GPU Memory
        - CPU Cores
        - RAM

        Parameters
        ----------
        consumption_mode : str, required, default to consumption (consumption or monthly.postpaid) 
            GPU instance consumption mode
        """
        self._list_project_plans()
        self._list_addons()
        self._list_gpu_addons(consumption_mode)

        dict_consumption_mode = {
            'consumption': 'per hour',
            'monthly.postpaid': 'per month'
        }
        
        self.gpu_instances_data = []
        for addon_code in sorted(self._gpu_addons):
            addon = [p for p in self.catalog_dict['addons'] if p['planCode'] == addon_code]
            if addon:
                a = addon[0]
                self.gpu_instances_data.append({
                    'Name': a['invoiceName'],
                    f'Price {dict_consumption_mode.get(consumption_mode)}': a['pricings'][0]['formattedPrice'],
                    'GPU Model': a['blobs']['technical']['gpu']['model'],
                    'GPU Memory': f"{a['blobs']['technical']['gpu']['memory']['size']}GB",
                    'CPU Cores': a['blobs']['technical']['cpu']['cores'],
                    'RAM': f"{a['blobs']['technical']['memory']['size']}GB"
                })
    
        logger.info("Available GPU instances:")
        print(tabulate(self.gpu_instances_data, headers='keys', tablefmt='grid', showindex=range(1, len(self.gpu_instances_data)+1)))    
        
