Helper.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { KeyValuePair } from '@zilliz/milvus2-sdk-node/dist/milvus/types/Common';
  2. import { FieldSchema } from '@zilliz/milvus2-sdk-node/dist/milvus/types/Response';
  3. export const findKeyValue = (obj: KeyValuePair[], key: string) =>
  4. obj.find(v => v.key === key)?.value;
  5. export const genDataByType = ({ data_type, type_params }: FieldSchema) => {
  6. switch (data_type) {
  7. case 'Bool':
  8. return Math.random() > 0.5;
  9. case 'Int8':
  10. return Math.floor(Math.random() * 127);
  11. case 'Int16':
  12. return Math.floor(Math.random() * 32767);
  13. case 'Int32':
  14. return Math.floor(Math.random() * 214748364);
  15. case 'Int64':
  16. return Math.floor(Math.random() * 214748364);
  17. case 'FloatVector':
  18. return Array.from({ length: (type_params as any)[0].value }).map(() =>
  19. Math.random()
  20. );
  21. case 'BinaryVector':
  22. return Array.from({ length: (type_params as any)[0].value / 8 }).map(() =>
  23. Math.random() > 0.5 ? 1 : 0
  24. );
  25. case 'VarChar':
  26. return makeRandomId((type_params as any)[0].value);
  27. }
  28. };
  29. export const genRow = (fields: FieldSchema[]) => {
  30. const result: any = {};
  31. fields.forEach(field => {
  32. if (!field.autoID) {
  33. result[field.name] = genDataByType(field);
  34. }
  35. });
  36. return result;
  37. };
  38. export const genRows = (fields: FieldSchema[], size: number) => {
  39. const result = [];
  40. for (let i = 0; i < size; i++) {
  41. result[i] = genRow(fields);
  42. }
  43. return result;
  44. };
  45. export const makeRandomId = (length: number): string => {
  46. let result = '';
  47. const characters =
  48. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  49. const charactersLength = characters.length;
  50. for (let i = 0; i < length; i++) {
  51. result += characters.charAt(Math.floor(Math.random() * charactersLength));
  52. }
  53. return result;
  54. };