ESPWorkunit.js 25 KB

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