Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add java code view #152

Merged
merged 3 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 6 additions & 18 deletions client/src/components/code/CodeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const getStyles = makeStyles((theme: Theme) => ({
padding: theme.spacing(4),
backgroundColor: theme.palette.attuDark.main,
borderRadius: 8,

display: 'flex',
flexDirection: 'column',
color: '#fff',
},
title: {
Expand All @@ -26,7 +27,6 @@ const getStyles = makeStyles((theme: Theme) => ({
minHeight: 0,

'& .MuiTab-wrapper': {
textTransform: 'uppercase',
fontWeight: 'bold',
color: '#fff',
},
Expand Down Expand Up @@ -68,32 +68,20 @@ const getStyles = makeStyles((theme: Theme) => ({
},

block: {
/**
* container height minus:
* 1. CodeView padding top and bottom (32 * 2)
* 2. CodeBlock padding top and bottom (24 * 2)
* 3. title height and margin bottom (24 + 16)
* 4. tab title height and margin bottom (36 + 16)
*/
height: (props: { height: number }) =>
props.height - 32 * 2 - 24 * 2 - (24 + 16) - (36 + 16),
height: `calc(100% - ${theme.spacing(4)})`,
overflowY: 'auto',
},
}));

const CodeView: FC<CodeViewProps> = ({
wrapperClass = '',
data,
height = 0,
}) => {
const classes = getStyles({ height });
const CodeView: FC<CodeViewProps> = ({ wrapperClass = '', data }) => {
const classes = getStyles();
const { t: commonTrans } = useTranslation();

const tabs: ITab[] = data.map(item => ({
label: item.label,
component: (
<CodeBlock
wrapperClass={height !== 0 ? classes.block : ''}
wrapperClass={classes.block}
language={item.language}
code={item.code}
/>
Expand Down
2 changes: 2 additions & 0 deletions client/src/components/code/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export interface CodeViewProps {
export enum CodeLanguageEnum {
javascript = 'javascript',
python = 'python',
java = 'java',
go = 'go',
}

export interface CodeBlockProps {
Expand Down
18 changes: 1 addition & 17 deletions client/src/components/customDialog/DialogTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,6 @@ const DialogTemplate: FC<DialogContainerProps> = ({
const onCancel = handleCancel || handleClose;

const dialogRef = useRef(null);
const [dialogHeight, setDialogHeight] = useState<number>(0);

/**
* code mode height should not over original dialog height
* everytime children change, should recalculate dialog height
*/
useEffect(() => {
if (dialogRef.current) {
shanghaikid marked this conversation as resolved.
Show resolved Hide resolved
const height = (dialogRef.current as any).offsetHeight;
setDialogHeight(height);
}
}, [children]);

const _handleConfirm = (event: React.FormEvent<HTMLFormElement>) => {
handleConfirm();
Expand Down Expand Up @@ -127,11 +115,7 @@ const DialogTemplate: FC<DialogContainerProps> = ({

<div className={`${classes.block} ${classes.codeWrapper}`}>
{showCode && (
<CodeView
height={dialogHeight}
wrapperClass={classes.code}
data={codeBlocksData}
/>
<CodeView wrapperClass={classes.code} data={codeBlocksData} />
)}
</div>
</form>
Expand Down
5 changes: 3 additions & 2 deletions client/src/i18n/cn/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ const commonTrans = {
search: 'Search by name',
code: 'Code View',
view: 'View Code',
js: 'NODE.JS',
py: 'PYTHON',
js: 'Node.js',
py: 'Python',
java: 'Java',
community: {
hi: 'Hi, there!',
growing: 'Our growing community is here!',
Expand Down
5 changes: 3 additions & 2 deletions client/src/i18n/en/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ const commonTrans = {
search: 'Search by name',
code: 'Code View',
view: 'View Code',
js: 'NODE.JS',
py: 'PYTHON',
js: 'Node.js',
py: 'Python',
java: 'Java',
community: {
hi: 'Hi, there!',
growing: 'Our growing community is here!',
Expand Down
34 changes: 19 additions & 15 deletions client/src/pages/schema/Create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { useFormValidation } from '../../hooks/Form';
import { getCreateIndexJSCode } from '../../utils/code/Js';
import { getCreateIndexPYCode } from '../../utils/code/Py';
import { getCreateIndexJavaCode } from '../../utils/code/Java';
import { formatForm, getMetricOptions } from '../../utils/Form';
import { computMilvusRecommonds, formatSize } from '../../utils/SizingTool';
import { DataTypeEnum, DataTypeStringEnum } from '../collections/Types';
Expand Down Expand Up @@ -210,33 +211,36 @@ const CreateIndex = (props: {
* create index code mode
*/
const codeBlockData: CodeViewData[] = useMemo(() => {
console.log('m', indexSetting.index_name);
const vectorTypes = [
DataTypeStringEnum.BinaryVector,
DataTypeStringEnum.FloatVector,
];
const isScalarField = !vectorTypes.includes(fieldType);
const getCodeParams = {
collectionName,
fieldName,
extraParams,
isScalarField,
indexName: indexSetting.index_name,
metricType: indexSetting.metric_type,
indexType: indexSetting.index_type,
};
return [
{
label: commonTrans('py'),
language: CodeLanguageEnum.python,
code: getCreateIndexPYCode({
collectionName,
fieldName,
indexName: indexSetting.index_name,
extraParams,
isScalarField,
}),
code: getCreateIndexPYCode(getCodeParams),
},
{
label: commonTrans('java'),
language: CodeLanguageEnum.java,
code: getCreateIndexJavaCode(getCodeParams),
},
{
label: commonTrans('js'),
language: CodeLanguageEnum.javascript,
code: getCreateIndexJSCode({
collectionName,
fieldName,
extraParams,
indexName: indexSetting.index_name,
isScalarField,
}),
code: getCreateIndexJSCode(getCodeParams),
},
];
}, [
Expand All @@ -256,7 +260,7 @@ const CreateIndex = (props: {
// setDisabled(true);
setIndexSetting(v => ({
...v,
index_name: '',
index_name: v.index_name,
metric_type: defaultMetricType,
M: '',
m: '4',
Expand Down
34 changes: 34 additions & 0 deletions client/src/utils/code/Java.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { CreateIndexCodeParam } from './Types';

export const getCreateIndexJavaCode = (params: CreateIndexCodeParam) => {
const {
collectionName,
fieldName,
indexName,
metricType,
extraParams,
indexType,
} = params;

const JavaCode = `import io.milvus.param.*;

// Index type
final IndexType INDEX_TYPE = IndexType.IVF_FLAT;
// Index param
final String INDEX_PARAM = ${JSON.stringify(extraParams, null, 2)};
// Create index
milvusClient.createIndex(
CreateIndexParam.newBuilder()
.withCollectionName("${collectionName}")
.withIndexName("${indexName}")
.withFieldName("${fieldName}")
.withIndexType("IndexType.${indexType}")
.withMetricType("${metricType}")
.withExtraParam(INDEX_PARAM)
.withSyncMode(Boolean.FALSE)
.build()
);
`;

return JavaCode;
};
13 changes: 10 additions & 3 deletions client/src/utils/code/Js.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { CreateIndexCodeParam } from './Types';

export const getCreateIndexJSCode = (params: CreateIndexCodeParam) => {
const { collectionName, fieldName, indexName, isScalarField, extraParams } =
params;
const {
collectionName,
fieldName,
indexName,
isScalarField,
extraParams,
indexType,
} = params;

const jsCode = `import { MilvusClient } from '@zilliz/milvus2-sdk-node';
const client = new MilvusClient(milvus_address);
Expand All @@ -16,7 +22,8 @@ client.indexManager.createIndex({
? ''
: `extra_params: ${JSON.stringify(extraParams, null, 2)},`
}
});`;
});
`;

return jsCode;
};
6 changes: 4 additions & 2 deletions client/src/utils/code/Py.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ export const getCreateIndexPYCode = (params: CreateIndexCodeParam) => {
...extraParams,
params: parseValue(extraParams.params),
};
const pyCode = `from pymilvus_orm import Collection
const pyCode = `from pymilvus import Collection

collection = Collection('${collectionName}')

${
isScalarField
? ''
Expand All @@ -29,7 +30,8 @@ collection.create_index(
field_name="${fieldName}",
${isScalarField ? '' : `index_params=index_params,`}
index_name="${indexName}"
)`;
)
`;

return pyCode;
};
2 changes: 2 additions & 0 deletions client/src/utils/code/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ export interface CreateIndexCodeParam {
indexName: string;
extraParams: IndexExtraParam;
isScalarField: boolean;
metricType: string;
indexType: string;
}