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/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) {
  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. }).then(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. 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 "/esp/files/img/workunit_completed.png";
  453. case 2:
  454. return "/esp/files/img/workunit_running.png";
  455. case 3:
  456. return "/esp/files/img/workunit_completed.png";
  457. case 4:
  458. return "/esp/files/img/workunit_failed.png";
  459. case 5:
  460. return "/esp/files/img/workunit_warning.png";
  461. case 6:
  462. return "/esp/files/img/workunit_aborting.png";
  463. case 7:
  464. return "/esp/files/img/workunit_failed.png";
  465. case 8:
  466. return "/esp/files/img/workunit_warning.png";
  467. case 9:
  468. return "/esp/files/img/workunit_submitted.png";
  469. case 10:
  470. return "/esp/files/img/workunit_warning.png";
  471. case 11:
  472. return "/esp/files/img/workunit_running.png";
  473. case 12:
  474. return "/esp/files/img/workunit_warning.png";
  475. case 13:
  476. return "/esp/files/img/workunit_warning.png";
  477. case 14:
  478. return "/esp/files/img/workunit_warning.png";
  479. case 15:
  480. return "/esp/files/img/workunit_running.png";
  481. case 16:
  482. return "/esp/files/img/workunit_warning.png";
  483. case 999:
  484. return "/esp/files/img/workunit_deleted.png";
  485. }
  486. return "/esp/files/img/workunit.png";
  487. },
  488. getProtectedImage: function () {
  489. if (this.Protected) {
  490. return "/esp/files/img/locked.png"
  491. }
  492. return "/esp/files/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, force) {
  586. var idx = this.getGraphIndex(name);
  587. if (idx >= 0) {
  588. this.fetchGraphXgmml(idx, onFetchGraphXgmml, force);
  589. }
  590. },
  591. fetchGraphXgmml: function (idx, onFetchGraphXgmml, force) {
  592. if (!force && 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. isInstanceOfWorkunit: function (obj) {
  621. return obj.isInstanceOf(Workunit);
  622. },
  623. Create: function (params) {
  624. retVal = new Workunit(params);
  625. retVal.create();
  626. return retVal;
  627. },
  628. Get: function (wuid) {
  629. var store = new Store();
  630. return store.get(wuid);
  631. },
  632. CreateWUQueryStore: function (options) {
  633. var store = new Store(options);
  634. return Observable(store);
  635. }
  636. };
  637. });