Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
// Copyright (c) IPython Development Team.
2
// Distributed under the terms of the Modified BSD License.
3
4
define([
5
'base/js/namespace',
6
'jquery',
7
'base/js/utils',
8
], function(IPython, $, utils) {
9
"use strict";
10
11
var ClusterList = function (selector, options) {
12
this.selector = selector;
13
if (this.selector !== undefined) {
14
this.element = $(selector);
15
this.style();
16
this.bind_events();
17
}
18
options = options || {};
19
this.options = options;
20
this.base_url = options.base_url || utils.get_body_data("baseUrl");
21
this.notebook_path = options.notebook_path || utils.get_body_data("notebookPath");
22
};
23
24
ClusterList.prototype.style = function () {
25
$('#cluster_list').addClass('list_container');
26
$('#cluster_toolbar').addClass('list_toolbar');
27
$('#cluster_list_info').addClass('toolbar_info');
28
$('#cluster_buttons').addClass('toolbar_buttons');
29
};
30
31
32
ClusterList.prototype.bind_events = function () {
33
var that = this;
34
$('#refresh_cluster_list').click(function () {
35
that.load_list();
36
});
37
};
38
39
40
ClusterList.prototype.load_list = function () {
41
var settings = {
42
processData : false,
43
cache : false,
44
type : "GET",
45
dataType : "json",
46
success : $.proxy(this.load_list_success, this),
47
error : utils.log_ajax_error,
48
};
49
var url = utils.url_join_encode(this.base_url, 'clusters');
50
$.ajax(url, settings);
51
};
52
53
54
ClusterList.prototype.clear_list = function () {
55
this.element.children('.list_item').remove();
56
};
57
58
ClusterList.prototype.load_list_success = function (data, status, xhr) {
59
this.clear_list();
60
var len = data.length;
61
for (var i=0; i<len; i++) {
62
var element = $('<div/>');
63
var item = new ClusterItem(element, this.options);
64
item.update_state(data[i]);
65
element.data('item', item);
66
this.element.append(element);
67
}
68
};
69
70
71
var ClusterItem = function (element, options) {
72
this.element = $(element);
73
this.base_url = options.base_url || utils.get_body_data("baseUrl");
74
this.notebook_path = options.notebook_path || utils.get_body_data("notebookPath");
75
this.data = null;
76
this.style();
77
};
78
79
ClusterItem.prototype.style = function () {
80
this.element.addClass('list_item').addClass("row");
81
};
82
83
ClusterItem.prototype.update_state = function (data) {
84
this.data = data;
85
if (data.status === 'running') {
86
this.state_running();
87
} else if (data.status === 'stopped') {
88
this.state_stopped();
89
}
90
};
91
92
93
ClusterItem.prototype.state_stopped = function () {
94
var that = this;
95
var profile_col = $('<div/>').addClass('profile_col col-xs-4').text(this.data.profile);
96
var status_col = $('<div/>').addClass('status_col col-xs-3').text('stopped');
97
var engines_col = $('<div/>').addClass('engine_col col-xs-3');
98
var input = $('<input/>').attr('type','number')
99
.attr('min',1)
100
.attr('size',3)
101
.addClass('engine_num_input form-control');
102
engines_col.append(input);
103
var start_button = $('<button/>').addClass("btn btn-default btn-xs").text("Start");
104
var action_col = $('<div/>').addClass('action_col col-xs-2').append(
105
$("<span/>").addClass("item_buttons btn-group").append(
106
start_button
107
)
108
);
109
this.element.empty()
110
.append(profile_col)
111
.append(status_col)
112
.append(engines_col)
113
.append(action_col);
114
start_button.click(function (e) {
115
var n = that.element.find('.engine_num_input').val();
116
if (!/^\d+$/.test(n) && n.length>0) {
117
status_col.text('invalid engine #');
118
} else {
119
var settings = {
120
cache : false,
121
data : {n:n},
122
type : "POST",
123
dataType : "json",
124
success : function (data, status, xhr) {
125
that.update_state(data);
126
},
127
error : function (xhr, status, error) {
128
status_col.text("error starting cluster");
129
utils.log_ajax_error(xhr, status, error);
130
}
131
};
132
status_col.text('starting');
133
var url = utils.url_join_encode(
134
that.base_url,
135
'clusters',
136
that.data.profile,
137
'start'
138
);
139
$.ajax(url, settings);
140
}
141
});
142
};
143
144
145
ClusterItem.prototype.state_running = function () {
146
var that = this;
147
var profile_col = $('<div/>').addClass('profile_col col-xs-4').text(this.data.profile);
148
var status_col = $('<div/>').addClass('status_col col-xs-3').text('running');
149
var engines_col = $('<div/>').addClass('engines_col col-xs-3').text(this.data.n);
150
var stop_button = $('<button/>').addClass("btn btn-default btn-xs").text("Stop");
151
var action_col = $('<div/>').addClass('action_col col-xs-2').append(
152
$("<span/>").addClass("item_buttons btn-group").append(
153
stop_button
154
)
155
);
156
this.element.empty()
157
.append(profile_col)
158
.append(status_col)
159
.append(engines_col)
160
.append(action_col);
161
stop_button.click(function (e) {
162
var settings = {
163
cache : false,
164
type : "POST",
165
dataType : "json",
166
success : function (data, status, xhr) {
167
that.update_state(data);
168
},
169
error : function (xhr, status, error) {
170
utils.log_ajax_error(xhr, status, error),
171
status_col.text("error stopping cluster");
172
}
173
};
174
status_col.text('stopping');
175
var url = utils.url_join_encode(
176
that.base_url,
177
'clusters',
178
that.data.profile,
179
'stop'
180
);
181
$.ajax(url, settings);
182
});
183
};
184
185
// For backwards compatability.
186
IPython.ClusterList = ClusterList;
187
IPython.ClusterItem = ClusterItem;
188
189
return {
190
'ClusterList': ClusterList,
191
'ClusterItem': ClusterItem,
192
};
193
});
194
195