ESPWorkunit.js 9.7 KB

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