|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Multiwoven::Integrations::Source |
| 4 | + module AwsSagemakerModel |
| 5 | + include Multiwoven::Integrations::Core |
| 6 | + class Client < SourceConnector |
| 7 | + def check_connection(connection_config) |
| 8 | + connection_config = connection_config.with_indifferent_access |
| 9 | + create_connection(connection_config) |
| 10 | + response = @client.describe_endpoint(endpoint_name: connection_config[:endpoint_name]) |
| 11 | + if response.endpoint_status == "InService" |
| 12 | + success_status |
| 13 | + else |
| 14 | + failure_status |
| 15 | + end |
| 16 | + rescue StandardError => e |
| 17 | + ConnectionStatus.new(status: ConnectionStatusType["failed"], message: e.message).to_multiwoven_message |
| 18 | + end |
| 19 | + |
| 20 | + def discover(_connection_config) |
| 21 | + catalog_json = read_json(CATALOG_SPEC_PATH) |
| 22 | + catalog = build_catalog(catalog_json) |
| 23 | + catalog.to_multiwoven_message |
| 24 | + rescue StandardError => e |
| 25 | + handle_exception(e, { |
| 26 | + context: "AWS:SAGEMAKER MODEL:DISCOVER:EXCEPTION", |
| 27 | + type: "error" |
| 28 | + }) |
| 29 | + end |
| 30 | + |
| 31 | + def read(sync_config) |
| 32 | + connection_config = sync_config.source.connection_specification |
| 33 | + connection_config = connection_config.with_indifferent_access |
| 34 | + payload = sync_config.model.query |
| 35 | + create_connection(connection_config) |
| 36 | + run_model(connection_config, payload) |
| 37 | + rescue StandardError => e |
| 38 | + handle_exception(e, { |
| 39 | + context: "AWS:SAGEMAKER MODEL:READ:EXCEPTION", |
| 40 | + type: "error", |
| 41 | + sync_id: sync_config.sync_id, |
| 42 | + sync_run_id: sync_config.sync_run_id |
| 43 | + }) |
| 44 | + end |
| 45 | + |
| 46 | + private |
| 47 | + |
| 48 | + def create_connection(connection_config) |
| 49 | + @client = Aws::SageMaker::Client.new( |
| 50 | + region: connection_config[:region], |
| 51 | + access_key_id: connection_config[:access_key], |
| 52 | + secret_access_key: connection_config[:secret_access_key] |
| 53 | + ) |
| 54 | + |
| 55 | + @client_runtime = Aws::SageMakerRuntime::Client.new( |
| 56 | + region: connection_config[:region], |
| 57 | + access_key_id: connection_config[:access_key], |
| 58 | + secret_access_key: connection_config[:secret_access_key] |
| 59 | + ) |
| 60 | + end |
| 61 | + |
| 62 | + def run_model(connection_config, payload) |
| 63 | + response = @client_runtime.invoke_endpoint( |
| 64 | + endpoint_name: connection_config[:endpoint_name], |
| 65 | + content_type: "application/json", |
| 66 | + body: payload |
| 67 | + ) |
| 68 | + process_response(response) |
| 69 | + rescue StandardError => e |
| 70 | + handle_exception(e, context: "AWS:SAGEMAKER MODEL:RUN_MODEL:EXCEPTION", type: "error") |
| 71 | + end |
| 72 | + |
| 73 | + def process_response(response) |
| 74 | + data = JSON.parse(response.body.read) |
| 75 | + [RecordMessage.new(data: { response: data }, emitted_at: Time.now.to_i).to_multiwoven_message] |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | +end |
0 commit comments