Class: Bitfab::Datasets
- Inherits:
-
Object
- Object
- Bitfab::Datasets
- Defined in:
- lib/bitfab/datasets.rb
Overview
Dataset operations for the authenticated organization, reached as
client.datasets. A dataset is a named bucket of traces scoped to one
trace function. Experiments replay against it and its graders score its
members. Every method returns the parsed JSON response as a Hash with
string keys matching the HTTP API ("traceCount", "addedTraceIds", ...).
Constant Summary collapse
- DEFAULT_RERUN_TIMEOUT_SECONDS =
90- DEFAULT_RERUN_POLL_INTERVAL_SECONDS =
1- TERMINAL_RERUN_STATUSES =
["completed", "errored"].freeze
Instance Method Summary collapse
-
#add_graders(dataset_id, grader_ids) ⇒ Object
Assign graders to the dataset (1 to 100 ids per call).
-
#add_traces(dataset_id, trace_ids) ⇒ Object
Add traces to the dataset (1 to 100 ids per call).
-
#get(dataset_id) ⇒ Object
Fetch one dataset by id.
-
#get_grader_rerun(dataset_id, run_id: nil) ⇒ Object
The dataset's active grader re-run, or the run named by run_id:.
-
#initialize(http_client) ⇒ Datasets
constructor
A new instance of Datasets.
-
#list(trace_function_key: nil) ⇒ Object
List datasets, scoped to one trace function when given and organization-wide otherwise.
-
#list_traces(dataset_id) ⇒ Object
The ids of every trace in the dataset, the same membership a replay with dataset_id: selects.
-
#remove_graders(dataset_id, grader_ids) ⇒ Object
Unassign graders from the dataset.
-
#remove_traces(dataset_id, trace_ids) ⇒ Object
Remove traces from the dataset.
-
#rerun_graders(dataset_id, grader_ids: nil, wait: true, timeout: DEFAULT_RERUN_TIMEOUT_SECONDS, poll_interval: DEFAULT_RERUN_POLL_INTERVAL_SECONDS) ⇒ Object
Re-run graders over every trace in the dataset.
-
#save(trace_function_key:, name:, description: nil) ⇒ Object
Create a dataset, or update the one already named this way under the same trace function.
Constructor Details
#initialize(http_client) ⇒ Datasets
Returns a new instance of Datasets.
14 15 16 |
# File 'lib/bitfab/datasets.rb', line 14 def initialize(http_client) @http_client = http_client end |
Instance Method Details
#add_graders(dataset_id, grader_ids) ⇒ Object
Assign graders to the dataset (1 to 100 ids per call). Graders outside the organization or under another trace function are reported in "skippedGraderIds" rather than failing the call.
61 62 63 |
# File 'lib/bitfab/datasets.rb', line 61 def add_graders(dataset_id, grader_ids) @http_client.request(dataset_path(dataset_id, "/graders"), {"graderIds" => grader_ids}) end |
#add_traces(dataset_id, trace_ids) ⇒ Object
Add traces to the dataset (1 to 100 ids per call). Traces outside the organization or under another trace function are reported in "skippedTraceIds" rather than failing the call.
49 50 51 |
# File 'lib/bitfab/datasets.rb', line 49 def add_traces(dataset_id, trace_ids) @http_client.request(dataset_path(dataset_id, "/traces"), {"traceIds" => trace_ids}) end |
#get(dataset_id) ⇒ Object
Fetch one dataset by id. A dataset outside this organization raises Net::HTTPError with a 404.
36 37 38 |
# File 'lib/bitfab/datasets.rb', line 36 def get(dataset_id) @http_client.get(dataset_path(dataset_id))["dataset"] end |
#get_grader_rerun(dataset_id, run_id: nil) ⇒ Object
The dataset's active grader re-run, or the run named by run_id:. Returns nil when nothing is active or the run is not this dataset's.
92 93 94 95 |
# File 'lib/bitfab/datasets.rb', line 92 def get_grader_rerun(dataset_id, run_id: nil) query = run_id.nil? ? "" : "?#{URI.encode_www_form("runId" => run_id)}" @http_client.get(dataset_path(dataset_id, "/rerunGraders#{query}"))["run"] end |
#list(trace_function_key: nil) ⇒ Object
List datasets, scoped to one trace function when given and organization-wide otherwise.
29 30 31 32 |
# File 'lib/bitfab/datasets.rb', line 29 def list(trace_function_key: nil) query = trace_function_key.nil? ? "" : "?#{URI.encode_www_form("traceFunctionKey" => trace_function_key)}" @http_client.get("/api/sdk/datasets#{query}")["datasets"] end |
#list_traces(dataset_id) ⇒ Object
The ids of every trace in the dataset, the same membership a replay with dataset_id: selects.
42 43 44 |
# File 'lib/bitfab/datasets.rb', line 42 def list_traces(dataset_id) @http_client.get(dataset_path(dataset_id, "/traces")) end |
#remove_graders(dataset_id, grader_ids) ⇒ Object
Unassign graders from the dataset.
66 67 68 |
# File 'lib/bitfab/datasets.rb', line 66 def remove_graders(dataset_id, grader_ids) @http_client.request(dataset_path(dataset_id, "/removeGraders"), {"graderIds" => grader_ids}) end |
#remove_traces(dataset_id, trace_ids) ⇒ Object
Remove traces from the dataset. The traces themselves are never deleted.
54 55 56 |
# File 'lib/bitfab/datasets.rb', line 54 def remove_traces(dataset_id, trace_ids) @http_client.request(dataset_path(dataset_id, "/removeTraces"), {"traceIds" => trace_ids}) end |
#rerun_graders(dataset_id, grader_ids: nil, wait: true, timeout: DEFAULT_RERUN_TIMEOUT_SECONDS, poll_interval: DEFAULT_RERUN_POLL_INTERVAL_SECONDS) ⇒ Object
Re-run graders over every trace in the dataset. Defaults to every
assigned grader, and an unassigned id is rejected. Waits for the run to
finish (up to timeout seconds) unless wait: is false, and returns the
last run state seen either way. A request matching an in-flight run
joins it.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/bitfab/datasets.rb', line 75 def rerun_graders(dataset_id, grader_ids: nil, wait: true, timeout: DEFAULT_RERUN_TIMEOUT_SECONDS, poll_interval: DEFAULT_RERUN_POLL_INTERVAL_SECONDS) payload = grader_ids.nil? ? {} : {"graderIds" => grader_ids} started = @http_client.request(dataset_path(dataset_id, "/rerunGraders"), payload) return started unless wait deadline = monotonic_now + timeout run = started["run"] while !TERMINAL_RERUN_STATUSES.include?(run["status"]) && monotonic_now < deadline sleep(poll_interval) run = get_grader_rerun(dataset_id, run_id: run["id"]) || run end {"run" => run, "joinedExisting" => started["joinedExisting"]} end |
#save(trace_function_key:, name:, description: nil) ⇒ Object
Create a dataset, or update the one already named this way under the same trace function. The result's "created" reports which happened. A nil description leaves an existing description untouched.
21 22 23 24 25 |
# File 'lib/bitfab/datasets.rb', line 21 def save(trace_function_key:, name:, description: nil) payload = {"traceFunctionKey" => trace_function_key, "name" => name} payload["description"] = description unless description.nil? @http_client.request("/api/sdk/datasets", payload) end |