Class: ForestAdminDatasourceRpc::Datasource

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

Instance Method Summary collapse

Constructor Details

#initialize(options, introspection) ⇒ Datasource

Returns a new instance of Datasource.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/forest_admin_datasource_rpc/datasource.rb', line 5

def initialize(options, introspection)
  super()

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

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

  @options = options
  @charts = introspection[:charts]
  @rpc_relations = introspection[:rpc_relations]

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

  @schema = { charts: @charts }
end

Instance Method Details

#execute_native_query(connection_name, query, binds) ⇒ Object



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

def execute_native_query(connection_name, query, binds)
  client = RpcClient.new(@options[:uri], ForestAdminRpcAgent::Facades::Container.cache(:auth_secret))
  url = 'forest/rpc/native-query'

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

  client.call_rpc(url, method: :post, payload: { connection_name: connection_name, query: query, binds: binds })
end

#render_chart(caller, name) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/forest_admin_datasource_rpc/datasource.rb', line 28

def render_chart(caller, name)
  client = RpcClient.new(@options[:uri], ForestAdminRpcAgent::Facades::Container.cache(:auth_secret))
  url = 'forest/rpc/datasource-chart'

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

  client.call_rpc(url, method: :post, payload: { chart: name, caller: caller.to_h })
end