Format.ts 5.9 KB

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