pig.ko.js 7.9 KB

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