Format.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import {
  2. BYTE_UNITS,
  3. DEFAULT_MILVUS_PORT,
  4. DEFAULT_PROMETHEUS_PORT,
  5. VectorTypes,
  6. } from '@/consts';
  7. import {
  8. CreateFieldType,
  9. CreateField,
  10. } from '@/pages/databases/collections/Types';
  11. import { FieldObject } from '@server/types';
  12. /**
  13. * transform large capacity to capacity in b.
  14. * @param capacity like: 10g, 10gb, 10m, 10mb, 10k, 10kb, 10b,
  15. * @return number
  16. */
  17. export const parseByte = (capacity?: string | number): number => {
  18. // if it's number return it
  19. if (!isNaN(Number(capacity))) {
  20. return capacity as number;
  21. }
  22. // capacity is '' or 0 or undefined
  23. if (!capacity) {
  24. return 0;
  25. }
  26. let lowerCapacity = (capacity as string).toLowerCase();
  27. const units = BYTE_UNITS;
  28. const isAlpha = /[a-zA-Z]/;
  29. const lastStr = lowerCapacity.charAt(lowerCapacity.length - 1);
  30. const secLastStr = lowerCapacity.charAt(lowerCapacity.length - 2);
  31. // if last two alpha is string, like: mb gb kb.
  32. // delete last alpha b
  33. if (isAlpha.test(lastStr) && isAlpha.test(secLastStr)) {
  34. lastStr === 'b' &&
  35. (lowerCapacity = lowerCapacity.slice(0, lowerCapacity.length - 1));
  36. }
  37. const suffix = lowerCapacity.charAt(lowerCapacity.length - 1);
  38. const digitsPart = lowerCapacity.slice(0, lowerCapacity.length - 1);
  39. if (units[suffix]) {
  40. return Number(digitsPart) * units[suffix];
  41. }
  42. throw new Error(
  43. 'The specified value for memory ({0}) should specify the units. The postfix should be one of the `b` `k` `m` `g` characters'
  44. );
  45. };
  46. /**
  47. *
  48. * @param search ?name=czz&age=18
  49. * @returns {name:'czz',age:'18'}
  50. */
  51. export const parseLocationSearch = (search: string) => {
  52. const pairs = search.substring(1).split('&');
  53. let obj: any = {};
  54. for (let i in pairs) {
  55. if (pairs[i] === '') continue;
  56. const pair = pairs[i].split('=');
  57. obj[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
  58. }
  59. return obj;
  60. };
  61. export const getEnumKeyByValue = (enumObj: any, enumValue: any) => {
  62. const match = Object.entries(enumObj).find(
  63. ([, value]) => value === enumValue
  64. );
  65. if (match) {
  66. const [key] = match;
  67. return key;
  68. }
  69. return '--';
  70. };
  71. /**
  72. * @param pairs e.g. [{key: 'key', value: 'value'}]
  73. * @returns object, e.g. {key: value}
  74. */
  75. export const getObjFromKeyValuePair = (
  76. pairs: { key: string; value: any }[]
  77. ): { [key in string]: any } => {
  78. const obj = pairs.reduce((acc, cur) => {
  79. acc[cur.key] = cur.value;
  80. return acc;
  81. }, {} as { [key in string]: any });
  82. return obj;
  83. };
  84. // BinarySubstructure includes Superstructure and Substructure
  85. export const checkIsBinarySubstructure = (metricLabel: string): boolean => {
  86. return metricLabel === 'Superstructure' || metricLabel === 'Substructure';
  87. };
  88. export const getCreateFieldType = (config: CreateField): CreateFieldType => {
  89. if (config.is_primary_key) {
  90. return 'primaryKey';
  91. }
  92. if (config.isDefault) {
  93. return 'defaultVector';
  94. }
  95. if (VectorTypes.includes(config.data_type)) {
  96. return 'vector';
  97. }
  98. return 'number';
  99. };
  100. // Trim the address
  101. export const formatAddress = (address: string): string => {
  102. // remove http or https prefix from address
  103. const ip = address.replace(/(http):\/\//, '');
  104. return ip.includes(':') ? ip : `${ip}:${DEFAULT_MILVUS_PORT}`;
  105. };
  106. // format the prometheus address
  107. export const formatPrometheusAddress = (address: string): string => {
  108. let formatAddress = address;
  109. // add protocal (default http)
  110. const withProtocol = address.includes('http');
  111. if (!withProtocol) formatAddress = 'http://' + formatAddress;
  112. // add port (default 9090)
  113. const withPort = address.includes(':');
  114. if (!withPort) formatAddress = formatAddress + ':' + DEFAULT_PROMETHEUS_PORT;
  115. return formatAddress;
  116. };
  117. export const formatByteSize = (
  118. size: number,
  119. capacityTrans: { [key in string]: string }
  120. ): { value: string; unit: string; power: number } => {
  121. const power = Math.round(Math.log(size) / Math.log(1024));
  122. let unit = '';
  123. switch (power) {
  124. case 1:
  125. unit = capacityTrans.kb;
  126. break;
  127. case 2:
  128. unit = capacityTrans.mb;
  129. break;
  130. case 3:
  131. unit = capacityTrans.gb;
  132. break;
  133. case 4:
  134. unit = capacityTrans.tb;
  135. break;
  136. case 5:
  137. unit = capacityTrans.pb;
  138. break;
  139. default:
  140. unit = capacityTrans.b;
  141. break;
  142. }
  143. const byteValue = size / 1024 ** power;
  144. return {
  145. value: byteValue.toFixed(2),
  146. unit,
  147. power,
  148. };
  149. };
  150. // generate a sting like 20.22/98.33MB with proper unit
  151. export const getByteString = (
  152. value1: number,
  153. value2: number,
  154. capacityTrans: { [key in string]: string }
  155. ) => {
  156. if (!value1 || !value2) return `0${capacityTrans.b}`;
  157. const formatValue1 = formatByteSize(value1, capacityTrans);
  158. const formatValue2 = value2 / 1024 ** formatValue1.power;
  159. return `${formatValue1.value}/${formatValue2.toFixed(2)} ${
  160. formatValue1.unit
  161. }`;
  162. };
  163. /**
  164. * time: 2022-02-15 07:03:32.2981238 +0000 UTC m=+2.434915801
  165. * @returns 2022-02-15 07:03:32
  166. */
  167. export const formatSystemTime = (time: string): string => {
  168. return time.split('.')[0] || '';
  169. };
  170. /**
  171. * When number is larger than js max number, transform to string by BigInt.
  172. * @param bigNumber
  173. * @returns
  174. */
  175. export const formatUtcToMilvus = (bigNumber: number) => {
  176. const milvusTimeStamp = BigInt(bigNumber) << BigInt(18);
  177. return milvusTimeStamp.toString();
  178. };
  179. /**
  180. * Format field
  181. * @param bigNumber
  182. * @returns
  183. */
  184. export const formatFieldType = (field: FieldObject) => {
  185. const { data_type, element_type, maxLength, maxCapacity, dimension } = field;
  186. const elementType =
  187. element_type !== 'None'
  188. ? `<${element_type}${maxLength !== -1 ? `(${maxLength})` : ''}>`
  189. : '';
  190. const maxCap = maxCapacity !== -1 ? `[${maxCapacity}]` : '';
  191. const dim = dimension !== -1 ? `(${dimension})` : '';
  192. const maxLn = data_type === 'VarChar' ? `(${maxLength})` : '';
  193. return `${data_type}${elementType}${maxCap}${dim}${maxLn}`;
  194. };