pig.ko.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. var Resource = function (resource) {
  17. var self = this;
  18. self.type = ko.observable(resource.type);
  19. self.value = ko.observable(resource.value);
  20. };
  21. var PigScript = function (pigScript) {
  22. var self = this;
  23. self.id = ko.observable(pigScript.id);
  24. self.isDesign = ko.observable(pigScript.isDesign);
  25. self.name = ko.observable(pigScript.name);
  26. self.script = ko.observable(pigScript.script);
  27. self.scriptSumup = ko.observable(pigScript.script.replace(/\W+/g, ' ').substring(0, 100));
  28. self.isRunning = ko.observable(false);
  29. self.selected = ko.observable(false);
  30. self.watchUrl = ko.observable("");
  31. self.actions = ko.observableArray([]);
  32. self.handleSelect = function (row, e) {
  33. this.selected(!this.selected());
  34. };
  35. self.hovered = ko.observable(false);
  36. self.toggleHover = function (row, e) {
  37. this.hovered(!this.hovered());
  38. };
  39. self.parameters = ko.observableArray(pigScript.parameters);
  40. self.addParameter = function () {
  41. self.parameters.push({name: '', value: ''});
  42. };
  43. self.removeParameter = function () {
  44. self.parameters.remove(this);
  45. };
  46. self.getParameters = function () {
  47. var params = {};
  48. var variables = this.script().match(/\$(\w)+/g);
  49. if (variables) {
  50. $.each(variables, function(index, param) {
  51. var p = param.substring(1);
  52. params[p] = '';
  53. $.each(self.parameters(), function(index, param) {
  54. if (param['name'] == p) {
  55. params[p] = param['value'];
  56. }
  57. });
  58. });
  59. }
  60. return params;
  61. };
  62. self.resources = ko.observableArray([]);
  63. ko.utils.arrayForEach(pigScript.resources, function (resource) {
  64. self.resources.push(new Resource({type: resource.type, value: resource.value}));
  65. });
  66. self.addResource = function () {
  67. self.resources.push(new Resource({type: 'file', value: ''}));
  68. };
  69. self.removeResource = function () {
  70. self.resources.remove(this);
  71. };
  72. }
  73. var Workflow = function (wf) {
  74. return {
  75. id: wf.id,
  76. scriptId: wf.scriptId,
  77. scriptContent: wf.scriptContent,
  78. lastModTime: wf.lastModTime,
  79. endTime: wf.endTime,
  80. status: wf.status,
  81. statusClass: "label " + getStatusClass(wf.status),
  82. isRunning: wf.isRunning,
  83. duration: wf.duration,
  84. appName: wf.appName,
  85. progress: wf.progress,
  86. progressPercent: wf.progressPercent,
  87. progressClass: "bar " + getStatusClass(wf.status, "bar-"),
  88. user: wf.user,
  89. absoluteUrl: wf.absoluteUrl,
  90. watchUrl: wf.watchUrl,
  91. canEdit: wf.canEdit,
  92. killUrl: wf.killUrl,
  93. created: wf.created,
  94. run: wf.run
  95. }
  96. }
  97. var PigViewModel = function (props) {
  98. var self = this;
  99. self.LABELS = props.labels;
  100. self.LIST_SCRIPTS = props.listScripts;
  101. self.SAVE_URL = props.saveUrl;
  102. self.RUN_URL = props.runUrl;
  103. self.STOP_URL = props.stopUrl;
  104. self.COPY_URL = props.copyUrl;
  105. self.DELETE_URL = props.deleteUrl;
  106. self.isLoading = ko.observable(false);
  107. self.allSelected = ko.observable(false);
  108. self.submissionVariables = ko.observableArray([]);
  109. self.scripts = ko.observableArray([]);
  110. self.filteredScripts = ko.observableArray(self.scripts());
  111. self.runningScripts = ko.observableArray([]);
  112. self.completedScripts = ko.observableArray([]);
  113. self.isDashboardLoaded = false;
  114. var _defaultScript = {
  115. id: -1,
  116. name: self.LABELS.NEW_SCRIPT_NAME,
  117. script: self.LABELS.NEW_SCRIPT_CONTENT,
  118. parameters: self.LABELS.NEW_SCRIPT_PARAMETERS,
  119. resources: self.LABELS.NEW_SCRIPT_RESOURCES
  120. };
  121. self.currentScript = ko.observable(new PigScript(_defaultScript));
  122. self.currentDeleteType = ko.observable("");
  123. self.selectedScripts = ko.computed(function () {
  124. return ko.utils.arrayFilter(self.scripts(), function (script) {
  125. return script.selected();
  126. });
  127. }, self);
  128. self.selectedScript = ko.computed(function () {
  129. return self.selectedScripts()[0];
  130. }, self);
  131. self.selectAll = function () {
  132. self.allSelected(! self.allSelected());
  133. ko.utils.arrayForEach(self.scripts(), function (script) {
  134. script.selected(self.allSelected());
  135. });
  136. return true;
  137. };
  138. self.getScriptById = function (id) {
  139. var _s = null;
  140. ko.utils.arrayForEach(self.scripts(), function (script) {
  141. if (script.id() == id) {
  142. _s = script;
  143. }
  144. });
  145. return _s;
  146. }
  147. self.filterScripts = function (filter) {
  148. self.filteredScripts(ko.utils.arrayFilter(self.scripts(), function (script) {
  149. return script.isDesign() && script.name().toLowerCase().indexOf(filter.toLowerCase()) > -1
  150. }));
  151. };
  152. self.loadScript = function (id) {
  153. var _s = self.getScriptById(id);
  154. if (_s != null) {
  155. self.currentScript(_s);
  156. }
  157. else {
  158. self.currentScript(new PigScript(_defaultScript));
  159. }
  160. }
  161. self.newScript = function () {
  162. self.currentScript(new PigScript(_defaultScript));
  163. $(document).trigger("loadEditor");
  164. $(document).trigger("showEditor");
  165. };
  166. self.editScript = function (script) {
  167. $(document).trigger("showEditor");
  168. };
  169. self.editScriptProperties = function (script) {
  170. $(document).trigger("showProperties");
  171. };
  172. self.showScriptLogs = function (script) {
  173. $(document).trigger("showLogs");
  174. };
  175. self.viewScript = function (script) {
  176. self.currentScript(script);
  177. $(document).trigger("loadEditor");
  178. $(document).trigger("showEditor");
  179. };
  180. self.saveScript = function () {
  181. callSave(self.currentScript());
  182. };
  183. self.runScript = function () {
  184. callRun(self.currentScript());
  185. };
  186. self.copyScript = function () {
  187. callCopy(self.currentScript());
  188. };
  189. self.confirmDeleteScript = function () {
  190. self.currentDeleteType("single");
  191. showDeleteModal();
  192. };
  193. self.stopScript = function () {
  194. callStop(self.currentScript());
  195. };
  196. self.listRunScript = function () {
  197. self.currentScript(self.selectedScript());
  198. self.runOrShowSubmissionModal();
  199. };
  200. self.listCopyScript = function () {
  201. callCopy(self.selectedScript());
  202. };
  203. self.listConfirmDeleteScripts = function () {
  204. self.currentDeleteType("multiple");
  205. showDeleteModal();
  206. };
  207. self.deleteScripts = function () {
  208. var ids = [];
  209. if (self.currentDeleteType() == "single") {
  210. ids.push(self.currentScript().id());
  211. }
  212. if (self.currentDeleteType() == "multiple") {
  213. $(self.selectedScripts()).each(function (index, script) {
  214. ids.push(script.id());
  215. });
  216. }
  217. callDelete(ids);
  218. };
  219. self.updateScripts = function () {
  220. $.getJSON(self.LIST_SCRIPTS, function (data) {
  221. self.scripts(ko.utils.arrayMap(data, function (script) {
  222. return new PigScript(script);
  223. }));
  224. self.filteredScripts(self.scripts());
  225. $(document).trigger("scriptsRefreshed");
  226. });
  227. };
  228. self.updateDashboard = function (workflows) {
  229. self.isDashboardLoaded = true;
  230. var koWorkflows = ko.utils.arrayMap(workflows, function (wf) {
  231. return new Workflow(wf);
  232. });
  233. self.runningScripts(ko.utils.arrayFilter(koWorkflows, function (wf) {
  234. return wf.isRunning
  235. }));
  236. self.completedScripts(ko.utils.arrayFilter(koWorkflows, function (wf) {
  237. return !wf.isRunning
  238. }));
  239. }
  240. self.runOrShowSubmissionModal = function runOrShowSubmissionModal() {
  241. var script = self.currentScript();
  242. if (! $.isEmptyObject(script.getParameters())) {
  243. self.submissionVariables.removeAll();
  244. $.each(script.getParameters(), function (key, value) {
  245. self.submissionVariables.push({'name': key, 'value': value});
  246. });
  247. $("#runScriptBtn").button("reset");
  248. $("#runScriptBtn").attr("data-loading-text", $("#runScriptBtn").text() + " ...");
  249. $("#submitModal").modal({
  250. keyboard: true,
  251. show: true
  252. });
  253. } else {
  254. self.runScript();
  255. }
  256. };
  257. self.showStopModal = function showStopModal() {
  258. $("#stopScriptBtn").button("reset");
  259. $("#stopScriptBtn").attr("data-loading-text", $("#stopScriptBtn").text() + " ...");
  260. $("#stopModal").modal({
  261. keyboard: true,
  262. show: true
  263. });
  264. }
  265. self.showFileChooser = function showFileChooser() {
  266. var inputPath = this;
  267. var path = inputPath.value().substr(0, inputPath.value().lastIndexOf("/"));
  268. $("#filechooser").jHueFileChooser({
  269. initialPath: path,
  270. onFileChoose: function (filePath) {
  271. inputPath.value(filePath);
  272. $("#chooseFile").modal("hide");
  273. },
  274. createFolder: false
  275. });
  276. $("#chooseFile").modal("show");
  277. };
  278. function showDeleteModal() {
  279. $(".deleteMsg").addClass("hide");
  280. if (self.currentDeleteType() == "single") {
  281. $(".deleteMsg.single").removeClass("hide");
  282. }
  283. if (self.currentDeleteType() == "multiple") {
  284. if (self.selectedScripts().length > 1) {
  285. $(".deleteMsg.multiple").removeClass("hide");
  286. }
  287. else {
  288. $(".deleteMsg.single").removeClass("hide");
  289. }
  290. }
  291. $("#deleteModal").modal({
  292. keyboard: true,
  293. show: true
  294. });
  295. }
  296. function showStopModal() {
  297. $(".stopMsg").addClass("hide");
  298. if (self.currentStopType() == "single") {
  299. $(".stopMsg.single").removeClass("hide");
  300. }
  301. if (self.currentStopType() == "multiple") {
  302. if (self.selectedScripts().length > 1) {
  303. $(".stopMsg.multiple").removeClass("hide");
  304. } else {
  305. $(".stopMsg.single").removeClass("hide");
  306. }
  307. }
  308. $("#stopModal").modal({
  309. keyboard: true,
  310. show: true
  311. });
  312. }
  313. function callSave(script) {
  314. $(document).trigger("saving");
  315. $.post(self.SAVE_URL,
  316. {
  317. id: script.id(),
  318. name: script.name(),
  319. script: script.script(),
  320. parameters: ko.utils.stringifyJson(script.parameters()),
  321. resources: ko.toJSON(script.resources())
  322. },
  323. function (data) {
  324. self.currentScript().id(data.id);
  325. $(document).trigger("saved");
  326. self.updateScripts();
  327. }, "json");
  328. }
  329. function callRun(script) {
  330. self.currentScript(script);
  331. $(document).trigger("running");
  332. $.post(self.RUN_URL,
  333. {
  334. id: script.id(),
  335. name: script.name(),
  336. script: script.script(),
  337. parameters: ko.utils.stringifyJson(script.parameters()),
  338. submissionVariables: ko.utils.stringifyJson(self.submissionVariables()),
  339. resources: ko.toJSON(script.resources())
  340. },
  341. function (data) {
  342. if (data.id && self.currentScript().id() != data.id){
  343. script.id(data.id);
  344. $(document).trigger("loadEditor");
  345. }
  346. script.isRunning(true);
  347. script.watchUrl(data.watchUrl);
  348. $(document).trigger("startLogsRefresh");
  349. $(document).trigger("refreshDashboard");
  350. $(document).trigger("showLogs");
  351. self.updateScripts();
  352. $("#submitModal").modal("hide");
  353. }, "json");
  354. }
  355. function callStop(script) {
  356. $(document).trigger("stopping");
  357. $.post(self.STOP_URL, {
  358. id: script.id()
  359. },
  360. function (data) {
  361. $(document).trigger("stopped");
  362. $("#stopModal").modal("hide");
  363. }, "json");
  364. }
  365. function callCopy(script) {
  366. $.post(self.COPY_URL,
  367. {
  368. id: script.id()
  369. },
  370. function (data) {
  371. self.currentScript(new PigScript(data));
  372. $(document).trigger("loadEditor");
  373. self.updateScripts();
  374. }, "json");
  375. }
  376. function callDelete(ids) {
  377. if (ids.indexOf(self.currentScript().id()) > -1) {
  378. self.currentScript(new PigScript(_defaultScript));
  379. $(document).trigger("loadEditor");
  380. }
  381. $.post(self.DELETE_URL,
  382. {
  383. ids: ids.join(",")
  384. },
  385. function (data) {
  386. self.updateScripts();
  387. $("#deleteModal").modal("hide");
  388. }, "json");
  389. }
  390. self.viewSubmittedScript = function (workflow) {
  391. self.loadScript(workflow.scriptId);
  392. self.currentScript().script(workflow.scriptContent);
  393. self.currentScript().isRunning(true);
  394. self.currentScript().watchUrl(workflow.watchUrl);
  395. $(document).trigger("loadEditor");
  396. $(document).trigger("clearLogs");
  397. $(document).trigger("startLogsRefresh");
  398. $(document).trigger("showLogs");
  399. };
  400. self.showLogsInterval = -1;
  401. self.showLogsAtEnd = true;
  402. self.showLogs = function (workflow) {
  403. window.clearInterval(self.showLogsInterval);
  404. $("#logsModal pre").scroll(function () {
  405. self.showLogsAtEnd = $(this).scrollTop() + $(this).height() + 20 >= $(this)[0].scrollHeight;
  406. });
  407. if (workflow.isRunning) {
  408. $("#logsModal img").removeClass("hide");
  409. $("#logsModal pre").addClass("hide");
  410. $("#logsModal").modal({
  411. keyboard: true,
  412. show: true
  413. });
  414. $("#logsModal").on("hide", function () {
  415. window.clearInterval(self.showLogsInterval);
  416. });
  417. self.showLogsInterval = window.setInterval(function () {
  418. $.getJSON(workflow.watchUrl, function (data) {
  419. if (data.workflow && !data.workflow.isRunning) {
  420. window.clearInterval(self.showLogsInterval);
  421. }
  422. if (data.logs.pig) {
  423. $("#logsModal img").addClass("hide");
  424. $("#logsModal pre").removeClass("hide");
  425. var _logsEl = $("#logsModal pre");
  426. var newLines = data.logs.pig.split("\n").slice(_logsEl.text().split("\n").length);
  427. if (newLines.length > 0){
  428. _logsEl.text(_logsEl.text() + newLines.join("\n") + "\n");
  429. }
  430. if (self.showLogsAtEnd) {
  431. _logsEl.scrollTop(_logsEl[0].scrollHeight - _logsEl.height());
  432. }
  433. }
  434. });
  435. }, 1000);
  436. }
  437. };
  438. };