Class: ChalkRuby::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/chalk_ruby/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chalk_config, opts = {}) ⇒ Client

Initializes the ChalkRuby client. Generally, you should not need to call this directly. Instead, use ChalkRuby::Client.create or ChalkRuby::Client.create_with_config.

Parameters:

  • chalk_config (ChalkRuby::Config)

    A ChalkRuby::Config object which contains your CLIENT_ID and CLIENT_SECRET

  • adapter (Hash)

    a customizable set of options

  • logger (Hash)

    a customizable set of options

  • http_requester (Hash)

    a customizable set of options



194
195
196
197
198
199
200
201
# File 'lib/chalk_ruby/client.rb', line 194

def initialize(chalk_config, opts = {})
  @token       = nil
  @config      = chalk_config
  adapter      = opts[:adapter] || Defaults::ADAPTER
  logger       = opts[:logger] || LoggerHelper.create
  requester    = opts[:http_requester] || Defaults::REQUESTER_CLASS.new(adapter: adapter, logger: logger)
  @transporter = Http::HttpRequesterChalk.new(requester: requester)
end

Class Method Details

.create(client_id = nil, client_secret = nil, environment = nil, query_server = nil, api_server = nil, additional_headers = {}) ⇒ Object

Create a new client.

Parameters:

  • client_id (String) (defaults to: nil)

    Chalk client ID. If not provided, it will be read from the CHALK_CLIENT_ID environment variable.

  • client_secret (String) (defaults to: nil)

    Chalk client secret. If not provided, it will be read from the CHALK_CLIENT_SECRET environment variable.

  • environment (String) (defaults to: nil)

    The Chalk environment id (not the name of the environment). If not provided, it will be read from the CHALK_ACTIVE_ENVIRONMENT environment variable.

  • query_server (String) (defaults to: nil)

    ChalkRuby query server

  • api_server (String) (defaults to: nil)

    ChalkRuby API server

  • additional_headers (Hash[String, String]) (defaults to: {})

    Additional headers to be sent with every request. Typically not required.

Returns:

  • self



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/chalk_ruby/client.rb', line 34

def self.create(
  client_id = nil,
  client_secret = nil,
  environment = nil,
  query_server = nil,
  api_server = nil,
  additional_headers = {}
)
  config = Config.new(
    client_id: client_id,
    client_secret: client_secret,
    environment: environment,
    query_server: query_server,
    api_server: api_server,
    additional_headers: additional_headers
  )
  create_with_config(config)
end

.create_with_config(config) ⇒ Object

Create a new client providing only a ChalkRuby::Config object

Parameters:

Returns:

  • self



59
60
61
# File 'lib/chalk_ruby/client.rb', line 59

def self.create_with_config(config)
  new(config)
end

Instance Method Details

#get_tokenObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/chalk_ruby/client.rb', line 165

def get_token
  Token.new(
    api_server_request(
      method: :post,
      path: '/v1/oauth/token',
      body: {
        client_id: @config.client_id,
        client_secret: @config.client_secret,
        grant_type: 'client_credentials'
      },
      headers: get_unauthenticated_headers
    )
  )
end

#query(input:, output:, now: nil, staleness: nil, tags: nil, branch: nil, correlation_id: nil, query_name: nil, meta: nil, explain: nil, include_meta: nil, store_plan_stages: nil) ⇒ Hash[Symbol, String]

Compute features values using online resolvers. See docs.chalk.ai/docs/query-basics for more information.

Parameters:

  • input (Hash[String, any])

    The features for which there are known values, mapped to those values.

  • output ([String])

    Outputs are the features that you’d like to compute from the inputs. For example, ‘[’user.age’, ‘user.name’, ‘user.email’]‘.

    If an empty sequence, the output will be set to all features on the namespace of the query. For example, if you pass as input ‘1234`, then the query is defined on the User namespace, and all features on the User namespace (excluding has-one and has-many relationships) will be used as outputs.

  • now (DateTime?) (defaults to: nil)

    The time at which to evaluate the query. If not specified, the current time will be used. This parameter is complex in the context of online_query since the online store only stores the most recent value of an entity’s features. If now is in the past, it is extremely likely that None will be returned for cache-only features.

    This parameter is primarily provided to support:

    - controlling the time window for aggregations over cached has-many relationships
    - controlling the time window for aggregations over has-many relationships loaded from an
      external database
    

    If you are trying to perform an exploratory analysis of past feature values, prefer offline_query.

  • staleness (Hash[String, String]?) (defaults to: nil)

    Maximum staleness overrides for any output features or intermediate features. See docs.chalk.ai/docs/query-caching for more information.

  • tags (Hash[String, String]?) (defaults to: nil)

    The tags used to scope the resolvers. See docs.chalk.ai/docs/resolver-tags for more information.

  • branch (String?) (defaults to: nil)

    If specified, Chalk will route your request to the relevant branch.

  • correlation_id (String?) (defaults to: nil)

    You can specify a correlation ID to be used in logs and web interfaces. This should be globally unique, i.e. a uuid or similar. Logs generated during the execution of your query will be tagged with this correlation id.

  • query_name (String?) (defaults to: nil)

    The semantic name for the query you’re making, for example, ‘“loan_application_model”`. Typically, each query that you make from your application should have a name. Chalk will present metrics and dashboard functionality grouped by ’query_name’.

  • meta (Hash[String, String]?) (defaults to: nil)

    Arbitrary key:value pairs to associate with a query.

  • explain (Boolean?) (defaults to: nil)

    Log the query execution plan. Requests using ‘explain=true` will be slower than requests using `explain=false`. If `“only”`, the query will not be executed, and only the query plan will be returned.

    If true, ‘include_meta’ will be set to true as well.

  • include_meta (Boolean?) (defaults to: nil)

    Whether to include metadata about the query execution.

  • store_plan_stages (Boolean?) (defaults to: nil)

    If true, the output of each of the query plan stages will be stored. This option dramatically impacts the performance of the query, so it should only be used for debugging.

Returns:

  • (Hash[Symbol, String])


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/chalk_ruby/client.rb', line 130

def query(
  input:,
  output:,
  now: nil,
  staleness: nil,
  tags: nil,
  branch: nil,
  correlation_id: nil,
  query_name: nil,
  meta: nil,
  explain: nil,
  include_meta: nil,
  store_plan_stages: nil
)
  query_server_request(
    method: :post,
    path: 'v1/query/online',
    body: {
      inputs: input,
      outputs: output,
      now: now,
      staleness: staleness,
      context: tags && { tags: tags },
      branch_id: branch,
      correlation_id: correlation_id,
      query_name: query_name,
      meta: meta,
      explain: explain || false,
      include_meta: include_meta || false,
      store_plan_stages: store_plan_stages || false
    },
    headers: get_authenticated_engine_headers(branch: branch)
  )
end