nexosis_api 1.1.2 → 1.2.0

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.
@@ -1,75 +1,74 @@
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
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::Column] 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
+ s3_import_url = "/imports/s3"
40
+ column_json = Column.to_json(column_metadata)
41
+ body = {
42
+ "dataSetName" => dataset_name,
43
+ "bucket" => bucket_name,
44
+ "path" => path,
45
+ "region" => region,
46
+ "columns" => column_json
47
+ }
48
+ response = self.class.post(s3_import_url, :headers => @headers, :body => body.to_json)
49
+ if(response.success?)
50
+ NexosisApi::ImportsResponse.new(response.parsed_response)
51
+ else
52
+ raise HttpException.new("There was a problem importing from s3: #{response.code}.", "uploading dataset from s3 #{dataset_name}" ,response)
53
+ end
54
+ end
55
+
56
+ # Get s3 response back from import created previously. Presumably to check status.
57
+ #
58
+ # @param import_id [String] The id returned from a previous request to import
59
+ # @return [NexosisApi::ImportsResponse]
60
+ # @example get S3 import
61
+ # NexosisApi.client.retrieve_import('740dca2a-b488-4322-887e-fa473b1caa54')
62
+ def retrieve_import(import_id)
63
+ raise ArgumentError "import_id was not provided and is not optional " unless import_id.to_s.empty? == false
64
+ imports_url = "/imports/#{import_id}"
65
+ response = self.class.get(imports_url, :headers => @headers)
66
+ if(response.success?)
67
+ NexosisApi::ImportsResponse.new(response.parsed_response)
68
+ else
69
+ raise HttpException.new("There was a problem getting the import #{response.code}.", "requesting an import #{import_id}" ,response)
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -1,213 +1,213 @@
1
- require 'csv'
2
- require 'json'
3
-
4
- module NexosisApi
5
- class Client
6
- # Session-based API operations
7
- #
8
- # @see http://docs.nexosis.com/
9
- module Sessions
10
-
11
- # List sessions previously submitted
12
- #
13
- # @param query_options [Hash] optionally provide query parameters to limit the search of sessions.
14
- # @param page [Integer] optionally provide a page number for paging. Defaults to 0 (first page).
15
- # @param pageSize [Integer] optionally provide a page size to limit the total number of results. Defaults to 50, max 1000
16
- # @return [Array of NexosisApi::SessionResponse] with all sessions matching the query or all if no query
17
- # @note query parameters hash members are dataset_name, event_name, requested_before_date, and requested_after_date.
18
- # After and before dates refer to the session requested date.
19
- # @example query for just one dataset
20
- # sessions = NexosisApi.client.list_sessions :dataset_name => 'MyDataset'
21
- def list_sessions(query_options = {}, page = 0, pageSize = 50)
22
- sessions_url = '/sessions'
23
- query = {
24
- "dataSetName" => query_options[:dataset_name],
25
- "eventName" => query_options[:event_name],
26
- "requestedAfterDate" => query_options[:requested_after_date],
27
- "requestedBeforeDate" => query_options[:requested_before_date],
28
- "type" => query_options[:type],
29
- "page" => page,
30
- "pageSize" => pageSize
31
- }
32
- response = self.class.get(sessions_url, :headers => @headers, :query => query)
33
- if(response.success?)
34
- all_responses = []
35
- response.parsed_response["items"].each do |session_hash|
36
- response_hash = {"session" => session_hash}.merge(response.headers)
37
- all_responses << NexosisApi::SessionResponse.new(response_hash)
38
- end
39
- all_responses
40
- else
41
- raise HttpException.new("Could not retrieve sessions","Get all sessions with query #{query_options.to_s}",response)
42
- end
43
- end
44
-
45
- # Remove a session
46
- # @param session_id [String] required session identifier
47
- def remove_session(session_id)
48
- if(session_id.to_s.empty?)
49
- raise ArgumentError 'session_id cannot be empty or nil'
50
- end
51
- session_url = '/session/#{session_id.to_s}'
52
- response = self.class.delete(session_url)
53
- if(response.success?)
54
- return
55
- else
56
- raise HttpException.new("Could not delete session with given id","remove session with id #{session_id.to_s}",response)
57
- end
58
- end
59
-
60
- # Remove sessions that have been run. All query options are optional and will be used to limit the sessions removed.
61
- # @param query_options [Hash] optionally provide query parametes to limit the set of sessions removed.
62
- # @note query parameters hash members are type, dataset_name, event_name, start_date, and end_date.
63
- # Start and end dates refer to the session requested date.
64
- # Results are not removed but then can only be accessed by dataset name
65
- # @example Remove all sessions based on a dataset by name
66
- # NexosisApi.client.remove_sessions :dataset_name => 'existing_dataset'
67
- def remove_sessions(query_options = {})
68
- sessions_url = '/sessions'
69
- response = self.class.delete(sessions_url, :headers => @headers, :query => get_query_from_options(query_options))
70
- if(response.success?)
71
- return
72
- else
73
- raise HttpException.new("Could not remove sessions","Remove sessions with query #{query_options.to_s}",response)
74
- end
75
- end
76
-
77
- # Initiate a new forecast session based on a named dataset.
78
- #
79
- # @param dataset_name [String] The name of the saved data set that has the data to forecast on.
80
- # @param start_date [DateTime] The starting date of the forecast period. Can be ISO 8601 string.
81
- # @param end_date [DateTime] The ending date of the forecast period. Can be ISO 8601 string.
82
- # @param target_column [String] The name of the column for which you want predictions. Nil if defined in dataset.
83
- # @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.
84
- # @param column_metadata [Array of NexosisApi::DatasetColumn] (optional) - specification for how to handle columns if different from existing metadata on dataset
85
- # @return [NexosisApi::SessionResponse] providing information about the sesssion
86
- # @note The time interval selected must be greater than or equal to the finest granularity of the data provided.
87
- # For instance if your data includes many recoreds per hour, then you could request hour, day, or any other result interval.
88
- # 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.
89
- def create_forecast_session(dataset_name, start_date, end_date, target_column = nil, result_interval = NexosisApi::TimeInterval::DAY, column_metadata = nil)
90
- create_session(dataset_name, start_date, end_date, target_column, false, nil, "forecast", result_interval, column_metadata)
91
- end
92
-
93
- # Estimate the cost of a forecast from data already saved to the API.
94
- #
95
- # @param dataset_name [String] The name of the saved data set that has the data to forecast on.
96
- # @param start_date [DateTime] The starting date of the forecast period. Can be ISO 8601 string.
97
- # @param end_date [DateTime] The ending date of the forecast period. Can be ISO 8601 string.
98
- # @param target_column [String] The name of the column for which you want predictions. Nil if defined in dataset.
99
- # @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.
100
- # @return [NexosisApi::SessionResponse] providing information about the sesssion, including the cost
101
- def estimate_forecast_session(dataset_name, start_date, end_date, target_column = nil, result_interval = NexosisApi::TimeInterval::DAY)
102
- create_session(dataset_name, start_date, end_date, target_column, true, nil, "forecast", result_interval)
103
- end
104
-
105
- # Analyze impact for an event with data already saved to the API.
106
- #
107
- # @param dataset_name [String] The name of the saved data set that has the data to forecast on.
108
- # @param start_date [DateTime] The starting date of the impactful event. Can be ISO 8601 string.
109
- # @param end_date [DateTime] The ending date of the impactful event. Can be ISO 8601 string.
110
- # @param event_name [String] The name of the event.
111
- # @param target_column [String] The name of the column for which you want predictions. Nil if defined in datatset.
112
- # @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.
113
- # @param column_metadata [Array of NexosisApi::DatasetColumn] (optional) - specification for how to handle columns if different from existing metadata on dataset
114
- # @return [NexosisApi::SessionResponse] providing information about the sesssion
115
- def create_impact_session(dataset_name, start_date, end_date, event_name, target_column = nil, result_interval = NexosisApi::TimeInterval::DAY, column_metadata = nil)
116
- create_session(dataset_name, start_date, end_date, target_column, false, event_name, "impact", result_interval, column_metadata)
117
- end
118
-
119
- # Estimate the cost of impact analysis for an event with data already saved to the API.
120
- #
121
- # @param dataset_name [String] The name of the saved data set that has the data to forecast on.
122
- # @param start_date [DateTime] The starting date of the impactful event. Can be ISO 8601 string.
123
- # @param end_date [DateTime] The ending date of the impactful event. Can be ISO 8601 string.
124
- # @param event_name [String] The name of the event.
125
- # @param target_column [String] The name of the column for which you want predictions. Nil if defined in dataset.
126
- # @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.
127
- # @return [NexosisApi::SessionResponse] providing information about the sesssion, including the cost
128
- def estimate_impact_session(dataset_name, start_date, end_date, event_name, target_column = nil, result_interval = NexosisApi::TimeInterval::DAY)
129
- create_session(dataset_name, start_date, end_date, target_column, true, event_name, "impact", result_interval)
130
- end
131
-
132
- # Get the results of the session.
133
- #
134
- # @param session_id [String] the Guid string returned in a previous session request
135
- # @param as_csv [Boolean] indicate whether results should be returned in csv format
136
- # @return [NexosisApi::SessionResult] SessionResult if parsed, String of csv data otherwise
137
- def get_session_results(session_id, as_csv = false)
138
- session_result_url = "/sessions/#{session_id}/results"
139
-
140
- if as_csv
141
- @headers["Accept"] = "text/csv"
142
- end
143
- response = self.class.get(session_result_url, @options)
144
- @headers.delete("Accept")
145
-
146
- if(response.success?)
147
- if(as_csv)
148
- response.body
149
- else
150
- NexosisApi::SessionResult.new(response.parsed_response)
151
- end
152
- else
153
- raise HttpException.new("There was a problem getting the session: #{response.code}.", "get results for session #{session_id}" ,response)
154
- end
155
- end
156
-
157
- # Get a specific session by id.
158
- #
159
- # @param session_id [String] the Guid string returned in a previous session request
160
- # @return [NexosisApi::Session] a Session object populated with the session's data
161
- def get_session(session_id)
162
- session_url = "/sessions/#{session_id}"
163
- response = self.class.get(session_url, @options)
164
- if(response.success?)
165
- NexosisApi::Session.new(response.parsed_response)
166
- else
167
- raise HttpException.new("There was a problem getting the session: #{response.code}.", "getting session #{session_id}" ,response)
168
- end
169
- end
170
- private
171
- def create_session(dataset_name, start_date, end_date, target_column = nil, is_estimate=false, event_name = nil, type = "forecast", result_interval = NexosisApi::TimeInterval::DAY, column_metadata = nil)
172
- session_url = "/sessions/#{type}"
173
- query = {
174
- "targetColumn" => target_column.to_s,
175
- "startDate" => start_date.to_s,
176
- "endDate" => end_date.to_s,
177
- "isestimate" => is_estimate.to_s,
178
- "resultInterval" => result_interval.to_s
179
- }
180
- query["dataSetName"] = dataset_name.to_s unless dataset_name.to_s.empty?
181
- if(event_name.nil? == false)
182
- query["eventName"] = event_name
183
- end
184
- body = ''
185
- if(column_metadata.nil? == false)
186
- column_json = DatasetColumn.to_json(column_metadata)
187
- body = {
188
- "dataSetName" => dataset_name,
189
- "columns" => column_json
190
- }
191
- end
192
- response = self.class.post(session_url, :headers => @headers, :query => query, :body => body.to_json)
193
- if(response.success?)
194
- session_hash = {"session" => response.parsed_response}.merge(response.headers)
195
- NexosisApi::SessionResponse.new(session_hash)
196
- else
197
- raise HttpException.new("Unable to create new #{type} session","Create session for dataset #{dataset_name}",response)
198
- end
199
- end
200
-
201
- def get_query_from_options(options)
202
- query = {
203
- "dataSetName" => options[:dataset_name],
204
- "eventName" => options[:event_name],
205
- "startDate" => options[:start_date],
206
- "endDate" => options[:end_date],
207
- "type" => options[:type]
208
- }
209
- query
210
- end
211
- end
212
- end
213
- end
1
+ require 'csv'
2
+ require 'json'
3
+
4
+ module NexosisApi
5
+ class Client
6
+ # Session-based API operations
7
+ #
8
+ # @see http://docs.nexosis.com/
9
+ module Sessions
10
+
11
+ # List sessions previously submitted
12
+ #
13
+ # @param query_options [Hash] optionally provide query parameters to limit the search of sessions.
14
+ # @param page [Integer] optionally provide a page number for paging. Defaults to 0 (first page).
15
+ # @param pageSize [Integer] optionally provide a page size to limit the total number of results. Defaults to 50, max 1000
16
+ # @return [Array of NexosisApi::SessionResponse] with all sessions matching the query or all if no query
17
+ # @note query parameters hash members are dataset_name, event_name, requested_before_date, and requested_after_date.
18
+ # After and before dates refer to the session requested date.
19
+ # @example query for just one dataset
20
+ # sessions = NexosisApi.client.list_sessions :dataset_name => 'MyDataset'
21
+ def list_sessions(query_options = {}, page = 0, pageSize = 50)
22
+ sessions_url = '/sessions'
23
+ query = {
24
+ 'dataSetName' => query_options[:dataset_name],
25
+ 'eventName' => query_options[:event_name],
26
+ 'requestedAfterDate' => query_options[:requested_after_date],
27
+ 'requestedBeforeDate' => query_options[:requested_before_date],
28
+ 'type' => query_options[:type],
29
+ 'page' => page,
30
+ 'pageSize' => pageSize
31
+ }
32
+ response = self.class.get(sessions_url, :headers => @headers, :query => query)
33
+ if(response.success?)
34
+ all_responses = []
35
+ response.parsed_response['items'].each do |session_hash|
36
+ response_hash = { 'session' => session_hash }.merge(response.headers)
37
+ all_responses << NexosisApi::SessionResponse.new(response_hash)
38
+ end
39
+ all_responses
40
+ else
41
+ raise HttpException.new('Could not retrieve sessions',"Get all sessions with query #{query_options.to_s}",response)
42
+ end
43
+ end
44
+
45
+ # Remove a session
46
+ # @param session_id [String] required session identifier
47
+ def remove_session(session_id)
48
+ if(session_id.to_s.empty?)
49
+ raise ArgumentError 'session_id cannot be empty or nil'
50
+ end
51
+ session_url = "/sessions/#{session_id}"
52
+ response = self.class.delete(session_url, headers: @headers)
53
+ if(response.success?)
54
+ return
55
+ else
56
+ raise HttpException.new('Could not delete session with given id', "remove session with id #{session_id.to_s}",response)
57
+ end
58
+ end
59
+
60
+ # Remove sessions that have been run. All query options are optional and will be used to limit the sessions removed.
61
+ # @param query_options [Hash] optionally provide query parametes to limit the set of sessions removed.
62
+ # @note query parameters hash members are type, dataset_name, event_name, start_date, and end_date.
63
+ # Start and end dates refer to the session requested date.
64
+ # Results are not removed but then can only be accessed by dataset name
65
+ # @example Remove all sessions based on a dataset by name
66
+ # NexosisApi.client.remove_sessions :dataset_name => 'existing_dataset'
67
+ def remove_sessions(query_options = {})
68
+ sessions_url = '/sessions'
69
+ response = self.class.delete(sessions_url, :headers => @headers, :query => get_query_from_options(query_options))
70
+ if(response.success?)
71
+ return
72
+ else
73
+ raise HttpException.new('Could not remove sessions', "Remove sessions with query #{query_options.to_s}",response)
74
+ end
75
+ end
76
+
77
+ # Initiate a new forecast session based on a named dataset.
78
+ #
79
+ # @param dataset_name [String] The name of the saved data set that has the data to forecast on.
80
+ # @param start_date [DateTime] The starting date of the forecast period. Can be ISO 8601 string.
81
+ # @param end_date [DateTime] The ending date of the forecast period. Can be ISO 8601 string.
82
+ # @param target_column [String] The name of the column for which you want predictions. Nil if defined in dataset.
83
+ # @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.
84
+ # @param column_metadata [Array of NexosisApi::Column] (optional) - specification for how to handle columns if different from existing metadata on dataset
85
+ # @return [NexosisApi::SessionResponse] providing information about the sesssion
86
+ # @note The time interval selected must be greater than or equal to the finest granularity of the data provided.
87
+ # For instance if your data includes many recoreds per hour, then you could request hour, day, or any other result interval.
88
+ # 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.
89
+ def create_forecast_session(dataset_name, start_date, end_date, target_column = nil, result_interval = NexosisApi::TimeInterval::DAY, column_metadata = nil)
90
+ create_session(dataset_name, start_date, end_date, target_column, false, nil, 'forecast', result_interval, column_metadata)
91
+ end
92
+
93
+ # Estimate the cost of a forecast from data already saved to the API.
94
+ #
95
+ # @param dataset_name [String] The name of the saved data set that has the data to forecast on.
96
+ # @param start_date [DateTime] The starting date of the forecast period. Can be ISO 8601 string.
97
+ # @param end_date [DateTime] The ending date of the forecast period. Can be ISO 8601 string.
98
+ # @param target_column [String] The name of the column for which you want predictions. Nil if defined in dataset.
99
+ # @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.
100
+ # @return [NexosisApi::SessionResponse] providing information about the sesssion, including the cost
101
+ def estimate_forecast_session(dataset_name, start_date, end_date, target_column = nil, result_interval = NexosisApi::TimeInterval::DAY)
102
+ create_session(dataset_name, start_date, end_date, target_column, true, nil, 'forecast', result_interval)
103
+ end
104
+
105
+ # Analyze impact for an event with data already saved to the API.
106
+ #
107
+ # @param dataset_name [String] The name of the saved data set that has the data to forecast on.
108
+ # @param start_date [DateTime] The starting date of the impactful event. Can be ISO 8601 string.
109
+ # @param end_date [DateTime] The ending date of the impactful event. Can be ISO 8601 string.
110
+ # @param event_name [String] The name of the event.
111
+ # @param target_column [String] The name of the column for which you want predictions. Nil if defined in datatset.
112
+ # @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.
113
+ # @param column_metadata [Array of NexosisApi::Column] (optional) - specification for how to handle columns if different from existing metadata on dataset
114
+ # @return [NexosisApi::SessionResponse] providing information about the sesssion
115
+ def create_impact_session(dataset_name, start_date, end_date, event_name, target_column = nil, result_interval = NexosisApi::TimeInterval::DAY, column_metadata = nil)
116
+ create_session(dataset_name, start_date, end_date, target_column, false, event_name, 'impact', result_interval, column_metadata)
117
+ end
118
+
119
+ # Estimate the cost of impact analysis for an event with data already saved to the API.
120
+ #
121
+ # @param dataset_name [String] The name of the saved data set that has the data to forecast on.
122
+ # @param start_date [DateTime] The starting date of the impactful event. Can be ISO 8601 string.
123
+ # @param end_date [DateTime] The ending date of the impactful event. Can be ISO 8601 string.
124
+ # @param event_name [String] The name of the event.
125
+ # @param target_column [String] The name of the column for which you want predictions. Nil if defined in dataset.
126
+ # @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.
127
+ # @return [NexosisApi::SessionResponse] providing information about the sesssion, including the cost
128
+ def estimate_impact_session(dataset_name, start_date, end_date, event_name, target_column = nil, result_interval = NexosisApi::TimeInterval::DAY)
129
+ create_session(dataset_name, start_date, end_date, target_column, true, event_name, 'impact', result_interval)
130
+ end
131
+
132
+ # Get the results of the session.
133
+ #
134
+ # @param session_id [String] the Guid string returned in a previous session request
135
+ # @param as_csv [Boolean] indicate whether results should be returned in csv format
136
+ # @return [NexosisApi::SessionResult] SessionResult if parsed, String of csv data otherwise
137
+ def get_session_results(session_id, as_csv = false)
138
+ session_result_url = "/sessions/#{session_id}/results"
139
+ if as_csv
140
+ @headers["Accept"] = 'text/csv'
141
+ end
142
+ response = self.class.get(session_result_url, @options)
143
+ @headers.delete('Accept')
144
+
145
+ if(response.success?)
146
+ if(as_csv)
147
+ response.body
148
+ else
149
+ NexosisApi::SessionResult.new(response.parsed_response)
150
+ end
151
+ else
152
+ raise HttpException.new("There was a problem getting the session: #{response.code}.", "get results for session #{session_id}" ,response)
153
+ end
154
+ end
155
+
156
+ # Get a specific session by id.
157
+ #
158
+ # @param session_id [String] the Guid string returned in a previous session request
159
+ # @return [NexosisApi::Session] a Session object populated with the session's data
160
+ def get_session(session_id)
161
+ session_url = "/sessions/#{session_id}"
162
+ response = self.class.get(session_url, @options)
163
+ if(response.success?)
164
+ NexosisApi::Session.new(response.parsed_response)
165
+ else
166
+ raise HttpException.new("There was a problem getting the session: #{response.code}.", "getting session #{session_id}" ,response)
167
+ end
168
+ end
169
+
170
+ private
171
+ def create_session(dataset_name, start_date, end_date, target_column = nil, is_estimate=false, event_name = nil, type = "forecast", result_interval = NexosisApi::TimeInterval::DAY, column_metadata = nil)
172
+ session_url = "/sessions/#{type}"
173
+ query = {
174
+ 'targetColumn' => target_column.to_s,
175
+ 'startDate' => start_date.to_s,
176
+ 'endDate' => end_date.to_s,
177
+ 'isestimate' => is_estimate.to_s,
178
+ 'resultInterval' => result_interval.to_s
179
+ }
180
+ query['dataSetName'] = dataset_name.to_s unless dataset_name.to_s.empty?
181
+ if(event_name.nil? == false)
182
+ query['eventName'] = event_name
183
+ end
184
+ body = ''
185
+ if(column_metadata.nil? == false)
186
+ column_json = Column.to_json(column_metadata)
187
+ body = {
188
+ 'dataSetName' => dataset_name,
189
+ 'columns' => column_json
190
+ }
191
+ end
192
+ response = self.class.post(session_url, :headers => @headers, :query => query, :body => body.to_json)
193
+ if(response.success?)
194
+ session_hash = {'session' => response.parsed_response}.merge(response.headers)
195
+ NexosisApi::SessionResponse.new(session_hash)
196
+ else
197
+ raise HttpException.new("Unable to create new #{type} session", "Create session for dataset #{dataset_name}",response)
198
+ end
199
+ end
200
+
201
+ def get_query_from_options(options)
202
+ query = {
203
+ 'dataSetName' => options[:dataset_name],
204
+ 'eventName' => options[:event_name],
205
+ 'startDate' => options[:start_date],
206
+ 'endDate' => options[:end_date],
207
+ 'type' => options[:type]
208
+ }
209
+ query
210
+ end
211
+ end
212
+ end
213
+ end