data_table.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. jQuery.extend(jQuery.fn.dataTableExt.oSort, {
  19. "num-html-pre": function(date_string) {
  20. date_string = $(date_string).text();
  21. return parseFloat(date_string, 10);
  22. },
  23. "num-html-asc": function (a, b) {
  24. return a - b;
  25. },
  26. "num-html-desc": function (a, b) {
  27. return b - a;
  28. },
  29. // @see utils/date.js
  30. "ambari-datetime-pre": function (date_string) {
  31. date_string = $.trim(date_string.replace(/<script[^>]*?>.*<\/script>/g, ''));
  32. date_string = $(date_string).attr('title');
  33. var date = date_string.substring(4);
  34. var month = date.substring(1, 4);
  35. var day = date.substring(5, 7);
  36. var year = date.substring(9, 13);
  37. var hours = date.substring(14, 16);
  38. var minutes = date.substring(17, 19);
  39. var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  40. month = months.indexOf(month);
  41. if (month < 10) month = '0' + month;
  42. return year + month + day + hours + minutes;
  43. },
  44. "ambari-datetime-asc": function (a, b) {
  45. return a - b;
  46. },
  47. "ambari-datetime-desc": function (a, b) {
  48. return b - a;
  49. },
  50. /**
  51. * Custom methods for correct bandwidth sorting
  52. */
  53. "ambari-bandwidth-pre": function (bandwidth_string) {
  54. var convertedRowValue;
  55. if (Boolean(bandwidth_string.match(/<.*>/g))) {
  56. bandwidth_string = jQuery(bandwidth_string).text();
  57. }
  58. if (bandwidth_string === '<1KB' || bandwidth_string === '&lt;1KB') {
  59. convertedRowValue = 1;
  60. } else {
  61. var rowValueScale = bandwidth_string.substr(bandwidth_string.length - 2, 2);
  62. switch (rowValueScale) {
  63. case 'KB':
  64. convertedRowValue = parseFloat(bandwidth_string)*1024;
  65. break;
  66. case 'MB':
  67. convertedRowValue = parseFloat(bandwidth_string)*1048576;
  68. break;
  69. }
  70. }
  71. return convertedRowValue;
  72. },
  73. "ambari-bandwidth-asc": function (a, b) {
  74. return a - b;
  75. },
  76. "ambari-bandwidth-desc": function (a, b) {
  77. return b - a;
  78. }
  79. });
  80. jQuery.extend(jQuery.fn.dataTableExt.oApi, {
  81. "fnFilterClear": function (oSettings) {
  82. /* Remove global filter */
  83. oSettings.oPreviousSearch.sSearch = "";
  84. /* Remove the text of the global filter in the input boxes */
  85. if (typeof oSettings.aanFeatures.f != 'undefined') {
  86. var n = oSettings.aanFeatures.f;
  87. for (var i = 0, iLen = n.length; i < iLen; i++) {
  88. $('input', n[i]).val('');
  89. }
  90. }
  91. /* Remove the search text for the column filters - NOTE - if you have input boxes for these
  92. * filters, these will need to be reset
  93. */
  94. for (var i = 0, iLen = oSettings.aoPreSearchCols.length; i < iLen; i++) {
  95. oSettings.aoPreSearchCols[i].sSearch = "";
  96. }
  97. /* Redraw */
  98. oSettings.oApi._fnReDraw(oSettings);
  99. }
  100. });
  101. jQuery.extend(jQuery.fn.dataTableExt.oApi, {
  102. "fnGetColumnData": function (oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty) {
  103. // check that we have a column id
  104. if (typeof iColumn == "undefined") return [];
  105. // by default we only wany unique data
  106. if (typeof bUnique == "undefined") bUnique = true;
  107. // by default we do want to only look at filtered data
  108. if (typeof bFiltered == "undefined") bFiltered = true;
  109. // by default we do not wany to include empty values
  110. if (typeof bIgnoreEmpty == "undefined") bIgnoreEmpty = true;
  111. // list of rows which we're going to loop through
  112. var aiRows;
  113. // use only filtered rows
  114. if (bFiltered == true) aiRows = oSettings.aiDisplay;
  115. // use all rows
  116. else aiRows = oSettings.aiDisplayMaster; // all row numbers
  117. // set up data array
  118. var asResultData = new Array();
  119. for (var i = 0, c = aiRows.length; i < c; i++) {
  120. iRow = aiRows[i];
  121. var sValue = this.fnGetData(iRow, iColumn);
  122. // ignore empty values?
  123. if (bIgnoreEmpty == true && sValue.length == 0) continue;
  124. // ignore unique values?
  125. else if (bUnique == true && jQuery.inArray(sValue, asResultData) > -1) continue;
  126. // else push the value onto the result data array
  127. else asResultData.push(sValue);
  128. }
  129. return asResultData;
  130. }
  131. });
  132. jQuery.extend($.fn.dataTableExt.afnFiltering.push(
  133. function (oSettings, aData, iDataIndex) {
  134. var inputFilters = [
  135. {iColumn: '0', elementId: 'star_filter', type: 'star'},
  136. {iColumn: '3', elementId: 'cpu_filter', type: 'number'},
  137. {iColumn: '6', elementId: 'load_avg_filter', type: 'number'},
  138. {iColumn: '5', elementId: 'user_filter', type: 'multiple'},
  139. {iColumn: '9', elementId: 'components_filter', type: 'multiple'},
  140. {iColumn: '6', elementId: 'jobs_filter', type: 'number' },
  141. {iColumn: '4', elementId: 'ram_filter', type: 'ambari-bandwidth' },
  142. {iColumn: '7', elementId: 'input_filter', type: 'ambari-bandwidth' },
  143. {iColumn: '8', elementId: 'output_filter', type: 'ambari-bandwidth' },
  144. {iColumn: '9', elementId: 'duration_filter', type: 'time' },
  145. {iColumn: '10', elementId: 'rundate_filter', type: 'ambari-datetime' }
  146. ];
  147. var match = true;
  148. for (var i = 0; i < inputFilters.length; i++) {
  149. var cellValue = jQuery('#' + inputFilters[i].elementId).val();
  150. if(cellValue === undefined){
  151. continue;
  152. }
  153. switch (inputFilters[i].type) {
  154. case 'ambari-datetime':
  155. if (cellValue !== 'Any' && match) {
  156. ambariDateFilter(cellValue, aData[inputFilters[i].iColumn]);
  157. }
  158. break;
  159. case 'number':
  160. if (cellValue && match) {
  161. numberFilter(cellValue, aData[inputFilters[i].iColumn]);
  162. }
  163. break;
  164. case 'multiple':
  165. if (cellValue && match) {
  166. multipleFilter(cellValue, aData[inputFilters[i].iColumn]);
  167. }
  168. break;
  169. case 'time':
  170. if (cellValue && match) {
  171. timeFilter(cellValue, aData[inputFilters[i].iColumn]);
  172. }
  173. break;
  174. case 'ambari-bandwidth':
  175. if (cellValue && match) {
  176. bandwidthFilter(cellValue, aData[inputFilters[i].iColumn], inputFilters[i].iColumn);
  177. }
  178. break;
  179. case 'star':
  180. if (cellValue && match) {
  181. starFilter(cellValue, aData[inputFilters[i].iColumn]);
  182. }
  183. break;
  184. }
  185. }
  186. function starFilter(d, rowValue) {
  187. match = false;
  188. if (rowValue == null) return;
  189. if (rowValue.indexOf(d) != -1) match = true;
  190. }
  191. function multipleFilter(condition, rowValue) {
  192. var options = condition.split(',');
  193. match = false;
  194. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  195. for (var i = 0; i < options.length; i++) {
  196. if (rowValue.indexOf(options[i]) !== -1) match = true;
  197. }
  198. }
  199. function timeFilter(rangeExp, rowValue) {
  200. var compareChar = rangeExp.charAt(0);
  201. var compareScale = rangeExp.charAt(rangeExp.length - 1);
  202. var compareValue = isNaN(parseInt(compareScale), 10) ? parseInt(rangeExp.substr(1, rangeExp.length - 2), 10) : parseInt(rangeExp.substr(1, rangeExp.length - 1), 10);
  203. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  204. var convertedRowValue = parseInt(rowValue.substr(0, rowValue.indexOf('secs')), 10);
  205. switch (compareScale) {
  206. case 'm':
  207. convertedRowValue /= 60;
  208. break;
  209. case 'h':
  210. convertedRowValue /= 3600;
  211. break;
  212. }
  213. match = false;
  214. switch (compareChar) {
  215. case '<':
  216. if (compareValue > convertedRowValue) match = true;
  217. break;
  218. case '>':
  219. if (compareValue < convertedRowValue) match = true;
  220. break;
  221. case '=':
  222. if (compareValue == convertedRowValue) match = true;
  223. break;
  224. default:
  225. match = false;
  226. }
  227. }
  228. function bandwidthFilter(rangeExp, rowValue, iColumn) {
  229. //rowValue = $(rowValue).text();
  230. var compareChar = isNaN(rangeExp.charAt(0)) ? rangeExp.charAt(0) : false;
  231. var compareScale = rangeExp.charAt(rangeExp.length - 1);
  232. var compareValue = compareChar ? parseFloat(rangeExp.substr(1, rangeExp.length)) : parseFloat(rangeExp.substr(0, rangeExp.length));
  233. switch (compareScale) {
  234. case 'g':
  235. compareValue *= 1073741824;
  236. break;
  237. case 'm':
  238. compareValue *= 1048576;
  239. break;
  240. case 'k':
  241. compareValue *= 1024;
  242. break;
  243. default:
  244. if (iColumn=='4') {
  245. compareValue *= 1073741824;
  246. } else {
  247. compareValue *= 1024;
  248. }
  249. }
  250. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  251. var convertedRowValue;
  252. if (rowValue === '<1KB') {
  253. convertedRowValue = 1;
  254. } else {
  255. var rowValueScale = rowValue.substr(rowValue.length - 2, 2);
  256. switch (rowValueScale) {
  257. case 'KB':
  258. convertedRowValue = parseFloat(rowValue)*1024;
  259. break;
  260. case 'MB':
  261. convertedRowValue = parseFloat(rowValue)*1048576;
  262. break;
  263. case 'GB':
  264. convertedRowValue = parseFloat(rowValue)*1073741824;
  265. break;
  266. }
  267. }
  268. match = false;
  269. switch (compareChar) {
  270. case '<':
  271. if (compareValue > convertedRowValue) match = true;
  272. break;
  273. case '>':
  274. if (compareValue < convertedRowValue) match = true;
  275. break;
  276. case false:
  277. case '=':
  278. if (compareValue == convertedRowValue) match = true;
  279. break;
  280. }
  281. }
  282. function ambariDateFilter(condition, rowValue) {
  283. if (typeof rowValue !== 'undefined') {
  284. var refinedRowValue = $.trim(rowValue.replace(/<script[^>]*?>.*<\/script>/g, ''));
  285. refinedRowValue = $(refinedRowValue).attr('title');
  286. var nowTime = new Date().getTime();
  287. var oneDayPast = nowTime - 86400000;
  288. var twoDaysPast = nowTime - 172800000;
  289. var sevenDaysPast = nowTime - 604800000;
  290. var fourteenDaysPast = nowTime - 1209600000;
  291. var thirtyDaysPast = nowTime - 2592000000;
  292. rowValue = (jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue;
  293. var lastChar = rowValue.charAt(rowValue.length - 1);
  294. rowValue = lastChar === '*' ? rowValue.substr(0, rowValue.length - 1) : rowValue;
  295. rowValue = new Date(refinedRowValue).getTime();
  296. match = false;
  297. switch (condition) {
  298. case 'Any':
  299. match = true;
  300. break;
  301. case 'Past 1 Day':
  302. if (nowTime > rowValue && rowValue > oneDayPast) match = true;
  303. break;
  304. case 'Past 2 Days':
  305. if (nowTime > rowValue && rowValue > twoDaysPast) match = true;
  306. break;
  307. case 'Past 7 Days':
  308. if (nowTime > rowValue && rowValue > sevenDaysPast) match = true;
  309. break;
  310. case 'Past 14 Days':
  311. if (nowTime > rowValue && rowValue > fourteenDaysPast) match = true;
  312. break;
  313. case 'Past 30 Days':
  314. if (nowTime > rowValue && rowValue > thirtyDaysPast) match = true;
  315. break;
  316. case 'Running Now':
  317. if (lastChar === '*') match = true;
  318. break;
  319. case 'Custom':
  320. var options = jQuery('#custom_rundate_filter').val();
  321. var optionsArray = options.split(',');
  322. if (optionsArray.length == 1) {
  323. equal = optionsArray[0];
  324. if (equal == rowValue) match = true;
  325. } else if (optionsArray.length == 2) {
  326. lowerBound = optionsArray[0];
  327. upperBound = optionsArray[1];
  328. if (upperBound > rowValue && rowValue > lowerBound) match = true;
  329. }
  330. break;
  331. }
  332. }
  333. }
  334. function numberFilter(rangeExp, rowValue) {
  335. var compareChar = rangeExp.charAt(0);
  336. var compareValue;
  337. if (rangeExp.length == 1) {
  338. if (isNaN(parseInt(compareChar))) {
  339. // User types only '=' or '>' or '<', so don't filter column values
  340. match = true;
  341. return match;
  342. }
  343. else {
  344. compareValue = parseFloat(parseFloat(rangeExp).toFixed(2));
  345. }
  346. }
  347. else {
  348. if (isNaN(parseInt(compareChar))) {
  349. compareValue = parseFloat(parseFloat(rangeExp.substr(1, rangeExp.length)).toFixed(2));
  350. }
  351. else {
  352. compareValue = parseFloat(parseFloat(rangeExp.substr(0, rangeExp.length)).toFixed(2));
  353. }
  354. }
  355. rowValue = parseFloat((jQuery(rowValue).text()) ? jQuery(rowValue).text() : rowValue);
  356. match = false;
  357. switch (compareChar) {
  358. case '<':
  359. if (compareValue > rowValue) match = true;
  360. break;
  361. case '>':
  362. if (compareValue < rowValue) match = true;
  363. break;
  364. case '=':
  365. if (compareValue == rowValue) match = true;
  366. default:
  367. if (rangeExp == rowValue) match = true;
  368. }
  369. }
  370. return match;
  371. }
  372. )
  373. );
  374. jQuery.extend(jQuery.fn.dataTableExt.oApi, {
  375. "fnFilterClear": function (oSettings) {
  376. /* Remove global filter */
  377. oSettings.oPreviousSearch.sSearch = "";
  378. /* Remove the text of the global filter in the input boxes */
  379. if (typeof oSettings.aanFeatures.f != 'undefined') {
  380. var n = oSettings.aanFeatures.f;
  381. for (var i = 0, iLen = n.length; i < iLen; i++) {
  382. $('input', n[i]).val('');
  383. }
  384. }
  385. /* Remove the search text for the column filters - NOTE - if you have input boxes for these
  386. * filters, these will need to be reset
  387. */
  388. for (var i = 0, iLen = oSettings.aoPreSearchCols.length; i < iLen; i++) {
  389. oSettings.aoPreSearchCols[i].sSearch = "";
  390. }
  391. /* Redraw */
  392. oSettings.oApi._fnReDraw(oSettings);
  393. }
  394. });
  395. jQuery.fn.dataTableExt.oApi.fnGetColumnData = function ( oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty ) {
  396. // check that we have a column id
  397. if ( typeof iColumn == "undefined" ) return [];
  398. // by default we only wany unique data
  399. if ( typeof bUnique == "undefined" ) bUnique = true;
  400. // by default we do want to only look at filtered data
  401. if ( typeof bFiltered == "undefined" ) bFiltered = true;
  402. // by default we do not wany to include empty values
  403. if ( typeof bIgnoreEmpty == "undefined" ) bIgnoreEmpty = true;
  404. // list of rows which we're going to loop through
  405. var aiRows;
  406. // use only filtered rows
  407. if (bFiltered == true) aiRows = oSettings.aiDisplay;
  408. // use all rows
  409. else aiRows = oSettings.aiDisplayMaster; // all row numbers
  410. // set up data array
  411. var asResultData = new Array();
  412. for (var i=0,c=aiRows.length; i<c; i++) {
  413. iRow = aiRows[i];
  414. var sValue = this.fnGetData(iRow, iColumn);
  415. // ignore empty values?
  416. if (bIgnoreEmpty == true && sValue.length == 0) continue;
  417. // ignore unique values?
  418. else if (bUnique == true && jQuery.inArray(sValue, asResultData) > -1) continue;
  419. // else push the value onto the result data array
  420. else asResultData.push(sValue);
  421. }
  422. return asResultData;
  423. };
  424. jQuery.fn.dataTableExt.ofnSearch['num-html'] = function ( sData ) {
  425. return sData.replace(/[\r\n]/g, " ").replace(/<.*?>/g, "");
  426. }
  427. jQuery.fn.dataTableExt.ofnSearch['ambari-bandwidth'] = function ( sData ) {
  428. return sData.replace(/[\r\n]/g, " ").replace(/<.*?>/g, "");
  429. }