pig.ko.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. lastModTime: wf.lastModTime,
  78. endTime: wf.endTime,
  79. status: wf.status,
  80. statusClass: "label " + getStatusClass(wf.status),
  81. isRunning: wf.isRunning,
  82. duration: wf.duration,
  83. appName: wf.appName,
  84. progress: wf.progress,
  85. progressPercent: wf.progressPercent,
  86. progressClass: "bar " + getStatusClass(wf.status, "bar-"),
  87. user: wf.user,
  88. absoluteUrl: wf.absoluteUrl,
  89. watchUrl: wf.watchUrl,
  90. canEdit: wf.canEdit,
  91. killUrl: wf.killUrl,
  92. created: wf.created,
  93. run: wf.run
  94. }
  95. }
  96. var PigViewModel = function (props) {
  97. var self = this;
  98. self.LABELS = props.labels;
  99. self.LIST_SCRIPTS = props.listScripts;
  100. self.SAVE_URL = props.saveUrl;
  101. self.RUN_URL = props.runUrl;
  102. self.STOP_URL = props.stopUrl;
  103. self.COPY_URL = props.copyUrl;
  104. self.DELETE_URL = props.deleteUrl;
  105. self.isLoading = ko.observable(false);
  106. self.allSelected = ko.observable(false);
  107. self.submissionVariables = ko.observableArray([]);
  108. self.scripts = ko.observableArray([]);
  109. self.filteredScripts = ko.observableArray(self.scripts());
  110. self.runningScripts = ko.observableArray([]);
  111. self.completedScripts = ko.observableArray([]);
  112. var _defaultScript = {
  113. id: -1,
  114. name: self.LABELS.NEW_SCRIPT_NAME,
  115. script: self.LABELS.NEW_SCRIPT_CONTENT,
  116. parameters: self.LABELS.NEW_SCRIPT_PARAMETERS,
  117. resources: self.LABELS.NEW_SCRIPT_RESOURCES
  118. };
  119. self.currentScript = ko.observable(new PigScript(_defaultScript));
  120. self.currentDeleteType = ko.observable("");
  121. self.selectedScripts = ko.computed(function () {
  122. return ko.utils.arrayFilter(self.scripts(), function (script) {
  123. return script.selected();
  124. });
  125. }, self);
  126. self.selectedScript = ko.computed(function () {
  127. return self.selectedScripts()[0];
  128. }, self);
  129. self.selectAll = function () {
  130. self.allSelected(!self.allSelected());
  131. ko.utils.arrayForEach(self.scripts(), function (script) {
  132. script.selected(self.allSelected());
  133. });
  134. return true;
  135. };
  136. self.getScriptById = function (id) {
  137. var _s = null;
  138. ko.utils.arrayForEach(self.scripts(), function (script) {
  139. if (script.id() == id) {
  140. _s = script;
  141. }
  142. });
  143. return _s;
  144. }
  145. self.filterScripts = function (filter) {
  146. self.filteredScripts(ko.utils.arrayFilter(self.scripts(), function (script) {
  147. return script.isDesign() && script.name().toLowerCase().indexOf(filter.toLowerCase()) > -1
  148. }));
  149. };
  150. self.loadScript = function (id) {
  151. var _s = self.getScriptById(id);
  152. if (_s != null) {
  153. self.currentScript(_s);
  154. }
  155. else {
  156. self.currentScript(new PigScript(_defaultScript));
  157. }
  158. }
  159. self.newScript = function () {
  160. self.currentScript(new PigScript(_defaultScript));
  161. $(document).trigger("loadEditor");
  162. $(document).trigger("showEditor");
  163. };
  164. self.editScript = function (script) {
  165. $(document).trigger("showEditor");
  166. };
  167. self.editScriptProperties = function (script) {
  168. $(document).trigger("showProperties");
  169. };
  170. self.showScriptLogs = function (script) {
  171. $(document).trigger("showLogs");
  172. };
  173. self.viewScript = function (script) {
  174. self.currentScript(script);
  175. $(document).trigger("loadEditor");
  176. $(document).trigger("showEditor");
  177. };
  178. self.saveScript = function () {
  179. callSave(self.currentScript());
  180. };
  181. self.runScript = function () {
  182. callRun(self.currentScript());
  183. };
  184. self.copyScript = function () {
  185. callCopy(self.currentScript());
  186. };
  187. self.confirmDeleteScript = function () {
  188. self.currentDeleteType("single");
  189. showDeleteModal();
  190. };
  191. self.stopScript = function () {
  192. callStop(self.currentScript());
  193. };
  194. self.listRunScript = function () {
  195. callRun(self.selectedScript());
  196. };
  197. self.listCopyScript = function () {
  198. callCopy(self.selectedScript());
  199. };
  200. self.listConfirmDeleteScripts = function () {
  201. self.currentDeleteType("multiple");
  202. showDeleteModal();
  203. };
  204. self.deleteScripts = function () {
  205. var ids = [];
  206. if (self.currentDeleteType() == "single") {
  207. ids.push(self.currentScript().id());
  208. }
  209. if (self.currentDeleteType() == "multiple") {
  210. $(self.selectedScripts()).each(function (index, script) {
  211. ids.push(script.id());
  212. });
  213. }
  214. callDelete(ids);
  215. };
  216. self.updateScripts = function () {
  217. $.getJSON(self.LIST_SCRIPTS, function (data) {
  218. self.scripts(ko.utils.arrayMap(data, function (script) {
  219. return new PigScript(script);
  220. }));
  221. self.filteredScripts(self.scripts());
  222. $(document).trigger("scriptsRefreshed");
  223. });
  224. };
  225. self.updateDashboard = function (workflows) {
  226. var koWorkflows = ko.utils.arrayMap(workflows, function (wf) {
  227. return new Workflow(wf);
  228. });
  229. self.runningScripts(ko.utils.arrayFilter(koWorkflows, function (wf) {
  230. return wf.isRunning
  231. }));
  232. self.completedScripts(ko.utils.arrayFilter(koWorkflows, function (wf) {
  233. return !wf.isRunning
  234. }));
  235. }
  236. self.runOrShowSubmissionModal = function runOrShowSubmissionModal() {
  237. var script = self.currentScript();
  238. if (script.getParameters().length > 0) {
  239. self.submissionVariables.removeAll();
  240. $.each(script.getParameters(), function (key, value) {
  241. self.submissionVariables.push({'name': key, 'value': value});
  242. });
  243. $("#runScriptBtn").button("reset");
  244. $("#runScriptBtn").attr("data-loading-text", $("#runScriptBtn").text() + " ...");
  245. $("#submitModal").modal({
  246. keyboard: true,
  247. show: true
  248. });
  249. } else {
  250. self.runScript();
  251. }
  252. };
  253. self.showStopModal = function showStopModal() {
  254. $("#stopScriptBtn").button("reset");
  255. $("#stopScriptBtn").attr("data-loading-text", $("#stopScriptBtn").text() + " ...");
  256. $("#stopModal").modal({
  257. keyboard: true,
  258. show: true
  259. });
  260. }
  261. self.showFileChooser = function showFileChooser() {
  262. var inputPath = this;
  263. var path = inputPath.value().substr(0, inputPath.value().lastIndexOf("/"));
  264. $("#filechooser").jHueFileChooser({
  265. initialPath: path,
  266. onFileChoose: function (filePath) {
  267. inputPath.value(filePath);
  268. $("#chooseFile").modal("hide");
  269. },
  270. createFolder: false
  271. });
  272. $("#chooseFile").modal("show");
  273. };
  274. function showDeleteModal() {
  275. $(".deleteMsg").addClass("hide");
  276. if (self.currentDeleteType() == "single") {
  277. $(".deleteMsg.single").removeClass("hide");
  278. }
  279. if (self.currentDeleteType() == "multiple") {
  280. if (self.selectedScripts().length > 1) {
  281. $(".deleteMsg.multiple").removeClass("hide");
  282. }
  283. else {
  284. $(".deleteMsg.single").removeClass("hide");
  285. }
  286. }
  287. $("#deleteModal").modal({
  288. keyboard: true,
  289. show: true
  290. });
  291. }
  292. function showStopModal() {
  293. $(".stopMsg").addClass("hide");
  294. if (self.currentStopType() == "single") {
  295. $(".stopMsg.single").removeClass("hide");
  296. }
  297. if (self.currentStopType() == "multiple") {
  298. if (self.selectedScripts().length > 1) {
  299. $(".stopMsg.multiple").removeClass("hide");
  300. } else {
  301. $(".stopMsg.single").removeClass("hide");
  302. }
  303. }
  304. $("#stopModal").modal({
  305. keyboard: true,
  306. show: true
  307. });
  308. }
  309. function callSave(script) {
  310. $(document).trigger("saving");
  311. $.post(self.SAVE_URL,
  312. {
  313. id: script.id(),
  314. name: script.name(),
  315. script: script.script(),
  316. parameters: ko.utils.stringifyJson(script.parameters()),
  317. resources: ko.toJSON(script.resources())
  318. },
  319. function (data) {
  320. self.currentScript().id(data.id);
  321. $(document).trigger("saved");
  322. self.updateScripts();
  323. }, "json");
  324. }
  325. function callRun(script) {
  326. $(document).trigger("running");
  327. $.post(self.RUN_URL,
  328. {
  329. id: script.id(),
  330. name: script.name(),
  331. script: script.script(),
  332. parameters: ko.utils.stringifyJson(script.parameters()),
  333. submissionVariables: ko.utils.stringifyJson(self.submissionVariables()),
  334. resources: ko.toJSON(script.resources())
  335. },
  336. function (data) {
  337. if (data.id && self.currentScript().id() != data.id){
  338. self.currentScript(script);
  339. script.id(data.id);
  340. $(document).trigger("loadEditor");
  341. }
  342. script.isRunning(true);
  343. script.watchUrl(data.watchUrl);
  344. $(document).trigger("startLogsRefresh");
  345. $(document).trigger("refreshDashboard");
  346. $(document).trigger("showLogs");
  347. self.updateScripts();
  348. $("#submitModal").modal("hide");
  349. }, "json");
  350. }
  351. function callStop(script) {
  352. $(document).trigger("stopping");
  353. $.post(self.STOP_URL, {
  354. id: script.id()
  355. },
  356. function (data) {
  357. $(document).trigger("stopped");
  358. $("#stopModal").modal("hide");
  359. }, "json");
  360. }
  361. function callCopy(script) {
  362. $.post(self.COPY_URL,
  363. {
  364. id: script.id()
  365. },
  366. function (data) {
  367. self.currentScript(new PigScript(data));
  368. $(document).trigger("loadEditor");
  369. self.updateScripts();
  370. }, "json");
  371. }
  372. function callDelete(ids) {
  373. if (ids.indexOf(self.currentScript().id()) > -1) {
  374. self.currentScript(new PigScript(_defaultScript));
  375. $(document).trigger("loadEditor");
  376. }
  377. $.post(self.DELETE_URL,
  378. {
  379. ids: ids.join(",")
  380. },
  381. function (data) {
  382. self.updateScripts();
  383. $("#deleteModal").modal("hide");
  384. }, "json");
  385. }
  386. self.viewSubmittedScript = function (workflow) {
  387. self.loadScript(workflow.scriptId);
  388. self.currentScript().isRunning(true);
  389. self.currentScript().watchUrl(workflow.watchUrl);
  390. $(document).trigger("startLogsRefresh");
  391. $(document).trigger("showLogs");
  392. };
  393. self.showLogsInterval = -1;
  394. self.showLogsAtEnd = true;
  395. self.showLogs = function (workflow) {
  396. window.clearInterval(self.showLogsInterval);
  397. $("#logsModal pre").scroll(function () {
  398. self.showLogsAtEnd = $(this).scrollTop() + $(this).height() + 20 >= $(this)[0].scrollHeight;
  399. });
  400. if (workflow.isRunning) {
  401. $("#logsModal img").removeClass("hide");
  402. $("#logsModal pre").addClass("hide");
  403. $("#logsModal").modal({
  404. keyboard: true,
  405. show: true
  406. });
  407. $("#logsModal").on("hide", function () {
  408. window.clearInterval(self.showLogsInterval);
  409. });
  410. self.showLogsInterval = window.setInterval(function () {
  411. $.getJSON(workflow.watchUrl, function (data) {
  412. if (data.workflow && !data.workflow.isRunning) {
  413. window.clearInterval(self.showLogsInterval);
  414. }
  415. if (data.logs.pig) {
  416. $("#logsModal img").addClass("hide");
  417. $("#logsModal pre").removeClass("hide");
  418. var _logsEl = $("#logsModal pre");
  419. var newLines = data.logs.pig.split("\n").slice(_logsEl.text().split("\n").length);
  420. _logsEl.text(_logsEl.text() + newLines.join("\n"));
  421. if (self.showLogsAtEnd) {
  422. _logsEl.scrollTop(_logsEl[0].scrollHeight - _logsEl.height());
  423. }
  424. }
  425. });
  426. }, 1000);
  427. }
  428. };
  429. };