jobs_controller.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. App.MainJobsController = Em.Controller.extend({
  20. /*
  21. * https://github.com/emberjs/ember.js/issues/1221 prevents this controller
  22. * from being an Ember.ArrayController. Doing so will keep the UI flashing
  23. * whenever any of the 'sortProperties' or 'sortAscending' properties are set.
  24. *
  25. * To bypass this issue this controller will be a regular controller. Also,
  26. * for memory-leak issues and sorting purposes, we are decoupling the backend
  27. * model and the UI model. There will be simple Ember POJOs for the UI which
  28. * will be periodically updated from backend Jobs model.
  29. */
  30. name:'mainJobsController',
  31. /**
  32. * Unsorted ArrayProxy
  33. */
  34. content: App.HiveJob.find(),
  35. /**
  36. * Sorted ArrayProxy
  37. */
  38. sortedContent: [],
  39. contentAndSortObserver : function() {
  40. Ember.run.once(this, 'contentAndSortUpdater');
  41. if(App.HiveJob.find().findProperty('id', this.get('lastJobID'))) {
  42. this.set('hasNewJobs', false);
  43. }
  44. }.observes('content.length', 'content.@each.id', 'content.@each.startTime', 'content.@each.endTime', 'sortProperties', 'sortAscending'),
  45. contentAndSortUpdater: function() {
  46. this.set('sortingDone', false);
  47. var content = this.get('content');
  48. var sortedContent = content.toArray();
  49. var sortProperty = this.get('sortProperty');
  50. var sortAscending = this.get('sortAscending');
  51. sortedContent.sort(function(r1, r2) {
  52. var r1id = r1.get(sortProperty);
  53. var r2id = r2.get(sortProperty);
  54. if (r1id < r2id)
  55. return sortAscending ? -1 : 1;
  56. if (r1id > r2id)
  57. return sortAscending ? 1 : -1;
  58. return 0;
  59. });
  60. var sortedArray = this.get('sortedContent');
  61. var count = 0;
  62. sortedContent.forEach(function(sortedJob){
  63. if(sortedArray.length <= count) {
  64. sortedArray.pushObject(Ember.Object.create());
  65. }
  66. sortedArray[count].set('failed', sortedJob.get('failed'));
  67. sortedArray[count].set('hasTezDag', sortedJob.get('hasTezDag'));
  68. sortedArray[count].set('queryText', sortedJob.get('queryText'));
  69. sortedArray[count].set('name', sortedJob.get('name'));
  70. sortedArray[count].set('user', sortedJob.get('user'));
  71. sortedArray[count].set('id', sortedJob.get('id'));
  72. sortedArray[count].set('startTimeDisplay', sortedJob.get('startTimeDisplay'));
  73. sortedArray[count].set('endTimeDisplay', sortedJob.get('endTimeDisplay'));
  74. sortedArray[count].set('durationDisplay', sortedJob.get('durationDisplay'));
  75. count ++;
  76. });
  77. if(sortedArray.length > count) {
  78. for(var c = sortedArray.length-1; c >= count; c--){
  79. sortedArray.removeObject(sortedArray[c]);
  80. }
  81. }
  82. sortedContent.length = 0;
  83. this.set('sortingDone', true);
  84. },
  85. navIDs: {
  86. backIDs: [],
  87. nextID: ''
  88. },
  89. lastJobID: '',
  90. hasNewJobs: false,
  91. loaded : false,
  92. loading : false,
  93. resetPagination: false,
  94. loadJobsTimeout: null,
  95. loadTimeout: null,
  96. jobsUpdateInterval: 6000,
  97. jobsUpdate: null,
  98. sortingColumn: null,
  99. sortProperty: 'id',
  100. sortAscending: true,
  101. sortingDone: true,
  102. jobsMessage: Em.I18n.t('jobs.loadingTasks'),
  103. sortingColumnObserver: function () {
  104. if(this.get('sortingColumn')){
  105. this.set('sortProperty', this.get('sortingColumn').get('name'));
  106. this.set('sortAscending', this.get('sortingColumn').get('status') == "sorting_desc" ? false : true );
  107. }
  108. }.observes('sortingColumn.name','sortingColumn.status'),
  109. updateJobsByClick: function () {
  110. this.set('navIDs.backIDs', []);
  111. this.set('navIDs.nextID', '');
  112. this.get('filterObject').set('nextFromId', '');
  113. this.get('filterObject').set('backFromId', '');
  114. this.get('filterObject').set('fromTs', '');
  115. this.set('hasNewJobs', false);
  116. this.set('resetPagination', true);
  117. this.loadJobs();
  118. },
  119. updateJobs: function (controllerName, funcName) {
  120. clearInterval(this.get('jobsUpdate'));
  121. var self = this;
  122. var interval = setInterval(function () {
  123. App.router.get(controllerName)[funcName]();
  124. }, this.jobsUpdateInterval);
  125. this.set('jobsUpdate', interval);
  126. },
  127. totalOfJobs: 0,
  128. setTotalOfJobs: function () {
  129. if(this.get('totalOfJobs') < this.get('content.length')){
  130. this.set('totalOfJobs', this.get('content.length'));
  131. }
  132. }.observes('content.length'),
  133. filterObject: Ember.Object.create({
  134. id: "",
  135. isIdFilterApplied: false,
  136. jobsLimit: 10,
  137. user: "",
  138. windowStart: "",
  139. windowEnd: "",
  140. nextFromId: "",
  141. backFromId: "",
  142. fromTs: "",
  143. isAnyFilterApplied: false,
  144. onApplyIdFilter: function () {
  145. if(this.get('id') == ""){
  146. this.set('isIdFilterApplied', false);
  147. }else{
  148. this.set('isIdFilterApplied', true);
  149. }
  150. }.observes('id'),
  151. /**
  152. * Direct binding to startTime filter field
  153. */
  154. startTime: "",
  155. onStartTimeChange:function(){
  156. var time = "";
  157. var curTime = new Date().getTime();
  158. switch (this.get('startTime')) {
  159. case 'Past 1 hour':
  160. time = curTime - 3600000;
  161. break;
  162. case 'Past 1 Day':
  163. time = curTime - 86400000;
  164. break;
  165. case 'Past 2 Days':
  166. time = curTime - 172800000;
  167. break;
  168. case 'Past 7 Days':
  169. time = curTime - 604800000;
  170. break;
  171. case 'Past 14 Days':
  172. time = curTime - 1209600000;
  173. break;
  174. case 'Past 30 Days':
  175. time = curTime - 2592000000;
  176. break;
  177. case 'Custom':
  178. this.showCustomDatePopup();
  179. break;
  180. case 'Any':
  181. time = "";
  182. break;
  183. }
  184. if(this.get('startTime') != "Custom"){
  185. this.set("windowStart", time);
  186. this.set("windowEnd", "");
  187. }
  188. }.observes("startTime"),
  189. // Fields values from Select Custom Dates form
  190. customDateFormFields: Ember.Object.create({
  191. startDate: null,
  192. hoursForStart: null,
  193. minutesForStart: null,
  194. middayPeriodForStart: null,
  195. endDate: null,
  196. hoursForEnd: null,
  197. minutesForEnd: null,
  198. middayPeriodForEnd: null
  199. }),
  200. errors: Ember.Object.create({
  201. isStartDateError: false,
  202. isEndDateError: false
  203. }),
  204. errorMessages: Ember.Object.create({
  205. startDate: '',
  206. endDate: ''
  207. }),
  208. showCustomDatePopup: function () {
  209. var self = this;
  210. var windowEnd = "";
  211. var windowStart = "";
  212. App.ModalPopup.show({
  213. header: Em.I18n.t('jobs.table.custom.date.header'),
  214. onPrimary: function () {
  215. self.validate();
  216. if(self.get('errors.isStartDateError') || self.get('errors.isEndDateError')){
  217. return false;
  218. }
  219. var windowStart = self.createCustomStartDate();
  220. var windowEnd = self.createCustomEndDate();
  221. self.set("windowStart", windowStart.getTime());
  222. self.set("windowEnd", windowEnd.getTime());
  223. this.hide();
  224. },
  225. onSecondary: function () {
  226. self.set('startTime','Any');
  227. this.hide();
  228. },
  229. bodyClass: App.JobsCustomDatesSelectView.extend({
  230. controller: self
  231. })
  232. });
  233. },
  234. createCustomStartDate : function () {
  235. var startDate = this.get('customDateFormFields.startDate');
  236. var hoursForStart = this.get('customDateFormFields.hoursForStart');
  237. var minutesForStart = this.get('customDateFormFields.minutesForStart');
  238. var middayPeriodForStart = this.get('customDateFormFields.middayPeriodForStart');
  239. if (startDate && hoursForStart && minutesForStart && middayPeriodForStart) {
  240. return new Date(startDate + ' ' + hoursForStart + ':' + minutesForStart + ' ' + middayPeriodForStart);
  241. }
  242. return null;
  243. },
  244. createCustomEndDate : function () {
  245. var endDate = this.get('customDateFormFields.endDate');
  246. var hoursForEnd = this.get('customDateFormFields.hoursForEnd');
  247. var minutesForEnd = this.get('customDateFormFields.minutesForEnd');
  248. var middayPeriodForEnd = this.get('customDateFormFields.middayPeriodForEnd');
  249. if (endDate && hoursForEnd && minutesForEnd && middayPeriodForEnd) {
  250. return new Date(endDate + ' ' + hoursForEnd + ':' + minutesForEnd + ' ' + middayPeriodForEnd);
  251. }
  252. return null;
  253. },
  254. clearErrors: function () {
  255. var errorMessages = this.get('errorMessages');
  256. Em.keys(errorMessages).forEach(function (key) {
  257. errorMessages.set(key, '');
  258. }, this);
  259. var errors = this.get('errors');
  260. Em.keys(errors).forEach(function (key) {
  261. errors.set(key, false);
  262. }, this);
  263. },
  264. // Validation for every field in customDateFormFields
  265. validate: function () {
  266. var formFields = this.get('customDateFormFields');
  267. var errors = this.get('errors');
  268. var errorMessages = this.get('errorMessages');
  269. this.clearErrors();
  270. // Check if feild is empty
  271. Em.keys(errorMessages).forEach(function (key) {
  272. if (!formFields.get(key)) {
  273. errors.set('is' + key.capitalize() + 'Error', true);
  274. errorMessages.set(key, Em.I18n.t('jobs.customDateFilter.error.required'));
  275. }
  276. }, this);
  277. // Check that endDate is after startDate
  278. var startDate = this.createCustomStartDate();
  279. var endDate = this.createCustomEndDate();
  280. if (startDate && endDate && (startDate > endDate)) {
  281. errors.set('isEndDateError', true);
  282. errorMessages.set('endDate', Em.I18n.t('jobs.customDateFilter.error.date.order'));
  283. }
  284. },
  285. /**
  286. * Create link for server request
  287. * @return {String}
  288. */
  289. createJobsFiltersLink: function() {
  290. var link = "?fields=events,primaryfilters,otherinfo&secondaryFilter=tez:true";
  291. var numberOfAppliedFilters = 0;
  292. if(this.get("id") !== "") {
  293. link = "/" + this.get("id") + link;
  294. numberOfAppliedFilters++;
  295. }
  296. link += "&limit=" + (parseInt(this.get("jobsLimit")) + 1);
  297. if(this.get("user") !== ""){
  298. link += "&primaryFilter=user:" + this.get("user");
  299. numberOfAppliedFilters++;
  300. }
  301. if(this.get("backFromId") != ""){
  302. link += "&fromId=" + this.get("backFromId");
  303. }
  304. if(this.get("nextFromId") != ""){
  305. link += "&fromId=" + this.get("nextFromId");
  306. }
  307. if(this.get("fromTs") != ""){
  308. link += "&fromTs=" + this.get("fromTs");
  309. }
  310. if(this.get("startTime") !== "" && this.get("startTime") !== "Any"){
  311. link += this.get("windowStart") !== "" ? ("&windowStart=" + this.get("windowStart")) : "";
  312. link += this.get("windowEnd") !== "" ? ("&windowEnd=" + this.get("windowEnd")) : "";
  313. numberOfAppliedFilters++;
  314. }
  315. if(numberOfAppliedFilters > 0){
  316. this.set('isAnyFilterApplied', true);
  317. }else{
  318. this.set('isAnyFilterApplied', false);
  319. }
  320. return link;
  321. }
  322. }),
  323. columnsName: Ember.ArrayController.create({
  324. content: [
  325. { name: Em.I18n.t('jobs.column.id'), index: 0 },
  326. { name: Em.I18n.t('jobs.column.user'), index: 1 },
  327. { name: Em.I18n.t('jobs.column.start.time'), index: 2 },
  328. { name: Em.I18n.t('jobs.column.end.time'), index: 3 },
  329. { name: Em.I18n.t('jobs.column.duration'), index: 4 }
  330. ]
  331. }),
  332. lastIDSuccessCallback: function(data, jqXHR, textStatus) {
  333. var lastReceivedID = data.entities[0].entity;
  334. if(this.get('lastJobID') == '') {
  335. this.set('lastJobID', lastReceivedID);
  336. } else if (this.get('lastJobID') !== lastReceivedID) {
  337. this.set('lastJobID', lastReceivedID);
  338. if(!App.HiveJob.find().findProperty('id', lastReceivedID)) {
  339. this.set('hasNewJobs', true);
  340. }
  341. }
  342. },
  343. lastIDErrorCallback: function(data, jqXHR, textStatus) {
  344. console.debug(jqXHR);
  345. },
  346. checkDataLoadingError: function (jqXHR){
  347. var atsComponent = App.HostComponent.find().findProperty('componentName','APP_TIMELINE_SERVER');
  348. if(atsComponent && atsComponent.get('workStatus') != "STARTED") {
  349. this.set('jobsMessage', Em.I18n.t('jobs.error.ats.down'));
  350. return true;
  351. }else if (jqXHR && jqXHR.status == 400) {
  352. this.set('jobsMessage', Em.I18n.t('jobs.error.400'));
  353. }else{
  354. this.set('jobsMessage', Em.I18n.t('jobs.loadingTasks'));
  355. }
  356. return false;
  357. },
  358. loadJobs : function() {
  359. var self = this;
  360. var timeout = this.get('loadTimeout');
  361. var yarnService = App.YARNService.find().objectAt(0);
  362. var retryLoad = this.checkDataLoadingError();
  363. if (yarnService != null && !retryLoad) {
  364. this.set('loading', true);
  365. var historyServerHostName = yarnService.get('appTimelineServerNode.hostName');
  366. var filtersLink = this.get('filterObject').createJobsFiltersLink();
  367. var hiveQueriesUrl = App.testMode ? "/data/jobs/hive-queries.json" : "/proxy?url=http://" + historyServerHostName
  368. + ":" + yarnService.get('ahsWebPort') + "/ws/v1/timeline/HIVE_QUERY_ID" + filtersLink;
  369. App.ajax.send({
  370. name: 'jobs.lastID',
  371. sender: self,
  372. data: {
  373. historyServerHostName: historyServerHostName,
  374. ahsWebPort: yarnService.get('ahsWebPort')
  375. },
  376. success: 'lastIDSuccessCallback',
  377. error : 'lastIDErrorCallback'
  378. }),
  379. App.HttpClient.get(hiveQueriesUrl, App.hiveJobsMapper, {
  380. complete : function(data, jqXHR, textStatus) {
  381. self.set('loading', false);
  382. if(self.get('loaded') == false || self.get('resetPagination') == true){
  383. self.initializePagination();
  384. self.set('resetPagination', false);
  385. }
  386. self.set('loaded', true);
  387. }
  388. }, function (jqXHR, textStatus) {
  389. self.checkDataLoadingError(jqXHR);
  390. App.hiveJobsMapper.map({entities : []});
  391. });
  392. }else{
  393. clearTimeout(timeout);
  394. timeout = setTimeout(function(){
  395. self.loadJobs();
  396. }, 300);
  397. }
  398. },
  399. initializePagination: function() {
  400. var back_link_IDs = this.get('navIDs.backIDs.[]');
  401. if(!back_link_IDs.contains(this.get('lastJobID'))) {
  402. back_link_IDs.push(this.get('lastJobID'));
  403. }
  404. this.set('filterObject.backFromId', this.get('lastJobID'));
  405. this.get('filterObject').set('fromTs', App.dateTime());
  406. },
  407. navigateNext: function() {
  408. this.set("filterObject.backFromId", '');
  409. var back_link_IDs = this.get('navIDs.backIDs.[]');
  410. var lastBackID = this.get('navIDs.nextID');
  411. if(!back_link_IDs.contains(lastBackID)) {
  412. back_link_IDs.push(lastBackID);
  413. }
  414. this.set('navIDs.backIDs.[]', back_link_IDs);
  415. this.set("filterObject.nextFromId", this.get('navIDs.nextID'));
  416. this.set('navIDs.nextID', '');
  417. this.loadJobs();
  418. },
  419. navigateBack: function() {
  420. this.set("filterObject.nextFromId", '');
  421. var back_link_IDs = this.get('navIDs.backIDs.[]');
  422. back_link_IDs.pop();
  423. var lastBackID = back_link_IDs[back_link_IDs.length - 1]
  424. this.set('navIDs.backIDs.[]', back_link_IDs);
  425. this.set("filterObject.backFromId", lastBackID);
  426. this.loadJobs();
  427. },
  428. refreshLoadedJobs : function() {
  429. var timeout = this.get('loadJobsTimeout');
  430. var self = this;
  431. clearTimeout(timeout);
  432. timeout = setTimeout(function(){
  433. self.loadJobs();
  434. }, 300);
  435. this.set('loadJobsTimeout', timeout);
  436. }.observes(
  437. 'filterObject.id',
  438. 'filterObject.jobsLimit',
  439. 'filterObject.user',
  440. 'filterObject.windowStart',
  441. 'filterObject.windowEnd'
  442. )
  443. })