ESPQuery.js 6.3 KB

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