pig.ko.js 8.6 KB

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