pig.ko.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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 HadoopProperty = function (property) {
  22. var self = this;
  23. self.name = ko.observable(property.name);
  24. self.value = ko.observable(property.value);
  25. };
  26. var PigParameter = HadoopProperty;
  27. var PigScript = function (pigScript) {
  28. var self = this;
  29. self.id = ko.observable(pigScript.id);
  30. self.isDesign = ko.observable(pigScript.isDesign);
  31. self.name = ko.observable(pigScript.name);
  32. self.script = ko.observable(pigScript.script);
  33. self.scriptSumup = ko.observable(pigScript.script.replace(/\W+/g, ' ').substring(0, 100));
  34. self.isRunning = ko.observable(false);
  35. self.selected = ko.observable(false);
  36. self.watchUrl = ko.observable("");
  37. self.actions = ko.observableArray([]);
  38. self.handleSelect = function (row, e) {
  39. this.selected(!this.selected());
  40. };
  41. self.hovered = ko.observable(false);
  42. self.toggleHover = function (row, e) {
  43. this.hovered(!this.hovered());
  44. };
  45. self.parameters = ko.observableArray([]);
  46. ko.utils.arrayForEach(pigScript.parameters, function (parameter) {
  47. self.parameters.push(new PigParameter({name: parameter.name, value: parameter.value}));
  48. });
  49. self.addParameter = function () {
  50. self.parameters.push(new PigParameter({name: '', value: ''}));
  51. };
  52. self.removeParameter = function () {
  53. self.parameters.remove(this);
  54. };
  55. self.getParameters = function () {
  56. var params = {};
  57. var variables = this.script().match(/\$\D(\w*)/g);
  58. if (variables) {
  59. $.each(variables, function(index, param) {
  60. var p = param.substring(1);
  61. params[p] = '';
  62. });
  63. }
  64. $.each(self.parameters(), function(index, param) {
  65. params[param.name()] = param.value();
  66. });
  67. return params;
  68. };
  69. self.hadoopProperties = ko.observableArray([]);
  70. ko.utils.arrayForEach(pigScript.hadoopProperties, function (property) {
  71. self.hadoopProperties.push(new HadoopProperty({name: property.name, value: property.value}));
  72. });
  73. self.addHadoopProperties = function () {
  74. self.hadoopProperties.push(new HadoopProperty({name: '', value: ''}));
  75. };
  76. self.removeHadoopProperties = function () {
  77. self.hadoopProperties.remove(this);
  78. };
  79. self.resources = ko.observableArray([]);
  80. ko.utils.arrayForEach(pigScript.resources, function (resource) {
  81. self.resources.push(new Resource({type: resource.type, value: resource.value}));
  82. });
  83. self.addResource = function () {
  84. self.resources.push(new Resource({type: 'file', value: ''}));
  85. };
  86. self.removeResource = function () {
  87. self.resources.remove(this);
  88. };
  89. }
  90. var Workflow = function (wf) {
  91. return {
  92. id: wf.id,
  93. scriptId: wf.scriptId,
  94. scriptContent: wf.scriptContent,
  95. lastModTime: wf.lastModTime,
  96. endTime: wf.endTime,
  97. status: wf.status,
  98. statusClass: "label " + getStatusClass(wf.status),
  99. isRunning: wf.isRunning,
  100. duration: wf.duration,
  101. appName: wf.appName,
  102. progress: wf.progress,
  103. progressPercent: wf.progressPercent,
  104. progressClass: "bar " + getStatusClass(wf.status, "bar-"),
  105. user: wf.user,
  106. absoluteUrl: wf.absoluteUrl,
  107. watchUrl: wf.watchUrl,
  108. canEdit: wf.canEdit,
  109. killUrl: wf.killUrl,
  110. created: wf.created,
  111. run: wf.run
  112. }
  113. }
  114. var PigViewModel = function (props) {
  115. var self = this;
  116. self.LABELS = props.labels;
  117. self.LIST_SCRIPTS = props.listScripts;
  118. self.SAVE_URL = props.saveUrl;
  119. self.RUN_URL = props.runUrl;
  120. self.STOP_URL = props.stopUrl;
  121. self.COPY_URL = props.copyUrl;
  122. self.DELETE_URL = props.deleteUrl;
  123. self.isLoading = ko.observable(false);
  124. self.allSelected = ko.observable(false);
  125. self.submissionVariables = ko.observableArray([]);
  126. self.scripts = ko.observableArray([]);
  127. self.filteredScripts = ko.observableArray(self.scripts());
  128. self.runningScripts = ko.observableArray([]);
  129. self.completedScripts = ko.observableArray([]);
  130. self.isDashboardLoaded = false;
  131. self.isDirty = ko.observable(false);
  132. var _defaultScript = {
  133. id: -1,
  134. name: self.LABELS.NEW_SCRIPT_NAME,
  135. script: self.LABELS.NEW_SCRIPT_CONTENT,
  136. parameters: self.LABELS.NEW_SCRIPT_PARAMETERS,
  137. resources: self.LABELS.NEW_SCRIPT_RESOURCES,
  138. hadoopProperties: self.LABELS.NEW_SCRIPT_HADOOP_PROPERTIES
  139. };
  140. self.currentScript = ko.observable(new PigScript(_defaultScript));
  141. self.loadingScript = null;
  142. self.currentDeleteType = ko.observable("");
  143. self.selectedScripts = ko.computed(function () {
  144. return ko.utils.arrayFilter(self.scripts(), function (script) {
  145. return script.selected();
  146. });
  147. }, self);
  148. self.selectedScript = ko.computed(function () {
  149. return self.selectedScripts()[0];
  150. }, self);
  151. self.selectAll = function () {
  152. self.allSelected(! self.allSelected());
  153. ko.utils.arrayForEach(self.scripts(), function (script) {
  154. script.selected(self.allSelected());
  155. });
  156. return true;
  157. };
  158. self.getScriptById = function (id) {
  159. var _s = null;
  160. ko.utils.arrayForEach(self.scripts(), function (script) {
  161. if (script.id() == id) {
  162. _s = script;
  163. }
  164. });
  165. return _s;
  166. }
  167. self.filterScripts = function (filter) {
  168. self.filteredScripts(ko.utils.arrayFilter(self.scripts(), function (script) {
  169. return script.isDesign() && script.name().toLowerCase().indexOf(filter.toLowerCase()) > -1
  170. }));
  171. };
  172. self.loadScript = function (id) {
  173. var _s = self.getScriptById(id);
  174. if (_s != null) {
  175. self.currentScript(_s);
  176. }
  177. else {
  178. self.currentScript(new PigScript(_defaultScript));
  179. }
  180. }
  181. self.confirmNewScript = function () {
  182. if (self.isDirty()) {
  183. showConfirmModal();
  184. }
  185. else {
  186. self.newScript();
  187. }
  188. };
  189. self.confirmScript = function () {
  190. if (self.loadingScript != null){
  191. self.viewScript(self.loadingScript);
  192. }
  193. else {
  194. self.newScript();
  195. }
  196. };
  197. self.newScript = function () {
  198. self.loadingScript = null;
  199. self.currentScript(new PigScript(_defaultScript));
  200. self.isDirty(false);
  201. $("#confirmModal").modal("hide");
  202. $(document).trigger("loadEditor");
  203. $(document).trigger("showEditor");
  204. };
  205. self.editScript = function (script) {
  206. $(document).trigger("showEditor");
  207. };
  208. self.editScriptProperties = function (script) {
  209. $(document).trigger("showProperties");
  210. };
  211. self.showScriptLogs = function (script) {
  212. $(document).trigger("showLogs");
  213. };
  214. self.confirmViewScript = function (script) {
  215. if (self.isDirty()) {
  216. self.loadingScript = script;
  217. showConfirmModal();
  218. }
  219. else {
  220. self.viewScript(script);
  221. }
  222. };
  223. self.viewScript = function (script) {
  224. self.loadingScript = null;
  225. self.currentScript(script);
  226. self.isDirty(false);
  227. $("#confirmModal").modal("hide");
  228. $(document).trigger("loadEditor");
  229. $(document).trigger("showEditor");
  230. };
  231. self.saveScript = function () {
  232. if (self.LABELS.NEW_SCRIPT_NAME == self.currentScript().name()){
  233. showNameModal();
  234. }
  235. else {
  236. $("#nameModal").modal("hide");
  237. callSave(self.currentScript());
  238. viewModel.isDirty(false);
  239. }
  240. };
  241. self.runScript = function () {
  242. callRun(self.currentScript());
  243. };
  244. self.copyScript = function () {
  245. callCopy(self.currentScript());
  246. viewModel.isDirty(true);
  247. };
  248. self.confirmDeleteScript = function () {
  249. self.currentDeleteType("single");
  250. showDeleteModal();
  251. };
  252. self.stopScript = function () {
  253. callStop(self.currentScript());
  254. };
  255. self.listRunScript = function () {
  256. self.currentScript(self.selectedScript());
  257. self.runOrShowSubmissionModal();
  258. };
  259. self.listCopyScript = function () {
  260. callCopy(self.selectedScript());
  261. };
  262. self.listConfirmDeleteScripts = function () {
  263. self.currentDeleteType("multiple");
  264. showDeleteModal();
  265. };
  266. self.deleteScripts = function () {
  267. var ids = [];
  268. if (self.currentDeleteType() == "single") {
  269. ids.push(self.currentScript().id());
  270. }
  271. if (self.currentDeleteType() == "multiple") {
  272. $(self.selectedScripts()).each(function (index, script) {
  273. ids.push(script.id());
  274. });
  275. }
  276. callDelete(ids);
  277. };
  278. self.updateScripts = function () {
  279. $.getJSON(self.LIST_SCRIPTS, function (data) {
  280. self.scripts(ko.utils.arrayMap(data, function (script) {
  281. return new PigScript(script);
  282. }));
  283. self.filteredScripts(self.scripts());
  284. $(document).trigger("scriptsRefreshed");
  285. });
  286. };
  287. self.updateDashboard = function (workflows) {
  288. self.isDashboardLoaded = true;
  289. var koWorkflows = ko.utils.arrayMap(workflows, function (wf) {
  290. return new Workflow(wf);
  291. });
  292. self.runningScripts(ko.utils.arrayFilter(koWorkflows, function (wf) {
  293. return wf.isRunning
  294. }));
  295. self.completedScripts(ko.utils.arrayFilter(koWorkflows, function (wf) {
  296. return !wf.isRunning
  297. }));
  298. }
  299. self.runOrShowSubmissionModal = function runOrShowSubmissionModal() {
  300. var script = self.currentScript();
  301. if (! $.isEmptyObject(script.getParameters())) {
  302. self.submissionVariables.removeAll();
  303. $.each(script.getParameters(), function (key, value) {
  304. self.submissionVariables.push({'name': key, 'value': value});
  305. });
  306. $("#runScriptBtn").button("reset");
  307. $("#runScriptBtn").attr("data-loading-text", $("#runScriptBtn").text() + " ...");
  308. $("#submitModal").modal({
  309. keyboard: true,
  310. show: true
  311. });
  312. } else {
  313. self.runScript();
  314. }
  315. };
  316. self.showStopModal = function showStopModal() {
  317. $("#stopScriptBtn").button("reset");
  318. $("#stopScriptBtn").attr("data-loading-text", $("#stopScriptBtn").text() + " ...");
  319. $("#stopModal").modal({
  320. keyboard: true,
  321. show: true
  322. });
  323. }
  324. self.showFileChooser = function showFileChooser() {
  325. var inputPath = this;
  326. var path = inputPath.value().substr(0, inputPath.value().lastIndexOf("/"));
  327. $("#filechooser").jHueFileChooser({
  328. initialPath: path,
  329. onFileChoose: function (filePath) {
  330. inputPath.value(filePath);
  331. $("#chooseFile").modal("hide");
  332. },
  333. createFolder: false
  334. });
  335. $("#chooseFile").modal("show");
  336. };
  337. function showDeleteModal() {
  338. $(".deleteMsg").addClass("hide");
  339. if (self.currentDeleteType() == "single") {
  340. $(".deleteMsg.single").removeClass("hide");
  341. }
  342. if (self.currentDeleteType() == "multiple") {
  343. if (self.selectedScripts().length > 1) {
  344. $(".deleteMsg.multiple").removeClass("hide");
  345. }
  346. else {
  347. $(".deleteMsg.single").removeClass("hide");
  348. }
  349. }
  350. $("#deleteModal").modal({
  351. keyboard: true,
  352. show: true
  353. });
  354. }
  355. function showStopModal() {
  356. $(".stopMsg").addClass("hide");
  357. if (self.currentStopType() == "single") {
  358. $(".stopMsg.single").removeClass("hide");
  359. }
  360. if (self.currentStopType() == "multiple") {
  361. if (self.selectedScripts().length > 1) {
  362. $(".stopMsg.multiple").removeClass("hide");
  363. } else {
  364. $(".stopMsg.single").removeClass("hide");
  365. }
  366. }
  367. $("#stopModal").modal({
  368. keyboard: true,
  369. show: true
  370. });
  371. }
  372. function showConfirmModal() {
  373. $("#confirmModal").modal({
  374. keyboard: true,
  375. show: true
  376. });
  377. }
  378. function showNameModal() {
  379. $("#nameModal").modal({
  380. keyboard: true,
  381. show: true
  382. });
  383. }
  384. function callSave(script) {
  385. $(document).trigger("saving");
  386. $.post(self.SAVE_URL,
  387. {
  388. id: script.id(),
  389. name: script.name(),
  390. script: script.script(),
  391. parameters: ko.toJSON(script.parameters()),
  392. resources: ko.toJSON(script.resources()),
  393. hadoopProperties: ko.toJSON(script.hadoopProperties()),
  394. },
  395. function (data) {
  396. self.currentScript().id(data.id);
  397. $(document).trigger("saved");
  398. self.updateScripts();
  399. }, "json");
  400. }
  401. function callRun(script) {
  402. self.currentScript(script);
  403. $(document).trigger("running");
  404. $.post(self.RUN_URL,
  405. {
  406. id: script.id(),
  407. name: script.name(),
  408. script: script.script(),
  409. parameters: ko.toJSON(script.parameters()),
  410. submissionVariables: ko.utils.stringifyJson(self.submissionVariables()),
  411. resources: ko.toJSON(script.resources()),
  412. hadoopProperties: ko.toJSON(script.hadoopProperties())
  413. },
  414. function (data) {
  415. if (data.id && self.currentScript().id() != data.id){
  416. script.id(data.id);
  417. $(document).trigger("loadEditor");
  418. }
  419. script.isRunning(true);
  420. script.watchUrl(data.watchUrl);
  421. $(document).trigger("startLogsRefresh");
  422. $(document).trigger("refreshDashboard");
  423. $(document).trigger("showLogs");
  424. self.updateScripts();
  425. $("#submitModal").modal("hide");
  426. }, "json");
  427. }
  428. function callStop(script) {
  429. $(document).trigger("stopping");
  430. $.post(self.STOP_URL, {
  431. id: script.id()
  432. },
  433. function (data) {
  434. $(document).trigger("stopped");
  435. $("#stopModal").modal("hide");
  436. }, "json");
  437. }
  438. function callCopy(script) {
  439. $.post(self.COPY_URL,
  440. {
  441. id: script.id()
  442. },
  443. function (data) {
  444. self.currentScript(new PigScript(data));
  445. $(document).trigger("loadEditor");
  446. self.updateScripts();
  447. }, "json");
  448. }
  449. function callDelete(ids) {
  450. if (ids.indexOf(self.currentScript().id()) > -1) {
  451. self.currentScript(new PigScript(_defaultScript));
  452. $(document).trigger("loadEditor");
  453. }
  454. $.post(self.DELETE_URL, {
  455. ids: ids.join(",")
  456. },
  457. function (data) {
  458. self.updateScripts();
  459. $("#deleteModal").modal("hide");
  460. viewModel.isDirty(false);
  461. }, "json");
  462. }
  463. self.viewSubmittedScript = function (workflow) {
  464. self.loadScript(workflow.scriptId);
  465. self.currentScript().script(workflow.scriptContent);
  466. self.currentScript().isRunning(true);
  467. self.currentScript().watchUrl(workflow.watchUrl);
  468. $(document).trigger("loadEditor");
  469. $(document).trigger("clearLogs");
  470. $(document).trigger("startLogsRefresh");
  471. $(document).trigger("showLogs");
  472. };
  473. self.showLogsInterval = -1;
  474. self.showLogsAtEnd = true;
  475. self.showLogs = function (workflow) {
  476. window.clearInterval(self.showLogsInterval);
  477. $("#logsModal pre").scroll(function () {
  478. self.showLogsAtEnd = $(this).scrollTop() + $(this).height() + 20 >= $(this)[0].scrollHeight;
  479. });
  480. if (workflow.isRunning) {
  481. $("#logsModal img").removeClass("hide");
  482. $("#logsModal pre").addClass("hide");
  483. $("#logsModal").modal({
  484. keyboard: true,
  485. show: true
  486. });
  487. $("#logsModal").on("hide", function () {
  488. window.clearInterval(self.showLogsInterval);
  489. });
  490. self.showLogsInterval = window.setInterval(function () {
  491. $.getJSON(workflow.watchUrl, function (data) {
  492. if (data.workflow && !data.workflow.isRunning) {
  493. window.clearInterval(self.showLogsInterval);
  494. }
  495. if (data.logs.pig) {
  496. $("#logsModal img").addClass("hide");
  497. $("#logsModal pre").removeClass("hide");
  498. var _logsEl = $("#logsModal pre");
  499. var newLines = data.logs.pig.split("\n").slice(_logsEl.html().split("<br>").length);
  500. if (newLines.length > 0){
  501. _logsEl.html(_logsEl.html() + newLines.join("<br>") + "<br>");
  502. }
  503. if (self.showLogsAtEnd) {
  504. _logsEl.scrollTop(_logsEl[0].scrollHeight - _logsEl.height());
  505. }
  506. }
  507. });
  508. }, 1000);
  509. }
  510. };
  511. };