pig.ko.js 17 KB

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