You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 4.0 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { i18n } from '~/langs';
  2. import { ACC_CARD_TYPE } from '~/const';
  3. export const getListValueWithKey = (list, key, k = 'k', v = 'v') => {
  4. for (let i = 0, iLen = list.length; i < iLen; i++) {
  5. const listI = list[i];
  6. if (listI[k] === key) return listI[v];
  7. }
  8. return key;
  9. };
  10. export const getUrlSearchParams = () => {
  11. const params = new URLSearchParams(location.search);
  12. const obj = {};
  13. params.forEach((value, key) => {
  14. obj[key] = value;
  15. });
  16. return obj;
  17. };
  18. export const transFileSize = (srcSize) => {
  19. if (null == srcSize || srcSize == '') {
  20. return '0 Bytes';
  21. }
  22. const unitArr = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  23. srcSize = parseFloat(srcSize);
  24. const index = Math.floor(Math.log(srcSize) / Math.log(1024));
  25. const size = (srcSize / Math.pow(1024, index)).toFixed(2);
  26. return size + ' ' + unitArr[index];
  27. };
  28. export const renderSpecStr = (spec, showPoint) => {
  29. if (!spec) return '';
  30. var ngpu = `${spec.ComputeResource}: ${spec.AccCardsNum + '*' + getListValueWithKey(ACC_CARD_TYPE, spec.AccCardType)}`;
  31. var gpuMemStr = spec.GPUMemGiB != 0 ? `${i18n.t('resourcesManagement.gpuMem')}: ${spec.GPUMemGiB}GB, ` : '';
  32. var sharedMemStr = spec.ShareMemGiB != 0 ? `, ${i18n.t('resourcesManagement.shareMem')}: ${spec.ShareMemGiB}GB` : '';
  33. var pointStr = showPoint ? `, ${spec.UnitPrice == 0 ? i18n.t('resourcesManagement.free') : spec.UnitPrice + i18n.t('resourcesManagement.point_hr')}` : '';
  34. var specStr = `${ngpu}, CPU: ${spec.CpuCores}, ${gpuMemStr}${i18n.t('resourcesManagement.mem')}: ${spec.MemGiB}GB${sharedMemStr}${pointStr}`;
  35. return specStr;
  36. };
  37. const Minute = 60;
  38. const Hour = 60 * Minute;
  39. const Day = 24 * Hour;
  40. const Week = 7 * Day;
  41. const Month = 30 * Day;
  42. const Year = 12 * Month;
  43. const computeTimeDiff = (diff) => {
  44. let diffStr = '';
  45. switch (true) {
  46. case diff <= 0:
  47. diff = 0;
  48. diffStr = i18n.t('timeObj.now');
  49. break;
  50. case diff < 2:
  51. diff = 0;
  52. diffStr = i18n.t('timeObj.1s');
  53. break;
  54. case diff < 1 * Minute:
  55. diffStr = i18n.t('timeObj.seconds', { msg: Math.floor(diff) });
  56. diff = 0;
  57. break;
  58. case diff < 2 * Minute:
  59. diff -= 1 * Minute;
  60. diffStr = i18n.t('timeObj.1m');
  61. break;
  62. case diff < 1 * Hour:
  63. diffStr = i18n.t('timeObj.minutes', { msg: Math.floor(diff / Minute) });
  64. diff -= diff / Minute * Minute;
  65. break;
  66. case diff < 2 * Hour:
  67. diff -= 1 * Hour;
  68. diffStr = i18n.t('timeObj.1h');
  69. break;
  70. case diff < 1 * Day:
  71. diffStr = i18n.t('timeObj.hours', { msg: Math.floor(diff / Hour) });
  72. diff -= diff / Hour * Hour;
  73. break;
  74. case diff < 2 * Day:
  75. diff -= 1 * Day;
  76. diffStr = i18n.t('timeObj.1d');
  77. break;
  78. case diff < 1 * Week:
  79. diffStr = i18n.t('timeObj.days', { msg: Math.floor(diff / Day) });
  80. diff -= diff / Day * Day;
  81. break;
  82. case diff < 2 * Week:
  83. diff -= 1 * Week;
  84. diffStr = i18n.t('timeObj.1w');
  85. break;
  86. case diff < 1 * Month:
  87. diffStr = i18n.t('timeObj.weeks', { msg: Math.floor(diff / Week) });
  88. diff -= diff / Week * Week;
  89. break;
  90. case diff < 2 * Month:
  91. diff -= 1 * Month;
  92. diffStr = i18n.t('timeObj.1mon');
  93. break;
  94. case diff < 1 * Year:
  95. diffStr = i18n.t('timeObj.months', { msg: Math.floor(diff / Month) });
  96. diff -= diff / Month * Month;
  97. break;
  98. case diff < 2 * Year:
  99. diff -= 1 * Year;
  100. diffStr = i18n.t('timeObj.1y');
  101. break;
  102. default:
  103. diffStr = i18n.t('timeObj.years', { msg: Math.floor(diff / Year) });
  104. diff -= (diff / Year) * Year;
  105. break;
  106. }
  107. return { diff, diffStr };
  108. };
  109. export const timeSinceUnix = (then, now) => {
  110. let lbl = 'timeObj.ago';
  111. let diff = now - then;
  112. if (then > now) {
  113. lbl = 'timeObj.from_now';
  114. diff = then - now;
  115. }
  116. if (diff <= 0) {
  117. return i18n.t('timeObj.now');
  118. }
  119. const out = computeTimeDiff(diff);
  120. return i18n.t(lbl, { msg: out.diffStr });
  121. };