converter.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import Constants from 'yarn-ui/constants';
  2. export default {
  3. containerIdToAttemptId: function(containerId) {
  4. if (containerId) {
  5. var arr = containerId.split('_');
  6. var attemptId = ["appattempt", arr[1],
  7. arr[2], this.padding(arr[3], 6)];
  8. return attemptId.join('_');
  9. }
  10. },
  11. attemptIdToAppId: function(attemptId) {
  12. if (attemptId) {
  13. var arr = attemptId.split('_');
  14. var appId = ["application", arr[1],
  15. arr[2]].join('_');
  16. return appId;
  17. }
  18. },
  19. padding: function(str, toLen=2) {
  20. str = str.toString();
  21. if (str.length >= toLen) {
  22. return str;
  23. }
  24. return '0'.repeat(toLen - str.length) + str;
  25. },
  26. resourceToString: function(mem, cpu) {
  27. mem = Math.max(0, mem);
  28. cpu = Math.max(0, cpu);
  29. return mem + " MBs, " + cpu + " VCores";
  30. },
  31. msToElapsedTime: function(timeInMs) {
  32. var sec_num = timeInMs / 1000; // don't forget the second param
  33. var hours = Math.floor(sec_num / 3600);
  34. var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
  35. var seconds = sec_num - (hours * 3600) - (minutes * 60);
  36. var timeStrArr = [];
  37. if (hours > 0) {
  38. timeStrArr.push(hours + ' Hrs');
  39. }
  40. if (minutes > 0) {
  41. timeStrArr.push(minutes + ' Mins');
  42. }
  43. if (seconds > 0) {
  44. timeStrArr.push(Math.round(seconds) + " Secs");
  45. }
  46. return timeStrArr.join(' : ');
  47. },
  48. elapsedTimeToMs: function(elapsedTime) {
  49. elapsedTime = elapsedTime.toLowerCase();
  50. var arr = elapsedTime.split(' : ');
  51. var total = 0;
  52. for (var i = 0; i < arr.length; i++) {
  53. if (arr[i].indexOf('hr') > 0) {
  54. total += parseInt(arr[i].substring(0, arr[i].indexOf(' '))) * 3600;
  55. } else if (arr[i].indexOf('min') > 0) {
  56. total += parseInt(arr[i].substring(0, arr[i].indexOf(' '))) * 60;
  57. } else if (arr[i].indexOf('sec') > 0) {
  58. total += parseInt(arr[i].substring(0, arr[i].indexOf(' ')));
  59. }
  60. }
  61. return total * 1000;
  62. },
  63. timeStampToDate: function(timeStamp) {
  64. var dateTimeString = moment(parseInt(timeStamp)).format("YYYY/MM/DD HH:mm:ss");
  65. return dateTimeString;
  66. },
  67. dateToTimeStamp: function(date) {
  68. if (date) {
  69. var ts = moment(date, "YYYY/MM/DD HH:mm:ss").valueOf();
  70. return ts;
  71. }
  72. },
  73. splitForContainerLogs: function(id) {
  74. if (id) {
  75. var splits = id.split(Constants.PARAM_SEPARATOR);
  76. var splitLen = splits.length;
  77. if (splitLen < 3) {
  78. return null;
  79. }
  80. var fileName = splits[2];
  81. var index;
  82. for (index = 3; index < splitLen; index++) {
  83. fileName = fileName + Constants.PARAM_SEPARATOR + splits[index];
  84. }
  85. return [splits[0], splits[1], fileName];
  86. }
  87. },
  88. };