ESPWorkunit.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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, remoteDali) {
  266. this._assertHasWuid();
  267. var context = this;
  268. WsWorkunits.WUPublishWorkunit({
  269. request: {
  270. Wuid: this.Wuid,
  271. JobName: jobName,
  272. RemoteDali: remoteDali,
  273. Activate: 1,
  274. UpdateWorkUnitName: 1,
  275. Wait: 5000
  276. },
  277. load: function (response) {
  278. context.updateData(response.WUPublishWorkunitResponse);
  279. }
  280. });
  281. },
  282. refresh: function (full) {
  283. if (full || this.changedCount === 0) {
  284. this.getInfo({
  285. onGetText: function () {
  286. },
  287. onGetWUExceptions: function () {
  288. },
  289. onGetVariables: function () {
  290. },
  291. onGetTimers: function () {
  292. },
  293. onGetApplicationValues: function () {
  294. }
  295. });
  296. } else {
  297. this.getQuery();
  298. }
  299. },
  300. getQuery: function () {
  301. this._assertHasWuid();
  302. var context = this;
  303. WsWorkunits.WUQuery({
  304. request: {
  305. Wuid: this.Wuid
  306. },
  307. load: function (response) {
  308. if (lang.exists("WUQueryResponse.Workunits.ECLWorkunit", response)) {
  309. arrayUtil.forEach(response.WUQueryResponse.Workunits.ECLWorkunit, function (item, index) {
  310. context.updateData(item);
  311. });
  312. }
  313. }
  314. });
  315. },
  316. getInfo: function (args) {
  317. this._assertHasWuid();
  318. var context = this;
  319. WsWorkunits.WUInfo({
  320. request: {
  321. Wuid: this.Wuid,
  322. TruncateEclTo64k: args.onGetText ? false : true,
  323. IncludeExceptions: args.onGetWUExceptions ? true : false,
  324. IncludeGraphs: args.onGetGraphs ? true : false,
  325. IncludeSourceFiles: args.onGetSourceFiles ? true : false,
  326. IncludeResults: (args.onGetResults || args.onGetSequenceResults) ? true : false,
  327. IncludeResultsViewNames: (args.onGetResults || args.onGetSequenceResults) ? true : false,
  328. IncludeVariables: args.onGetVariables ? true : false,
  329. IncludeTimers: args.onGetTimers ? true : false,
  330. IncludeDebugValues: false,
  331. IncludeApplicationValues: args.onGetApplicationValues ? true : false,
  332. IncludeWorkflows: false,
  333. IncludeXmlSchemas: false,
  334. SuppressResultSchemas: true
  335. }
  336. }).then(function(response) {
  337. if (lang.exists("WUInfoResponse.Workunit", response)) {
  338. if (!args.onGetText && lang.exists("WUInfoResponse.Workunit.Query", response)) {
  339. // A truncated version of ECL just causes issues ---
  340. delete response.WUInfoResponse.Workunit.Query;
  341. }
  342. if (lang.exists("WUInfoResponse.ResultViews", response) && lang.exists("WUInfoResponse.Workunit.Results", response)) {
  343. lang.mixin(response.WUInfoResponse.Workunit.Results, {
  344. ResultViews: response.WUInfoResponse.ResultViews
  345. });
  346. }
  347. context.updateData(response.WUInfoResponse.Workunit);
  348. if (args.onGetText && lang.exists("Query.Text", context)) {
  349. args.onGetText(context.Query.Text);
  350. }
  351. if (args.onGetWUExceptions && lang.exists("Exceptions.ECLException", context)) {
  352. args.onGetWUExceptions(context.Exceptions.ECLException);
  353. }
  354. if (args.onGetApplicationValues && lang.exists("ApplicationValues.ApplicationValue", context)) {
  355. args.onGetApplicationValues(context.ApplicationValues.ApplicationValue)
  356. }
  357. if (args.onGetVariables && lang.exists("variables", context)) {
  358. args.onGetVariables(context.variables);
  359. }
  360. if (args.onGetResults && lang.exists("results", context)) {
  361. args.onGetResults(context.results);
  362. }
  363. if (args.onGetSequenceResults && lang.exists("sequenceResults", context)) {
  364. args.onGetSequenceResults(context.sequenceResults);
  365. }
  366. if (args.onGetSourceFiles && lang.exists("sourceFiles", context)) {
  367. args.onGetSourceFiles(context.sourceFiles);
  368. }
  369. if (args.onGetTimers && lang.exists("timers", context)) {
  370. args.onGetTimers(context.timers);
  371. }
  372. if (args.onGetGraphs && lang.exists("graphs", context)) {
  373. if (context.timers || lang.exists("ApplicationValues.ApplicationValue", context)) {
  374. for (var i = 0; i < context.graphs.length; ++i) {
  375. if (context.timers) {
  376. context.graphs[i].Time = 0;
  377. for (var j = 0; j < context.timers.length; ++j) {
  378. if (context.timers[j].GraphName == context.graphs[i].Name) {
  379. context.graphs[i].Time += context.timers[j].Seconds;
  380. }
  381. context.graphs[i].Time = Math.round(context.graphs[i].Time * 1000) / 1000;
  382. }
  383. }
  384. if (lang.exists("ApplicationValues.ApplicationValue", context)) {
  385. var idx = context.getApplicationValueIndex("ESPWorkunit.js", context.graphs[i].Name + "_SVG");
  386. if (idx >= 0) {
  387. context.graphs[i].svg = context.ApplicationValues.ApplicationValue[idx].Value;
  388. }
  389. }
  390. }
  391. }
  392. args.onGetGraphs(context.graphs)
  393. }
  394. if (args.onAfterSend) {
  395. args.onAfterSend(context);
  396. }
  397. }
  398. });
  399. },
  400. getGraphIndex: function (name) {
  401. for (var i = 0; i < this.graphs.length; ++i) {
  402. if (this.graphs[i].Name == name) {
  403. return i;
  404. }
  405. }
  406. return -1;
  407. },
  408. getApplicationValueIndex: function (application, name) {
  409. if (lang.exists("ApplicationValues.ApplicationValue", this)) {
  410. for (var i = 0; i < this.ApplicationValues.ApplicationValue.length; ++i) {
  411. if (this.ApplicationValues.ApplicationValue[i].Application == application && this.ApplicationValues.ApplicationValue[i].Name == name) {
  412. return i;
  413. }
  414. }
  415. }
  416. return -1;
  417. },
  418. getState: function () {
  419. return this.State;
  420. },
  421. getStateIconClass: function () {
  422. switch (this.StateID) {
  423. case 1:
  424. case 3:
  425. return "iconCompleted";
  426. case 2:
  427. case 11:
  428. case 15:
  429. return "iconRunning";
  430. case 4:
  431. case 7:
  432. return "iconFailed";
  433. case 5:
  434. case 8:
  435. case 10:
  436. case 12:
  437. case 13:
  438. case 14:
  439. case 16:
  440. return "iconArchived";
  441. case 6:
  442. return "iconAborting";
  443. case 9:
  444. return "iconSubmitted";
  445. case 999:
  446. return "iconDeleted";
  447. }
  448. return "iconWorkunit";
  449. },
  450. getStateImage: function () {
  451. switch (this.StateID) {
  452. case 1:
  453. return "img/workunit_completed.png";
  454. case 2:
  455. return "img/workunit_running.png";
  456. case 3:
  457. return "img/workunit_completed.png";
  458. case 4:
  459. return "img/workunit_failed.png";
  460. case 5:
  461. return "img/workunit_warning.png";
  462. case 6:
  463. return "img/workunit_aborting.png";
  464. case 7:
  465. return "img/workunit_failed.png";
  466. case 8:
  467. return "img/workunit_warning.png";
  468. case 9:
  469. return "img/workunit_submitted.png";
  470. case 10:
  471. return "img/workunit_warning.png";
  472. case 11:
  473. return "img/workunit_running.png";
  474. case 12:
  475. return "img/workunit_warning.png";
  476. case 13:
  477. return "img/workunit_warning.png";
  478. case 14:
  479. return "img/workunit_warning.png";
  480. case 15:
  481. return "img/workunit_running.png";
  482. case 16:
  483. return "img/workunit_warning.png";
  484. case 999:
  485. return "img/workunit_deleted.png";
  486. }
  487. return "img/workunit.png";
  488. },
  489. getProtectedImage: function () {
  490. if (this.Protected) {
  491. return "img/locked.png"
  492. }
  493. return "img/unlocked.png"
  494. },
  495. fetchText: function (onFetchText) {
  496. var context = this;
  497. if (lang.exists("Query.Text", context)) {
  498. onFetchText(this.Query.Text);
  499. return;
  500. }
  501. this.getInfo({
  502. onGetText: onFetchText
  503. });
  504. },
  505. fetchXML: function (onFetchXML) {
  506. if (this.xml) {
  507. onFetchXML(this.xml);
  508. return;
  509. }
  510. this._assertHasWuid();
  511. var context = this;
  512. WsWorkunits.WUFile({
  513. request: {
  514. Wuid: this.Wuid,
  515. Type: "XML"
  516. },
  517. load: function (response) {
  518. context.xml = response;
  519. onFetchXML(response);
  520. }
  521. });
  522. },
  523. fetchResults: function (onFetchResults) {
  524. if (this.results && this.results.length) {
  525. onFetchResults(this.results);
  526. return;
  527. }
  528. this.getInfo({
  529. onGetResults: onFetchResults
  530. });
  531. },
  532. fetchNamedResults: function (resultNames) {
  533. var deferred = new Deferred()
  534. var context = this;
  535. this.fetchResults(function (results) {
  536. var resultContents = [];
  537. arrayUtil.forEach(resultNames, function (item, idx) {
  538. resultContents.push(context.namedResults[item].fetchContent());
  539. });
  540. all(resultContents).then(function (resultContents) {
  541. var results = [];
  542. arrayUtil.forEach(resultContents, function (item, idx) {
  543. results[resultNames[idx]] = item;
  544. });
  545. deferred.resolve(results);
  546. });
  547. });
  548. return deferred.promise;
  549. },
  550. fetchSequenceResults: function (onFetchSequenceResults) {
  551. if (this.sequenceResults && this.sequenceResults.length) {
  552. onFetchSequenceResults(this.sequenceResults);
  553. return;
  554. }
  555. this.getInfo({
  556. onGetSequenceResults: onFetchSequenceResults
  557. });
  558. },
  559. fetchSourceFiles: function (onFetchSourceFiles) {
  560. if (this.sourceFiles && this.sourceFiles.length) {
  561. onFetchSourceFiles(this.sourceFiles);
  562. return;
  563. }
  564. this.getInfo({
  565. onGetSourceFiles: onFetchSourceFiles
  566. });
  567. },
  568. fetchTimers: function (onFetchTimers) {
  569. if (this.timers && this.timers.length) {
  570. onFetchTimers(this.timers);
  571. return;
  572. }
  573. this.getInfo({
  574. onGetTimers: onFetchTimers
  575. });
  576. },
  577. fetchGraphs: function (onFetchGraphs) {
  578. if (this.graphs && this.graphs.length) {
  579. onFetchGraphs(this.graphs);
  580. return;
  581. }
  582. this.getInfo({
  583. onGetGraphs: onFetchGraphs
  584. });
  585. },
  586. fetchGraphXgmmlByName: function (name, onFetchGraphXgmml) {
  587. var idx = this.getGraphIndex(name);
  588. if (idx >= 0) {
  589. this.fetchGraphXgmml(idx, onFetchGraphXgmml);
  590. }
  591. },
  592. fetchGraphXgmml: function (idx, onFetchGraphXgmml) {
  593. if (this.graphs && this.graphs[idx] && this.graphs[idx].xgmml) {
  594. onFetchGraphXgmml(this.graphs[idx].xgmml, this.graphs[idx].svg);
  595. return;
  596. }
  597. this._assertHasWuid();
  598. var context = this;
  599. WsWorkunits.WUGetGraph({
  600. request: {
  601. Wuid: this.Wuid,
  602. GraphName: this.graphs[idx].Name
  603. },
  604. load: function (response) {
  605. context.graphs[idx].xgmml = response.WUGetGraphResponse.Graphs.ECLGraphEx[0].Graph;
  606. onFetchGraphXgmml(context.graphs[idx].xgmml, context.graphs[idx].svg);
  607. }
  608. });
  609. },
  610. setGraphSvg: function (graphName, svg) {
  611. var idx = this.getGraphIndex(graphName);
  612. if (idx >= 0) {
  613. this.graphs[idx].svg = svg;
  614. var appData = [];
  615. appData[graphName + "_SVG"] = svg;
  616. this.update({}, appData);
  617. }
  618. }
  619. });
  620. return {
  621. Create: function (params) {
  622. retVal = new Workunit(params);
  623. retVal.create();
  624. return retVal;
  625. },
  626. Get: function (wuid) {
  627. var store = new Store();
  628. return store.get(wuid);
  629. },
  630. CreateWUQueryStore: function (options) {
  631. var store = new Store(options);
  632. return Observable(store);
  633. },
  634. CreateWUQueryObjectStore: function (options) {
  635. return new ObjectStore({ objectStore: this.CreateWUQueryStore(options) });
  636. }
  637. };
  638. });