ESPWorkunit.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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 1: //WUStateCompiled
  48. if (lang.exists("WUInfoResponse.ActionEx", this) && this.WUInfoResponse.ActionEx == "compile") {
  49. return true;
  50. }
  51. break;
  52. case 3: //WUStateCompleted:
  53. case 4: //WUStateFailed:
  54. case 5: //WUStateArchived:
  55. case 7: //WUStateAborted:
  56. return true;
  57. }
  58. return false;
  59. },
  60. monitor: function (callback, monitorDuration) {
  61. if (!monitorDuration)
  62. monitorDuration = 0;
  63. var request = {};
  64. request['Wuid'] = this.wuid;
  65. request['rawxml_'] = "1";
  66. var context = this;
  67. xhr.post({
  68. url: this.getBaseURL() + "/WUQuery.json",
  69. handleAs: "json",
  70. content: request,
  71. load: function (response) {
  72. var workunit = response.WUQueryResponse.Workunits.ECLWorkunit[0];
  73. context.stateID = workunit.StateID;
  74. context.state = workunit.State;
  75. context.protected = workunit.Protected;
  76. if (callback) {
  77. callback(workunit);
  78. }
  79. if (!context.isComplete()) {
  80. var timeout = 30; // Seconds
  81. if (monitorDuration < 5) {
  82. timeout = 1;
  83. } else if (monitorDuration < 10) {
  84. timeout = 2;
  85. } else if (monitorDuration < 30) {
  86. timeout = 5;
  87. } else if (monitorDuration < 60) {
  88. timeout = 10;
  89. } else if (monitorDuration < 120) {
  90. timeout = 20;
  91. }
  92. setTimeout(function () {
  93. context.monitor(callback, monitorDuration + timeout);
  94. }, timeout * 1000);
  95. }
  96. },
  97. error: function () {
  98. done = true;
  99. }
  100. });
  101. },
  102. create: function (ecl) {
  103. var request = {};
  104. request['rawxml_'] = "1";
  105. var context = this;
  106. xhr.post({
  107. url: this.getBaseURL() + "/WUCreate.json",
  108. handleAs: "json",
  109. content: request,
  110. load: function (response) {
  111. context.wuid = response.WUCreateResponse.Workunit.Wuid;
  112. context.onCreate();
  113. },
  114. error: function () {
  115. }
  116. });
  117. },
  118. update: function (request, appData, callback) {
  119. lang.mixin(request, {
  120. Wuid: this.wuid,
  121. rawxml_: true
  122. });
  123. if (this.WUInfoResponse) {
  124. lang.mixin(request, {
  125. StateOrig: this.WUInfoResponse.State,
  126. JobnameOrig: this.WUInfoResponse.Jobname,
  127. DescriptionOrig: this.WUInfoResponse.Description,
  128. ProtectedOrig: this.WUInfoResponse.Protected,
  129. ScopeOrig: this.WUInfoResponse.Scope,
  130. ClusterOrig: this.WUInfoResponse.Cluster
  131. });
  132. }
  133. if (appData) {
  134. request['ApplicationValues.ApplicationValue.itemcount'] = appData.length;
  135. var i = 0;
  136. for (key in appData) {
  137. request['ApplicationValues.ApplicationValue.' + i + '.Application'] = "ESPWorkunit.js";
  138. request['ApplicationValues.ApplicationValue.' + i + '.Name'] = key;
  139. request['ApplicationValues.ApplicationValue.' + i + '.Value'] = appData[key];
  140. ++i;
  141. }
  142. }
  143. var context = this;
  144. xhr.post({
  145. url: this.getBaseURL() + "/WUUpdate.json",
  146. handleAs: "json",
  147. content: request,
  148. load: function (response) {
  149. context.WUInfoResponse = lang.mixin(context.WUInfoResponse, response.WUUpdateResponse.Workunit);
  150. context.onUpdate();
  151. if (callback && callback.load) {
  152. callback.load(response);
  153. }
  154. },
  155. error: function (error) {
  156. if (callback && callback.error) {
  157. callback.error(e);
  158. }
  159. }
  160. });
  161. },
  162. submit: function (target) {
  163. var request = {
  164. Wuid: this.wuid,
  165. Cluster: target
  166. };
  167. request['rawxml_'] = "1";
  168. var context = this;
  169. xhr.post({
  170. url: this.getBaseURL() + "/WUSubmit.json",
  171. handleAs: "json",
  172. content: request,
  173. load: function (response) {
  174. context.onSubmit();
  175. },
  176. error: function (error) {
  177. }
  178. });
  179. },
  180. _resubmit: function (clone, resetWorkflow, callback) {
  181. var request = {
  182. Wuids: this.wuid,
  183. CloneWorkunit: clone,
  184. ResetWorkflow: resetWorkflow,
  185. rawxml_: true
  186. };
  187. var context = this;
  188. xhr.post({
  189. url: this.getBaseURL() + "/WUResubmit.json",
  190. handleAs: "json",
  191. content: request,
  192. load: function (response) {
  193. if (callback && callback.load) {
  194. callback.load(response);
  195. }
  196. },
  197. error: function (e) {
  198. if (callback && callback.error) {
  199. callback.error(e);
  200. }
  201. }
  202. });
  203. },
  204. clone: function (callback) {
  205. this._resubmit(true, false, callback);
  206. },
  207. resubmit: function (callback) {
  208. this._resubmit(false, false, callback);
  209. },
  210. restart: function (callback) {
  211. this._resubmit(false, true, callback);
  212. },
  213. _action: function (action, callback) {
  214. var request = {
  215. Wuids: this.wuid,
  216. ActionType: action,
  217. rawxml_: true
  218. };
  219. var context = this;
  220. xhr.post({
  221. url: this.getBaseURL() + "/WUAction.json",
  222. handleAs: "json",
  223. content: request,
  224. load: function (response) {
  225. if (callback && callback.load) {
  226. callback.load(response);
  227. }
  228. },
  229. error: function (e) {
  230. if (callback && callback.error) {
  231. callback.error(e);
  232. }
  233. }
  234. });
  235. },
  236. abort: function (callback) {
  237. this._action("Abort", callback);
  238. },
  239. doDelete: function (callback) {
  240. this._action("Delete", callback);
  241. },
  242. publish: function (jobName) {
  243. var request = {
  244. Wuid: this.wuid,
  245. JobName: jobName,
  246. Activate: 1,
  247. UpdateWorkUnitName: 1,
  248. Wait: 5000,
  249. rawxml_: true
  250. };
  251. var context = this;
  252. xhr.post({
  253. url: this.getBaseURL() + "/WUPublishWorkunit.json",
  254. handleAs: "json",
  255. content: request,
  256. load: function (response) {
  257. },
  258. error: function (e) {
  259. }
  260. });
  261. },
  262. getInfo: function (args) {
  263. var request = {
  264. Wuid: this.wuid,
  265. TruncateEclTo64k: args.onGetText ? false : true,
  266. IncludeExceptions: args.onGetExceptions ? true : false,
  267. IncludeGraphs: args.onGetGraphs ? true : false,
  268. IncludeSourceFiles: args.onGetSourceFiles ? true : false,
  269. IncludeResults: args.onGetResults ? true : false,
  270. IncludeResultsViewNames: false,
  271. IncludeVariables: args.onGetVariables ? true : false,
  272. IncludeTimers: args.onGetTimers ? true : false,
  273. IncludeDebugValues: false,
  274. IncludeApplicationValues: args.onGetApplicationValues ? true : false,
  275. IncludeWorkflows: false,
  276. IncludeXmlSchemas: args.onGetResults ? true : false,
  277. SuppressResultSchemas: args.onGetResults ? false : true,
  278. rawxml_: true
  279. };
  280. var context = this;
  281. xhr.post({
  282. url: this.getBaseURL() + "/WUInfo.json",
  283. handleAs: "json",
  284. content: request,
  285. load: function (response) {
  286. //var workunit = context.getValue(xmlDom, "Workunit", ["ECLException", "ECLResult", "ECLGraph", "ECLTimer", "ECLSchemaItem", "ApplicationValue"]);
  287. var workunit = response.WUInfoResponse.Workunit;
  288. context.WUInfoResponse = workunit;
  289. if (args.onGetText && workunit.Query.Text) {
  290. context.text = workunit.Query.Text;
  291. args.onGetText(context.text);
  292. }
  293. if (args.onGetExceptions && workunit.Exceptions && workunit.Exceptions.ECLException) {
  294. context.exceptions = [];
  295. for (var i = 0; i < workunit.Exceptions.ECLException.length; ++i) {
  296. context.exceptions.push(workunit.Exceptions.ECLException[i]);
  297. }
  298. args.onGetExceptions(context.exceptions);
  299. }
  300. if (args.onGetApplicationValues && workunit.ApplicationValues && workunit.ApplicationValues.ApplicationValue) {
  301. context.applicationValues = workunit.ApplicationValues.ApplicationValue;
  302. args.onGetApplicationValues(context.applicationValues)
  303. }
  304. if (args.onGetVariables && workunit.Variables && workunit.Variables.ECLResult) {
  305. context.variables = [];
  306. var variables = workunit.Variables.ECLResult;
  307. for (var i = 0; i < variables.length; ++i) {
  308. context.variables.push(lang.mixin({
  309. ColumnType: variables[i].ECLSchemas && variables[i].ECLSchemas.ECLSchemaItem.length ? variables[i].ECLSchemas.ECLSchemaItem[0].ColumnType : "unknown"
  310. }, variables[i]));
  311. }
  312. args.onGetVariables(context.variables);
  313. }
  314. if (args.onGetResults && workunit.Results && workunit.Results.ECLResult) {
  315. context.results = [];
  316. var results = workunit.Results.ECLResult;
  317. for (var i = 0; i < results.length; ++i) {
  318. context.results.push(new ESPResult(lang.mixin({ wuid: context.wuid }, results[i])));
  319. }
  320. args.onGetResults(context.results);
  321. }
  322. if (args.onGetSourceFiles && workunit.SourceFiles && workunit.SourceFiles.ECLSourceFile) {
  323. context.sourceFiles = [];
  324. var sourceFiles = workunit.SourceFiles.ECLSourceFile;
  325. for (var i = 0; i < sourceFiles.length; ++i) {
  326. context.sourceFiles.push(new ESPResult(lang.mixin({ wuid: context.wuid }, sourceFiles[i])));
  327. }
  328. args.onGetSourceFiles(context.sourceFiles);
  329. }
  330. if (args.onGetTimers && workunit.Timers && workunit.Timers.ECLTimer) {
  331. context.timers = [];
  332. for (var i = 0; i < workunit.Timers.ECLTimer.length; ++i) {
  333. var timeParts = workunit.Timers.ECLTimer[i].Value.split(":");
  334. var secs = 0;
  335. for (var j = 0; j < timeParts.length; ++j) {
  336. secs = secs * 60 + timeParts[j] * 1;
  337. }
  338. context.timers.push(lang.mixin(workunit.Timers.ECLTimer[i], {
  339. Seconds: Math.round(secs * 1000) / 1000,
  340. HasSubGraphId: workunit.Timers.ECLTimer[i].SubGraphId && workunit.Timers.ECLTimer[i].SubGraphId != "" ? true : false
  341. }));
  342. }
  343. args.onGetTimers(context.timers);
  344. }
  345. if (args.onGetGraphs && workunit.Graphs && workunit.Graphs.ECLGraph) {
  346. context.graphs = workunit.Graphs.ECLGraph;
  347. if (context.timers || context.applicationValues) {
  348. for (var i = 0; i < context.graphs.length; ++i) {
  349. if (context.timers) {
  350. context.graphs[i].Time = 0;
  351. for (var j = 0; j < context.timers.length; ++j) {
  352. if (context.timers[j].GraphName == context.graphs[i].Name) {
  353. context.graphs[i].Time += context.timers[j].Seconds;
  354. }
  355. context.graphs[i].Time = Math.round(context.graphs[i].Time * 1000) / 1000;
  356. }
  357. }
  358. if (context.applicationValues) {
  359. var idx = context.getApplicationValueIndex("ESPWorkunit.js", context.graphs[i].Name + "_SVG");
  360. if (idx >= 0) {
  361. context.graphs[i].svg = context.applicationValues[idx].Value;
  362. }
  363. }
  364. }
  365. }
  366. args.onGetGraphs(context.graphs)
  367. }
  368. if (args.onGetAll) {
  369. args.onGetAll(workunit);
  370. }
  371. },
  372. error: function (e) {
  373. }
  374. });
  375. },
  376. getGraphIndex: function (name) {
  377. for (var i = 0; i < this.graphs.length; ++i) {
  378. if (this.graphs[i].Name == name) {
  379. return i;
  380. }
  381. }
  382. return -1;
  383. },
  384. getApplicationValueIndex: function (application, name) {
  385. for (var i = 0; i < this.applicationValues.length; ++i) {
  386. if (this.applicationValues[i].Application == application && this.applicationValues[i].Name == name) {
  387. return i;
  388. }
  389. }
  390. return -1;
  391. },
  392. getState: function () {
  393. return this.state;
  394. },
  395. getStateImage: function () {
  396. switch (this.stateID) {
  397. case 1:
  398. return "img/workunit_completed.png";
  399. case 2:
  400. return "img/workunit_running.png";
  401. case 3:
  402. return "img/workunit_completed.png";
  403. case 4:
  404. return "img/workunit_failed.png";
  405. case 5:
  406. return "img/workunit_warning.png";
  407. case 6:
  408. return "img/workunit_aborting.png";
  409. case 7:
  410. return "img/workunit_failed.png";
  411. case 8:
  412. return "img/workunit_warning.png";
  413. case 9:
  414. return "img/workunit_submitted.png";
  415. case 10:
  416. return "img/workunit_warning.png";
  417. case 11:
  418. return "img/workunit_running.png";
  419. case 12:
  420. return "img/workunit_warning.png";
  421. case 13:
  422. return "img/workunit_warning.png";
  423. case 14:
  424. return "img/workunit_warning.png";
  425. case 15:
  426. return "img/workunit_running.png";
  427. case 999:
  428. return "img/workunit_deleted.png";
  429. }
  430. return "img/workunit.png";
  431. },
  432. getProtectedImage: function () {
  433. if (this.protected) {
  434. return "img/locked.png"
  435. }
  436. return "img/unlocked.png"
  437. },
  438. fetchText: function (onFetchText) {
  439. if (this.text) {
  440. onFetchText(this.text);
  441. return;
  442. }
  443. this.getInfo({
  444. onGetText: onFetchText
  445. });
  446. },
  447. fetchXML: function (onFetchXML) {
  448. if (this.xml) {
  449. onFetchXML(this.xml);
  450. return;
  451. }
  452. var request = {
  453. Wuid: this.wuid,
  454. Type: "XML"
  455. };
  456. var context = this;
  457. xhr.post({
  458. url: this.getBaseURL() + "/WUFile.json",
  459. handleAs: "text",
  460. content: request,
  461. load: function (response) {
  462. context.xml = response;
  463. onFetchXML(response);
  464. },
  465. error: function (e) {
  466. }
  467. });
  468. },
  469. fetchResults: function (onFetchResults) {
  470. if (this.results && this.results.length) {
  471. onFetchResults(this.results);
  472. return;
  473. }
  474. this.getInfo({
  475. onGetResults: onFetchResults
  476. });
  477. },
  478. fetchTimers: function (onFetchTimers) {
  479. if (this.timers && this.timers.length) {
  480. onFetchTimers(this.timers);
  481. return;
  482. }
  483. this.getInfo({
  484. onGetTimers: onFetchTimers
  485. });
  486. },
  487. fetchGraphs: function (onFetchGraphs) {
  488. if (this.graphs && this.graphs.length) {
  489. onFetchGraphs(this.graphs);
  490. return;
  491. }
  492. this.getInfo({
  493. onGetGraphs: onFetchGraphs
  494. });
  495. },
  496. fetchGraphXgmmlByName: function (name, onFetchGraphXgmml) {
  497. var idx = this.getGraphIndex(name);
  498. if (idx >= 0) {
  499. this.fetchGraphXgmml(idx, onFetchGraphXgmml);
  500. }
  501. },
  502. fetchGraphXgmml: function (idx, onFetchGraphXgmml) {
  503. var request = {};
  504. request['Wuid'] = this.wuid;
  505. request['GraphName'] = this.graphs[idx].Name;
  506. request['rawxml_'] = "1";
  507. var context = this;
  508. xhr.post({
  509. url: this.getBaseURL() + "/WUGetGraph.json",
  510. handleAs: "json",
  511. content: request,
  512. load: function (response) {
  513. context.graphs[idx].xgmml = response.WUGetGraphResponse.Graphs.ECLGraphEx[0].Graph;
  514. onFetchGraphXgmml(context.graphs[idx].xgmml, context.graphs[idx].svg);
  515. },
  516. error: function () {
  517. }
  518. });
  519. },
  520. setGraphSvg: function (graphName, svg) {
  521. var idx = this.getGraphIndex(graphName);
  522. if (idx >= 0) {
  523. this.graphs[idx].svg = svg;
  524. var appData = [];
  525. appData[graphName + "_SVG"] = svg;
  526. this.update({}, appData);
  527. }
  528. }
  529. });
  530. });