pig.ko.js 8.3 KB

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