ESPActivity.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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/ESPQueue",
  26. "hpcc/ESPWorkunit",
  27. "hpcc/ESPDFUWorkunit"
  28. ], function (declare, arrayUtil, lang, Memory, Observable,
  29. WsSMC, ESPUtil, ESPRequest, ESPQueue, ESPWorkunit, ESPDFUWorkunit) {
  30. var _workunits = {};
  31. var Store = declare([Memory], {
  32. idProperty: "__hpcc_id",
  33. mayHaveChildren: function (item) {
  34. return (item.getChildCount && item.getChildCount());
  35. },
  36. getChildren: function (parent, options) {
  37. return parent.queryChildren();
  38. }
  39. });
  40. var Activity = declare([ESPUtil.Singleton, ESPUtil.Monitor], {
  41. // Asserts ---
  42. _assertHasWuid: function () {
  43. if (!this.Wuid) {
  44. throw new Error("Wuid cannot be empty.");
  45. }
  46. },
  47. // Attributes ---
  48. _StateIDSetter: function (StateID) {
  49. this.StateID = StateID;
  50. var actionEx = lang.exists("ActionEx", this) ? this.ActionEx : null;
  51. this.set("hasCompleted", WsWorkunits.isComplete(this.StateID, actionEx));
  52. },
  53. // --- --- ---
  54. constructor: function (args) {
  55. this.store = new Store();
  56. this._watched = [];
  57. this.observableStore = new Observable(this.store)
  58. },
  59. isInstanceOfQueue: function (obj) {
  60. return ESPQueue.isInstanceOfQueue(obj);
  61. },
  62. isInstanceOfWorkunit: function (obj) {
  63. return ESPWorkunit.isInstanceOfWorkunit(obj) || ESPDFUWorkunit.isInstanceOfWorkunit(obj);
  64. },
  65. setBanner: function (bannerText) {
  66. this.getActivity({
  67. FromSubmitBtn: true,
  68. BannerAction: bannerText != "",
  69. EnableChatURL: 0,
  70. BannerContent: bannerText,
  71. BannerColor: "red",
  72. BannerSize: 4,
  73. BannerScroll: 2
  74. });
  75. },
  76. resolve: function (id) {
  77. var queue = this.observableStore.get(id);
  78. if (queue) {
  79. return queue;
  80. }
  81. var wu = id[0] === "D" ? ESPDFUWorkunit.Get(id) : ESPWorkunit.Get(id);
  82. if (wu) {
  83. // is wu still in a queue?
  84. queue = wu.get("ESPQueue");
  85. if (queue) {
  86. return queue.getChild(id);
  87. }
  88. }
  89. return null;
  90. },
  91. monitor: function (callback) {
  92. if (callback && this.changedCount) {
  93. callback(this);
  94. }
  95. if (!this.hasCompleted) {
  96. var context = this;
  97. this.watch("changedCount", function (name, oldValue, newValue) {
  98. if (oldValue !== newValue && newValue) {
  99. if (callback) {
  100. callback(context);
  101. }
  102. }
  103. });
  104. }
  105. },
  106. getActivity: function (request) {
  107. var context = this;
  108. return WsSMC.Activity({
  109. request: request
  110. }).then(function (response) {
  111. if (lang.exists("ActivityResponse", response)) {
  112. var targetClusters = [];
  113. var targetClusterMap = {};
  114. context.refreshTargetClusters(lang.getObject("ActivityResponse.HThorClusterList.TargetCluster", false, response), targetClusters, targetClusterMap);
  115. context.refreshTargetClusters(lang.getObject("ActivityResponse.ThorClusterList.TargetCluster", false, response), targetClusters, targetClusterMap);
  116. context.refreshTargetClusters(lang.getObject("ActivityResponse.RoxieClusterList.TargetCluster", false, response), targetClusters, targetClusterMap);
  117. context.refreshTargetClusters(lang.getObject("ActivityResponse.ServerJobQueues.ServerJobQueue", false, response), targetClusters, targetClusterMap);
  118. context.refreshActiveWorkunits(lang.getObject("ActivityResponse.Running.ActiveWorkunit", false, response), targetClusters, targetClusterMap);
  119. context.store.setData(targetClusters);
  120. context.updateData(response.ActivityResponse);
  121. }
  122. return response;
  123. });
  124. },
  125. refreshTargetClusters: function (responseTargetClusters, targetClusters, targetClusterMap) {
  126. var context = this;
  127. if (responseTargetClusters) {
  128. arrayUtil.forEach(responseTargetClusters, function (item, idx) {
  129. var queue = null;
  130. if (item.ClusterName) {
  131. queue = ESPQueue.GetTargetCluster(item.ClusterName);
  132. } else {
  133. queue = ESPQueue.GetServerJobQueue(item.QueueName);
  134. }
  135. queue.updateData(item);
  136. queue.set("DisplayName", queue.getDisplayName());
  137. queue.clearChildren();
  138. targetClusters.push(queue);
  139. targetClusterMap[queue.__hpcc_id] = queue;
  140. if (!context._watched[queue.__hpcc_id]) {
  141. context._watched[queue.__hpcc_id] = queue.watch("changedCount", function (name, oldValue, newValue) {
  142. if (oldValue !== newValue) {
  143. context.observableStore.notify(queue, queue.__hpcc_id);
  144. }
  145. });
  146. }
  147. });
  148. }
  149. },
  150. refreshActiveWorkunits: function (responseActiveWorkunits, targetClusters, targetClusterMap) {
  151. if (responseActiveWorkunits) {
  152. arrayUtil.forEach(responseActiveWorkunits, function (item, idx) {
  153. item["__hpcc_id"] = item.Wuid;
  154. var queue = null;
  155. if (item.ClusterName) {
  156. queue = ESPQueue.GetTargetCluster(item.ClusterName);
  157. } else {
  158. queue = ESPQueue.GetServerJobQueue(item.QueueName);
  159. }
  160. var wu = item.Server === "DFUserver" ? ESPDFUWorkunit.Get(item.Wuid) : ESPWorkunit.Get(item.Wuid);
  161. wu.updateData(lang.mixin({
  162. __hpcc_id: item.Wuid,
  163. }, item));
  164. queue.addChild(wu);
  165. });
  166. }
  167. },
  168. inRefresh: false,
  169. refresh: function (full) {
  170. var context = this;
  171. if (this.inRefresh) {
  172. return;
  173. }
  174. this.inRefresh = true;
  175. this.getActivity({
  176. }).then(function (response) {
  177. context.inRefresh = false;
  178. }, function (err) {
  179. context.inRefresh = false;
  180. });
  181. },
  182. getStore: function () {
  183. return this.observableStore;
  184. }
  185. });
  186. var globalActivity = null;
  187. return {
  188. Get: function () {
  189. if (!globalActivity) {
  190. globalActivity = new Activity;
  191. globalActivity.startMonitor();
  192. globalActivity.refresh();
  193. }
  194. return globalActivity;
  195. },
  196. CreateActivityStore: function (options) {
  197. var store = new Store(options);
  198. return Observable(store);
  199. }
  200. };
  201. });