ESPWorkunit.js 24 KB

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