ESPWorkunit.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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/lang",
  19. "dojo/_base/xhr",
  20. "hpcc/ESPResult",
  21. "hpcc/ESPBase"
  22. ], function (declare, lang, xhr, ESPResult, ESPBase) {
  23. return declare(ESPBase, {
  24. wuid: "",
  25. stateID: 0,
  26. state: "",
  27. text: "",
  28. resultCount: 0,
  29. results: [],
  30. graphs: [],
  31. exceptions: [],
  32. timers: [],
  33. onCreate: function () {
  34. },
  35. onUpdate: function () {
  36. },
  37. onSubmit: function () {
  38. },
  39. constructor: function (args) {
  40. declare.safeMixin(this, args);
  41. if (!this.wuid) {
  42. this.create();
  43. }
  44. },
  45. isComplete: function () {
  46. switch (this.stateID) {
  47. case 3: //WUStateCompleted:
  48. case 4: //WUStateFailed:
  49. case 5: //WUStateArchived:
  50. case 7: //WUStateAborted:
  51. return true;
  52. }
  53. return false;
  54. },
  55. monitor: function (callback, monitorDuration) {
  56. if (!monitorDuration)
  57. monitorDuration = 0;
  58. var request = {};
  59. request['Wuid'] = this.wuid;
  60. request['rawxml_'] = "1";
  61. var context = this;
  62. xhr.post({
  63. url: this.getBaseURL() + "/WUQuery.json",
  64. handleAs: "json",
  65. content: request,
  66. load: function (response) {
  67. var workunit = response.WUQueryResponse.Workunits.ECLWorkunit[0];
  68. context.stateID = workunit.StateID;
  69. context.state = workunit.State;
  70. if (callback) {
  71. callback(context);
  72. }
  73. if (!context.isComplete()) {
  74. var timeout = 30; // Seconds
  75. if (monitorDuration < 5) {
  76. timeout = 1;
  77. } else if (monitorDuration < 10) {
  78. timeout = 2;
  79. } else if (monitorDuration < 30) {
  80. timeout = 5;
  81. } else if (monitorDuration < 60) {
  82. timeout = 10;
  83. } else if (monitorDuration < 120) {
  84. timeout = 20;
  85. }
  86. setTimeout(function () {
  87. context.monitor(callback, monitorDuration + timeout);
  88. }, timeout * 1000);
  89. }
  90. },
  91. error: function () {
  92. done = true;
  93. }
  94. });
  95. },
  96. create: function (ecl) {
  97. var request = {};
  98. request['rawxml_'] = "1";
  99. var context = this;
  100. xhr.post({
  101. url: this.getBaseURL() + "/WUCreate.json",
  102. handleAs: "json",
  103. content: request,
  104. load: function (response) {
  105. context.wuid = response.WUCreateResponse.Workunit.Wuid;
  106. context.onCreate();
  107. },
  108. error: function () {
  109. }
  110. });
  111. },
  112. update: function (ecl, graphName, svg) {
  113. var request = {};
  114. request['Wuid'] = this.wuid;
  115. if (ecl) {
  116. request['QueryText'] = ecl;
  117. }
  118. if (graphName && svg) {
  119. /*
  120. request['ApplicationValues'] = {
  121. ApplicationValue: {
  122. itemcount: 1,
  123. Application: "ESPWorkunit.js",
  124. Name: graphName + "_SVG",
  125. Value: svg
  126. }
  127. }
  128. */
  129. request['ApplicationValues.ApplicationValue.itemcount'] = 1;
  130. request['ApplicationValues.ApplicationValue.0.Application'] = "ESPWorkunit.js";
  131. request['ApplicationValues.ApplicationValue.0.Name'] = graphName + "_SVG";
  132. request['ApplicationValues.ApplicationValue.0.Value'] = svg;
  133. }
  134. request['rawxml_'] = "1";
  135. var context = this;
  136. xhr.post({
  137. url: this.getBaseURL() + "/WUUpdate.json",
  138. handleAs: "json",
  139. content: request,
  140. load: function (response) {
  141. context.onUpdate();
  142. },
  143. error: function (error) {
  144. }
  145. });
  146. },
  147. submit: function (target) {
  148. var request = {
  149. Wuid: this.wuid,
  150. Cluster: target
  151. };
  152. request['rawxml_'] = "1";
  153. var context = this;
  154. xhr.post({
  155. url: this.getBaseURL() + "/WUSubmit.json",
  156. handleAs: "json",
  157. content: request,
  158. load: function (response) {
  159. context.onSubmit();
  160. },
  161. error: function (error) {
  162. }
  163. });
  164. },
  165. getInfo: function (args) {
  166. var request = {
  167. Wuid: this.wuid,
  168. TruncateEclTo64k: args.onGetText ? false : true,
  169. IncludeExceptions: args.onGetExceptions ? true : false,
  170. IncludeGraphs: args.onGetGraphs ? true : false,
  171. IncludeSourceFiles: args.onGetSourceFiles ? true : false,
  172. IncludeResults: args.onGetResults ? true : false,
  173. IncludeResultsViewNames: false,
  174. IncludeVariables: false,
  175. IncludeTimers: args.onGetTimers ? true : false,
  176. IncludeDebugValues: false,
  177. IncludeApplicationValues: args.onGetApplicationValues ? true : false,
  178. IncludeWorkflows: false,
  179. SuppressResultSchemas: args.onGetResults ? false : true,
  180. };
  181. request['rawxml_'] = "1";
  182. var context = this;
  183. xhr.post({
  184. url: this.getBaseURL() + "/WUInfo.json",
  185. handleAs: "json",
  186. content: request,
  187. load: function (response) {
  188. //var workunit = context.getValue(xmlDom, "Workunit", ["ECLException", "ECLResult", "ECLGraph", "ECLTimer", "ECLSchemaItem", "ApplicationValue"]);
  189. var workunit = response.WUInfoResponse.Workunit;
  190. if (args.onGetText && workunit.Query.Text) {
  191. context.text = workunit.Query.Text;
  192. args.onGetText(context.text);
  193. }
  194. if (args.onGetExceptions && workunit.Exceptions && workunit.Exceptions.ECLException) {
  195. context.exceptions = [];
  196. for (var i = 0; i < workunit.Exceptions.ECLException.length; ++i) {
  197. if (workunit.Exceptions.ECLException[i].Severity == "Error" ||
  198. workunit.Exceptions.ECLException[i].Severity == "Warning")
  199. context.exceptions.push(workunit.Exceptions.ECLException[i]);
  200. }
  201. args.onGetExceptions(context.exceptions);
  202. }
  203. if (args.onGetApplicationValues && workunit.ApplicationValues && workunit.ApplicationValues.ApplicationValue) {
  204. context.applicationValues = workunit.ApplicationValues.ApplicationValue;
  205. args.onGetApplicationValues(context.applicationValues)
  206. }
  207. if (args.onGetResults && workunit.Results && workunit.Results.ECLResult) {
  208. context.results = [];
  209. var results = workunit.Results.ECLResult;
  210. for (var i = 0; i < results.length; ++i) {
  211. context.results.push(new ESPResult(lang.mixin({ wuid: context.wuid }, results[i])));
  212. }
  213. args.onGetResults(context.results);
  214. }
  215. if (args.onGetSourceFiles && workunit.SourceFiles && workunit.SourceFiles.ECLSourceFile) {
  216. context.sourceFiles = [];
  217. var sourceFiles = workunit.SourceFiles.ECLSourceFile;
  218. for (var i = 0; i < sourceFiles.length; ++i) {
  219. context.sourceFiles.push(new ESPResult(lang.mixin({ wuid: context.wuid }, sourceFiles[i])));
  220. }
  221. args.onGetSourceFiles(context.sourceFiles);
  222. }
  223. if (args.onGetTimers && workunit.Timers && workunit.Timers.ECLTimer) {
  224. context.timers = workunit.Timers.ECLTimer;
  225. args.onGetTimers(context.timers);
  226. }
  227. if (args.onGetGraphs && workunit.Graphs && workunit.Graphs.ECLGraph) {
  228. context.graphs = workunit.Graphs.ECLGraph;
  229. if (context.timers || context.applicationValues) {
  230. for (var i = 0; i < context.graphs.length; ++i) {
  231. if (context.timers) {
  232. context.graphs[i].Time = 0;
  233. for (var j = 0; j < context.timers.length; ++j) {
  234. if (context.timers[j].GraphName == context.graphs[i].Name) {
  235. context.graphs[i].Time += parseFloat(context.timers[j].Value);
  236. }
  237. context.graphs[i].Time = Math.round(context.graphs[i].Time * 1000) / 1000;
  238. }
  239. }
  240. if (context.applicationValues) {
  241. var idx = context.getApplicationValueIndex("ESPWorkunit.js", context.graphs[i].Name + "_SVG");
  242. if (idx >= 0) {
  243. context.graphs[i].svg = context.applicationValues[idx].Value;
  244. }
  245. }
  246. }
  247. }
  248. args.onGetGraphs(context.graphs)
  249. }
  250. if (args.onGetAll) {
  251. args.onGetAll(workunit);
  252. }
  253. },
  254. error: function (e) {
  255. }
  256. });
  257. },
  258. getGraphIndex: function (name) {
  259. for (var i = 0; i < this.graphs.length; ++i) {
  260. if (this.graphs[i].Name == name) {
  261. return i;
  262. }
  263. }
  264. return -1;
  265. },
  266. getApplicationValueIndex: function (application, name) {
  267. for (var i = 0; i < this.applicationValues.length; ++i) {
  268. if (this.applicationValues[i].Application == application && this.applicationValues[i].Name == name) {
  269. return i;
  270. }
  271. }
  272. return -1;
  273. },
  274. fetchText: function (onFetchText) {
  275. if (this.text) {
  276. onFetchText(this.text);
  277. return;
  278. }
  279. this.getInfo({
  280. onGetText: onFetchText
  281. });
  282. },
  283. fetchResults: function (onFetchResults) {
  284. if (this.results && this.results.length) {
  285. onFetchResults(this.results);
  286. return;
  287. }
  288. this.getInfo({
  289. onGetResults: onFetchResults
  290. });
  291. },
  292. fetchGraphs: function (onFetchGraphs) {
  293. if (this.graphs && this.graphs.length) {
  294. onFetchGraphs(this.graphs);
  295. return;
  296. }
  297. this.getInfo({
  298. onGetGraphs: onFetchGraphs
  299. });
  300. },
  301. fetchGraphXgmmlByName: function (name, onFetchGraphXgmml) {
  302. var idx = this.getGraphIndex(name);
  303. if (idx >= 0) {
  304. this.fetchGraphXgmml(idx, onFetchGraphXgmml);
  305. }
  306. },
  307. fetchGraphXgmml: function (idx, onFetchGraphXgmml) {
  308. var request = {};
  309. request['Wuid'] = this.wuid;
  310. request['GraphName'] = this.graphs[idx].Name;
  311. request['rawxml_'] = "1";
  312. var context = this;
  313. xhr.post({
  314. url: this.getBaseURL() + "/WUGetGraph.json",
  315. handleAs: "json",
  316. content: request,
  317. load: function (response) {
  318. context.graphs[idx].xgmml = response.WUGetGraphResponse.Graphs.ECLGraphEx[0].Graph;
  319. onFetchGraphXgmml(context.graphs[idx].xgmml, context.graphs[idx].svg);
  320. },
  321. error: function () {
  322. }
  323. });
  324. },
  325. setGraphSvg: function (graphName, svg) {
  326. var idx = this.getGraphIndex(graphName);
  327. if (idx >= 0) {
  328. this.graphs[idx].svg = svg;
  329. this.update(null, graphName, svg);
  330. }
  331. }
  332. });
  333. });