ESPWorkunit.js 25 KB

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