Class: ForestAdminDatasourceRpc::Datasource

Inherits:
ForestAdminDatasourceToolkit::Datasource
  • Object
show all
Includes:
Utils
Defined in:
lib/forest_admin_datasource_rpc/datasource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, introspection, schema_polling_client = nil) ⇒ Datasource

Returns a new instance of Datasource.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/forest_admin_datasource_rpc/datasource.rb', line 7

def initialize(options, introspection, schema_polling_client = nil)
  super()

  ForestAdminAgent::Facades::Container.logger.log(
    'Info',
    "Building Rpc Datasource with #{introspection[:collections].length} " \
    "collections and #{introspection[:charts].length} charts."
  )

  @shared_rpc_client = RpcClient.new(
    options[:uri],
    options[:auth_secret] || ForestAdminAgent::Facades::Container.cache(:auth_secret)
  )

  introspection[:collections].each do |schema|
    add_collection(Collection.new(self, schema[:name], schema))
  end

  @charts = introspection[:charts]
  @rpc_relations = introspection[:rpc_relations]
  @schema_polling_client = schema_polling_client
  @cleaned_up = false

  native_query_connections = introspection[:native_query_connections] || []
  @live_query_connections = native_query_connections.to_h { |conn| [conn[:name], conn[:name]] }

  @schema = { charts: @charts }

  # Register shutdown hook to cleanup schema polling gracefully
  register_shutdown_hook if @schema_polling_client
end

Instance Attribute Details

#rpc_relationsObject (readonly)

Returns the value of attribute rpc_relations.



5
6
7
# File 'lib/forest_admin_datasource_rpc/datasource.rb', line 5

def rpc_relations
  @rpc_relations
end

#shared_rpc_clientObject (readonly)

Returns the value of attribute shared_rpc_client.



5
6
7
# File 'lib/forest_admin_datasource_rpc/datasource.rb', line 5

def shared_rpc_client
  @shared_rpc_client
end

Instance Method Details

#build_binding_symbol(connection_name, binds) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/forest_admin_datasource_rpc/datasource.rb', line 66

def build_binding_symbol(connection_name, binds)
  url = 'forest/rpc-binding-symbol'

  ForestAdminAgent::Facades::Container.logger&.log(
    'Debug',
    "Requesting a binding symbol for connection '#{connection_name}' from the Rpc agent on #{url}."
  )

  @shared_rpc_client.call_rpc(
    url,
    method: :post,
    payload: { connection_name: connection_name, binds_count: binds.size }
  )
rescue ForestAdminAgent::Http::Exceptions::NotFoundError
  raise ForestAdminDatasourceToolkit::Exceptions::ForestException,
        'Upgrade forest_admin_rpc_agent: this version does not support binding symbols yet.'
end

#cleanupObject



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/forest_admin_datasource_rpc/datasource.rb', line 100

def cleanup
  return if @cleaned_up

  @cleaned_up = true

  if @schema_polling_client
    log_info('[RPCDatasource] Stopping schema polling...')
    @schema_polling_client.stop
    log_info('[RPCDatasource] Schema polling stopped')
  end
rescue StandardError => e
  log_error("[RPCDatasource] Error during cleanup: #{e.class} - #{e.message}")
end

#execute_native_query(connection_name, query, binds) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/forest_admin_datasource_rpc/datasource.rb', line 50

def execute_native_query(connection_name, query, binds)
  url = 'forest/rpc-native-query'

  ForestAdminAgent::Facades::Container.logger.log(
    'Debug',
    "Forwarding native query for connection '#{connection_name}' to the Rpc agent on #{url}."
  )

  result = @shared_rpc_client.call_rpc(
    url,
    method: :post,
    payload: { connection_name: connection_name, query: query, binds: binds }
  )
  ForestAdminDatasourceToolkit::Utils::HashHelper.convert_keys(result.to_a)
end

#refresh!(new_schema) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/forest_admin_datasource_rpc/datasource.rb', line 84

def refresh!(new_schema)
  # Replace collections with those from the new schema
  @collections = {}
  new_schema[:collections].each do |collection|
    add_collection(Collection.new(self, collection[:name], collection))
  end

  @charts = new_schema[:charts]
  @rpc_relations = new_schema[:rpc_relations]

  native_query_connections = new_schema[:native_query_connections] || []
  @live_query_connections = native_query_connections.to_h { |conn| [conn[:name], conn[:name]] }

  @schema = { charts: @charts }
end

#render_chart(caller, name, parameters = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/forest_admin_datasource_rpc/datasource.rb', line 39

def render_chart(caller, name, parameters = {})
  url = 'forest/rpc-datasource-chart'

  ForestAdminAgent::Facades::Container.logger.log(
    'Debug',
    "Forwarding datasource chart '#{name}' call to the Rpc agent on #{url}."
  )

  @shared_rpc_client.call_rpc(url, caller: caller, method: :post, payload: { chart: name, parameters: parameters })
end