ESPActivity.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*##############################################################################
  2. # HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################## */
  16. define([
  17. "dojo/_base/declare",
  18. "dojo/_base/array",
  19. "dojo/_base/lang",
  20. "dojo/store/Memory",
  21. "dojo/store/Observable",
  22. "hpcc/WsSMC",
  23. "hpcc/ESPUtil",
  24. "hpcc/ESPRequest",
  25. "hpcc/ESPWorkunit",
  26. "hpcc/ESPDFUWorkunit"
  27. ], function (declare, arrayUtil, lang, Memory, Observable,
  28. WsSMC, ESPUtil, ESPRequest, ESPWorkunit, ESPDFUWorkunit) {
  29. var _workunits = {};
  30. var Store = declare([Memory], {
  31. idProperty: "__hpcc_id",
  32. mayHaveChildren: function (item) {
  33. return (item.children && item.children.length)
  34. },
  35. getChildren: function (parent, options) {
  36. var store = Observable(new Memory({
  37. idProperty: "__hpcc_id",
  38. parent: parent,
  39. _watched: [],
  40. data: []
  41. }));
  42. arrayUtil.forEach(parent.children, function (item, idx) {
  43. var wu = item.Server === "DFUserver" ? ESPDFUWorkunit.Get(item.Wuid) : ESPWorkunit.Get(item.Wuid);
  44. wu.updateData(item);
  45. try {
  46. store.add(wu);
  47. if (!store._watched[item.Wuid]) {
  48. store._watched[item.Wuid] = wu.watch("changedCount", function (name, oldValue, newValue) {
  49. if (oldValue !== newValue) {
  50. store.notify(wu, item.__hpcc_id);
  51. }
  52. });
  53. }
  54. } catch (e) {
  55. }
  56. });
  57. return store.query();
  58. }
  59. });
  60. var Activity = declare([ESPUtil.Singleton, ESPUtil.Monitor], {
  61. // Asserts ---
  62. _assertHasWuid: function () {
  63. if (!this.Wuid) {
  64. throw new Error("Wuid cannot be empty.");
  65. }
  66. },
  67. // Attributes ---
  68. _StateIDSetter: function (StateID) {
  69. this.StateID = StateID;
  70. var actionEx = lang.exists("ActionEx", this) ? this.ActionEx : null;
  71. this.set("hasCompleted", WsWorkunits.isComplete(this.StateID, actionEx));
  72. },
  73. // --- --- ---
  74. constructor: function (args) {
  75. this.inherited(arguments);
  76. this.store = new Store();
  77. this.observableStore = new Observable(this.store)
  78. },
  79. monitor: function (callback) {
  80. if (callback && this.changedCount) {
  81. callback(this);
  82. }
  83. if (!this.hasCompleted) {
  84. var context = this;
  85. this.watch("changedCount", function (name, oldValue, newValue) {
  86. if (oldValue !== newValue && newValue) {
  87. if (callback) {
  88. callback(context);
  89. }
  90. }
  91. });
  92. }
  93. },
  94. getActivity: function (request) {
  95. var context = this;
  96. return WsSMC.Activity({
  97. request: request
  98. }).then(function (response) {
  99. if (lang.exists("ActivityResponse", response)) {
  100. context.updateData(response.ActivityResponse);
  101. var targetClusters = [];
  102. var targetClusterMap = {};
  103. context.refreshTargetClusters("HThorClusterList.TargetCluster", targetClusters, targetClusterMap);
  104. context.refreshTargetClusters("ThorClusterList.TargetCluster", targetClusters, targetClusterMap);
  105. context.refreshTargetClusters("RoxieClusterList.TargetCluster", targetClusters, targetClusterMap);
  106. context.refreshServerJobQueue("ServerJobQueues.ServerJobQueue", targetClusters, targetClusterMap);
  107. context.refreshActiveWorkunits("Running.ActiveWorkunit", targetClusters, targetClusterMap);
  108. context.store.setData(targetClusters);
  109. context.updateData({
  110. targetClusters: targetClusters
  111. });
  112. }
  113. return response;
  114. });
  115. },
  116. setBanner: function (bannerText) {
  117. this.getActivity({
  118. FromSubmitBtn: true,
  119. BannerAction: bannerText != "",
  120. EnableChatURL: 0,
  121. BannerContent: bannerText,
  122. BannerColor: "red",
  123. BannerSize: 4,
  124. BannerScroll: 2
  125. });
  126. },
  127. refreshTargetClusters: function (targetClusterStr, targetClusters, targetClusterMap) {
  128. if (lang.exists(targetClusterStr, this)) {
  129. arrayUtil.forEach(lang.getObject(targetClusterStr, false, this), function (item, idx) {
  130. item["__hpcc_type"] = "TargetCluster";
  131. item["__hpcc_id"] = item.ClusterName;
  132. item.children = [];
  133. targetClusters.push(item);
  134. targetClusterMap[item.ClusterName] = item;
  135. });
  136. }
  137. },
  138. refreshServerJobQueue: function (serverJobQueueStr, targetClusters, targetClusterMap) {
  139. if (lang.exists(serverJobQueueStr, this)) {
  140. arrayUtil.forEach(lang.getObject(serverJobQueueStr, false, this), function (item, idx) {
  141. item["__hpcc_type"] = "TargetCluster";
  142. item["__hpcc_id"] = item.QueueName;
  143. item.children = [];
  144. targetClusters.push(item);
  145. targetClusterMap[item.QueueName] = item;
  146. });
  147. }
  148. },
  149. refreshActiveWorkunits: function (activeWorkunitsStr, targetClusters, targetClusterMap) {
  150. if (lang.exists(activeWorkunitsStr, this)) {
  151. arrayUtil.forEach(lang.getObject(activeWorkunitsStr, false, this), function (item, idx) {
  152. item["__hpcc_id"] = item.Wuid;
  153. targetClusterMap[item.ClusterName ? item.ClusterName : item.QueueName].children.push(item);
  154. });
  155. }
  156. },
  157. inRefresh: false,
  158. refresh: function (full) {
  159. var context = this;
  160. if (this.inRefresh) {
  161. return;
  162. }
  163. this.inRefresh = true;
  164. this.getActivity({
  165. }).then(function (response) {
  166. context.inRefresh = false;
  167. }, function (err) {
  168. context.inRefresh = false;
  169. });
  170. },
  171. getStore: function () {
  172. return this.observableStore;
  173. }
  174. });
  175. var globalActivity = null;
  176. return {
  177. Get: function () {
  178. if (!globalActivity) {
  179. globalActivity = new Activity;
  180. globalActivity.startMonitor();
  181. globalActivity.refresh();
  182. }
  183. return globalActivity;
  184. },
  185. CreateActivityStore: function (options) {
  186. var store = new Store(options);
  187. return Observable(store);
  188. }
  189. };
  190. });