ESPWorkunit.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 = [];
  225. for (var i = 0; i < workunit.Timers.ECLTimer.length; ++i) {
  226. if (workunit.Timers.ECLTimer[i].GraphName && workunit.Timers.ECLTimer[i].SubGraphId) {
  227. var timeParts = workunit.Timers.ECLTimer[i].Value.split(":");
  228. var secs = 0;
  229. for (var j = 0; j < timeParts.length; ++j) {
  230. secs = secs * 60 + timeParts[j] * 1;
  231. }
  232. context.timers.push(lang.mixin(workunit.Timers.ECLTimer[i], { Seconds: Math.round(secs * 1000) / 1000 }));
  233. }
  234. }
  235. args.onGetTimers(context.timers);
  236. }
  237. if (args.onGetGraphs && workunit.Graphs && workunit.Graphs.ECLGraph) {
  238. context.graphs = workunit.Graphs.ECLGraph;
  239. if (context.timers || context.applicationValues) {
  240. for (var i = 0; i < context.graphs.length; ++i) {
  241. if (context.timers) {
  242. context.graphs[i].Time = 0;
  243. for (var j = 0; j < context.timers.length; ++j) {
  244. if (context.timers[j].GraphName == context.graphs[i].Name) {
  245. context.graphs[i].Time += context.timers[j].Seconds;
  246. }
  247. context.graphs[i].Time = Math.round(context.graphs[i].Time * 1000) / 1000;
  248. }
  249. }
  250. if (context.applicationValues) {
  251. var idx = context.getApplicationValueIndex("ESPWorkunit.js", context.graphs[i].Name + "_SVG");
  252. if (idx >= 0) {
  253. context.graphs[i].svg = context.applicationValues[idx].Value;
  254. }
  255. }
  256. }
  257. }
  258. args.onGetGraphs(context.graphs)
  259. }
  260. if (args.onGetAll) {
  261. args.onGetAll(workunit);
  262. }
  263. },
  264. error: function (e) {
  265. }
  266. });
  267. },
  268. getGraphIndex: function (name) {
  269. for (var i = 0; i < this.graphs.length; ++i) {
  270. if (this.graphs[i].Name == name) {
  271. return i;
  272. }
  273. }
  274. return -1;
  275. },
  276. getApplicationValueIndex: function (application, name) {
  277. for (var i = 0; i < this.applicationValues.length; ++i) {
  278. if (this.applicationValues[i].Application == application && this.applicationValues[i].Name == name) {
  279. return i;
  280. }
  281. }
  282. return -1;
  283. },
  284. fetchText: function (onFetchText) {
  285. if (this.text) {
  286. onFetchText(this.text);
  287. return;
  288. }
  289. this.getInfo({
  290. onGetText: onFetchText
  291. });
  292. },
  293. fetchResults: function (onFetchResults) {
  294. if (this.results && this.results.length) {
  295. onFetchResults(this.results);
  296. return;
  297. }
  298. this.getInfo({
  299. onGetResults: onFetchResults
  300. });
  301. },
  302. fetchTimers: function (onFetchTimers) {
  303. if (this.timers && this.timers.length) {
  304. onFetchTimers(this.timers);
  305. return;
  306. }
  307. this.getInfo({
  308. onGetTimers: onFetchTimers
  309. });
  310. },
  311. fetchGraphs: function (onFetchGraphs) {
  312. if (this.graphs && this.graphs.length) {
  313. onFetchGraphs(this.graphs);
  314. return;
  315. }
  316. this.getInfo({
  317. onGetGraphs: onFetchGraphs
  318. });
  319. },
  320. fetchGraphXgmmlByName: function (name, onFetchGraphXgmml) {
  321. var idx = this.getGraphIndex(name);
  322. if (idx >= 0) {
  323. this.fetchGraphXgmml(idx, onFetchGraphXgmml);
  324. }
  325. },
  326. fetchGraphXgmml: function (idx, onFetchGraphXgmml) {
  327. var request = {};
  328. request['Wuid'] = this.wuid;
  329. request['GraphName'] = this.graphs[idx].Name;
  330. request['rawxml_'] = "1";
  331. var context = this;
  332. xhr.post({
  333. url: this.getBaseURL() + "/WUGetGraph.json",
  334. handleAs: "json",
  335. content: request,
  336. load: function (response) {
  337. context.graphs[idx].xgmml = response.WUGetGraphResponse.Graphs.ECLGraphEx[0].Graph;
  338. onFetchGraphXgmml(context.graphs[idx].xgmml, context.graphs[idx].svg);
  339. },
  340. error: function () {
  341. }
  342. });
  343. },
  344. setGraphSvg: function (graphName, svg) {
  345. var idx = this.getGraphIndex(graphName);
  346. if (idx >= 0) {
  347. this.graphs[idx].svg = svg;
  348. this.update(null, graphName, svg);
  349. }
  350. }
  351. });
  352. });