##// END OF EJS Templates
Add docstrings to session.js
Jessica B. Hamrick -
Show More
@@ -1,203 +1,279
1 // Copyright (c) IPython Development Team.
1 // Copyright (c) IPython Development Team.
2 // Distributed under the terms of the Modified BSD License.
2 // Distributed under the terms of the Modified BSD License.
3
3
4 define([
4 define([
5 'base/js/namespace',
5 'base/js/namespace',
6 'jquery',
6 'jquery',
7 'base/js/utils',
7 'base/js/utils',
8 'services/kernels/js/kernel',
8 'services/kernels/js/kernel',
9 ], function(IPython, $, utils, kernel) {
9 ], function(IPython, $, utils, kernel) {
10 "use strict";
10 "use strict";
11
11
12 /**
13 * Session object for accessing the session REST api. The session
14 * should be used to start kernels and then shut them down -- for
15 * all other operations, the kernel object should be used.
16 *
17 * Options should include:
18 * - notebook_name: the notebook name
19 * - notebook_path: the path (not including name) to the notebook
20 * - kernel_name: the type of kernel (e.g. python3)
21 * - base_url: the root url of the notebook server
22 * - ws_url: the url to access websockets
23 * - notebook: Notebook object
24 *
25 * @class Session
26 * @param {Object} options
27 */
12 var Session = function (options) {
28 var Session = function (options) {
13 this.id = null;
29 this.id = null;
14 this.notebook_model = {
30 this.notebook_model = {
15 name: options.notebook_name,
31 name: options.notebook_name,
16 path: options.notebook_path
32 path: options.notebook_path
17 };
33 };
18 this.kernel_model = {
34 this.kernel_model = {
19 id: null,
35 id: null,
20 name: options.kernel_name
36 name: options.kernel_name
21 };
37 };
22
38
23 this.base_url = options.base_url;
39 this.base_url = options.base_url;
24 this.ws_url = options.ws_url;
40 this.ws_url = options.ws_url;
25 this.session_service_url = utils.url_join_encode(this.base_url, 'api/sessions');
41 this.session_service_url = utils.url_join_encode(this.base_url, 'api/sessions');
26 this.session_url = null;
42 this.session_url = null;
27
43
28 this.notebook = options.notebook;
44 this.notebook = options.notebook;
29 this.kernel = null;
45 this.kernel = null;
30 this.events = options.events;
46 this.events = options.notebook.events;
31 };
47 };
32
48
49 // Public REST api functions
50
33 /**
51 /**
34 * GET /api/sessions
52 * GET /api/sessions
53 *
54 * Get a list of the current sessions.
55 *
56 * @function list
57 * @param {function} [success] - function executed on ajax success
58 * @param {function} [error] - functon executed on ajax error
35 */
59 */
36 Session.prototype.list = function (success, error) {
60 Session.prototype.list = function (success, error) {
37 $.ajax(this.session_service_url, {
61 $.ajax(this.session_service_url, {
38 processData: false,
62 processData: false,
39 cache: false,
63 cache: false,
40 type: "GET",
64 type: "GET",
41 dataType: "json",
65 dataType: "json",
42 success: success,
66 success: success,
43 error: this._on_error(error)
67 error: this._on_error(error)
44 });
68 });
45 };
69 };
46
70
47 /**
71 /**
48 * POST /api/sessions
72 * POST /api/sessions
73 *
74 * Start a new session. This function can only executed once.
75 *
76 * @function start
77 * @param {function} [success] - function executed on ajax success
78 * @param {function} [error] - functon executed on ajax error
49 */
79 */
50 Session.prototype.start = function (success, error) {
80 Session.prototype.start = function (success, error) {
51 if (this.kernel !== null) {
81 if (this.kernel !== null) {
52 throw new Error("session has already been started");
82 throw new Error("session has already been started");
53 };
83 };
54
84
55 var that = this;
85 var that = this;
56 var on_success = function (data, status, xhr) {
86 var on_success = function (data, status, xhr) {
57 var kernel_service_url = utils.url_path_join(that.base_url, "api/kernels");
87 var kernel_service_url = utils.url_path_join(that.base_url, "api/kernels");
58 that.kernel = new kernel.Kernel(
88 that.kernel = new kernel.Kernel(
59 kernel_service_url, that.ws_url, that.notebook,
89 kernel_service_url, that.ws_url, that.notebook,
60 that.kernel_model.id, that.kernel_model.name);
90 that.kernel_model.id, that.kernel_model.name);
61 that.kernel._kernel_started(data.kernel);
91 that.kernel._kernel_started(data.kernel);
62 if (success) {
92 if (success) {
63 success(data, status, xhr);
93 success(data, status, xhr);
64 }
94 }
65 };
95 };
66 var on_error = function (xhr, status, err) {
96 var on_error = function (xhr, status, err) {
67 that.events.trigger('no_kernel.Kernel');
97 that.events.trigger('no_kernel.Kernel');
68 if (error) {
98 if (error) {
69 error(xhr, status, err);
99 error(xhr, status, err);
70 }
100 }
71 };
101 };
72
102
73 $.ajax(this.session_service_url, {
103 $.ajax(this.session_service_url, {
74 processData: false,
104 processData: false,
75 cache: false,
105 cache: false,
76 type: "POST",
106 type: "POST",
77 data: JSON.stringify(this._get_model()),
107 data: JSON.stringify(this._get_model()),
78 dataType: "json",
108 dataType: "json",
79 success: this._on_success(on_success),
109 success: this._on_success(on_success),
80 error: this._on_error(on_error)
110 error: this._on_error(on_error)
81 });
111 });
82 };
112 };
83
113
84 /**
114 /**
85 * GET /api/sessions/[:session_id]
115 * GET /api/sessions/[:session_id]
116 *
117 * Get information about a session.
118 *
119 * @function get_info
120 * @param {function} [success] - function executed on ajax success
121 * @param {function} [error] - functon executed on ajax error
86 */
122 */
87 Session.prototype.get_info = function (success, error) {
123 Session.prototype.get_info = function (success, error) {
88 $.ajax(this.session_url, {
124 $.ajax(this.session_url, {
89 processData: false,
125 processData: false,
90 cache: false,
126 cache: false,
91 type: "GET",
127 type: "GET",
92 dataType: "json",
128 dataType: "json",
93 success: this._on_success(success),
129 success: this._on_success(success),
94 error: this._on_error(error)
130 error: this._on_error(error)
95 });
131 });
96 };
132 };
97
133
98 /**
134 /**
99 * PATCH /api/sessions/[:session_id]
135 * PATCH /api/sessions/[:session_id]
136 *
137 * Rename or move a notebook. If the given name or path are
138 * undefined, then they will not be changed.
139 *
140 * @function rename_notebook
141 * @param {string} [name] - new notebook name
142 * @param {string} [path] - new path to notebook
143 * @param {function} [success] - function executed on ajax success
144 * @param {function} [error] - functon executed on ajax error
100 */
145 */
101 Session.prototype.change = function (notebook_name, notebook_path, kernel_name, success, error) {
146 Session.prototype.rename_notebook = function (name, path, success, error) {
102 if (notebook_name !== undefined) {
147 if (name !== undefined) {
103 this.notebook_model.name = notebook_name;
148 this.notebook_model.name = name;
104 }
105 if (notebook_path !== undefined) {
106 this.notebook_model.path = notebook_path;
107 }
149 }
108 if (kernel_name !== undefined) {
150 if (path !== undefined) {
109 this.kernel_model.name = kernel_name;
151 this.notebook_model.path = path;
110 }
152 }
111
153
112 console.log(JSON.stringify(this._get_model()));
113
114 $.ajax(this.session_url, {
154 $.ajax(this.session_url, {
115 processData: false,
155 processData: false,
116 cache: false,
156 cache: false,
117 type: "PATCH",
157 type: "PATCH",
118 data: JSON.stringify(this._get_model()),
158 data: JSON.stringify(this._get_model()),
119 dataType: "json",
159 dataType: "json",
120 success: this._on_success(success),
160 success: this._on_success(success),
121 error: this._on_error(error)
161 error: this._on_error(error)
122 });
162 });
123 };
163 };
124
164
125 Session.prototype.rename_notebook = function (name, path, success, error) {
126 this.change(name, path, undefined, success, error);
127 };
128
129 /**
165 /**
130 * DELETE /api/sessions/[:session_id]
166 * DELETE /api/sessions/[:session_id]
167 *
168 * Kill the kernel and shutdown the session.
169 *
170 * @function delete
171 * @param {function} [success] - function executed on ajax success
172 * @param {function} [error] - functon executed on ajax error
131 */
173 */
132 Session.prototype.delete = function (success, error) {
174 Session.prototype.delete = function (success, error) {
133 if (this.kernel) {
175 if (this.kernel) {
134 this.kernel._kernel_dead();
176 this.kernel._kernel_dead();
135 }
177 }
136
178
137 $.ajax(this.session_url, {
179 $.ajax(this.session_url, {
138 processData: false,
180 processData: false,
139 cache: false,
181 cache: false,
140 type: "DELETE",
182 type: "DELETE",
141 dataType: "json",
183 dataType: "json",
142 success: this._on_success(success),
184 success: this._on_success(success),
143 error: this._on_error(error)
185 error: this._on_error(error)
144 });
186 });
145 };
187 };
146
188
189 // Helper functions
190
191 /**
192 * Get the data model for the session, which includes the notebook
193 * (name and path) and kernel (name and id).
194 *
195 * @function _get_model
196 * @returns {Object} - the data model
197 */
147 Session.prototype._get_model = function () {
198 Session.prototype._get_model = function () {
148 return {
199 return {
149 notebook: this.notebook_model,
200 notebook: this.notebook_model,
150 kernel: this.kernel_model
201 kernel: this.kernel_model
151 };
202 };
152 };
203 };
153
204
205 /**
206 * Update the data model from the given JSON object, which should
207 * have attributes of `id`, `notebook`, and/or `kernel`. If
208 * provided, the notebook data must include name and path, and the
209 * kernel data must include name and id.
210 *
211 * @function _update_model
212 * @param {Object} data - updated data model
213 */
154 Session.prototype._update_model = function (data) {
214 Session.prototype._update_model = function (data) {
155 if (data && data.id) {
215 if (data && data.id) {
156 this.id = data.id;
216 this.id = data.id;
157 this.session_url = utils.url_join_encode(this.session_service_url, this.id);
217 this.session_url = utils.url_join_encode(this.session_service_url, this.id);
158 }
218 }
159 if (data && data.notebook) {
219 if (data && data.notebook) {
160 this.notebook_model.name = data.notebook.name;
220 this.notebook_model.name = data.notebook.name;
161 this.notebook_model.path = data.notebook.path;
221 this.notebook_model.path = data.notebook.path;
162 }
222 }
163 if (data && data.kernel) {
223 if (data && data.kernel) {
164 this.kernel_model.name = data.kernel.name;
224 this.kernel_model.name = data.kernel.name;
165 this.kernel_model.id = data.kernel.id;
225 this.kernel_model.id = data.kernel.id;
166 }
226 }
167 };
227 };
168
228
229 /**
230 * Handle a successful AJAX request by updating the session data
231 * model with the response, and then optionally calling a provided
232 * callback.
233 *
234 * @function _on_success
235 * @param {function} success - callback
236 */
169 Session.prototype._on_success = function (success) {
237 Session.prototype._on_success = function (success) {
170 var that = this;
238 var that = this;
171 return function (data, status, xhr) {
239 return function (data, status, xhr) {
172 that._update_model(data);
240 that._update_model(data);
173 if (success) {
241 if (success) {
174 success(data, status, xhr);
242 success(data, status, xhr);
175 }
243 }
176 };
244 };
177 };
245 };
178
246
247 /**
248 * Handle a failed AJAX request by logging the error message, and
249 * then optionally calling a provided callback.
250 *
251 * @function _on_error
252 * @param {function} error - callback
253 */
179 Session.prototype._on_error = function (error) {
254 Session.prototype._on_error = function (error) {
180 return function (xhr, status, err) {
255 return function (xhr, status, err) {
181 utils.log_ajax_error(xhr, status, err);
256 utils.log_ajax_error(xhr, status, err);
182 if (error) {
257 if (error) {
183 error(xhr, status, err);
258 error(xhr, status, err);
184 }
259 }
185 };
260 };
186 };
261 };
187
262
188
263 /**
264 * Error type indicating that the session is already starting.
265 */
189 var SessionAlreadyStarting = function (message) {
266 var SessionAlreadyStarting = function (message) {
190 this.name = "SessionAlreadyStarting";
267 this.name = "SessionAlreadyStarting";
191 this.message = (message || "");
268 this.message = (message || "");
192 };
269 };
193
194 SessionAlreadyStarting.prototype = Error.prototype;
270 SessionAlreadyStarting.prototype = Error.prototype;
195
271
196 // For backwards compatability.
272 // For backwards compatability.
197 IPython.Session = Session;
273 IPython.Session = Session;
198
274
199 return {
275 return {
200 Session: Session,
276 Session: Session,
201 SessionAlreadyStarting: SessionAlreadyStarting
277 SessionAlreadyStarting: SessionAlreadyStarting
202 };
278 };
203 });
279 });
General Comments 0
You need to be logged in to leave comments. Login now