-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue-dev-table.js
295 lines (256 loc) · 7.81 KB
/
queue-dev-table.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import templater from "microdata-template";
/**
* THIS IS A PRELIMINARY CONCEPT EXPLORATION
*
* queue-chart
* Means to Monitor Progress of Work Processing on HPC Cluster
*
* @author psylwester(at)idmod(dot)org
* @version 0.1.0, 2019/02/20
* @requires ES6, microdata-template
*
*/
const ENDPOINT = "data/";
const API = {
"QueueState": "QueueState.json",
"Stats": "Stats.json",
"Experiments": "Experiments.json"
};
const PRIORITY = {
1: {
key: "Highest",
name: "Highest"
},
2: {
key: "AboveNormal",
name: "Above Normal"
},
3: {
key: "Normal",
name: "Normal"
},
4: {
key: "BelowNormal",
name: "Below Normal"
},
5: {
key: "Lowest",
name: "Lowest"
},
6: {
key: "Work",
name: "Work Item"
}
};
const STATE = {
"PreActive": [
"Created",
"QueuedForCommission",
"CommissionRequested",
"Commissioned",
"Provisioning",
"Validating"
],
"Active": [
"Running",
"Waiting",
"QueuedForResume",
"ResumeRequested",
"Resumed",
"Retry"
],
"PostActive": [
"CancelRequested",
"Canceling",
"Canceled",
"Succeeded",
"Failed"
]
};
const collection = {
output: {},
prep: function (data) {
const mockedData = true;
const vitalizeMockDate = function (dateString) {
let yesterday = new Date(Date.now() - (36 * 60 * 60 * 1000));
let yesterdate = yesterday.toISOString().split("T")[0];
let recently = new Date(Date.parse(yesterdate + "T" + dateString.split("T")[1]) + (16 * 60 * 60 * 1000));
return recently.toISOString();
};
const dateTransform = function (node) {
/* preprocess dates from service-supplied GMT to ui-conducive Local */
let basis, basic, simple, elapsed;
if (node.hasOwnProperty("LastCreateTime")) {
if (mockedData) {
node.LastCreateTime = vitalizeMockDate(node.LastCreateTime);
}
basis = new Date(Date.parse(node.LastCreateTime));
basic = basis.toLocaleDateString("en-US",{ month: "long", day: "numeric", hour:"2-digit", minute:"2-digit", second:"2-digit" });
simple = basis.toLocaleDateString("en-US",{ weekday:"short", hour:"2-digit", minute:"2-digit" });
elapsed = ((Date.now() - basis)/1000/60/60).toFixed(1);
node["LastCreateBasic"] = basic;
node["LastCreateParts"] = simple.replace(/^(.*)(\d+\:\d+)(\s+)(.*)$/, "$1$2$4").split(/\s+/);
node["ElapsedTime"] = elapsed;
}
};
Object.values(data).forEach(value => {
if (Array.isArray(value)) {
value.forEach(item => {
dateTransform(item);
});
}
});
Object.values(PRIORITY).forEach(bucket => {
if(bucket.key in data) {} else {
data[bucket.key] = [];
}
});
return data;
},
merge: function(data) {
Object.values(this.output).forEach(value => {
if (Array.isArray(value)) {
value.forEach(item => {
if ("ExperimentId" in item && item.ExperimentId in data) {
Object.assign(item, data[item.ExperimentId]);
}
});
}
});
},
update: function (data) {
this.output = this.prep(data);
return this.output;
},
append: function (data) {
this.merge(data);
return this.output;
},
get latest () {
return this.output;
}
};
const doClick = function(event) {
event.preventDefault();
let id, temp, cell, row, ele = event.target;
while (!/^TD$/i.test(ele.nodeName)) {
ele = ele.parentElement;
}
cell = ele;
while (!/^TR$/i.test(ele.nodeName)) {
ele = ele.parentElement;
}
row = ele;
if (!!row && row.hasAttribute("itemid")) {
id = row.getAttribute("itemid");
if (cell.classList.contains("cancel")) {
if (window.confirm(`Are you sure you want to Cancel AND Delete this job!\n${id}?`)) {
temp = row.querySelector("TD[itemprop=PreActive]");
temp.querySelectorAll("LI").forEach(item => {
item.setAttribute("class", "CancelRequested process");
});
temp = row.querySelector("TD[itemprop=Active]");
temp.querySelectorAll("LI").forEach(item => {
item.setAttribute("class", "CancelRequested process");
});
}
} else if (cell.classList.contains("toggle")) {
row.parentNode.classList.toggle("active");
}
}
};
const fetchAll = function (successCallback, failureCallback) {
fetch(ENDPOINT+API.QueueState, { method:"GET" })
.then(response => response.json())
.then(data => collection.update(data.QueueState))
.then(response => fetch(ENDPOINT+API.Stats, { method:"GET" }))
.then(response => response.json())
.then(data => collection.append(data.Stats))
.then(update => new Promise(function(resolve) {
successCallback();
setTimeout(function () {
resolve(update);
}, 0);
}))
.catch(function (error) {
failureCallback(error);
})
.finally(function () {
console.log("Done!");
});
};
const recoup = function () {
};
const refresh = function () {
fetchAll(render, recoup);
};
const redraw = function (rootElement=document) {
let table = rootElement.querySelector("DIV[itemid=QueueChart] TABLE");
if (!!table) {
let template = table.querySelector("TBODY[hidden]");
while (!!table && table.lastChild !== template) {
table.removeChild(table.lastChild);
}
setTimeout(function () {
table.removeEventListener("click", doClick);
fetchAll(render, recoup);
},0);
}
};
const render = function (rootElement=document) {
let table = rootElement.querySelector("DIV[itemid=QueueChart] TABLE");
let tbody = !!table ? table.querySelector("TBODY[itemref]") : null;
if (!!tbody) {
Object.values(PRIORITY).forEach(bucket => {
let key = bucket.key;
let temp = tbody.cloneNode(true);
temp.setAttribute("itemref", key);
temp.removeAttribute("hidden");
temp.querySelector("TH").innerText = bucket.name;
table.appendChild(temp);
templater.render(temp.querySelector("TR[hidden]"), collection.latest[key]);
});
setTimeout(function () {
/* TODO: Deep dig via Collection.Prep and/or Templatize */
Object.values(collection.latest).forEach(batch => {
batch.forEach(item => {
let row = table.querySelector(`TR[itemid='${item.ExperimentId}']`);
Object.entries(STATE).forEach(category => {
category[1].forEach(state => {
let cell = row.querySelector(`TD[itemprop='${category[0]}'] UL`);
if (state in item["SimulationStateCount"]) {
let node = document.createElement("LI");
node.appendChild(document.createTextNode(item["SimulationStateCount"][state]));
node.classList.add(state);
if (/RUN|WAIT/i.test(state)) {
node.classList.add("process");
}
cell.appendChild(node);
}
});
});
});
});
/* TEMP: Insert icons for Work Items */
let work = table.querySelectorAll("TBODY[itemref=Work] TD.goto");
work.forEach(function (item,index) {
while (item.lastChild) {
item.removeChild(item.lastChild);
}
let span = document.createElement("SPAN");
span.classList.add("avatar");
if (index == 2) {
span.innerHTML = '<svg width="26" height="26"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon_Experiments"></use></svg>';
} else {
span.innerHTML = '<svg width="26" height="26"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon_WorkItems"></use></svg>';
}
item.appendChild(span);
});
table.addEventListener("click", doClick);
}, 0);
} else {
alert("failed to render!");
console.error("failed to render", collection.latest);
}
};
export default { refresh, redraw };