ESPWorkunit.js 22 KB

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