datarobot-ai_api 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +3 -0
  3. data/Gemfile +3 -0
  4. data/README.md +12 -0
  5. data/Rakefile +54 -0
  6. data/datarobot-ai_api.gemspec +19 -0
  7. data/lib/datarobot.rb +1 -0
  8. data/lib/datarobot/ai_api.rb +190 -0
  9. data/lib/datarobot/ai_api/ai.rb +191 -0
  10. data/lib/datarobot/ai_api/dataset.rb +99 -0
  11. data/lib/datarobot/ai_api/deployment.rb +20 -0
  12. data/lib/datarobot/ai_api/evaluation.rb +21 -0
  13. data/lib/datarobot/ai_api/learning_session.rb +112 -0
  14. data/lib/datarobot/ai_api/output.rb +58 -0
  15. data/lib/datarobot/ai_api/page.rb +50 -0
  16. data/lib/datarobot/ai_api/prediction.rb +20 -0
  17. data/lib/datarobot/ai_api/refreshable.rb +20 -0
  18. data/lib/datarobot/ai_api/task.rb +86 -0
  19. data/test/cassettes/Datarobot_AiApi/_ping/returns_unauthorized_when_not_authorized.yml +41 -0
  20. data/test/cassettes/Datarobot_AiApi/_ping/works_when_authorized.yml +58 -0
  21. data/test/cassettes/Datarobot_AiApi/_ping_me/returns_unauthorized_when_not_authorized.yml +41 -0
  22. data/test/cassettes/Datarobot_AiApi/_ping_me/works_when_authorized.yml +58 -0
  23. data/test/cassettes/Datarobot_AiApi_AI/when_authorized/404s_if_there_is_no_matching_output.yml +190 -0
  24. data/test/cassettes/Datarobot_AiApi_AI/when_authorized/_find/should_accept_a_block.yml +66 -0
  25. data/test/cassettes/Datarobot_AiApi_AI/when_authorized/_next_page/can_go_to_the_next_page.yml +132 -0
  26. data/test/cassettes/Datarobot_AiApi_AI/when_authorized/_next_page/returns_the_same_page_if_there_is_no_next_page.yml +123 -0
  27. data/test/cassettes/Datarobot_AiApi_AI/when_authorized/_previous_page/can_go_to_the_previous_page.yml +262 -0
  28. data/test/cassettes/Datarobot_AiApi_AI/when_authorized/_previous_page/returns_the_same_page_if_there_is_no_previous_page.yml +67 -0
  29. data/test/cassettes/Datarobot_AiApi_AI/when_authorized/_refresh/can_refresh_to_a_new_object.yml +129 -0
  30. data/test/cassettes/Datarobot_AiApi_AI/when_authorized/_refresh_/can_refresh_its_values.yml +129 -0
  31. data/test/cassettes/Datarobot_AiApi_AI/when_authorized/can_add_datasets_to_AIs.yml +120 -0
  32. data/test/cassettes/Datarobot_AiApi_AI/when_authorized/can_add_learning_sessions_to_AIs.yml +120 -0
  33. data/test/cassettes/Datarobot_AiApi_AI/when_authorized/can_be_trained.yml +4554 -0
  34. data/test/cassettes/Datarobot_AiApi_AI/when_authorized/can_create_AIs.yml +120 -0
  35. data/test/cassettes/Datarobot_AiApi_AI/when_authorized/can_delete_AIs.yml +58 -0
  36. data/test/cassettes/Datarobot_AiApi_AI/when_authorized/can_find_AIs.yml +63 -0
  37. data/test/cassettes/Datarobot_AiApi_AI/when_authorized/can_get_AI_datasets.yml +131 -0
  38. data/test/cassettes/Datarobot_AiApi_AI/when_authorized/can_get_AI_outputs.yml +124 -0
  39. data/test/cassettes/Datarobot_AiApi_AI/when_authorized/can_list_AIs.yml +123 -0
  40. data/test/cassettes/Datarobot_AiApi_AI/when_authorized/can_predict_things.yml +190 -0
  41. data/test/cassettes/Datarobot_AiApi_AI/when_not_authorized/can_not_create_AIs.yml +43 -0
  42. data/test/cassettes/Datarobot_AiApi_AI/when_not_authorized/can_not_find_AIs.yml +43 -0
  43. data/test/cassettes/Datarobot_AiApi_AI/when_not_authorized/can_not_list_AIs.yml +45 -0
  44. data/test/cassettes/Datarobot_AiApi_Dataset/when_authorized/_find/should_accept_a_block.yml +64 -0
  45. data/test/cassettes/Datarobot_AiApi_Dataset/when_authorized/_next_page/can_go_to_the_next_page.yml +126 -0
  46. data/test/cassettes/Datarobot_AiApi_Dataset/when_authorized/_next_page/returns_the_same_page_if_there_is_no_next_page.yml +113 -0
  47. data/test/cassettes/Datarobot_AiApi_Dataset/when_authorized/_previous_page/can_go_to_the_previous_page.yml +250 -0
  48. data/test/cassettes/Datarobot_AiApi_Dataset/when_authorized/_previous_page/returns_the_same_page_if_there_is_no_previous_page.yml +64 -0
  49. data/test/cassettes/Datarobot_AiApi_Dataset/when_authorized/_refresh/can_refresh_to_a_new_object.yml +125 -0
  50. data/test/cassettes/Datarobot_AiApi_Dataset/when_authorized/_refresh_/can_refresh_its_values.yml +125 -0
  51. data/test/cassettes/Datarobot_AiApi_Dataset/when_authorized/can_delete_datasets.yml +58 -0
  52. data/test/cassettes/Datarobot_AiApi_Dataset/when_authorized/can_find_datasets.yml +61 -0
  53. data/test/cassettes/Datarobot_AiApi_Dataset/when_authorized/can_import_a_dataset_from_a_file.yml +1077 -0
  54. data/test/cassettes/Datarobot_AiApi_Dataset/when_authorized/can_import_a_dataset_from_a_url.yml +55 -0
  55. data/test/cassettes/Datarobot_AiApi_Dataset/when_authorized/can_list_datasets.yml +113 -0
  56. data/test/cassettes/Datarobot_AiApi_LearningSession/when_authorized/_find/should_accept_a_block.yml +67 -0
  57. data/test/cassettes/Datarobot_AiApi_LearningSession/when_authorized/_next_page/can_go_to_the_next_page.yml +67 -0
  58. data/test/cassettes/Datarobot_AiApi_LearningSession/when_authorized/_next_page/returns_the_same_page_if_there_is_no_next_page.yml +100 -0
  59. data/test/cassettes/Datarobot_AiApi_LearningSession/when_authorized/_previous_page/can_go_to_the_previous_page.yml +67 -0
  60. data/test/cassettes/Datarobot_AiApi_LearningSession/when_authorized/_previous_page/returns_the_same_page_if_there_is_no_previous_page.yml +100 -0
  61. data/test/cassettes/Datarobot_AiApi_LearningSession/when_authorized/_refresh/can_refresh_to_a_new_object.yml +131 -0
  62. data/test/cassettes/Datarobot_AiApi_LearningSession/when_authorized/_refresh_/can_refresh_its_values.yml +130 -0
  63. data/test/cassettes/Datarobot_AiApi_LearningSession/when_authorized/can_create_learning_sessions.yml +2815 -0
  64. data/test/cassettes/Datarobot_AiApi_LearningSession/when_authorized/can_delete_learning_sessions.yml +58 -0
  65. data/test/cassettes/Datarobot_AiApi_LearningSession/when_authorized/can_find_learning_sessions.yml +63 -0
  66. data/test/cassettes/Datarobot_AiApi_LearningSession/when_authorized/can_get_learning_session_deployment_data.yml +123 -0
  67. data/test/cassettes/Datarobot_AiApi_LearningSession/when_authorized/can_get_learning_session_evaluation_data.yml +67 -0
  68. data/test/cassettes/Datarobot_AiApi_LearningSession/when_authorized/can_get_learning_session_features.yml +127 -0
  69. data/test/cassettes/Datarobot_AiApi_LearningSession/when_authorized/can_list_learning_sessions.yml +100 -0
  70. data/test/cassettes/Datarobot_AiApi_Output/when_authorized/can_create_outputs.yml +60 -0
  71. data/test/cassettes/Datarobot_AiApi_Output/when_authorized/can_find_named_outputs.yml +123 -0
  72. data/test/cassettes/Datarobot_AiApi_Output/when_authorized/can_get_features_of_an_output.yml +184 -0
  73. data/test/cassettes/Datarobot_AiApi_Output/when_authorized/can_get_the_evaluation_of_an_output.yml +131 -0
  74. data/test/cassettes/Datarobot_AiApi_Output/when_authorized/can_list_outputs.yml +124 -0
  75. data/test/cassettes/Datarobot_AiApi_Task/when_authorized/_find/should_accept_a_block.yml +63 -0
  76. data/test/cassettes/Datarobot_AiApi_Task/when_authorized/_refresh/can_refresh_to_a_new_object.yml +124 -0
  77. data/test/cassettes/Datarobot_AiApi_Task/when_authorized/_refresh_/can_refresh_its_values.yml +125 -0
  78. data/test/cassettes/Datarobot_AiApi_Task/when_authorized/can_stop_a_task.yml +58 -0
  79. data/test/cassettes/Datarobot_AiApi_Task/when_authorized/can_wait_until_completed.yml +1138 -0
  80. data/test/cassettes/Datarobot_AiApi_Task/when_authorized/check_the_status_of_a_task.yml +64 -0
  81. data/test/data/delete-me.csv +8037 -0
  82. data/test/data/test.csv +8037 -0
  83. data/test/datarobot/ai_api/test_ai.rb +133 -0
  84. data/test/datarobot/ai_api/test_dataset.rb +64 -0
  85. data/test/datarobot/ai_api/test_learning_session.rb +77 -0
  86. data/test/datarobot/ai_api/test_output.rb +54 -0
  87. data/test/datarobot/ai_api/test_page.rb +50 -0
  88. data/test/datarobot/ai_api/test_prediction.rb +0 -0
  89. data/test/datarobot/ai_api/test_refreshable.rb +39 -0
  90. data/test/datarobot/ai_api/test_task.rb +53 -0
  91. data/test/datarobot/test_ai_api.rb +52 -0
  92. data/test/test_helper.rb +40 -0
  93. metadata +217 -0
@@ -0,0 +1,99 @@
1
+ module Datarobot
2
+ module AiApi
3
+ class Dataset
4
+ include Datarobot::AiApi::Refreshable
5
+ attr_accessor :name
6
+ attr_reader :task, :created_at, :id, :ai_id
7
+
8
+ # Retrieves all datasets as a paginated resource.
9
+ #
10
+ # @param [Integer] limit Number of datasets per page
11
+ # @param [Integer] offset How many datasets to skip before this page
12
+ #
13
+ # @return [Datarobot::AiApi::Page(Datarobot::AiApi::Dataset)]
14
+ def self.all(limit: 50, offset: 0)
15
+ Datarobot::AiApi.request_endpoint('/aiapi/datasets/', params: {limit: limit, offset: offset}) do |data|
16
+ Datarobot::AiApi::Page.new(self, data)
17
+ end
18
+ end
19
+
20
+ # Retrieves an dataset given an ID.
21
+ #
22
+ # @param [String] id The ID of the dataset to retrieve
23
+ #
24
+ # @return [Datarobot::AiApi::Dataset]
25
+ def self.find(id, &block)
26
+ raise Datarobot::AiApi::NotFoundError, "Cannot find Dataset with id: nil" if id.nil?
27
+ Datarobot::AiApi.request_endpoint("/aiapi/datasets/#{id}") do |data|
28
+ if block_given?
29
+ yield data
30
+ else
31
+ self.new(data)
32
+ end
33
+ end
34
+ end
35
+
36
+ # Deletes a dataset. Returns `nil` if the action was successful. Will
37
+ # raise an error if the action was unsuccessful
38
+ #
39
+ # @param [String] id The ID of the dataset to delete
40
+ #
41
+ # @return [nil]
42
+ def self.delete(id)
43
+ Datarobot::AiApi.request_endpoint("/aiapi/datasets/#{id}", method: "delete")
44
+ end
45
+
46
+ # Creates a dataset from the file at the given (local) path
47
+ #
48
+ # @param [String] file_path The path to the file to upload
49
+ #
50
+ # @return [Datarobot::AiApi::Dataset]
51
+ def self.create_from_file(file_path)
52
+ Datarobot::AiApi.upload_to_endpoint('/aiapi/datasets/fileImports', file_path) do |data|
53
+ task = Datarobot::AiApi::Task.new(data)
54
+ task.wait_until_complete
55
+ end
56
+ end
57
+
58
+ # Creates a dataset from the file at the given (publicly accessible) url
59
+ #
60
+ # @param [String] url The url to the file to upload
61
+ #
62
+ # @return [Datarobot::AiApi::Dataset]
63
+ def self.create_from_url(url)
64
+ uri = URI.parse(url)
65
+ name = File.basename(uri.path)
66
+ Datarobot::AiApi.request_endpoint("/aiapi/datasets/urlImports/", method: "post", body: {url: url}) do |data|
67
+ dataset = new(data)
68
+ dataset.name = name
69
+ dataset
70
+ end
71
+ end
72
+
73
+ # Given a parsed response body from the API, will create a new Dataset object
74
+ def initialize(options = {})
75
+ # Suppresses warnings about uninitialized variables
76
+ @ai_id = nil
77
+ @name = nil
78
+ @created_at = nil
79
+
80
+ set_from_options(options)
81
+ end
82
+
83
+ # Takes a response body from the API. Will set all Dataset attributes from the
84
+ # response body
85
+ #
86
+ # @param [Hash] options A parsed response body
87
+ # @return [void]
88
+ def set_from_options(options = {})
89
+ # one-liner replacement for `stringify_keys`
90
+ options = options.collect{|k,v| [k.to_s, v]}.to_h
91
+
92
+ @created_at = options.dig("createdOn") || @created_at
93
+ @name = options.dig("datasetName") || options.dig("name") || @name
94
+ @id = options.dig("id") || options.dig("datasetId") || @id
95
+ @ai_id = options.dig("aiId") || @ai_id
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,20 @@
1
+ module Datarobot
2
+ module AiApi
3
+ class Deployment
4
+ attr_reader :datarobotKey, :deployment_id, :url, :target
5
+
6
+ # Takes a response body from the API. Will set all deployment attributes
7
+ # from the response body
8
+ #
9
+ # @param [Hash] options A parsed response body
10
+ def initialize(options={})
11
+ options = options.collect{|k,v| [k.to_s, v]}.to_h
12
+
13
+ @datarobotKey ||= options.dig("datarobot-key")
14
+ @deploymentId ||= options.dig("deploymentId")
15
+ @url ||= options.dig("url")
16
+ @target ||= options.dig("target")
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ module Datarobot
2
+ module AiApi
3
+ class Evaluation
4
+ attr_reader :summary, :score, :label
5
+
6
+ # Takes a response body from the API. Will set all prediction attributes
7
+ # from the response body
8
+ #
9
+ # @param [Hash] options A parsed response body
10
+ def initialize(options={})
11
+ # one-liner replacement for `stringify_keys`
12
+ options = options.collect{|k,v| [k.to_s, v]}.to_h
13
+
14
+ @summary = options.dig("summary")
15
+ @score = options.dig("score")
16
+ @label = options.dig("label")
17
+ end
18
+ end
19
+ end
20
+ end
21
+
@@ -0,0 +1,112 @@
1
+ module Datarobot
2
+ module AiApi
3
+ class LearningSession
4
+ include Datarobot::AiApi::Refreshable
5
+ attr_reader :name, :id, :created_at, :dataset_id, :target, :evaluation
6
+
7
+ # Retrieves all learning sessions as a paginated resource.
8
+ #
9
+ # @param [Integer] limit Number of learning sessions per page
10
+ # @param [Integer] offset How many learning sessions to skip before this page
11
+ #
12
+ # @return [Datarobot::AiApi::Page(Datarobot::AiApi::LearningSession)]
13
+ def self.all(limit: 50, offset: 0)
14
+ Datarobot::AiApi.request_endpoint('/aiapi/learningSessions/', params: {limit: limit, offset: offset}) do |data|
15
+ Datarobot::AiApi::Page.new(self, data)
16
+ end
17
+ end
18
+
19
+ # Retrieves a leanring sessoin given an ID.
20
+ #
21
+ # @param [String] id The ID of the learning session to retrieve
22
+ #
23
+ # @return [Datarobot::AiApi::LearningSession]
24
+ def self.find(id, &block)
25
+ raise Datarobot::AiApi::NotFoundError, "Cannot find LearningSession with id: nil" if id.nil?
26
+ Datarobot::AiApi.request_endpoint("/aiapi/learningSessions/#{id}") do |data|
27
+ if block_given?
28
+ yield data
29
+ else
30
+ self.new(data)
31
+ end
32
+ end
33
+ end
34
+
35
+ # Creates a new learning session on the given target for a given dataset
36
+ #
37
+ # @param [String] dataset_id The ID of the dataset to learn on
38
+ # @param [String] target Name of the target feature
39
+ # @return [Datarobot::AiApi::LearningSession]
40
+ def self.create(dataset_id:, target:)
41
+ raise "Need both dataset_id and target" unless dataset_id && target
42
+
43
+ name ||= "Learning #{target}"
44
+ Datarobot::AiApi.request_endpoint('/aiapi/learningSessions/', method: 'post', body: { datasetId: dataset_id, target: target }) do |data|
45
+ task_data = Datarobot::AiApi.get(data["links"]["result"])
46
+ task = Datarobot::AiApi::Task.new(task_data)
47
+ task.wait_until_complete
48
+ end
49
+ end
50
+
51
+ # Deletes a learning session. Returns `nil` if the action was successful.
52
+ # Will raise an error if the action was unsuccessful
53
+ #
54
+ # @param [String] id The ID of the learning session to delete
55
+ #
56
+ # @return [nil]
57
+ def self.delete(id)
58
+ Datarobot::AiApi.request_endpoint("/aiapi/learningSessions/#{id}", method: "delete")
59
+ end
60
+
61
+ # Given a parsed response body from the API, will create a new leanring sessoin object
62
+ def initialize(options = {})
63
+ # Suppresses warnings about uninitialized variables
64
+ @id = nil
65
+ @name = nil
66
+ @dataset_id = nil
67
+ @target = nil
68
+ @created_at = nil
69
+
70
+ set_from_options(options)
71
+ @features = nil
72
+ @deployment = nil
73
+ end
74
+
75
+ # Takes a response body from the API. Will set all learning session
76
+ # attributes from the response body
77
+ #
78
+ # @param [Hash] options A parsed response body
79
+ # @return [void]
80
+ def set_from_options(options = {})
81
+ # one-liner replacement for `stringify_keys`
82
+ options = options.collect{|k,v| [k.to_s, v]}.to_h
83
+
84
+ @name = options.dig("name") || @name
85
+ @id = options.dig("id") || @id
86
+ @created_at = options.dig("created") || @created_at
87
+ @dataset_id = options.dig("datasetId") || @dataset_id
88
+ @target = options.dig("target") || @target
89
+ @evaluation = Datarobot::AiApi::Evaluation.new(options.dig("evaluation") || {})
90
+ end
91
+
92
+ # gets all feature metadata for learned features of the associated
93
+ # dataset
94
+ #
95
+ # @return [Array]
96
+ def features
97
+ Datarobot::AiApi.request_endpoint("/aiapi/learningSessions/#{id}/features/") do |data|
98
+ data = data.collect{|k,v| [k.to_s, v]}.to_h
99
+ @features = data["features"]
100
+ end
101
+ end
102
+
103
+ # Gets the deployment for the learning session
104
+ # @return Datarobot::AiApi::Deployment
105
+ def deployment
106
+ Datarobot::AiApi.request_endpoint("/aiapi/learningSessions/#{id}/deployment") do |data|
107
+ @deployment = Datarobot::AiApi::Deployment.new(data)
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,58 @@
1
+ module Datarobot
2
+ module AiApi
3
+ class Output
4
+ attr_accessor :name, :target
5
+ attr_reader :evaluation, :ai_id, :links, :source
6
+
7
+ # Given a parsed response body from the API, will create a new ouptut object
8
+ def initialize(options = {})
9
+ set_from_options(options)
10
+ @features = nil
11
+ end
12
+
13
+ # Takes a response body from the API. Will set all output attributes from the
14
+ # response body
15
+ #
16
+ # @param [Hash] options A parsed response body
17
+ # @return [void]
18
+ def set_from_options(options = {})
19
+ # one-liner replacement for `stringify_keys`
20
+ options = options.collect{|k,v| [k.to_s, v]}.to_h
21
+
22
+ @name ||= options.dig("name")
23
+ @target ||= options.dig("target")
24
+ @source ||= options.dig("source")
25
+ @links ||= options.dig("links")
26
+ @ai_id ||= options.dig("aiId")
27
+ @evaluation ||= Datarobot::AiApi::Evaluation.new(options.dig("evaluation") || {})
28
+ end
29
+
30
+ # Creates a new output on the given target for a given dataset
31
+ #
32
+ # @param [String] ai_id The ID of the AI to associate this output with
33
+ # @param [String] learning_session_id The ID of the learning session
34
+ # assocaite this output with
35
+ # @param [String] output_name The name of the output
36
+ # @return [Datarobot::AiApi::Output]
37
+ def self.create(ai_id:, learning_session_id:, output_name:)
38
+ req_body = {learningSessionId: learning_session_id, outputName: output_name}
39
+ Datarobot::AiApi.request_endpoint("/aiapi/ais/#{ai_id}/outputs/", method: "put", body: req_body) do |data|
40
+ output = new(data)
41
+ output.name = output_name
42
+ output
43
+ end
44
+ end
45
+
46
+ # gets all feature metadata for learned features of the associated
47
+ # dataset
48
+ #
49
+ # @return [Array]
50
+ def features
51
+ raise "no ai id" unless @ai_id
52
+ Datarobot::AiApi.request_endpoint("/aiapi/ais/#{@ai_id}/outputs/#{@name}/features") do |data|
53
+ @features = data["features"]
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,50 @@
1
+ module Datarobot
2
+ module AiApi
3
+ class Page < Delegator
4
+ attr_reader :total, :items, :klass, :links
5
+
6
+ # Creates a paginated collectoin of objects of type klass. Uses a parsed
7
+ # response body of paginated data to initialize group of objects
8
+ def initialize(klass, param_collection)
9
+ @items = param_collection["data"].map do |params|
10
+ klass.new(params)
11
+ end
12
+ @klass = klass
13
+ @total = param_collection["total"]
14
+ if param_collection["links"] # predictions don't have pages
15
+ @links = param_collection["links"]
16
+ @next_page = param_collection["links"]["next"]
17
+ @previous_page = param_collection["links"]["previous"]
18
+ end
19
+ end
20
+
21
+ # Goes to the next page in the dataset if there is one. Otherwise,
22
+ # returns the current page
23
+ # @return [Datarobot::AiApi::Page[klass]]
24
+ def next_page
25
+ return self if @next_page.nil?
26
+
27
+ Datarobot::AiApi.get(@next_page) do |data|
28
+ self.class.new(@klass, data)
29
+ end
30
+ end
31
+
32
+ # Goes to the prevoius page in the dataset if there is one. Otherwise,
33
+ # returns the current page
34
+ # @return [Datarobot::AiApi::Page[klass]]
35
+ def previous_page
36
+ return self if @previous_page.nil?
37
+
38
+ Datarobot::AiApi.get(@previous_page) do |data|
39
+ self.class.new(@klass, data)
40
+ end
41
+ end
42
+
43
+ # Allows all Array-like methods to be called on this class. They will be
44
+ # delegated to @items
45
+ def __getobj__
46
+ @items
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,20 @@
1
+ module Datarobot
2
+ module AiApi
3
+ class Prediction
4
+ attr_reader :prediction, :row_id, :data
5
+
6
+ # Takes a response body from the API. Will set all prediction attributes
7
+ # from the response body
8
+ #
9
+ # @param [Hash] options A parsed response body
10
+ def initialize(options={})
11
+ # one-liner replacement for `stringify_keys`
12
+ options = options.collect{|k,v| [k.to_s, v]}.to_h
13
+
14
+ @prediction = options.dig("prediction")
15
+ @row_id = options.dig("rowId")
16
+ @data = options.dig("data")
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ module Datarobot::AiApi::Refreshable
2
+ # This was initially written to closely align with the other datarobot clients, but `reload` is much more activerecord-y
3
+ def refresh!
4
+ raise "cannot refresh #{self.class} - no method `find'" unless self.class.respond_to? :find
5
+ raise "cannot refresh #{self.class} - no method `set_from_options'" unless respond_to? :set_from_options
6
+ raise "cannot refresh #{self.class} - object does not have an id" unless @id
7
+ self.class.find(@id) do |data|
8
+ set_from_options(data)
9
+ end
10
+ end
11
+
12
+ def refresh
13
+ raise "cannot refresh #{self.class} - no method `find'" unless self.class.respond_to? :find
14
+ raise "cannot refresh #{self.class} - object does not have an id" unless @id
15
+ self.class.find(@id)
16
+ end
17
+
18
+ alias_method :reload!, :refresh!
19
+ alias_method :reload, :refresh
20
+ end
@@ -0,0 +1,86 @@
1
+ module Datarobot
2
+ module AiApi
3
+ class Task
4
+ include Datarobot::AiApi::Refreshable
5
+
6
+ attr_reader :id, :code, :status_type, :status_id, :created_at,
7
+ :description, :message, :status, :links
8
+
9
+ # Given a parsed response body from the API, will create a new leanring sessoin object
10
+ def initialize(options = {})
11
+ # Suppresses warnings about uninitialized variables
12
+ @code = nil
13
+ @status = nil
14
+ @status_type = nil
15
+ @status_id = nil
16
+ @id = nil
17
+ @created_at = nil
18
+ @description = nil
19
+ @message = nil
20
+ @links = nil
21
+
22
+ set_from_options(options)
23
+ end
24
+
25
+ # Takes a response body from the API. Will set all task attributes from
26
+ # the response body
27
+ #
28
+ # @param [Hash] options A parsed response body
29
+ # @return [void]
30
+ def set_from_options(options = {})
31
+ # one-liner replacement for `stringify_keys`
32
+ options = options.collect{|k,v| [k.to_s, v]}.to_h
33
+
34
+ @code = options.dig("code") || @code
35
+ @status = options.dig("status") || @status
36
+ @status_type = options.dig("statusType") || @status_type
37
+ @status_id = options.dig("statusId") || @status_id
38
+ @id = options.dig("statusId") || @id
39
+ @created_at = options.dig("created") || @created_at
40
+ @description = options.dig("description") || @description
41
+ @message = options.dig("message") || @message
42
+ @links = options.dig("links") || @links
43
+ end
44
+
45
+ # Finds a task by id
46
+ #
47
+ # @param [String] id The id of the task to find
48
+ # @return [Datarobot::AiApi::Task]
49
+ def self.find(id, &block)
50
+ raise Datarobot::AiApi::NotFoundError, "Cannot find Task with id: nil" if id.nil?
51
+ Datarobot::AiApi.request_endpoint("/aiapi/status/#{id}") do |data|
52
+ if block_given?
53
+ yield data
54
+ else
55
+ self.new(data)
56
+ end
57
+ end
58
+ end
59
+
60
+ # Stops a task by id
61
+ #
62
+ # @param [String] id The id of the task to stop
63
+ # @return [Datarobot::AiApi::Task]
64
+ def self.stop(id)
65
+ Datarobot::AiApi.request_endpoint("/aiapi/status/#{id}", method: "delete")
66
+ end
67
+
68
+ # Waits for a task to complete and returns the resulting object.
69
+ #
70
+ # @return [Datarobot::AiApi::*]
71
+ def wait_until_complete
72
+ refreshed = self
73
+ until refreshed.status == 'COMPLETED'
74
+ sleep 1 unless ENV['FAST_TEST_RESPONSE'] == 'true'
75
+ refreshed = Datarobot::AiApi::Task.find(@id)
76
+ end
77
+
78
+ result = refreshed.links["result"]
79
+ Datarobot::AiApi.get(result) do |data|
80
+ klass = Datarobot::AiApi.determine_object(result)
81
+ klass.new(data)
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end