pig.ko.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 App = function (app) {
  17. return {
  18. id:app.id,
  19. name:app.name,
  20. archive:app.archive,
  21. submitUrl:app.submitUrl,
  22. watchUrl:app.watchUrl,
  23. saveUrl:app.saveUrl,
  24. killUrl:"",
  25. rerunUrl:"",
  26. description:app.description,
  27. isRunning:ko.observable(false),
  28. status:ko.observable(""),
  29. progress:ko.observable(""),
  30. progressCss:ko.observable("width: 0%"),
  31. intervalId:-1,
  32. actions:ko.observableArray(),
  33. output:ko.observable(""),
  34. tooltip:ko.observable("")
  35. }
  36. }
  37. var pigModel = function (serviceUrl, labels) {
  38. var self = this;
  39. self.serviceUrl = ko.observable(serviceUrl);
  40. self.LABELS = labels;
  41. self.apps = ko.observableArray();
  42. self.history = ko.observableArray();
  43. self.isLoading = ko.observable(true);
  44. self.retrieveData = function () {
  45. self.isLoading(true);
  46. $.getJSON(self.serviceUrl() + self.getDocId(), function (data) {
  47. self.apps(ko.utils.arrayMap(data.apps, function (app) {
  48. return getInitApp(app);
  49. }));
  50. self.isLoading(false);
  51. if ($){
  52. $(document).trigger("updateTooltips");
  53. }
  54. });
  55. function getInitApp(app) {
  56. var a = new App(app);
  57. a.tooltip(self.LABELS.TOOLTIP_PLAY);
  58. return a;
  59. }
  60. };
  61. self.getDocId = function() {
  62. var hash = window.location.hash;
  63. var jobId = ""
  64. if (hash != null && hash.indexOf("/") > -1) {
  65. jobId = hash.substring(hash.indexOf("/") + 1);
  66. }
  67. return jobId;
  68. }
  69. self.manageApp = function (app) {
  70. if (app.status() == "" || app.status() == "FAILED" || app.status() == "STOPPED") {
  71. self.submitApp(app);
  72. }
  73. if (app.isRunning()) {
  74. self.killApp(app);
  75. }
  76. if (app.status() == "SUCCEEDED") {
  77. self.rerunApp(app);
  78. }
  79. };
  80. self.submitApp = function (app) {
  81. $.post(app.submitUrl, $("#queryForm").serialize(), function (data) {
  82. window.location.hash = "#/" + data.jobId;
  83. app.watchUrl = data.watchUrl;
  84. self.updateAppStatus(app);
  85. app.intervalId = window.setInterval(function () {
  86. self.updateAppStatus(app);
  87. }, 1000);
  88. }, "json");
  89. };
  90. self.save = function (app) {
  91. $.post(app.submitUrl, $("#queryForm").serialize(), function (data) {
  92. if (self.getDocId() == "") {
  93. window.location.hash += "/" + data.doc_id;
  94. }
  95. $.jHueNotify.info(self.LABELS.SAVED);
  96. }, "json");
  97. };
  98. self.killApp = function (app) {
  99. app.isRunning(false);
  100. app.status("STOPPED");
  101. app.tooltip(self.LABELS.TOOLTIP_PLAY);
  102. if (app.intervalId > -1) {
  103. window.clearInterval(app.intervalId);
  104. self.appendToHistory(app);
  105. }
  106. if ($) {
  107. $(document).trigger("updateTooltips");
  108. }
  109. if (app.killUrl) {
  110. $.post(app.killUrl,function (data) {
  111. if (data.status != 0) {
  112. $.jHueNotify.error(data.data);
  113. }
  114. }, "json").error(function () {
  115. $.jHueNotify.error(self.LABELS.KILL_ERROR);
  116. });
  117. }
  118. };
  119. self.rerunApp = function (app) {
  120. if (app.intervalId > -1) {
  121. window.clearInterval(app.intervalId);
  122. }
  123. self.submitApp(app);
  124. };
  125. self.updateAppStatus = function (app) {
  126. $.getJSON(app.watchUrl, function (watch) {
  127. var previousTooltip = app.tooltip();
  128. app.tooltip(self.LABELS.TOOLTIP_STOP);
  129. app.isRunning(watch.workflow.isRunning);
  130. app.status(watch.workflow.status);
  131. app.progress(watch.workflow.progress);
  132. app.progressCss("width: " + app.progress() + "%");
  133. app.actions(watch.workflow.actions);
  134. app.killUrl = watch.workflow.killUrl;
  135. app.rerunUrl = watch.workflow.rerunUrl;
  136. app.output(watch.output);
  137. if (!watch.workflow.isRunning) {
  138. window.clearInterval(app.intervalId);
  139. app.tooltip(self.LABELS.TOOLTIP_PLAY);
  140. self.appendToHistory(app);
  141. }
  142. if (app.tooltip() != previousTooltip) {
  143. if ($) {
  144. $(document).trigger("updateTooltips");
  145. }
  146. }
  147. });
  148. };
  149. self.resetAppStatus = function (app) {
  150. app.isRunning(false);
  151. app.status("");
  152. };
  153. self.appendToHistory = function (app) {
  154. var newApp = {
  155. id:app.id,
  156. name:app.name,
  157. when:moment().format('MM/DD/YYYY, h:mm:ss a'),
  158. archive:app.archive,
  159. submitUrl:app.submitUrl,
  160. watchUrl:app.watchUrl,
  161. killUrl:app.killUrl,
  162. rerunUrl:app.rerunUrl,
  163. description:app.description,
  164. isRunning:ko.observable(app.isRunning()),
  165. status:ko.observable(app.status()),
  166. progress:ko.observable(app.progress()),
  167. progressCss:ko.observable(app.progressCss()),
  168. intervalId:app.intervalId,
  169. actions:ko.observableArray(app.actions()),
  170. output:ko.observable(app.output())
  171. };
  172. var history = self.history();
  173. history.push(newApp);
  174. self.history(history);
  175. };
  176. };