ESPWorkunit.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. "hpcc/WsWorkunits",
  25. "hpcc/ESPUtil",
  26. "hpcc/ESPRequest",
  27. "hpcc/ESPResult"
  28. ], function (declare, arrayUtil, lang, Deferred, ObjectStore, QueryResults, Observable,
  29. WsWorkunits, ESPUtil, ESPRequest, ESPResult) {
  30. var _workunits = {};
  31. var Store = declare([ESPRequest.Store], {
  32. service: "WsWorkunits",
  33. action: "WUQuery",
  34. responseQualifier: "WUQueryResponse.Workunits.ECLWorkunit",
  35. responseTotalQualifier: "WUQueryResponse.NumWUs",
  36. idProperty: "Wuid",
  37. startProperty: "PageStartFrom",
  38. countProperty: "Count",
  39. _watched: [],
  40. create: function (id) {
  41. return new Workunit({
  42. Wuid: id
  43. });
  44. },
  45. update: function (id, item) {
  46. var storeItem = this.get(id);
  47. storeItem.updateData(item);
  48. if (!this._watched[id]) {
  49. var context = this;
  50. this._watched[id] = storeItem.watch("changedCount", function (name, oldValue, newValue) {
  51. if (oldValue !== newValue) {
  52. context.notify(storeItem, id);
  53. }
  54. });
  55. }
  56. }
  57. });
  58. var Workunit = declare([ESPUtil.Singleton, ESPUtil.Monitor], {
  59. // Asserts ---
  60. _assertHasWuid: function () {
  61. if (!this.Wuid) {
  62. throw new Error("Wuid cannot be empty.");
  63. }
  64. },
  65. // Attributes ---
  66. _StateIDSetter: function (StateID) {
  67. this.StateID = StateID;
  68. var actionEx = lang.exists("ActionEx", this) ? this.ActionEx : null;
  69. this.set("hasCompleted", WsWorkunits.isComplete(this.StateID, actionEx));
  70. },
  71. _ActionExSetter: function (ActionEx) {
  72. if (this.StateID) {
  73. this.ActionEx = ActionEx;
  74. this.set("hasCompleted", WsWorkunits.isComplete(this.StateID, this.ActionEx));
  75. }
  76. },
  77. _VariablesSetter: function (Variables) {
  78. this.set("variables", Variables.ECLResult);
  79. },
  80. _ResultsSetter: function (Results) {
  81. var results = [];
  82. var sequenceResults = [];
  83. for (var i = 0; i < Results.ECLResult.length; ++i) {
  84. var espResult = ESPResult.Get(lang.mixin({ wu: this.wu, Wuid: this.Wuid }, Results.ECLResult[i]));
  85. results.push(espResult);
  86. sequenceResults[Results.ECLResult[i].Sequence] = espResult;
  87. }
  88. this.set("results", results);
  89. this.set("sequenceResults", sequenceResults);
  90. },
  91. _SourceFilesSetter: function (SourceFiles) {
  92. var sourceFiles = [];
  93. for (var i = 0; i < SourceFiles.ECLSourceFile.length; ++i) {
  94. sourceFiles.push(ESPResult.Get(lang.mixin({ wu: this.wu, Wuid: this.Wuid }, SourceFiles.ECLSourceFile[i])));
  95. }
  96. this.set("sourceFiles", sourceFiles);
  97. },
  98. _TimersSetter: function (Timers) {
  99. var timers = [];
  100. for (var i = 0; i < Timers.ECLTimer.length; ++i) {
  101. var timeParts = Timers.ECLTimer[i].Value.split(":");
  102. var secs = 0;
  103. for (var j = 0; j < timeParts.length; ++j) {
  104. secs = secs * 60 + timeParts[j] * 1;
  105. }
  106. timers.push(lang.mixin(Timers.ECLTimer[i], {
  107. Seconds: Math.round(secs * 1000) / 1000,
  108. HasSubGraphId: Timers.ECLTimer[i].SubGraphId && Timers.ECLTimer[i].SubGraphId != "" ? true : false
  109. }));
  110. }
  111. this.set("timers", timers);
  112. },
  113. _GraphsSetter: function (Graphs) {
  114. this.set("graphs", Graphs.ECLGraph);
  115. },
  116. // --- --- ---
  117. onCreate: function () {
  118. },
  119. onUpdate: function () {
  120. },
  121. onSubmit: function () {
  122. },
  123. constructor: function (args) {
  124. this.inherited(arguments);
  125. declare.safeMixin(this, args);
  126. this.wu = this;
  127. },
  128. isComplete: function () {
  129. return this.hasCompleted;
  130. },
  131. monitor: function (callback) {
  132. if (callback) {
  133. callback(this);
  134. }
  135. if (!this.hasCompleted) {
  136. var context = this;
  137. this.watch("changedCount", function (name, oldValue, newValue) {
  138. if (oldValue !== newValue && newValue) {
  139. if (callback) {
  140. callback(context);
  141. }
  142. }
  143. });
  144. }
  145. },
  146. create: function (ecl) {
  147. var context = this;
  148. WsWorkunits.WUCreate({
  149. load: function (response) {
  150. _workunits[response.WUCreateResponse.Workunit.Wuid] = context;
  151. context.Wuid = response.WUCreateResponse.Workunit.Wuid;
  152. context.startMonitor(true);
  153. context.updateData(response.WUCreateResponse.Workunit);
  154. context.onCreate();
  155. }
  156. });
  157. },
  158. update: function (request, appData) {
  159. this._assertHasWuid();
  160. lang.mixin(request, {
  161. Wuid: this.Wuid
  162. });
  163. lang.mixin(request, {
  164. StateOrig: this.State,
  165. JobnameOrig: this.Jobname,
  166. DescriptionOrig: this.Description,
  167. ProtectedOrig: this.Protected,
  168. ScopeOrig: this.Scope,
  169. ClusterOrig: this.Cluster,
  170. ApplicationValues: appData
  171. });
  172. var context = this;
  173. WsWorkunits.WUUpdate({
  174. request: request,
  175. load: function (response) {
  176. context.updateData(response.WUUpdateResponse.Workunit);
  177. context.onUpdate();
  178. }
  179. });
  180. },
  181. submit: function (target) {
  182. this._assertHasWuid();
  183. var context = this;
  184. WsWorkunits.WUSubmit({
  185. request: {
  186. Wuid: this.Wuid,
  187. Cluster: target
  188. },
  189. load: function (response) {
  190. context.onSubmit();
  191. }
  192. });
  193. },
  194. _resubmit: function (clone, resetWorkflow) {
  195. this._assertHasWuid();
  196. var context = this;
  197. WsWorkunits.WUResubmit({
  198. request: {
  199. Wuids: this.Wuid,
  200. CloneWorkunit: clone,
  201. ResetWorkflow: resetWorkflow
  202. },
  203. load: function (response) {
  204. context.refresh();
  205. }
  206. });
  207. },
  208. clone: function () {
  209. this._resubmit(true, false);
  210. },
  211. resubmit: function () {
  212. this._resubmit(false, false);
  213. },
  214. restart: function () {
  215. this._resubmit(false, true);
  216. },
  217. _action: function (action) {
  218. this._assertHasWuid();
  219. var context = this;
  220. return WsWorkunits.WUAction([{ Wuid: this.Wuid }], action, {
  221. load: function (response) {
  222. context.refresh();
  223. }
  224. });
  225. },
  226. setToFailed: function () {
  227. return this._action("setToFailed");
  228. },
  229. abort: function () {
  230. return this._action("Abort");
  231. },
  232. doDelete: function () {
  233. return this._action("Delete");
  234. },
  235. publish: function (jobName) {
  236. this._assertHasWuid();
  237. var context = this;
  238. WsWorkunits.WUPublishWorkunit({
  239. request: {
  240. Wuid: this.Wuid,
  241. JobName: jobName,
  242. Activate: 1,
  243. UpdateWorkUnitName: 1,
  244. Wait: 5000
  245. },
  246. load: function (response) {
  247. context.updateData(response.WUPublishWorkunitResponse);
  248. }
  249. });
  250. },
  251. refresh: function (full) {
  252. if (full || this.changedCount === 0) {
  253. this.getInfo({
  254. onGetText: function () {
  255. },
  256. onGetWUExceptions: function () {
  257. },
  258. onGetVariables: function () {
  259. },
  260. onGetTimers: function () {
  261. },
  262. onGetApplicationValues: function () {
  263. }
  264. });
  265. } else {
  266. this.getQuery();
  267. }
  268. },
  269. getQuery: function () {
  270. this._assertHasWuid();
  271. var context = this;
  272. WsWorkunits.WUQuery({
  273. request: {
  274. Wuid: this.Wuid
  275. },
  276. load: function (response) {
  277. if (lang.exists("WUQueryResponse.Workunits.ECLWorkunit", response)) {
  278. arrayUtil.forEach(response.WUQueryResponse.Workunits.ECLWorkunit, function (item, index) {
  279. context.updateData(item);
  280. });
  281. }
  282. }
  283. });
  284. },
  285. getInfo: function (args) {
  286. this._assertHasWuid();
  287. var context = this;
  288. WsWorkunits.WUInfo({
  289. request: {
  290. Wuid: this.Wuid,
  291. TruncateEclTo64k: args.onGetText ? false : true,
  292. IncludeExceptions: args.onGetWUExceptions ? true : false,
  293. IncludeGraphs: args.onGetGraphs ? true : false,
  294. IncludeSourceFiles: args.onGetSourceFiles ? true : false,
  295. IncludeResults: (args.onGetResults || args.onGetSequenceResults) ? true : false,
  296. IncludeResultsViewNames: false,
  297. IncludeVariables: args.onGetVariables ? true : false,
  298. IncludeTimers: args.onGetTimers ? true : false,
  299. IncludeDebugValues: false,
  300. IncludeApplicationValues: args.onGetApplicationValues ? true : false,
  301. IncludeWorkflows: false,
  302. IncludeXmlSchemas: false,
  303. SuppressResultSchemas: true
  304. }
  305. }).then(function(response) {
  306. if (lang.exists("WUInfoResponse.Workunit", response)) {
  307. if (!args.onGetText && lang.exists("WUInfoResponse.Workunit.Query", response)) {
  308. // A truncated version of ECL just causes issues ---
  309. delete response.WUInfoResponse.Workunit.Query;
  310. }
  311. context.updateData(response.WUInfoResponse.Workunit);
  312. if (args.onGetText && lang.exists("Query.Text", context)) {
  313. args.onGetText(context.Query.Text);
  314. }
  315. if (args.onGetWUExceptions && lang.exists("Exceptions.ECLException", context)) {
  316. args.onGetWUExceptions(context.Exceptions.ECLException);
  317. }
  318. if (args.onGetApplicationValues && lang.exists("ApplicationValues.ApplicationValue", context)) {
  319. args.onGetApplicationValues(context.ApplicationValues.ApplicationValue)
  320. }
  321. if (args.onGetVariables && lang.exists("variables", context)) {
  322. args.onGetVariables(context.variables);
  323. }
  324. if (args.onGetResults && lang.exists("results", context)) {
  325. args.onGetResults(context.results);
  326. }
  327. if (args.onGetSequenceResults && lang.exists("sequenceResults", context)) {
  328. args.onGetSequenceResults(context.sequenceResults);
  329. }
  330. if (args.onGetSourceFiles && lang.exists("sourceFiles", context)) {
  331. args.onGetSourceFiles(context.sourceFiles);
  332. }
  333. if (args.onGetTimers && lang.exists("timers", context)) {
  334. args.onGetTimers(context.timers);
  335. }
  336. if (args.onGetGraphs && lang.exists("graphs", context)) {
  337. if (context.timers || lang.exists("ApplicationValues.ApplicationValue", context)) {
  338. for (var i = 0; i < context.graphs.length; ++i) {
  339. if (context.timers) {
  340. context.graphs[i].Time = 0;
  341. for (var j = 0; j < context.timers.length; ++j) {
  342. if (context.timers[j].GraphName == context.graphs[i].Name) {
  343. context.graphs[i].Time += context.timers[j].Seconds;
  344. }
  345. context.graphs[i].Time = Math.round(context.graphs[i].Time * 1000) / 1000;
  346. }
  347. }
  348. if (lang.exists("ApplicationValues.ApplicationValue", context)) {
  349. var idx = context.getApplicationValueIndex("ESPWorkunit.js", context.graphs[i].Name + "_SVG");
  350. if (idx >= 0) {
  351. context.graphs[i].svg = context.ApplicationValues.ApplicationValue[idx].Value;
  352. }
  353. }
  354. }
  355. }
  356. args.onGetGraphs(context.graphs)
  357. }
  358. if (args.onAfterSend) {
  359. args.onAfterSend(context);
  360. }
  361. }
  362. });
  363. },
  364. getGraphIndex: function (name) {
  365. for (var i = 0; i < this.graphs.length; ++i) {
  366. if (this.graphs[i].Name == name) {
  367. return i;
  368. }
  369. }
  370. return -1;
  371. },
  372. getApplicationValueIndex: function (application, name) {
  373. if (lang.exists("ApplicationValues.ApplicationValue", this)) {
  374. for (var i = 0; i < this.ApplicationValues.ApplicationValue.length; ++i) {
  375. if (this.ApplicationValues.ApplicationValue[i].Application == application && this.ApplicationValues.ApplicationValue[i].Name == name) {
  376. return i;
  377. }
  378. }
  379. }
  380. return -1;
  381. },
  382. getState: function () {
  383. return this.State;
  384. },
  385. getStateIconClass: function () {
  386. switch (this.StateID) {
  387. case 1:
  388. case 3:
  389. return "iconCompleted";
  390. case 2:
  391. case 11:
  392. case 15:
  393. return "iconRunning";
  394. case 4:
  395. case 7:
  396. return "iconFailed";
  397. case 5:
  398. case 8:
  399. case 10:
  400. case 12:
  401. case 13:
  402. case 14:
  403. case 16:
  404. return "iconArchived";
  405. case 6:
  406. return "iconAborting";
  407. case 9:
  408. return "iconSubmitted";
  409. case 999:
  410. return "iconDeleted";
  411. }
  412. return "iconWorkunit";
  413. },
  414. getStateImage: function () {
  415. switch (this.StateID) {
  416. case 1:
  417. return "img/workunit_completed.png";
  418. case 2:
  419. return "img/workunit_running.png";
  420. case 3:
  421. return "img/workunit_completed.png";
  422. case 4:
  423. return "img/workunit_failed.png";
  424. case 5:
  425. return "img/workunit_warning.png";
  426. case 6:
  427. return "img/workunit_aborting.png";
  428. case 7:
  429. return "img/workunit_failed.png";
  430. case 8:
  431. return "img/workunit_warning.png";
  432. case 9:
  433. return "img/workunit_submitted.png";
  434. case 10:
  435. return "img/workunit_warning.png";
  436. case 11:
  437. return "img/workunit_running.png";
  438. case 12:
  439. return "img/workunit_warning.png";
  440. case 13:
  441. return "img/workunit_warning.png";
  442. case 14:
  443. return "img/workunit_warning.png";
  444. case 15:
  445. return "img/workunit_running.png";
  446. case 16:
  447. return "img/workunit_warning.png";
  448. case 999:
  449. return "img/workunit_deleted.png";
  450. }
  451. return "img/workunit.png";
  452. },
  453. getProtectedImage: function () {
  454. if (this.Protected) {
  455. return "img/locked.png"
  456. }
  457. return "img/unlocked.png"
  458. },
  459. fetchText: function (onFetchText) {
  460. var context = this;
  461. if (lang.exists("Query.Text", context)) {
  462. onFetchText(this.Query.Text);
  463. return;
  464. }
  465. this.getInfo({
  466. onGetText: onFetchText
  467. });
  468. },
  469. fetchXML: function (onFetchXML) {
  470. if (this.xml) {
  471. onFetchXML(this.xml);
  472. return;
  473. }
  474. this._assertHasWuid();
  475. var context = this;
  476. WsWorkunits.WUFile({
  477. request: {
  478. Wuid: this.Wuid,
  479. Type: "XML"
  480. },
  481. load: function (response) {
  482. context.xml = response;
  483. onFetchXML(response);
  484. }
  485. });
  486. },
  487. fetchResults: function (onFetchResults) {
  488. if (this.results && this.results.length) {
  489. onFetchResults(this.results);
  490. return;
  491. }
  492. this.getInfo({
  493. onGetResults: onFetchResults
  494. });
  495. },
  496. fetchSequenceResults: function (onFetchSequenceResults) {
  497. if (this.sequenceResults && this.sequenceResults.length) {
  498. onFetchSequenceResults(this.sequenceResults);
  499. return;
  500. }
  501. this.getInfo({
  502. onGetSequenceResults: onFetchSequenceResults
  503. });
  504. },
  505. fetchSourceFiles: function (onFetchSourceFiles) {
  506. if (this.sourceFiles && this.sourceFiles.length) {
  507. onFetchSourceFiles(this.sourceFiles);
  508. return;
  509. }
  510. this.getInfo({
  511. onGetSourceFiles: onFetchSourceFiles
  512. });
  513. },
  514. fetchTimers: function (onFetchTimers) {
  515. if (this.timers && this.timers.length) {
  516. onFetchTimers(this.timers);
  517. return;
  518. }
  519. this.getInfo({
  520. onGetTimers: onFetchTimers
  521. });
  522. },
  523. fetchGraphs: function (onFetchGraphs) {
  524. if (this.graphs && this.graphs.length) {
  525. onFetchGraphs(this.graphs);
  526. return;
  527. }
  528. this.getInfo({
  529. onGetGraphs: onFetchGraphs
  530. });
  531. },
  532. fetchGraphXgmmlByName: function (name, onFetchGraphXgmml) {
  533. var idx = this.getGraphIndex(name);
  534. if (idx >= 0) {
  535. this.fetchGraphXgmml(idx, onFetchGraphXgmml);
  536. }
  537. },
  538. fetchGraphXgmml: function (idx, onFetchGraphXgmml) {
  539. if (this.graphs && this.graphs[idx] && this.graphs[idx].xgmml) {
  540. onFetchGraphXgmml(this.graphs[idx].xgmml, this.graphs[idx].svg);
  541. return;
  542. }
  543. this._assertHasWuid();
  544. var context = this;
  545. WsWorkunits.WUGetGraph({
  546. request: {
  547. Wuid: this.Wuid,
  548. GraphName: this.graphs[idx].Name
  549. },
  550. load: function (response) {
  551. context.graphs[idx].xgmml = response.WUGetGraphResponse.Graphs.ECLGraphEx[0].Graph;
  552. onFetchGraphXgmml(context.graphs[idx].xgmml, context.graphs[idx].svg);
  553. }
  554. });
  555. },
  556. setGraphSvg: function (graphName, svg) {
  557. var idx = this.getGraphIndex(graphName);
  558. if (idx >= 0) {
  559. this.graphs[idx].svg = svg;
  560. var appData = [];
  561. appData[graphName + "_SVG"] = svg;
  562. this.update({}, appData);
  563. }
  564. }
  565. });
  566. return {
  567. Create: function (params) {
  568. retVal = new Workunit(params);
  569. retVal.create();
  570. return retVal;
  571. },
  572. Get: function (wuid) {
  573. var store = new Store();
  574. return store.get(wuid);
  575. },
  576. CreateWUQueryStore: function (options) {
  577. var store = new Store(options);
  578. return Observable(store);
  579. },
  580. CreateWUQueryObjectStore: function (options) {
  581. return new ObjectStore({ objectStore: this.CreateWUQueryStore(options) });
  582. }
  583. };
  584. });