ESPQuery.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/_base/Deferred",
  21. "dojo/store/Observable",
  22. "dojo/Stateful",
  23. "dojox/xml/parser",
  24. "hpcc/WsWorkunits",
  25. "hpcc/WsEcl",
  26. "hpcc/ESPRequest",
  27. "hpcc/ESPUtil",
  28. "hpcc/ESPWorkunit"
  29. ], function (declare, arrayUtil, lang, Deferred, Observable, Stateful,
  30. parser,
  31. WsWorkunits, WsEcl, ESPRequest, ESPUtil, ESPWorkunit) {
  32. var _logicalFiles = {};
  33. var Store = declare([ESPRequest.Store], {
  34. service: "WsWorkunits",
  35. action: "WUListQueries",
  36. responseQualifier: "WUListQueriesResponse.QuerysetQueries.QuerySetQuery",
  37. responseTotalQualifier: "WUListQueriesResponse.NumberOfQueries",
  38. idProperty: "__hpcc_id",
  39. startProperty: "PageStartFrom",
  40. countProperty: "NumberOfQueries",
  41. _watched: [],
  42. create: function (__hpcc_id) {
  43. var tmp = __hpcc_id.split(":");
  44. return new Query({
  45. __hpcc_id: __hpcc_id,
  46. QuerySetId: tmp[0],
  47. Id: tmp[1]
  48. });
  49. },
  50. update: function (id, item) {
  51. var storeItem = this.get(id);
  52. storeItem.updateData(item);
  53. if (!this._watched[id]) {
  54. var context = this;
  55. this._watched[id] = storeItem.watch("changedCount", function (name, oldValue, newValue) {
  56. if (oldValue !== newValue) {
  57. context.notify(storeItem, id);
  58. }
  59. });
  60. }
  61. },
  62. preProcessRow: function (item, request, query, options) {
  63. var ErrorCount = 0;
  64. var Suspended = false;
  65. item[this.idProperty] = item.QuerySetId + ":" + item.Id;
  66. if (lang.exists("Clusters", item)) {
  67. arrayUtil.forEach(item.Clusters.ClusterQueryState, function(cqs, idx){
  68. if (lang.exists("Errors", cqs) && cqs.Errors != null && cqs.Errors != "" && cqs.State == "Suspended"){
  69. ErrorCount++
  70. Suspended = true;
  71. }
  72. });
  73. }
  74. lang.mixin(item, {
  75. ErrorCount:ErrorCount,
  76. Suspended: Suspended
  77. });
  78. }
  79. });
  80. var Query = declare([ESPUtil.Singleton], {
  81. constructor: function (args) {
  82. this.inherited(arguments);
  83. if (args) {
  84. declare.safeMixin(this, args);
  85. }
  86. this.queries = {};
  87. },
  88. refresh: function (full) {
  89. return this.getDetails();
  90. },
  91. getDetails: function (args) {
  92. var context = this;
  93. return WsWorkunits.WUQueryDetails({
  94. request:{
  95. QueryId: this.Id,
  96. QuerySet: this.QuerySetId
  97. }
  98. }).then(function (response) {
  99. if (lang.exists("WUQueryDetailsResponse", response)) {
  100. context.updateData(response.WUQueryDetailsResponse);
  101. }
  102. });
  103. },
  104. getWorkunit: function() {
  105. return ESPWorkunit.Get(this.Wuid);
  106. },
  107. SubmitXML: function (xml) {
  108. var deferred = new Deferred();
  109. if (this.queries[xml]) {
  110. deferred.resolve(this.queries[xml]);
  111. } else {
  112. var domXml = parser.parse(xml);
  113. var query = {};
  114. arrayUtil.forEach(domXml.firstChild.childNodes, function (item, idx) {
  115. query[item.tagName] = item.textContent;
  116. });
  117. var context = this;
  118. WsEcl.Submit(this.QuerySetId, this.Id, query).then(function (response) {
  119. context.queries[xml] = response;
  120. deferred.resolve(response);
  121. });
  122. }
  123. return deferred.promise;
  124. },
  125. doAction: function (action) {
  126. var context = this;
  127. return WsWorkunits.WUQuerysetQueryAction([{
  128. QuerySetId: this.QuerySetId,
  129. Id: this.Id,
  130. Name: this.Name
  131. }], action).then(function (responses) {
  132. context.refresh();
  133. });
  134. },
  135. setSuspended: function (suspended) {
  136. return this.doAction(suspended ? "Suspend" : "Unsuspend");
  137. },
  138. setActivated: function (activated) {
  139. return this.doAction(activated ? "Activate" : "Deactivate");
  140. },
  141. doDelete: function () {
  142. return this.doAction("Delete");
  143. }
  144. });
  145. return {
  146. Get: function (QuerySetId, Id) {
  147. var store = new Store();
  148. return store.get(QuerySetId + ":" + Id);
  149. },
  150. GetFromRequestXML: function (QuerySetId, requestXml) {
  151. try {
  152. var domXml = parser.parse(requestXml);
  153. // Not all XML is a "Request" ---
  154. if (lang.exists("firstChild.tagName", domXml) && domXml.firstChild.tagName.indexOf("Request") === domXml.firstChild.tagName.length - 7) {
  155. return this.Get(QuerySetId, domXml.firstChild.tagName.slice(0, -7));
  156. }
  157. } catch (e) {
  158. }
  159. return null;
  160. },
  161. CreateQueryStore: function (options) {
  162. var store = new Store(options);
  163. return new Observable(store);
  164. }
  165. };
  166. });