pig.ko.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 PigScript = function (pigScript) {
  17. var self = this;
  18. self.id = ko.observable(pigScript.id);
  19. self.isDesign = ko.observable(pigScript.isDesign);
  20. self.name = ko.observable(pigScript.name);
  21. self.script = ko.observable(pigScript.script);
  22. self.scriptSumup = ko.observable(pigScript.script.replace(/\W+/g, ' ').substring(0, 100));
  23. self.isRunning = ko.observable(false);
  24. self.selected = ko.observable(false);
  25. self.watchUrl = ko.observable("");
  26. self.actions = ko.observableArray([]);
  27. self.handleSelect = function (row, e) {
  28. this.selected(!this.selected());
  29. };
  30. self.hovered = ko.observable(false);
  31. self.toggleHover = function (row, e) {
  32. this.hovered(!this.hovered());
  33. };
  34. self.parameters = ko.observableArray(pigScript.parameters);
  35. self.addParameter = function () {
  36. self.parameters.push({name: '', value: ''});
  37. };
  38. self.removeParameter = function () {
  39. self.parameters.remove(this);
  40. };
  41. self.getParameters = function () {
  42. var params = {};
  43. var variables = this.script().match(/\$(\w)+/g);
  44. if (variables) {
  45. $.each(variables, function(index, param) {
  46. var p = param.substring(1);
  47. params[p] = '';
  48. $.each(self.parameters(), function(index, param) {
  49. if (param['name'] == p) {
  50. params[p] = param['value'];
  51. }
  52. });
  53. });
  54. }
  55. return params;
  56. };
  57. }
  58. var Workflow = function (wf) {
  59. return {
  60. id: wf.id,
  61. scriptId: wf.scriptId,
  62. lastModTime: wf.lastModTime,
  63. endTime: wf.endTime,
  64. status: wf.status,
  65. statusClass: "label " + getStatusClass(wf.status),
  66. isRunning: wf.isRunning,
  67. duration: wf.duration,
  68. appName: wf.appName,
  69. progress: wf.progress,
  70. progressClass: "bar " + getStatusClass(wf.status, "bar-"),
  71. user: wf.user,
  72. absoluteUrl: wf.absoluteUrl,
  73. canEdit: wf.canEdit,
  74. killUrl: wf.killUrl,
  75. created: wf.created,
  76. run: wf.run
  77. }
  78. }
  79. var PigViewModel = function (scripts, props) {
  80. var self = this;
  81. self.LABELS = props.labels;
  82. self.LIST_SCRIPTS = props.listScripts;
  83. self.SAVE_URL = props.saveUrl;
  84. self.RUN_URL = props.runUrl;
  85. self.COPY_URL = props.copyUrl;
  86. self.DELETE_URL = props.deleteUrl;
  87. self.isLoading = ko.observable(false);
  88. self.allSelected = ko.observable(false);
  89. self.submissionVariables = ko.observableArray([]);
  90. self.scripts = ko.observableArray(ko.utils.arrayMap(scripts, function (pigScript) {
  91. return new PigScript(pigScript);
  92. }));
  93. self.filteredScripts = ko.observableArray(self.scripts());
  94. self.runningScripts = ko.observableArray([]);
  95. self.completedScripts = ko.observableArray([]);
  96. var _defaultScript = {
  97. id: -1,
  98. name: self.LABELS.NEW_SCRIPT_NAME,
  99. script: self.LABELS.NEW_SCRIPT_CONTENT,
  100. parameters: self.LABELS.NEW_SCRIPT_PARAMETERS,
  101. };
  102. self.currentScript = ko.observable(new PigScript(_defaultScript));
  103. self.currentDeleteType = ko.observable("");
  104. self.selectedScripts = ko.computed(function () {
  105. return ko.utils.arrayFilter(self.scripts(), function (script) {
  106. return script.selected();
  107. });
  108. }, self);
  109. self.selectedScript = ko.computed(function () {
  110. return self.selectedScripts()[0];
  111. }, self);
  112. self.selectAll = function () {
  113. self.allSelected(!self.allSelected());
  114. ko.utils.arrayForEach(self.scripts(), function (script) {
  115. script.selected(self.allSelected());
  116. });
  117. return true;
  118. };
  119. self.getScriptById = function (id) {
  120. var _s = null;
  121. ko.utils.arrayForEach(self.scripts(), function (script) {
  122. if (script.id() == id) {
  123. _s = script;
  124. }
  125. });
  126. return _s;
  127. }
  128. self.filterScripts = function (filter) {
  129. self.filteredScripts(ko.utils.arrayFilter(self.scripts(), function (script) {
  130. return script.isDesign() && script.name().toLowerCase().indexOf(filter.toLowerCase()) > -1
  131. }));
  132. };
  133. self.loadScript = function (id) {
  134. var _s = self.getScriptById(id);
  135. if (_s != null) {
  136. self.currentScript(_s);
  137. }
  138. else {
  139. self.currentScript(new PigScript(_defaultScript));
  140. }
  141. }
  142. self.newScript = function () {
  143. self.currentScript(new PigScript(_defaultScript));
  144. $(document).trigger("loadEditor");
  145. $(document).trigger("showEditor");
  146. };
  147. self.editScript = function (script) {
  148. $(document).trigger("showEditor");
  149. };
  150. self.editScriptProperties = function (script) {
  151. $(document).trigger("showProperties");
  152. };
  153. self.showScriptLogs = function (script) {
  154. $(document).trigger("showLogs");
  155. };
  156. self.viewScript = function (script) {
  157. self.currentScript(script);
  158. $(document).trigger("loadEditor");
  159. $(document).trigger("showEditor");
  160. };
  161. self.saveScript = function () {
  162. callSave(self.currentScript());
  163. };
  164. self.runScript = function () {
  165. callRun(self.currentScript());
  166. };
  167. self.copyScript = function () {
  168. callCopy(self.currentScript());
  169. };
  170. self.confirmDeleteScript = function () {
  171. self.currentDeleteType("single");
  172. showDeleteModal();
  173. };
  174. self.listRunScript = function () {
  175. callRun(self.selectedScript());
  176. };
  177. self.listCopyScript = function () {
  178. callCopy(self.selectedScript());
  179. };
  180. self.listConfirmDeleteScripts = function () {
  181. self.currentDeleteType("multiple");
  182. showDeleteModal();
  183. };
  184. self.deleteScripts = function () {
  185. var ids = [];
  186. if (self.currentDeleteType() == "single") {
  187. ids.push(self.currentScript().id());
  188. }
  189. if (self.currentDeleteType() == "multiple") {
  190. $(self.selectedScripts()).each(function (index, script) {
  191. ids.push(script.id());
  192. });
  193. }
  194. callDelete(ids);
  195. };
  196. self.updateScripts = function () {
  197. $.getJSON(self.LIST_SCRIPTS, function (data) {
  198. self.scripts(ko.utils.arrayMap(data, function (script) {
  199. return new PigScript(script);
  200. }));
  201. self.filteredScripts(self.scripts());
  202. $(document).trigger("scriptsRefreshed");
  203. });
  204. };
  205. self.updateDashboard = function (workflows) {
  206. var koWorkflows = ko.utils.arrayMap(workflows, function (wf) {
  207. return new Workflow(wf);
  208. });
  209. self.runningScripts(ko.utils.arrayFilter(koWorkflows, function (wf) {
  210. return wf.isRunning
  211. }));
  212. self.completedScripts(ko.utils.arrayFilter(koWorkflows, function (wf) {
  213. return !wf.isRunning
  214. }));
  215. }
  216. self.showSubmissionModal = function showSubmissionModal() {
  217. var script = self.currentScript();
  218. self.submissionVariables.removeAll();
  219. $.each(script.getParameters(), function (key, value) {
  220. self.submissionVariables.push({'name': key, 'value': value});
  221. });
  222. $("#submitModal").modal({
  223. keyboard: true,
  224. show: true
  225. });
  226. }
  227. function showDeleteModal() {
  228. $(".deleteMsg").addClass("hide");
  229. if (self.currentDeleteType() == "single") {
  230. $(".deleteMsg.single").removeClass("hide");
  231. }
  232. if (self.currentDeleteType() == "multiple") {
  233. if (self.selectedScripts().length > 1) {
  234. $(".deleteMsg.multiple").removeClass("hide");
  235. }
  236. else {
  237. $(".deleteMsg.single").removeClass("hide");
  238. }
  239. }
  240. $("#deleteModal").modal({
  241. keyboard: true,
  242. show: true
  243. });
  244. }
  245. function callSave(script) {
  246. $(document).trigger("saving");
  247. $.post(self.SAVE_URL,
  248. {
  249. id: script.id(),
  250. name: script.name(),
  251. script: script.script(),
  252. parameters: ko.utils.stringifyJson(script.parameters())
  253. },
  254. function (data) {
  255. self.currentScript().id(data.id);
  256. $(document).trigger("saved");
  257. self.updateScripts();
  258. }, "json");
  259. }
  260. function callRun(script) {
  261. $(document).trigger("running");
  262. $.post(self.RUN_URL,
  263. {
  264. id: script.id(),
  265. name: script.name(),
  266. script: script.script(),
  267. parameters: ko.utils.stringifyJson(self.submissionVariables())
  268. },
  269. function (data) {
  270. if (data.id && self.currentScript().id() != data.id){
  271. self.currentScript(script);
  272. $(document).trigger("loadEditor");
  273. }
  274. script.isRunning(true);
  275. script.watchUrl(data.watchUrl);
  276. $(document).trigger("startLogsRefresh");
  277. $(document).trigger("refreshDashboard");
  278. $(document).trigger("showLogs");
  279. self.updateScripts();
  280. $("#submitModal").modal("hide");
  281. }, "json");
  282. }
  283. function callCopy(script) {
  284. $.post(self.COPY_URL,
  285. {
  286. id: script.id()
  287. },
  288. function (data) {
  289. self.currentScript(new PigScript(data));
  290. $(document).trigger("loadEditor");
  291. self.updateScripts();
  292. }, "json");
  293. }
  294. function callDelete(ids) {
  295. if (ids.indexOf(self.currentScript().id()) > -1) {
  296. self.currentScript(new PigScript(_defaultScript));
  297. $(document).trigger("loadEditor");
  298. }
  299. $.post(self.DELETE_URL,
  300. {
  301. ids: ids.join(",")
  302. },
  303. function (data) {
  304. self.updateScripts();
  305. $("#deleteModal").modal("hide");
  306. }, "json");
  307. }
  308. self.viewSubmittedScript = function (workflow) {
  309. self.loadScript(workflow.scriptId);
  310. $(document).trigger("loadEditor");
  311. $(document).trigger("showEditor");
  312. };
  313. };