jobs_controller.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. }.observes('content.length', 'content.@each.id', 'content.@each.startTime', 'content.@each.endTime', 'sortProperties', 'sortAscending'),
  42. contentAndSortUpdater: function() {
  43. this.set('sortingDone', false);
  44. var content = this.get('content');
  45. var sortedContent = content.toArray();
  46. var sortProperty = this.get('sortProperty');
  47. var sortAscending = this.get('sortAscending');
  48. sortedContent.sort(function(r1, r2) {
  49. var r1id = r1.get(sortProperty);
  50. var r2id = r2.get(sortProperty);
  51. if (r1id < r2id)
  52. return sortAscending ? -1 : 1;
  53. if (r1id > r2id)
  54. return sortAscending ? 1 : -1;
  55. return 0;
  56. });
  57. var sortedArray = this.get('sortedContent');
  58. var count = 0;
  59. sortedContent.forEach(function(sortedJob){
  60. if(sortedArray.length <= count) {
  61. sortedArray.pushObject(Ember.Object.create());
  62. }
  63. sortedArray[count].set('failed', sortedJob.get('failed'));
  64. sortedArray[count].set('hasTezDag', sortedJob.get('hasTezDag'));
  65. sortedArray[count].set('queryText', sortedJob.get('queryText'));
  66. sortedArray[count].set('name', sortedJob.get('name'));
  67. sortedArray[count].set('user', sortedJob.get('user'));
  68. sortedArray[count].set('id', sortedJob.get('id'));
  69. sortedArray[count].set('startTimeDisplay', sortedJob.get('startTimeDisplay'));
  70. sortedArray[count].set('endTimeDisplay', sortedJob.get('endTimeDisplay'));
  71. sortedArray[count].set('durationDisplay', sortedJob.get('durationDisplay'));
  72. count ++;
  73. });
  74. if(sortedArray.length > count) {
  75. for(var c = sortedArray.length-1; c >= count; c--){
  76. sortedArray.removeObject(sortedArray[c]);
  77. }
  78. }
  79. sortedContent.length = 0;
  80. this.set('sortingDone', true);
  81. },
  82. navIDs: {
  83. backIDs: [],
  84. nextID: ''
  85. },
  86. lastJobID: '',
  87. hasNewJobs: false,
  88. loaded : false,
  89. loading : false,
  90. resetPagination: false,
  91. loadJobsTimeout: null,
  92. loadTimeout: null,
  93. jobsUpdateInterval: 6000,
  94. jobsUpdate: null,
  95. sortingColumn: null,
  96. sortProperty: 'id',
  97. sortAscending: true,
  98. sortingDone: true,
  99. sortingColumnObserver: function () {
  100. if(this.get('sortingColumn')){
  101. this.set('sortProperty', this.get('sortingColumn').get('name'));
  102. this.set('sortAscending', this.get('sortingColumn').get('status') == "sorting_desc" ? false : true );
  103. }
  104. }.observes('sortingColumn.name','sortingColumn.status'),
  105. updateJobsByClick: function () {
  106. this.set('navIDs.backIDs', []);
  107. this.set('navIDs.nextID', '');
  108. this.get('filterObject').set('nextFromId', '');
  109. this.get('filterObject').set('backFromId', '');
  110. this.get('filterObject').set('fromTs', '');
  111. this.set('hasNewJobs', false);
  112. this.set('resetPagination', true);
  113. this.loadJobs();
  114. },
  115. updateJobs: function (controllerName, funcName) {
  116. clearInterval(this.get('jobsUpdate'));
  117. var self = this;
  118. var interval = setInterval(function () {
  119. App.router.get(controllerName)[funcName]();
  120. }, this.jobsUpdateInterval);
  121. this.set('jobsUpdate', interval);
  122. },
  123. totalOfJobs: 0,
  124. setTotalOfJobs: function () {
  125. if(this.get('totalOfJobs') < this.get('content.length')){
  126. this.set('totalOfJobs', this.get('content.length'));
  127. }
  128. }.observes('content.length'),
  129. filterObject: Ember.Object.create({
  130. id: "",
  131. isIdFilterApplied: false,
  132. jobsLimit: 10,
  133. user: "",
  134. windowStart: "",
  135. windowEnd: "",
  136. nextFromId: "",
  137. backFromId: "",
  138. fromTs: "",
  139. isAnyFilterApplied: false,
  140. onApplyIdFilter: function () {
  141. if(this.get('id') == ""){
  142. this.set('isIdFilterApplied', false);
  143. }else{
  144. this.set('isIdFilterApplied', true);
  145. }
  146. }.observes('id'),
  147. /**
  148. * Direct binding to startTime filter field
  149. */
  150. startTime: "",
  151. onStartTimeChange:function(){
  152. var time = "";
  153. var curTime = new Date().getTime();
  154. switch (this.get('startTime')) {
  155. case 'Past 1 hour':
  156. time = curTime - 3600000;
  157. break;
  158. case 'Past 1 Day':
  159. time = curTime - 86400000;
  160. break;
  161. case 'Past 2 Days':
  162. time = curTime - 172800000;
  163. break;
  164. case 'Past 7 Days':
  165. time = curTime - 604800000;
  166. break;
  167. case 'Past 14 Days':
  168. time = curTime - 1209600000;
  169. break;
  170. case 'Past 30 Days':
  171. time = curTime - 2592000000;
  172. break;
  173. case 'Custom':
  174. this.showCustomDatePopup();
  175. break;
  176. case 'Any':
  177. time = "";
  178. break;
  179. }
  180. if(this.get('startTime') != "Custom"){
  181. this.set("windowStart", time);
  182. this.set("windowEnd", "");
  183. }
  184. }.observes("startTime"),
  185. // Fields values from Select Custom Dates form
  186. customDateFormFields: Ember.Object.create({
  187. startDate: null,
  188. hoursForStart: null,
  189. minutesForStart: null,
  190. middayPeriodForStart: null,
  191. endDate: null,
  192. hoursForEnd: null,
  193. minutesForEnd: null,
  194. middayPeriodForEnd: null
  195. }),
  196. errors: Ember.Object.create({
  197. isStartDateError: false,
  198. isEndDateError: false
  199. }),
  200. errorMessages: Ember.Object.create({
  201. startDate: '',
  202. endDate: ''
  203. }),
  204. showCustomDatePopup: function () {
  205. var self = this;
  206. var windowEnd = "";
  207. var windowStart = "";
  208. App.ModalPopup.show({
  209. header: Em.I18n.t('jobs.table.custom.date.header'),
  210. onPrimary: function () {
  211. self.validate();
  212. if(self.get('errors.isStartDateError') || self.get('errors.isEndDateError')){
  213. return false;
  214. }
  215. var windowStart = self.createCustomStartDate();
  216. var windowEnd = self.createCustomEndDate();
  217. self.set("windowStart", windowStart.getTime());
  218. self.set("windowEnd", windowEnd.getTime());
  219. this.hide();
  220. },
  221. onSecondary: function () {
  222. self.set('startTime','Any');
  223. this.hide();
  224. },
  225. bodyClass: App.JobsCustomDatesSelectView.extend({
  226. controller: self
  227. })
  228. });
  229. },
  230. createCustomStartDate : function () {
  231. var startDate = this.get('customDateFormFields.startDate');
  232. var hoursForStart = this.get('customDateFormFields.hoursForStart');
  233. var minutesForStart = this.get('customDateFormFields.minutesForStart');
  234. var middayPeriodForStart = this.get('customDateFormFields.middayPeriodForStart');
  235. if (startDate && hoursForStart && minutesForStart && middayPeriodForStart) {
  236. return new Date(startDate + ' ' + hoursForStart + ':' + minutesForStart + ' ' + middayPeriodForStart);
  237. }
  238. return null;
  239. },
  240. createCustomEndDate : function () {
  241. var endDate = this.get('customDateFormFields.endDate');
  242. var hoursForEnd = this.get('customDateFormFields.hoursForEnd');
  243. var minutesForEnd = this.get('customDateFormFields.minutesForEnd');
  244. var middayPeriodForEnd = this.get('customDateFormFields.middayPeriodForEnd');
  245. if (endDate && hoursForEnd && minutesForEnd && middayPeriodForEnd) {
  246. return new Date(endDate + ' ' + hoursForEnd + ':' + minutesForEnd + ' ' + middayPeriodForEnd);
  247. }
  248. return null;
  249. },
  250. clearErrors: function () {
  251. var errorMessages = this.get('errorMessages');
  252. Em.keys(errorMessages).forEach(function (key) {
  253. errorMessages.set(key, '');
  254. }, this);
  255. var errors = this.get('errors');
  256. Em.keys(errors).forEach(function (key) {
  257. errors.set(key, false);
  258. }, this);
  259. },
  260. // Validation for every field in customDateFormFields
  261. validate: function () {
  262. var formFields = this.get('customDateFormFields');
  263. var errors = this.get('errors');
  264. var errorMessages = this.get('errorMessages');
  265. this.clearErrors();
  266. // Check if feild is empty
  267. Em.keys(errorMessages).forEach(function (key) {
  268. if (!formFields.get(key)) {
  269. errors.set('is' + key.capitalize() + 'Error', true);
  270. errorMessages.set(key, Em.I18n.t('jobs.customDateFilter.error.required'));
  271. }
  272. }, this);
  273. // Check that endDate is after startDate
  274. var startDate = this.createCustomStartDate();
  275. var endDate = this.createCustomEndDate();
  276. if (startDate && endDate && (startDate > endDate)) {
  277. errors.set('isEndDateError', true);
  278. errorMessages.set('endDate', Em.I18n.t('jobs.customDateFilter.error.date.order'));
  279. }
  280. },
  281. /**
  282. * Create link for server request
  283. * @return {String}
  284. */
  285. createJobsFiltersLink: function() {
  286. var link = "?fields=events,primaryfilters,otherinfo";
  287. var numberOfAppliedFilters = 0;
  288. if(this.get("id") !== "") {
  289. link = "/" + this.get("id") + link;
  290. numberOfAppliedFilters++;
  291. }
  292. link += "&limit=" + (parseInt(this.get("jobsLimit")) + 1);
  293. if(this.get("user") !== ""){
  294. link += "&primaryFilter=user:" + this.get("user");
  295. numberOfAppliedFilters++;
  296. }
  297. if(this.get("backFromId") != ""){
  298. link += "&fromId=" + this.get("backFromId");
  299. }
  300. if(this.get("nextFromId") != ""){
  301. link += "&fromId=" + this.get("nextFromId");
  302. }
  303. if(this.get("fromTs") != ""){
  304. link += "&fromTs=" + this.get("fromTs");
  305. }
  306. if(this.get("startTime") !== "" && this.get("startTime") !== "Any"){
  307. link += this.get("windowStart") !== "" ? ("&windowStart=" + this.get("windowStart")) : "";
  308. link += this.get("windowEnd") !== "" ? ("&windowEnd=" + this.get("windowEnd")) : "";
  309. numberOfAppliedFilters++;
  310. }
  311. if(numberOfAppliedFilters > 0){
  312. this.set('isAnyFilterApplied', true);
  313. }else{
  314. this.set('isAnyFilterApplied', false);
  315. }
  316. return link;
  317. }
  318. }),
  319. columnsName: Ember.ArrayController.create({
  320. content: [
  321. { name: Em.I18n.t('jobs.column.id'), index: 0 },
  322. { name: Em.I18n.t('jobs.column.user'), index: 1 },
  323. { name: Em.I18n.t('jobs.column.start.time'), index: 2 },
  324. { name: Em.I18n.t('jobs.column.end.time'), index: 3 },
  325. { name: Em.I18n.t('jobs.column.duration'), index: 4 }
  326. ]
  327. }),
  328. lastIDSuccessCallback: function(data, jqXHR, textStatus) {
  329. var lastReceivedID = data.entities[0].entity;
  330. if(this.get('lastJobID') == '') {
  331. this.set('lastJobID', lastReceivedID);
  332. } else if (this.get('lastJobID') !== lastReceivedID) {
  333. this.set('lastJobID', lastReceivedID);
  334. this.set('hasNewJobs', true);
  335. }
  336. },
  337. lastIDErrorCallback: function(data, jqXHR, textStatus) {
  338. console.debug(jqXHR);
  339. },
  340. loadJobs : function() {
  341. var self = this;
  342. var timeout = this.get('loadTimeout');
  343. var yarnService = App.YARNService.find().objectAt(0);
  344. if (yarnService != null) {
  345. this.set('loading', true);
  346. var historyServerHostName = yarnService.get('appTimelineServerNode.hostName');
  347. var filtersLink = this.get('filterObject').createJobsFiltersLink();
  348. var hiveQueriesUrl = App.testMode ? "/data/jobs/hive-queries.json" : "/proxy?url=http://" + historyServerHostName
  349. + ":" + yarnService.get('ahsWebPort') + "/ws/v1/timeline/HIVE_QUERY_ID" + filtersLink;
  350. App.ajax.send({
  351. name: 'jobs.lastID',
  352. sender: self,
  353. data: {
  354. historyServerHostName: historyServerHostName,
  355. ahsWebPort: yarnService.get('ahsWebPort')
  356. },
  357. success: 'lastIDSuccessCallback',
  358. error : 'lastIDErrorCallback'
  359. }),
  360. App.HttpClient.get(hiveQueriesUrl, App.hiveJobsMapper, {
  361. complete : function(data, jqXHR, textStatus) {
  362. self.set('loading', false);
  363. if(self.get('loaded') == false || self.get('resetPagination') == true){
  364. self.initializePagination();
  365. self.set('resetPagination', false);
  366. }
  367. self.set('loaded', true);
  368. }
  369. }, function (jqXHR, textStatus) {
  370. App.hiveJobsMapper.map({entities : []});
  371. });
  372. }else{
  373. clearTimeout(timeout);
  374. timeout = setTimeout(function(){
  375. self.loadJobs();
  376. }, 300);
  377. }
  378. },
  379. initializePagination: function() {
  380. var back_link_IDs = this.get('navIDs.backIDs.[]');
  381. if(!back_link_IDs.contains(App.HiveJob.find().objectAt(0).get('id'))) {
  382. back_link_IDs.push(App.HiveJob.find().objectAt(0).get('id'));
  383. }
  384. this.set('filterObject.backFromId', App.HiveJob.find().objectAt(0).get('id'));
  385. this.get('filterObject').set('fromTs', App.get('currentServerTime'));
  386. },
  387. navigateNext: function() {
  388. this.set("filterObject.backFromId", '');
  389. var back_link_IDs = this.get('navIDs.backIDs.[]');
  390. var lastBackID = this.get('navIDs.nextID');
  391. if(!back_link_IDs.contains(lastBackID)) {
  392. back_link_IDs.push(lastBackID);
  393. }
  394. this.set('navIDs.backIDs.[]', back_link_IDs);
  395. this.set("filterObject.nextFromId", this.get('navIDs.nextID'));
  396. this.set('navIDs.nextID', '');
  397. this.loadJobs();
  398. },
  399. navigateBack: function() {
  400. this.set("filterObject.nextFromId", '');
  401. var back_link_IDs = this.get('navIDs.backIDs.[]');
  402. back_link_IDs.pop();
  403. var lastBackID = back_link_IDs[back_link_IDs.length - 1]
  404. this.set('navIDs.backIDs.[]', back_link_IDs);
  405. this.set("filterObject.backFromId", lastBackID);
  406. this.loadJobs();
  407. },
  408. refreshLoadedJobs : function() {
  409. var timeout = this.get('loadJobsTimeout');
  410. var self = this;
  411. clearTimeout(timeout);
  412. timeout = setTimeout(function(){
  413. self.loadJobs();
  414. }, 300);
  415. this.set('loadJobsTimeout', timeout);
  416. }.observes(
  417. 'filterObject.id',
  418. 'filterObject.jobsLimit',
  419. 'filterObject.user',
  420. 'filterObject.windowStart',
  421. 'filterObject.windowEnd'
  422. )
  423. })