Class: Outhad::Integrations::Destination::Qdrant::Client

Inherits:
DestinationConnector
  • Object
show all
Defined in:
lib/outhad/integrations/destination/qdrant/client.rb

Instance Method Summary collapse

Instance Method Details

#check_connection(connection_config) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/outhad/integrations/destination/qdrant/client.rb', line 7

def check_connection(connection_config)
  connection_config = connection_config.with_indifferent_access
  api_url = connection_config[:api_url]
  api_key = connection_config[:api_key]

  response = Outhad::Integrations::Core::HttpClient.request(
    api_url,
    HTTP_GET,
    headers: auth_headers(api_key)
  )
  if success?(response)
    success_status
  else
    failure_status(nil)
  end
rescue StandardError => e
  handle_exception(e, {
                     context: "QDRANT:CHECK_CONNECTION:EXCEPTION",
                     type: "error"
                   })
  failure_status(e)
end

#discover(connection_config = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/outhad/integrations/destination/qdrant/client.rb', line 30

def discover(connection_config = nil)
  connection_config = connection_config.with_indifferent_access
  @api_url = connection_config[:api_url]
  @api_key = connection_config[:api_key]

  response = Outhad::Integrations::Core::HttpClient.request(
    "#{@api_url}/collections",
    HTTP_GET,
    headers: auth_headers(@api_key)
  )

  data = JSON.parse(response.body)
  catalog = build_catalog(data)
  catalog.to_outhad_message
rescue StandardError => e
  handle_exception(e, {
                     context: "QDRANT:DISCOVER:EXCEPTION",
                     type: "error"
                   })
end

#write(sync_config, records, _action = "upsert") ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/outhad/integrations/destination/qdrant/client.rb', line 51

def write(sync_config, records, _action = "upsert")
  connection_config = sync_config.destination.connection_specification.with_indifferent_access
  collection_name = sync_config.stream.name
  primary_key = sync_config.model.primary_key
  log_message_array = []

  api_url = connection_config[:api_url]
  api_key = connection_config[:api_key]

  write_success = 0
  write_failure = 0
  records.each do |record|
    points = []
    points.push({
                  id: record[primary_key],
                  vector: JSON.parse(record["vector"]),
                  payload: record["payload"]
                })
    begin
      response = upsert_points(api_url, api_key, collection_name, { points: points })
      if success?(response)
        write_success += 1
        log_message_array << log_request_response("info", { points: points }, JSON.parse(response.body))
      else
        # write_failure could be duplicated if JSON.parse errors.
        write_failure += 1
        log_message_array << log_request_response("error", { points: points }, JSON.parse(response.body))
      end
    rescue StandardError => e
      handle_exception(e, {
                         context: "QDRANT:RECORD:WRITE:EXCEPTION",
                         type: "error",
                         sync_id: sync_config.sync_id,
                         sync_run_id: sync_config.sync_run_id
                       })
      write_failure += 1
      log_message_array << log_request_response("error", { points: points }, e.message)
    end
  end
  tracking_message(write_success, write_failure, log_message_array)
rescue StandardError => e
  handle_exception(e, {
                     context: "QDRANT:RECORD:WRITE:EXCEPTION",
                     type: "error",
                     sync_id: sync_config.sync_id,
                     sync_run_id: sync_config.sync_run_id
                   })
end