ESPWorkunit.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. _workunits[id].refresh();
  45. }
  46. return _workunits[id];
  47. },
  48. remove: function (item) {
  49. if (_workunits[this.getIdentity(item)]) {
  50. _workunits[this.getIdentity(item)].stopMonitor();
  51. delete _workunits[this.getIdentity(item)];
  52. }
  53. },
  54. query: function (query, options) {
  55. var request = {};
  56. lang.mixin(request, options.query);
  57. if (options.start)
  58. request['PageStartFrom'] = options.start;
  59. if (options.count)
  60. request['Count'] = options.count;
  61. if (options.sort) {
  62. request['Sortby'] = options.sort[0].attribute;
  63. request['Descending'] = options.sort[0].descending;
  64. }
  65. var results = WsWorkunits.WUQuery({
  66. request: request
  67. });
  68. var deferredResults = new Deferred();
  69. deferredResults.total = results.then(function (response) {
  70. if (lang.exists("WUQueryResponse.NumWUs", response)) {
  71. return response.WUQueryResponse.NumWUs;
  72. }
  73. return 0;
  74. });
  75. var context = this;
  76. Deferred.when(results, function (response) {
  77. var workunits = [];
  78. for (key in context._watched) {
  79. context._watched[key].unwatch();
  80. }
  81. this._watched = {};
  82. if (lang.exists("WUQueryResponse.Workunits.ECLWorkunit", response)) {
  83. arrayUtil.forEach(response.WUQueryResponse.Workunits.ECLWorkunit, function (item, index) {
  84. var wu = context.get(item.Wuid);
  85. wu.updateData(item);
  86. workunits.push(wu);
  87. context._watched[wu.Wuid] = wu.watch("changedCount", function (name, oldValue, newValue) {
  88. if (oldValue !== newValue) {
  89. context.notify(wu, wu.Wuid);
  90. }
  91. });
  92. });
  93. }
  94. deferredResults.resolve(workunits);
  95. });
  96. return QueryResults(deferredResults);
  97. }
  98. });
  99. var Workunit = declare([ESPUtil.Singleton, ESPUtil.Monitor], {
  100. // Asserts ---
  101. _assertHasWuid: function () {
  102. if (!this.Wuid) {
  103. throw new Error("Wuid cannot be empty.");
  104. }
  105. },
  106. // Attributes ---
  107. _StateIDSetter: function (StateID) {
  108. this.StateID = StateID;
  109. var actionEx = lang.exists("ActionEx", this) ? this.ActionEx : null;
  110. this.set("hasCompleted", WsWorkunits.isComplete(this.StateID, actionEx));
  111. },
  112. _ActionExSetter: function (ActionEx) {
  113. if (this.StateID) {
  114. this.ActionEx = ActionEx;
  115. this.set("hasCompleted", WsWorkunits.isComplete(this.StateID, this.ActionEx));
  116. }
  117. },
  118. _VariablesSetter: function (Variables) {
  119. var variables = [];
  120. for (var i = 0; i < Variables.ECLResult.length; ++i) {
  121. context.variables.push(lang.mixin({
  122. ColumnType: Variables.ECLResult[i].ECLSchemas && Variables.ECLResult[i].ECLSchemas.ECLSchemaItem.length ? Variables.ECLResult[i].ECLSchemas.ECLSchemaItem[0].ColumnType : "unknown"
  123. }, variables[i]));
  124. }
  125. this.set("variables", variables);
  126. },
  127. _ResultsSetter: function (Results) {
  128. var results = [];
  129. for (var i = 0; i < Results.ECLResult.length; ++i) {
  130. results.push(ESPResult.Get(lang.mixin({ wu: this.wu, Wuid: this.Wuid }, Results.ECLResult[i])));
  131. }
  132. this.set("results", results);
  133. },
  134. _SourceFilesSetter: function (SourceFiles) {
  135. var sourceFiles = [];
  136. for (var i = 0; i < SourceFiles.ECLSourceFile.length; ++i) {
  137. sourceFiles.push(ESPResult.Get(lang.mixin({ wu: this.wu, Wuid: this.Wuid }, SourceFiles.ECLSourceFile[i])));
  138. }
  139. this.set("sourceFiles", sourceFiles);
  140. },
  141. _TimersSetter: function (Timers) {
  142. var timers = [];
  143. for (var i = 0; i < Timers.ECLTimer.length; ++i) {
  144. var timeParts = Timers.ECLTimer[i].Value.split(":");
  145. var secs = 0;
  146. for (var j = 0; j < timeParts.length; ++j) {
  147. secs = secs * 60 + timeParts[j] * 1;
  148. }
  149. timers.push(lang.mixin(Timers.ECLTimer[i], {
  150. Seconds: Math.round(secs * 1000) / 1000,
  151. HasSubGraphId: Timers.ECLTimer[i].SubGraphId && Timers.ECLTimer[i].SubGraphId != "" ? true : false
  152. }));
  153. }
  154. this.set("timers", timers);
  155. },
  156. _GraphsSetter: function (Graphs) {
  157. this.set("graphs", Graphs.ECLGraph);
  158. },
  159. // --- --- ---
  160. onCreate: function () {
  161. },
  162. onUpdate: function () {
  163. },
  164. onSubmit: function () {
  165. },
  166. constructor: function (args) {
  167. this.inherited(arguments);
  168. declare.safeMixin(this, args);
  169. this.wu = this;
  170. },
  171. isComplete: function () {
  172. return this.hasCompleted;
  173. },
  174. monitor: function (callback) {
  175. if (this.hasCompleted) {
  176. if (callback) {
  177. callback(this);
  178. }
  179. } else {
  180. var context = this;
  181. this.watch("changedCount", function (name, oldValue, newValue) {
  182. if (oldValue !== newValue && newValue) {
  183. if (callback) {
  184. callback(context);
  185. }
  186. }
  187. });
  188. }
  189. },
  190. create: function (ecl) {
  191. var context = this;
  192. WsWorkunits.WUCreate({
  193. load: function (response) {
  194. _workunits[response.WUCreateResponse.Workunit.Wuid] = context
  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. fetchTimers: function (onFetchTimers) {
  543. if (this.timers && this.timers.length) {
  544. onFetchTimers(this.timers);
  545. return;
  546. }
  547. this.getInfo({
  548. onGetTimers: onFetchTimers
  549. });
  550. },
  551. fetchGraphs: function (onFetchGraphs) {
  552. if (this.graphs && this.graphs.length) {
  553. onFetchGraphs(this.graphs);
  554. return;
  555. }
  556. this.getInfo({
  557. onGetGraphs: onFetchGraphs
  558. });
  559. },
  560. fetchGraphXgmmlByName: function (name, onFetchGraphXgmml) {
  561. var idx = this.getGraphIndex(name);
  562. if (idx >= 0) {
  563. this.fetchGraphXgmml(idx, onFetchGraphXgmml);
  564. }
  565. },
  566. fetchGraphXgmml: function (idx, onFetchGraphXgmml) {
  567. this._assertHasWuid();
  568. var context = this;
  569. WsWorkunits.WUGetGraph({
  570. request: {
  571. Wuid: this.Wuid,
  572. GraphName: this.graphs[idx].Name
  573. },
  574. load: function (response) {
  575. context.graphs[idx].xgmml = response.WUGetGraphResponse.Graphs.ECLGraphEx[0].Graph;
  576. onFetchGraphXgmml(context.graphs[idx].xgmml, context.graphs[idx].svg);
  577. }
  578. });
  579. },
  580. setGraphSvg: function (graphName, svg) {
  581. var idx = this.getGraphIndex(graphName);
  582. if (idx >= 0) {
  583. this.graphs[idx].svg = svg;
  584. var appData = [];
  585. appData[graphName + "_SVG"] = svg;
  586. this.update({}, appData);
  587. }
  588. }
  589. });
  590. return {
  591. Create: function (params) {
  592. retVal = new Workunit(params);
  593. retVal.create();
  594. return retVal;
  595. },
  596. Get: function (wuid) {
  597. var store = new Store();
  598. return store.get(wuid);
  599. },
  600. CreateWUQueryObjectStore: function (options) {
  601. var store = new Store(options);
  602. store = Observable(store);
  603. var objStore = new ObjectStore({ objectStore: store });
  604. return objStore;
  605. }
  606. };
  607. });