# -*- coding: utf-8 -*-
"""
Function: get_sys_performance.py moudle. This moudle mainly involves the
 querying System Performance function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2020-2021
"""
from scripts import common_function

LENGTH = 26
MFORMAT = '%-*s:%s'


def getsysperformance_init(parser, parser_list):
    """
    #=========================================================================
    #   @Description: get system performance subcommand init
    #   @Method:  getsysperformance_init
    #   @Param:
    #   @Return:
    #   @Date:
    #=========================================================================
    """

    sub_parser = parser.add_parser('getsysperf',
                                   help='get system performance information')
    parser_list['getsysperf'] = sub_parser

    return 'getsysperf'


def _printpartitionlists(partitionlists):
    """
    #=========================================================================
    #   @Description:  print partition lists
    #   @Method:  _printpartitionlists
    #   @Param:
    #   @Return:
    #   @Date:
    #=========================================================================
    """
    len_list = len(partitionlists)
    if partitionlists is None or len_list == 0:
        print('[PartitionLists]')
        print('')
        return

    print('[PartitionLists]')
    for partition_info in partitionlists:
        partitionname = partition_info.get('PartitionName', '')
        usagepercent = partition_info.get('UsagePercent', '')
        print('-' * 50)
        print(MFORMAT % (LENGTH, 'PartitionName', partitionname))
        print(MFORMAT % (LENGTH, 'UsagePercent', usagepercent))

        if partition_info == partitionlists[-1]:
            print('-' * 50)
            print('')


def getsysperformance(client, _):
    """
    #=========================================================================
    #   @Description:  get system performance
    #   @Method:  getsysperformance
    #   @Param:
    #   @Return:
    #   @Date:
    #=========================================================================
    """
    slotid = client.get_slotid()

    if slotid is None:
        return None

    url = "/redfish/v1/systems/%s" % slotid
    resp = client.get_resource(url)

    if resp is None:
        return None

    if resp.get('status_code') == 200:

        if 'Oem' not in resp['resource']:
            print('no data available for the resource')
            return resp

        resp_inner_dic = common_function.get_vendor_value(resp)

        cpusage = resp_inner_dic.get('CPUUsagePercent', '')
        memoryssage = resp_inner_dic.get('MemoryUsagePercent', '')
        partitionlists = resp_inner_dic.get('PartitionLists', '')
        cputhreshold = resp_inner_dic.get('CPUThresholdPercent', '')
        memorythreshold = resp_inner_dic.get('MemoryThresholdPercent', '')
        diskthreshold = resp_inner_dic.get('HardDiskThresholdPercent', '')

        print(MFORMAT % (LENGTH, 'CPUUsagePercent', cpusage))
        print(MFORMAT % (LENGTH, 'MemoryUsagePercent', memoryssage))

        print('')
        _printpartitionlists(partitionlists)

        print(MFORMAT % (LENGTH, 'CPUthresholdPercent', cputhreshold))
        print(MFORMAT % (LENGTH, 'MemorythresholdPercent', memorythreshold))
        print(MFORMAT % (LENGTH, 'HardDiskthresholdPercent', diskthreshold))

    return resp
