nexosis_api 1.0.3 → 1.0.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 52b0b16b9e0a7690ade72a0498b125aa2672f89d
4
- data.tar.gz: da93e102814f4b830e5b5411e873c2cd2c6017c0
3
+ metadata.gz: 0f9a520d658fe88b6f7d4e73e9d04482dec0183a
4
+ data.tar.gz: b7e5ccaa92a1970ce6562e497791cab3a3f7b418
5
5
  SHA512:
6
- metadata.gz: 8742572d3e9860871553bf5056d71e3ff30a5fae276e39479b73591ee443e5327b305bd4f35dafb16d451b9273facead20148b4524b7d57bc5b5924d9a9f8b94
7
- data.tar.gz: 9298f52be1a0a8d6fe131d3c63bcd28a6236a85d372fabf3380d86f121041af14f5164fe15eee3e025cb1271a5f6aef386575be727107bf62bbd94b0141ef319
6
+ metadata.gz: 61280b997acca1559619d03797003621f0e5c2a12f5ac5b78d4a86af2a5bd707783f2b9524358457b750a1b6db79df2725e6e2c4627fc4d815bc462fa239b180
7
+ data.tar.gz: 5f6b90b6bbac7c35072ca6da4991cd1d69d66edeed02fa627e2e8c435a5f91dc6896ab800c39f80368f5e0c3807f98660a73028080e766b9e49ce4c9da5f75ad
@@ -9,13 +9,16 @@ require 'nexosis_api/dataset_model'
9
9
  require 'nexosis_api/dataset_summary'
10
10
  require 'nexosis_api/http_exception'
11
11
  require 'nexosis_api/impact_metric'
12
+ require 'nexosis_api/imports_response'
12
13
  require 'nexosis_api/link'
13
14
  require 'nexosis_api/metric'
14
15
  require 'nexosis_api/session_response'
15
16
  require 'nexosis_api/session_result'
16
17
  require 'nexosis_api/session'
18
+ require 'nexosis_api/time_interval'
17
19
  require 'nexosis_api/client/sessions'
18
20
  require 'nexosis_api/client/datasets'
21
+ require 'nexosis_api/client/imports'
19
22
 
20
23
  module NexosisApi
21
24
  # Primary entry point to working with Nexosis API
@@ -24,6 +27,7 @@ module NexosisApi
24
27
  base_uri 'https://ml.nexosis.com/v1'
25
28
  include Client::Sessions
26
29
  include Client::Datasets
30
+ include Client::Imports
27
31
 
28
32
  def initialize(options = {})
29
33
  raise ArgumentError, 'api_key was not defined' unless options[:api_key].nil? == false
@@ -0,0 +1,75 @@
1
+ require 'json'
2
+ module NexosisApi
3
+ class Client
4
+ # Imports-based API operations
5
+ #
6
+ # @see http://docs.nexosis.com/
7
+ module Imports
8
+
9
+ # List all existing import requests
10
+ #
11
+ # @return [Array of NexosisApi::ImportsResponse]
12
+ def list_imports
13
+ imports_url = "/imports"
14
+ response = self.class.get(imports_url, :headers => @headers)
15
+ if(response.success?)
16
+ items = []
17
+ response.parsed_response["items"].each do |i|
18
+ items << NexosisApi::ImportsResponse.new(i)
19
+ end
20
+ items
21
+ else
22
+ raise HttpException.new("There was a problem getting the imports: #{response.code}.", "uploading dataset from s3 #{dataset_name}" ,response)
23
+ end
24
+ end
25
+
26
+ # Import a file from AWS s3 as your dataset
27
+ #
28
+ # @param dataset_name [String] the name to give to the new dataset or existing dataset to which this data will be upserted
29
+ # @param bucket_name [String] the AWS S3 bucket name in which the path will be found
30
+ # @param path [String] the path within the bucket (usually file name)
31
+ # @param region [String] the region in which your bucket exists. Defaults to us-east-1
32
+ # @param column_metadata [Array of NexosisApi::DatasetColumn] description of each column in target dataset. Optional.
33
+ # @return [NexosisApi::ImportsResponse]
34
+ # @see http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region for information on region names
35
+ def import_from_s3(dataset_name, bucket_name, path, region = "us-east-1", column_metadata = [])
36
+ raise ArgumentError "dataset_name was not provided and is not optional " unless dataset_name.to_s.empty? == false
37
+ raise ArgumentError "bucket_name was not provided and is not optional " unless bucket_name.to_s.empty? == false
38
+ raise ArgumentError "path was not provided and is not optional " unless path.to_s.empty? == false
39
+
40
+ s3_import_url = "/imports/s3"
41
+ column_json = DatasetColumn.to_json(column_metadata)
42
+ body = {
43
+ "dataSetName" => dataset_name,
44
+ "bucket" => bucket_name,
45
+ "path" => path,
46
+ "region" => region,
47
+ "columns" => column_json
48
+ }
49
+ response = self.class.post(s3_import_url, :headers => @headers, :body => body.to_json)
50
+ if(response.success?)
51
+ NexosisApi::ImportsResponse.new(response.parsed_response)
52
+ else
53
+ raise HttpException.new("There was a problem importing from s3: #{response.code}.", "uploading dataset from s3 #{dataset_name}" ,response)
54
+ end
55
+ end
56
+
57
+ # Get s3 response back from import created previously. Presumably to check status.
58
+ #
59
+ # @param import_id [String] The id returned from a previous request to import
60
+ # @return [NexosisApi::ImportsResponse]
61
+ # @example get S3 import
62
+ # NexosisApi.client.retrieve_import('740dca2a-b488-4322-887e-fa473b1caa54')
63
+ def retrieve_import(import_id)
64
+ raise ArgumentError "import_id was not provided and is not optional " unless import_id.to_s.empty? == false
65
+ imports_url = "/imports/#{import_id}"
66
+ response = self.class.get(imports_url, :headers => @headers)
67
+ if(response.success?)
68
+ NexosisApi::ImportsResponse.new(response.parsed_response)
69
+ else
70
+ raise HttpException.new("There was a problem getting the import #{response.code}.", "requesting an import #{import_id}" ,response)
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -11,7 +11,7 @@ module NexosisApi
11
11
  # List sessions previously submitted
12
12
  #
13
13
  # @param query_options [Hash] optionally provide query parameters to limit the search of sessions.
14
- # @return[Array of NexosisApi::SessionResponse] with all sessions matching the query or all if no query
14
+ # @return [Array of NexosisApi::SessionResponse] with all sessions matching the query or all if no query
15
15
  # @note query parameters hash members are dataset_name, event_name, requested_before_date, and requested_after_date.
16
16
  # After and before dates refer to the session requested date.
17
17
  # @example query for just one dataset
@@ -58,6 +58,8 @@ module NexosisApi
58
58
  # @note query parameters hash members are type, dataset_name, event_name, start_date, and end_date.
59
59
  # Start and end dates refer to the session requested date.
60
60
  # Results are not removed but then can only be accessed by dataset name
61
+ # @example Remove all sessions based on a dataset by name
62
+ # NexosisApi.client.remove_sessions :dataset_name => 'existing_dataset'
61
63
  def remove_sessions(query_options = {})
62
64
  sessions_url = '/sessions'
63
65
  response = self.class.delete(sessions_url, :headers => @headers, :query => get_query_from_options(query_options))
@@ -74,9 +76,13 @@ module NexosisApi
74
76
  # @param start_date [DateTime] The starting date of the forecast period. Can be ISO 8601 string.
75
77
  # @param end_date [DateTime] The ending date of the forecast period. Can be ISO 8601 string.
76
78
  # @param target_column [String] The name of the column for which you want predictions. Nil if defined in dataset.
79
+ # @param result_interval [NexosisApi::TimeInterval] (optional) - The date/time interval (e.g. Day, Hour) at which predictions should be generated. So, if Hour is specified for this parameter you will get a Result record for each hour between startDate and endDate. If unspecified, we’ll generate predictions at a Day interval.
77
80
  # @return [NexosisApi::SessionResponse] providing information about the sesssion
78
- def create_forecast_session(dataset_name, start_date, end_date, target_column = nil)
79
- create_session(dataset_name, start_date, end_date, target_column)
81
+ # @note The time interval selected must be greater than or equal to the finest granularity of the data provided.
82
+ # For instance if your data includes many recoreds per hour, then you could request hour, day, or any other result interval.
83
+ # However, if your data includes only a few records per day or fewer, then a request for an hourly result interval will produce poor results.
84
+ def create_forecast_session(dataset_name, start_date, end_date, target_column = nil, result_interval = NexosisApi::TimeInterval::DAY)
85
+ create_session(dataset_name, start_date, end_date, target_column, false, nil, "forecast", nil,nil,result_interval)
80
86
  end
81
87
 
82
88
  # Forecast from CSV formatted data.
@@ -85,13 +91,14 @@ module NexosisApi
85
91
  # @param start_date [DateTime] The starting date of the forecast period. Can be ISO 8601 parseable string.
86
92
  # @param end_date [DateTime] The ending date of the forecast period. Can be ISO 8601 parseable string.
87
93
  # @param target_column [String] The name of the column for which you want predictions.
94
+ # @param result_interval [NexosisApi::TimeInterval] (optional) - The date/time interval (e.g. Day, Hour) at which predictions should be generated. So, if Hour is specified for this parameter you will get a Result record for each hour between startDate and endDate. If unspecified, we’ll generate predictions at a Day interval.
88
95
  # @return [NexosisApi::SessionResponse] providing information about the sesssion
89
96
  # @example load and send local file
90
97
  # mycsv = CSV.read('.\mylocal.csv')
91
98
  # NexosisApi.client(:api_key=>mykey).create_forecast_session_csv(mycsv,'sales','01-01-2017','02-01-2017')
92
- def create_forecast_session_csv(csv, start_date, end_date, target_column)
99
+ def create_forecast_session_csv(csv, start_date, end_date, target_column, result_interval = NexosisApi::TimeInterval::DAY)
93
100
  content = process_csv_to_s csv
94
- create_session(nil, start_date, end_date, target_column, false, nil, "forecast", content)
101
+ create_session(nil, start_date, end_date, target_column, false, nil, "forecast", content, "text/csv", result_interval)
95
102
  end
96
103
 
97
104
  # Forecast from data posted in the request.
@@ -100,11 +107,12 @@ module NexosisApi
100
107
  # @param start_date [DateTime] The starting date of the forecast period. Can be ISO 8601 string.
101
108
  # @param end_date [DateTime] The ending date of the forecast period. Can be ISO 8601 string.
102
109
  # @param target_column [String] The name of the column for which you want predictions. Nil if defined in dataset
110
+ # @param result_interval [NexosisApi::TimeInterval] (optional) - The date/time interval (e.g. Day, Hour) at which predictions should be generated. So, if Hour is specified for this parameter you will get a Result record for each hour between startDate and endDate. If unspecified, we’ll generate predictions at a Day interval.
103
111
  # @return [NexosisApi::SessionResponse] providing information about the sesssion
104
112
  # @see https://developers.nexosis.com/docs/services/98847a3fbbe64f73aa959d3cededb3af/operations/5919ef80a730020dd851f233
105
- def create_forecast_session_data(json_data, start_date, end_date, target_column = nil)
113
+ def create_forecast_session_data(json_data, start_date, end_date, target_column = nil, result_interval = NexosisApi::TimeInterval::DAY)
106
114
  json_data = json_data.to_json unless json_data.is_a? String
107
- create_session nil, start_date, end_date, target_column, false, nil, "forecast", json_data, "application/json"
115
+ create_session nil, start_date, end_date, target_column, false, nil, "forecast", json_data, "application/json", result_interval
108
116
  end
109
117
 
110
118
  # Estimate the cost of a forecast from data already saved to the API.
@@ -125,9 +133,10 @@ module NexosisApi
125
133
  # @param end_date [DateTime] The ending date of the impactful event. Can be ISO 8601 string.
126
134
  # @param event_name [String] The name of the event.
127
135
  # @param target_column [String] The name of the column for which you want predictions. Nil if defined in datatset.
136
+ # @param result_interval [NexosisApi::TimeInterval] (optional) - The date/time interval (e.g. Day, Hour) at which predictions should be generated. So, if Hour is specified for this parameter you will get a Result record for each hour between startDate and endDate. If unspecified, we’ll generate predictions at a Day interval.
128
137
  # @return [NexosisApi::SessionResponse] providing information about the sesssion
129
- def create_impact_session(dataset_name, start_date, end_date, event_name, target_column = nil)
130
- create_session dataset_name, start_date, end_date, target_column, false, event_name, "impact"
138
+ def create_impact_session(dataset_name, start_date, end_date, event_name, target_column = nil, result_interval = NexosisApi::TimeInterval::DAY)
139
+ create_session dataset_name, start_date, end_date, target_column, false, event_name, "impact", nil, result_interval
131
140
  end
132
141
 
133
142
  # Analyze impact for an event with data in json format.
@@ -137,11 +146,12 @@ module NexosisApi
137
146
  # @param end_date [DateTime] The ending date of the impactful event. Can be ISO 8601 string.
138
147
  # @param event_name [String] The name of the event.
139
148
  # @param target_column [String] The name of the column for which you want predictions. Nil if defined in dataset.
149
+ # @param result_interval [NexosisApi::TimeInterval] (optional) - The date/time interval (e.g. Day, Hour) at which predictions should be generated. So, if Hour is specified for this parameter you will get a Result record for each hour between startDate and endDate. If unspecified, we’ll generate predictions at a Day interval.
140
150
  # @return [NexosisApi::SessionResponse] providing information about the sesssion
141
151
  # @see https://developers.nexosis.com/docs/services/98847a3fbbe64f73aa959d3cededb3af/operations/5919ef80a730020dd851f233
142
- def create_impact_session_data(json_data, start_date, end_date, event_name, target_column = nil)
152
+ def create_impact_session_data(json_data, start_date, end_date, event_name, target_column = nil, result_interval = NexosisApi::TimeInterval::DAY)
143
153
  json_data = json_data.to_json unless json_data.is_a? String
144
- create_session nil, start_date, end_date, target_column, false, event_name, "impact", json_data, "application/json"
154
+ create_session nil, start_date, end_date, target_column, false, event_name, "impact", json_data, "application/json", result_interval
145
155
  end
146
156
 
147
157
  # Estimate the cost of impact analysis for an event with data already saved to the API.
@@ -195,13 +205,14 @@ module NexosisApi
195
205
  end
196
206
  end
197
207
  private
198
- def create_session(dataset_name, start_date, end_date, target_column = nil, is_estimate=false, event_name = nil, type = "forecast", content = nil, content_type = "text/csv")
208
+ def create_session(dataset_name, start_date, end_date, target_column = nil, is_estimate=false, event_name = nil, type = "forecast", content = nil, content_type = "text/csv", result_interval = NexosisApi::TimeInterval::DAY)
199
209
  session_url = "/sessions/#{type}"
200
210
  query = {
201
211
  "targetColumn" => target_column.to_s,
202
212
  "startDate" => start_date.to_s,
203
213
  "endDate" => end_date.to_s,
204
- "isestimate" => is_estimate.to_s
214
+ "isestimate" => is_estimate.to_s,
215
+ "resultInterval" => result_interval.to_s
205
216
  }
206
217
  query["dataSetName"] = dataset_name.to_s unless dataset_name.to_s.empty?
207
218
  if(event_name.nil? == false)
@@ -20,5 +20,16 @@ module NexosisApi
20
20
  # @note Either none, timestamp, target, or feature
21
21
  # @return [NexosisApi::ColumnRole]
22
22
  attr_accessor :role
23
+
24
+ # utility method to format a column description in the way it is expected on input
25
+ def to_hash
26
+ { self.name => { "dataType" => self.type.to_s, "role" => self.role.to_s }}
27
+ end
28
+
29
+ def self.to_json(column_array)
30
+ result = {}
31
+ column_array.each {|col| result[col.to_hash.keys[0]] = col.to_hash.values[0] }
32
+ result
33
+ end
23
34
  end
24
35
  end
@@ -5,16 +5,13 @@ module NexosisApi
5
5
  def initialize(message = "", action = nil, http_obj)
6
6
  @message = message
7
7
  if(http_obj.is_a?(Hash))
8
- @message.prepend(http_obj["message"].concat(": ")) unless http_obj["message"].nil?
9
- @action = action
10
- if(http_obj.instance_of?(HTTParty::Response))
11
- @message = message.concat("|| Explanation: ").concat(http_obj["errorDetails"]["message"]) unless http_obj["errorDetails"]["message"].nil?
12
- @code = http_obj.parsed_response["statusCode"]
13
- @type = http_obj.parsed_response["errorType"]
14
- @response = http_obj.response
15
- @request = http_obj.request
16
- end
17
- else
8
+ @message.prepend(http_obj["message"].concat(": ")) unless http_obj["message"].nil?
9
+ @action = action
10
+ elsif(http_obj.instance_of?(HTTParty::Response))
11
+ @message = message.concat("|| Explanation: ").concat(http_obj.parsed_response["errorDetails"].to_s) unless http_obj.parsed_response["errorDetails"].nil?
12
+ @type = http_obj.parsed_response["errorType"]
13
+ @response = http_obj.response
14
+ @request = http_obj.request
18
15
  @code = http_obj.code
19
16
  end
20
17
  end
@@ -0,0 +1,66 @@
1
+ module NexosisApi
2
+ # class to parse results from an imports call
3
+ class ImportsResponse
4
+ def initialize(response_hash)
5
+ response_hash.each do |k,v|
6
+ if(k == "importId")
7
+ @import_id = v
8
+ elsif(k == "requestedDate")
9
+ @requested_date = v
10
+ elsif(k == "columns")
11
+ columns = []
12
+ next if v.nil?
13
+ v.keys.each do |col_key|
14
+ columns << NexosisApi::DatasetColumn.new(col_key, v[col_key])
15
+ end
16
+ @column_metadata = columns
17
+ elsif(k == "links")
18
+ links = Array.new
19
+ v.each do |l| links << NexosisApi::Link.new(l) end
20
+ instance_variable_set("@#{k}", links) unless v.nil?
21
+ else
22
+ instance_variable_set("@#{k}", v) unless v.nil?
23
+ end
24
+ end
25
+ end
26
+
27
+ # The unique identifier for this import request
28
+ # @return [String]
29
+ attr_accessor :import_id
30
+
31
+ # Currently always s3
32
+ # @return [String]
33
+ attr_accessor :type, :s3
34
+
35
+ # The current status of the import request
36
+ # @return [String]
37
+ # @note The import will be performed in a FIFO queue. Check back on status before attempting to start a session using the dataset.
38
+ attr_accessor :status
39
+
40
+ # echo back the dataset name provided
41
+ # @return [String]
42
+ attr_accessor :dataSetName
43
+
44
+ # The S3 parameters used to import a dataset
45
+ # @return [Hash]
46
+ # For an S3 response the keys of this hash should be 'bucket', 'path', and 'region'
47
+ attr_accessor :parameters
48
+
49
+ # The date of the import request
50
+ # @return [DateTime]
51
+ attr_accessor :requested_date
52
+
53
+ # Additional details. Normally empty.
54
+ # @return [Array]
55
+ attr_accessor :messages
56
+
57
+ # The column descriptors for the data in this session
58
+ # will reflect either the metadata sent in, defaults form dataset, or inferred values
59
+ # @return[Array of NexosisApi::DatasetColumn]
60
+ attr_accessor :column_metadata
61
+
62
+ # associated hypermedia
63
+ # @return [Array of NexosisApi::Link]
64
+ attr_accessor :links
65
+ end
66
+ end
@@ -9,6 +9,15 @@ module NexosisApi
9
9
  instance_variable_set("@#{k}", links) unless v.nil?
10
10
  elsif(k == "isEstimate")
11
11
  instance_variable_set("@is_estimate", v) unless v.nil?
12
+ elsif(k == "columns")
13
+ columns = []
14
+ next if v.nil?
15
+ v.keys.each do |col_key|
16
+ columns << NexosisApi::DatasetColumn.new(col_key, v[col_key])
17
+ end
18
+ @column_metadata = columns
19
+ elsif(k == "resultInterval")
20
+ @result_interval = v
12
21
  else
13
22
  instance_variable_set("@#{k}", v) unless v.nil?
14
23
  end
@@ -58,6 +67,15 @@ module NexosisApi
58
67
  # Is this session an estimate only session
59
68
  # @return [Boolean]
60
69
  attr_accessor :is_estimate
70
+
71
+ # The column descriptors for the data in this session
72
+ # will reflect either the metadata sent in, defaults form dataset, or inferred values
73
+ # @return[Array of NexosisApi::DatasetColumn]
74
+ attr_accessor :column_metadata
75
+
76
+ # The requested result interval. Default is DAY if none was requested during session creation.
77
+ # @return [NexosisApi::TimeInterval]
78
+ attr_accessor :result_interval
61
79
  end
62
80
  end
63
81
 
@@ -11,7 +11,7 @@ module NexosisApi
11
11
  instance_variable_set("@cost", v[0]) unless v.nil?
12
12
  elsif(k == "nexosis-account-balance")
13
13
  instance_variable_set("@account_balance", v[0]) unless v.nil?
14
- end
14
+ end
15
15
  end
16
16
  end
17
17
 
@@ -0,0 +1,15 @@
1
+ module NexosisApi
2
+ #constants for the date/time interval (e.g. Day, Hour) at which predictions should be generated.
3
+ module TimeInterval
4
+ # results summarized by hour
5
+ HOUR = :hour
6
+ # results summarized by day. Default option.
7
+ DAY = :day
8
+ # results summarized by week
9
+ WEEK = :week
10
+ # results summarized by month
11
+ MONTH = :month
12
+ # results summarized by year
13
+ YEAR = :year
14
+ end
15
+ end
data/nexosisapi.gemspec CHANGED
@@ -16,6 +16,6 @@ Gem::Specification.new do |spec|
16
16
  spec.require_paths = ['lib']
17
17
  spec.required_ruby_version = '>= 2.0.0'
18
18
  spec.summary = "Ruby client for working with the Nexosis API"
19
- spec.version = '1.0.3'
19
+ spec.version = '1.0.5'
20
20
  spec.metadata["yard.run"] = "yri"
21
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nexosis_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nexosis,Inc
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-28 00:00:00.000000000 Z
11
+ date: 2017-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -51,6 +51,7 @@ files:
51
51
  - lib/nexosis_api/algorithm_selection.rb
52
52
  - lib/nexosis_api/client.rb
53
53
  - lib/nexosis_api/client/datasets.rb
54
+ - lib/nexosis_api/client/imports.rb
54
55
  - lib/nexosis_api/client/sessions.rb
55
56
  - lib/nexosis_api/column_role.rb
56
57
  - lib/nexosis_api/column_type.rb
@@ -60,11 +61,13 @@ files:
60
61
  - lib/nexosis_api/dataset_summary.rb
61
62
  - lib/nexosis_api/http_exception.rb
62
63
  - lib/nexosis_api/impact_metric.rb
64
+ - lib/nexosis_api/imports_response.rb
63
65
  - lib/nexosis_api/link.rb
64
66
  - lib/nexosis_api/metric.rb
65
67
  - lib/nexosis_api/session.rb
66
68
  - lib/nexosis_api/session_response.rb
67
69
  - lib/nexosis_api/session_result.rb
70
+ - lib/nexosis_api/time_interval.rb
68
71
  - nexosisapi.gemspec
69
72
  homepage: https://github.com/nexosis/nexosisclient-rb
70
73
  licenses: