pig.ko.js 8.7 KB

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