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