Module: NexosisApi::Client::Sessions

Included in:
NexosisApi::Client
Defined in:
lib/nexosis_api/client/sessions.rb

Overview

Session-based API operations

Instance Method Summary collapse

Instance Method Details

#create_forecast_session(dataset_name, start_date, end_date, target_column = nil) ⇒ NexosisApi::SessionResponse

Forecast from data already saved to the API.

Parameters:

  • dataset_name (String)

    The name of the saved data set that has the data to forecast on.

  • start_date (DateTime)

    The starting date of the forecast period. Can be ISO 8601 string.

  • end_date (DateTime)

    The ending date of the forecast period. Can be ISO 8601 string.

  • target_column (String) (defaults to: nil)

    The name of the column for which you want predictions. Nil if defined in dataset.

Returns:

[View source]

78
79
80
# File 'lib/nexosis_api/client/sessions.rb', line 78

def create_forecast_session(dataset_name, start_date, end_date, target_column = nil)
	create_session(dataset_name, start_date, end_date, target_column)
end

#create_forecast_session_csv(csv, start_date, end_date, target_column) ⇒ NexosisApi::SessionResponse

Forecast from CSV formatted data.

Examples:

load and send local file

mycsv = CSV.read('.\mylocal.csv')
NexosisApi.client(:api_key=>mykey).create_forecast_session_csv(mycsv,'sales','01-01-2017','02-01-2017')

Parameters:

  • csv (CSV)

    initialized CSV object ready for reading.

  • start_date (DateTime)

    The starting date of the forecast period. Can be ISO 8601 parseable string.

  • end_date (DateTime)

    The ending date of the forecast period. Can be ISO 8601 parseable string.

  • target_column (String)

    The name of the column for which you want predictions.

Returns:

[View source]

92
93
94
95
# File 'lib/nexosis_api/client/sessions.rb', line 92

def create_forecast_session_csv(csv, start_date, end_date, target_column)
				content = process_csv_to_s csv
    create_session(nil, start_date, end_date, target_column, false, nil, "forecast", content)
end

#create_forecast_session_data(json_data, start_date, end_date, target_column = nil) ⇒ NexosisApi::SessionResponse

Forecast from data posted in the request.

Parameters:

  • json_data (String)

    Json dataset matching the dataset input schema.

  • start_date (DateTime)

    The starting date of the forecast period. Can be ISO 8601 string.

  • end_date (DateTime)

    The ending date of the forecast period. Can be ISO 8601 string.

  • target_column (String) (defaults to: nil)

    The name of the column for which you want predictions. Nil if defined in dataset

Returns:

See Also:

[View source]

105
106
107
108
# File 'lib/nexosis_api/client/sessions.rb', line 105

def create_forecast_session_data(json_data, start_date, end_date, target_column = nil)
	json_data = json_data.to_json unless json_data.is_a? String
	create_session nil, start_date, end_date, target_column, false, nil, "forecast", json_data, "application/json"
end

#create_impact_session(dataset_name, start_date, end_date, event_name, target_column = nil) ⇒ NexosisApi::SessionResponse

Analyze impact for an event with data already saved to the API.

Parameters:

  • dataset_name (String)

    The name of the saved data set that has the data to forecast on.

  • start_date (DateTime)

    The starting date of the impactful event. Can be ISO 8601 string.

  • end_date (DateTime)

    The ending date of the impactful event. Can be ISO 8601 string.

  • event_name (String)

    The name of the event.

  • target_column (String) (defaults to: nil)

    The name of the column for which you want predictions. Nil if defined in datatset.

Returns:

[View source]

129
130
131
# File 'lib/nexosis_api/client/sessions.rb', line 129

def create_impact_session(dataset_name, start_date, end_date, event_name, target_column = nil)
    create_session dataset_name, start_date, end_date, target_column, false, event_name, "impact"
end

#create_impact_session_data(json_data, start_date, end_date, event_name, target_column = nil) ⇒ NexosisApi::SessionResponse

Analyze impact for an event with data in json format.

Parameters:

  • json_data (String)

    Json dataset matching the dataset input schema.

  • start_date (DateTime)

    The starting date of the impactful event. Can be ISO 8601 string.

  • end_date (DateTime)

    The ending date of the impactful event. Can be ISO 8601 string.

  • event_name (String)

    The name of the event.

  • target_column (String) (defaults to: nil)

    The name of the column for which you want predictions. Nil if defined in dataset.

Returns:

See Also:

[View source]

142
143
144
145
# File 'lib/nexosis_api/client/sessions.rb', line 142

def create_impact_session_data(json_data, start_date, end_date, event_name, target_column = nil)
	json_data = json_data.to_json unless json_data.is_a? String
	create_session nil, start_date, end_date, target_column, false, event_name, "impact", json_data, "application/json"
end

#estimate_forecast_session(dataset_name, start_date, end_date, target_column = nil) ⇒ NexosisApi::SessionResponse

Estimate the cost of a forecast from data already saved to the API.

Parameters:

  • dataset_name (String)

    The name of the saved data set that has the data to forecast on.

  • start_date (DateTime)

    The starting date of the forecast period. Can be ISO 8601 string.

  • end_date (DateTime)

    The ending date of the forecast period. Can be ISO 8601 string.

  • target_column (String) (defaults to: nil)

    The name of the column for which you want predictions. Nil if defined in dataset.

Returns:

[View source]

117
118
119
# File 'lib/nexosis_api/client/sessions.rb', line 117

def estimate_forecast_session(dataset_name, start_date, end_date, target_column = nil)
    create_session dataset_name, start_date, end_date, target_column, true
end

#estimate_impact_session(dataset_name, start_date, end_date, event_name, target_column = nil) ⇒ NexosisApi::SessionResponse

Estimate the cost of impact analysis for an event with data already saved to the API.

Parameters:

  • dataset_name (String)

    The name of the saved data set that has the data to forecast on.

  • start_date (DateTime)

    The starting date of the impactful event. Can be ISO 8601 string.

  • end_date (DateTime)

    The ending date of the impactful event. Can be ISO 8601 string.

  • event_name (String)

    The name of the event.

  • target_column (String) (defaults to: nil)

    The name of the column for which you want predictions. Nil if defined in dataset.

Returns:

[View source]

155
156
157
# File 'lib/nexosis_api/client/sessions.rb', line 155

def estimate_impact_session(dataset_name, start_date, end_date, event_name, target_column = nil)
    create_session dataset_name, start_date, end_date, target_column, true, event_name, "impact"
end

#get_session(session_id) ⇒ NexosisApi::Session

Get a specific session by id.

Parameters:

  • session_id (String)

    the Guid string returned in a previous session request

Returns:

[View source]

188
189
190
191
192
193
194
195
196
# File 'lib/nexosis_api/client/sessions.rb', line 188

def get_session(session_id)
    session_url = "/sessions/#{session_id}"
    response = self.class.get(session_url, @options)
    if(response.success?)
        NexosisApi::Session.new(response.parsed_response)
    else
        raise HttpException.new("There was a problem getting the session: #{response.code}.", "getting session #{session_id}" ,response)
			    end
end

#get_session_results(session_id, as_csv = false) ⇒ NexosisApi::SessionResult

Get the results of the session.

Parameters:

  • session_id (String)

    the Guid string returned in a previous session request

  • as_csv (Boolean) (defaults to: false)

    indicate whether results should be returned in csv format

Returns:

[View source]

164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/nexosis_api/client/sessions.rb', line 164

def get_session_results(session_id, as_csv = false)
  		session_result_url = "/sessions/#{session_id}/results"
		
	if as_csv
 	@headers["Accept"] = "text/csv"
 end
 response = self.class.get(session_result_url, @options)
 @headers.delete("Accept")

 if(response.success?)
  if(as_csv)
   response.body
  else
   NexosisApi::SessionResult.new(response.parsed_response)
  end
          else
              raise HttpException.new("There was a problem getting the session: #{response.code}.", "get results for session #{session_id}" ,response)
 end
end

#list_sessions(query_options = {}) ⇒ Object

Note:

query parameters hash members are dataset_name, event_name, requested_before_date, and requested_after_date. After and before dates refer to the session requested date.

List sessions previously submitted

@return[Array of NexosisApi::SessionResponse] with all sessions matching the query or all if no query

Examples:

query for just one dataset

sessions = NexosisApi.client.list_sessions :dataset_name => 'MyDataset'

Parameters:

  • query_options (Hash) (defaults to: {})

    optionally provide query parameters to limit the search of sessions.

[View source]

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/nexosis_api/client/sessions.rb', line 19

def list_sessions(query_options = {})
	sessions_url = '/sessions'
	query = { 
		"dataSetName" => query_options[:dataset_name],
		"eventName" => query_options[:event_name],
		"requestedAfterDate" => query_options[:requested_after_date],
		"requestedBeforeDate" => query_options[:requested_before_date],
		"type" => query_options[:type]
	}
	response = self.class.get(sessions_url, :headers => @headers, :query => query)
	if(response.success?)
		all_responses = []
		response.parsed_response["items"].each do |session_hash|
			response_hash = {"session" => session_hash}.merge(response.headers)
			all_responses << NexosisApi::SessionResponse.new(response_hash)
		end
		all_responses
	else
		raise HttpException.new("Could not retrieve sessions","Get all sessions with query #{query_options.to_s}",response)
	end
end

#remove_session(session_id) ⇒ Object

Remove a session

Parameters:

  • session_id (String)

    required session identifier

[View source]

43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/nexosis_api/client/sessions.rb', line 43

def remove_session(session_id)
	if(session_id.to_s.empty?)
		raise ArgumentError 'session_id cannot be empty or nil'
	end
	session_url = '/session/#{session_id.to_s}'
	response = self.class.delete(session_url)	
	if(response.success?)
		return
	else
		raise HttpException.new("Could not delete session with given id","remove session with id #{session_id.to_s}",response)
	end
end

#remove_sessions(query_options = {}) ⇒ Object

Note:

query parameters hash members are type, dataset_name, event_name, start_date, and end_date. Start and end dates refer to the session requested date. Results are not removed but then can only be accessed by dataset name

Remove sessions that have been run. All query options are optional and will be used to limit the sessions removed.

Parameters:

  • query_options (Hash) (defaults to: {})

    optionally provide query parametes to limit the set of sessions removed.

[View source]

61
62
63
64
65
66
67
68
69
# File 'lib/nexosis_api/client/sessions.rb', line 61

def remove_sessions(query_options = {})
	sessions_url = '/sessions'
	response = self.class.delete(sessions_url, :headers => @headers, :query => get_query_from_options(query_options))
	if(response.success?)
		return
	else
		raise HttpException.new("Could not remove sessions","Remove sessions with query #{query_options.to_s}",response)
	end
end