data_table.js 15 KB

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