ESPWorkunit.js 24 KB

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