pig.ko.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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.showSubmissionModal = function showSubmissionModal() {
  237. var script = self.currentScript();
  238. self.submissionVariables.removeAll();
  239. $.each(script.getParameters(), function (key, value) {
  240. self.submissionVariables.push({'name': key, 'value': value});
  241. });
  242. $("#runScriptBtn").button("reset");
  243. $("#runScriptBtn").attr("data-loading-text", $("#runScriptBtn").text() + " ...");
  244. $("#submitModal").modal({
  245. keyboard: true,
  246. show: true
  247. });
  248. };
  249. self.showStopModal = function showStopModal() {
  250. $("#stopScriptBtn").button("reset");
  251. $("#stopScriptBtn").attr("data-loading-text", $("#stopScriptBtn").text() + " ...");
  252. $("#stopModal").modal({
  253. keyboard: true,
  254. show: true
  255. });
  256. }
  257. self.showFileChooser = function showFileChooser() {
  258. var inputPath = this;
  259. var path = inputPath.value().substr(0, inputPath.value().lastIndexOf("/"));
  260. $("#filechooser").jHueFileChooser({
  261. initialPath: path,
  262. onFileChoose: function (filePath) {
  263. inputPath.value(filePath);
  264. $("#chooseFile").modal("hide");
  265. },
  266. createFolder: false
  267. });
  268. $("#chooseFile").modal("show");
  269. };
  270. function showDeleteModal() {
  271. $(".deleteMsg").addClass("hide");
  272. if (self.currentDeleteType() == "single") {
  273. $(".deleteMsg.single").removeClass("hide");
  274. }
  275. if (self.currentDeleteType() == "multiple") {
  276. if (self.selectedScripts().length > 1) {
  277. $(".deleteMsg.multiple").removeClass("hide");
  278. }
  279. else {
  280. $(".deleteMsg.single").removeClass("hide");
  281. }
  282. }
  283. $("#deleteModal").modal({
  284. keyboard: true,
  285. show: true
  286. });
  287. }
  288. function showStopModal() {
  289. $(".stopMsg").addClass("hide");
  290. if (self.currentStopType() == "single") {
  291. $(".stopMsg.single").removeClass("hide");
  292. }
  293. if (self.currentStopType() == "multiple") {
  294. if (self.selectedScripts().length > 1) {
  295. $(".stopMsg.multiple").removeClass("hide");
  296. } else {
  297. $(".stopMsg.single").removeClass("hide");
  298. }
  299. }
  300. $("#stopModal").modal({
  301. keyboard: true,
  302. show: true
  303. });
  304. }
  305. function callSave(script) {
  306. $(document).trigger("saving");
  307. $.post(self.SAVE_URL,
  308. {
  309. id: script.id(),
  310. name: script.name(),
  311. script: script.script(),
  312. parameters: ko.utils.stringifyJson(script.parameters()),
  313. resources: ko.toJSON(script.resources())
  314. },
  315. function (data) {
  316. self.currentScript().id(data.id);
  317. $(document).trigger("saved");
  318. self.updateScripts();
  319. }, "json");
  320. }
  321. function callRun(script) {
  322. $(document).trigger("running");
  323. $.post(self.RUN_URL,
  324. {
  325. id: script.id(),
  326. name: script.name(),
  327. script: script.script(),
  328. parameters: ko.utils.stringifyJson(script.parameters()),
  329. submissionVariables: ko.utils.stringifyJson(self.submissionVariables()),
  330. resources: ko.toJSON(script.resources())
  331. },
  332. function (data) {
  333. if (data.id && self.currentScript().id() != data.id){
  334. self.currentScript(script);
  335. script.id(data.id);
  336. $(document).trigger("loadEditor");
  337. }
  338. script.isRunning(true);
  339. script.watchUrl(data.watchUrl);
  340. $(document).trigger("startLogsRefresh");
  341. $(document).trigger("refreshDashboard");
  342. $(document).trigger("showLogs");
  343. self.updateScripts();
  344. $("#submitModal").modal("hide");
  345. }, "json");
  346. }
  347. function callStop(script) {
  348. $(document).trigger("stopping");
  349. $.post(self.STOP_URL, {
  350. id: script.id()
  351. },
  352. function (data) {
  353. $(document).trigger("stopped");
  354. $("#stopModal").modal("hide");
  355. }, "json");
  356. }
  357. function callCopy(script) {
  358. $.post(self.COPY_URL,
  359. {
  360. id: script.id()
  361. },
  362. function (data) {
  363. self.currentScript(new PigScript(data));
  364. $(document).trigger("loadEditor");
  365. self.updateScripts();
  366. }, "json");
  367. }
  368. function callDelete(ids) {
  369. if (ids.indexOf(self.currentScript().id()) > -1) {
  370. self.currentScript(new PigScript(_defaultScript));
  371. $(document).trigger("loadEditor");
  372. }
  373. $.post(self.DELETE_URL,
  374. {
  375. ids: ids.join(",")
  376. },
  377. function (data) {
  378. self.updateScripts();
  379. $("#deleteModal").modal("hide");
  380. }, "json");
  381. }
  382. self.viewSubmittedScript = function (workflow) {
  383. self.loadScript(workflow.scriptId);
  384. self.currentScript().isRunning(true);
  385. self.currentScript().watchUrl(workflow.watchUrl);
  386. $(document).trigger("startLogsRefresh");
  387. $(document).trigger("showLogs");
  388. };
  389. self.showLogsInterval = -1;
  390. self.showLogsAtEnd = true;
  391. self.showLogs = function (workflow) {
  392. window.clearInterval(self.showLogsInterval);
  393. $("#logsModal pre").scroll(function () {
  394. self.showLogsAtEnd = $(this).scrollTop() + $(this).height() + 20 >= $(this)[0].scrollHeight;
  395. });
  396. if (workflow.isRunning) {
  397. $("#logsModal img").removeClass("hide");
  398. $("#logsModal pre").addClass("hide");
  399. $("#logsModal").modal({
  400. keyboard: true,
  401. show: true
  402. });
  403. $("#logsModal").on("hide", function () {
  404. window.clearInterval(self.showLogsInterval);
  405. });
  406. self.showLogsInterval = window.setInterval(function () {
  407. $.getJSON(workflow.watchUrl, function (data) {
  408. if (data.workflow && !data.workflow.isRunning) {
  409. window.clearInterval(self.showLogsInterval);
  410. }
  411. if (data.logs.pig) {
  412. $("#logsModal img").addClass("hide");
  413. $("#logsModal pre").removeClass("hide");
  414. var _logsEl = $("#logsModal pre");
  415. var newLines = data.logs.pig.split("\n").slice(_logsEl.text().split("\n").length);
  416. _logsEl.text(_logsEl.text() + newLines.join("\n"));
  417. if (self.showLogsAtEnd) {
  418. _logsEl.scrollTop(_logsEl[0].scrollHeight - _logsEl.height());
  419. }
  420. }
  421. });
  422. }, 1000);
  423. }
  424. };
  425. };