def get_knowledge_graphs(): """Retrieves Knowledge Graphs from Synthesys""" SynicAPI.check_connection() try: r = requests.get(SynicAPI.synic_api_url('kb')) if r.status_code == 200: response = r.json() kg_list = [] for kg in response: name = kg.get('name') if name is not None: kg_list.append(name) log.debug('Synic API /kb response', extra=kg_list) return kg_list else: raise Exception('Synic API List Knowledge Graphs request returned code {}'.format(r.status_code)) except Exception as e: print(str(e)) raise e
def check_connection(): """Checks Synthesys Connection""" try: r = requests.get(SynicAPI.synic_api_url('app')) if r.status_code == 200: response = r.json() log.debug('Synic API Version: {}'.format(response.get('version'))) return response else: raise Exception('Synic API connection health check returned code {}'.format(r.status_code)) except Exception as e: print(str(e)) raise e
def get_applications(): """Retrieves Synthesys Application list""" SynicAPI.check_connection() try: r = requests.get(SynicAPI.synic_api_url('application')) if r.status_code == 200: response = r.json() applications = {a['name']:a for a in response} log.debug('Synic API /application count: {}'.format(len(applications))) return applications else: raise Exception('Synic API List Applications request returned code {}'.format(r.status_code)) except Exception as e: print(str(e)) raise e
def initialize_settings(): """Initializes application settings""" log.debug('Initializing application settings') with session_scope() as session: for setting in ApplicationDefaults.list(): try: existing_setting = Query.Setting.get(session, setting.id) if existing_setting is None: log.debug('Initializing application default: {} -> {}'.format(setting.id, setting.safe_value())) session.add(setting) else: log.debug( 'Application default exists: {} -> {}'.format(setting.id, setting.safe_value())) except Exception as e: log.error('Failed to initialize setting: {} due to: {}'.format(setting.id, str(e))) log.debug('Application setting initialization complete')
def initialize_attributes(): """Initializes default attributes""" log.debug('Initializing attribute defaults') with session_scope() as session: attr_templates = load_yml(FileConfig.ATTR_SCHEMA_FILE) for attr_id, attr_template in attr_templates.items(): try: attribute = Attribute(id=attr_id, name=attr_template['name'], required=attr_template.get('required', False), arity=attr_template['arity'], ko_name=attr_template['ko_name'], description=attr_template['description'], type=attr_template['type']) attribute_existing = Query.Attribute.get(session, attr_id) if attribute_existing is None: log.debug('Initializing attribute: {}'.format(attribute.name)) session.add(attribute) # else: # log.debug('Attribute exists, skipping: {}'.format(attribute.name)) for field_id in attr_template['fields']: attr_field = Query.AttributeField.get(session, field_id) if attr_field is not None: linked_attr_field = LinkedAttributeField(attribute.id, attr_field.id) linked_attr_field_existing = Query.LinkedAttributeField.get(session, linked_attr_field.id) if linked_attr_field_existing is None: log.debug('Creating linked attribute field {} for attribute {}'.format(attr_field.name, attribute.name)) session.add(linked_attr_field) else: log.error('Attribute field {} is not defined in field definitions. Please add field to ' 'enumeration located in definitions.py. Skipping for now.'.format(field_id)) except Exception as e: log.error('Failed to initialize attribute: {} due to: {}'.format(attr_id, str(e))) log.debug('Attribute initialization complete')
def initialize_entity_schema(): """Initializes entity schema""" log.debug('Initializing entity schema defaults') with session_scope() as session: for defaults in AttributeTypes.list(), EntityTypes.list(), ArityTypes.list(), AttributeFields.list(): for default in defaults: def_type = type(default) try: def_existing = session.query(def_type).get(default.id) if def_existing is None: log.debug('Initializing default {}: {}'.format(def_type.__name__, default.name)) session.add(default) # else: # log.debug('Entry exists for {}: {}'.format(def_type.__name__, default.name)) except Exception as e: log.error( 'Failed to add default {}: {} due to: {}'.format(def_type.__name__, default.name, str(e))) session.rollback() log.debug('Entity initialization complete')
def ingest(kg_name, test=False): """Ingests an exported Entities XML file into Synthesys :param kg_name: Name of the Knowledge Graph to ingest into :param test: Test parameter used to bypass function :return: Task ID of the resulting process """ export_filename = os.path.join(AppConfig.INSTALL_DIR, AppConfig.RELATIVE_EXPORT_DIR) log.debug('Absolute path of export file on host machine: {}'.format(export_filename)) if not SynicAPI.has_ee_pipeline(): raise Exception('Synthesys instance is missing the External Entity Pipeline') params = { 'kb': kg_name, 'processType': 'jet', 'application': SynicAPI.EE_PIPELINE_NAME, 'invocationConfig': { 'input': export_filename, 'schemaResource': SynicAPI.ENTITY_SCHEMA_ENGINE, 'kgName': kg_name } } log.debug('Ingestion params', extra=params) log.debug('Ingestion URL: {}'.format(SynicAPI.synic_api_url('process', exclude_auth=True))) if test: log.debug('*** API Request to /process endpoint is being simulated ***') response = { "id": "test-request", "kb": kg_name, "application": "externalEntityPipeline", "applicationReadableName": "externalEntityPipeline", "processType": "jet", "username": AppConfig.SYNTHESYS_USER, "requestedTime": "2018-04-16T19:47:39.563Z", "startedTime": None, "completedTime": None, "status": "PENDING", "steps": [], "failure": None, "invocationConfig": { "input": export_filename, "schemaResource": SynicAPI.ENTITY_SCHEMA_ENGINE, "kgName": kg_name }, "stackTrace": None, "allowedCommands": [ "CANCEL", "CANCEL" ], "issuedCommand": None } else: r = requests.post(SynicAPI.synic_api_url('process', exclude_auth=True), json=params, auth=HTTPBasicAuth( AppConfig.SYNTHESYS_USER, AppConfig.SYNTHESYS_PASS )) if r.status_code == 200: response = r.json() else: raise Exception('Synic API EE Ingestion request returned code {}'.format(r.status_code)) task_id = response.get('id') if task_id is None: raise Exception('Synic API EE Ingestion response is missing Task ID') extra = { 'request': params, 'response': response, 'task_id': task_id } log.debug('Synic API /process request', extra=extra) return task_id
def has_ee_pipeline(): """Checks Synthesys for External Entity Pipeline""" applications = SynicAPI.get_applications() is_present = applications.get(SynicAPI.EE_PIPELINE_NAME) != None log.debug('EE Pipeline is present: {}'.format(is_present)) return is_present