ESPWorkunit.js 25 KB

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