Commit cedf6824 authored by Waleed Akbar's avatar Waleed Akbar
Browse files

changes for testing KPI manager and value API

parent 4c2f3c9d
Loading
Loading
Loading
Loading
+12 −20
Original line number Diff line number Diff line
@@ -32,29 +32,29 @@ class IDNotFoundError(Exception):

class KpiManagerServiceServicerImpl(KpiManagerServiceServicer):
    def __init__(self, name_mapping : NameMapping):
        LOGGER.debug('Init KpiManagerService')
        LOGGER.info('Init KpiManagerService')
        self.kpi_db_obj = KpiDB()
    
    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    def SetKpiDescriptor(self, request: KpiDescriptor, grpc_context: grpc.ServicerContext # type: ignore
                        ) -> KpiId: # type: ignore
        response = KpiId()
        LOGGER.debug("Received gRPC message object: {:}".format(request))
        LOGGER.info("Received gRPC message object: {:}".format(request))
        try:
            kpi_to_insert = KpiModel.convert_KpiDescriptor_to_row(request)
            if(self.kpi_db_obj.add_row_to_db(kpi_to_insert)):
                response.kpi_id.uuid = request.kpi_id.kpi_id.uuid
                # LOGGER.debug("Added Row: {:}".format(response))
                # LOGGER.info("Added Row: {:}".format(response))
            return response
        except Exception as e:
            LOGGER.debug("Unable to create KpiModel class object. {:}".format(e))
            LOGGER.info("Unable to create KpiModel class object. {:}".format(e))
    
    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)        
    def GetKpiDescriptor(self, request: KpiId, grpc_context: grpc.ServicerContext # type: ignore
                         ) -> KpiDescriptor: # type: ignore
        response = KpiDescriptor()
        print("--> Received gRPC message object: {:}".format(request))
        LOGGER.debug("Received gRPC message object: {:}".format(request))
        LOGGER.info("Received gRPC message object: {:}".format(request))
        try: 
            kpi_id_to_search = request.kpi_id.uuid
            row = self.kpi_db_obj.search_db_row_by_id(KpiModel, 'kpi_id', kpi_id_to_search)
@@ -63,46 +63,38 @@ class KpiManagerServiceServicerImpl(KpiManagerServiceServicer):
                return response
            if row is None:
                print ('No matching row found for kpi id: {:}'.format(kpi_id_to_search))
                LOGGER.debug('No matching row found kpi id: {:}'.format(kpi_id_to_search))
                LOGGER.info('No matching row found kpi id: {:}'.format(kpi_id_to_search))
                return Empty()
        except Exception as e:
            print ('Unable to search kpi id. {:}'.format(e))
            LOGGER.debug('Unable to search kpi id. {:}'.format(e))
            LOGGER.info('Unable to search kpi id. {:}'.format(e))
            raise e
        # kpi_id_to_search = request.kpi_id.uuid
        # row = self.kpi_db_obj.search_db_row_by_id(KpiModel, 'kpi_id', kpi_id_to_search)
        # if row is None:
        #     print ('Unable to search kpi id. {:}'.format(kpi_id_to_search))
        #     LOGGER.debug('Unable to search kpi id. {:}'.format(kpi_id_to_search))
        #     raise IDNotFoundError
        # response = KpiModel.convert_row_to_KpiDescriptor(row)
        # return response
    
    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    def DeleteKpiDescriptor(self, request: KpiId, grpc_context: grpc.ServicerContext # type: ignore
                            ) -> Empty: # type: ignore
        LOGGER.debug("Received gRPC message object: {:}".format(request))
        LOGGER.info("Received gRPC message object: {:}".format(request))
        try:
            kpi_id_to_search = request.kpi_id.uuid
            self.kpi_db_obj.delete_db_row_by_id(KpiModel, 'kpi_id', kpi_id_to_search)
        except Exception as e:
            LOGGER.debug('Unable to search kpi id. {:}'.format(e))
            LOGGER.info('Unable to search kpi id. {:}'.format(e))
        finally:
            return Empty()

    @safe_and_metered_rpc_method(METRICS_POOL, LOGGER)
    def SelectKpiDescriptor(self, filter: KpiDescriptorFilter, grpc_context: grpc.ServicerContext # type: ignore
                            ) -> KpiDescriptorList: # type: ignore
        LOGGER.debug("Received gRPC message object: {:}".format(filter))
        LOGGER.info("Received gRPC message object: {:}".format(filter))
        response = KpiDescriptorList()
        try:
            rows = self.kpi_db_obj.select_with_filter(KpiModel, filter)
        except Exception as e:
            LOGGER.debug('Unable to apply filter on kpi descriptor. {:}'.format(e))
            LOGGER.info('Unable to apply filter on kpi descriptor. {:}'.format(e))
        try:
            for row in rows:
                kpiDescriptor_obj = KpiModel.convert_row_to_KpiDescriptor(row)
                response.kpi_descriptor_list.append(kpiDescriptor_obj)
            return response
        except Exception as e:
            LOGGER.debug('Unable to process filter response {:}'.format(e))
            LOGGER.info('Unable to process filter response {:}'.format(e))
+6 −6
Original line number Diff line number Diff line
@@ -102,11 +102,11 @@ class KpiValueWriter:

            print("kpi descriptor received: {:}".format(kpi_descriptor_object))
            if isinstance (kpi_descriptor_object, KpiDescriptor):
                LOGGER.info("Extracted row: {:}".format(kpi_descriptor_object))
                print("Extracted row: {:}".format(kpi_descriptor_object))
                LOGGER.info("Extracted KpiDescriptor: {:}".format(kpi_descriptor_object))
                print("Extracted KpiDescriptor: {:}".format(kpi_descriptor_object))
            else:
                LOGGER.info("Error in extracting row {:}".format(kpi_descriptor_object))
                print("Error in extracting row {:}".format(kpi_descriptor_object))
                LOGGER.info("Error in extracting KpiDescriptor {:}".format(kpi_descriptor_object))
                print("Error in extracting KpiDescriptor {:}".format(kpi_descriptor_object))
        except Exception as e:
            LOGGER.info("Unable to get Descriptor. Error: {:}".format(e))
            print ("Unable to get Descriptor. Error: {:}".format(e))
            LOGGER.info("Unable to get KpiDescriptor. Error: {:}".format(e))
            print ("Unable to get KpiDescriptor. Error: {:}".format(e))
+3 −3
Original line number Diff line number Diff line
@@ -26,14 +26,14 @@ def test_GetKpiDescriptor():
    LOGGER.info(" >>> test_GetKpiDescriptor: START <<< ")
    kpi_manager_client = KpiManagerClient()
    # adding KPI
    LOGGER.info(" >>> calling SetKpiDescriptor ")
    LOGGER.info(" --->>> calling SetKpiDescriptor ")
    response_id = kpi_manager_client.SetKpiDescriptor(create_kpi_descriptor_request())
    # get KPI
    LOGGER.info(" >>> calling GetKpiDescriptor with response ID")
    LOGGER.info(" --->>> calling GetKpiDescriptor with response ID")
    response = kpi_manager_client.GetKpiDescriptor(response_id)
    LOGGER.info("Response gRPC message object: {:}".format(response))
    
    LOGGER.info(" >>> calling GetKpiDescriptor with random ID")
    LOGGER.info(" --->>> calling GetKpiDescriptor with random ID")
    rand_response = kpi_manager_client.GetKpiDescriptor(create_kpi_id_request())
    LOGGER.info("Response gRPC message object: {:}".format(rand_response))