Skip to content

Commit

Permalink
use count api to show count for loaded collection
Browse files Browse the repository at this point in the history
Signed-off-by: ryjiang <[email protected]>
  • Loading branch information
shanghaikid committed Nov 13, 2023
1 parent 23b53ea commit 01ba68e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 38 deletions.
7 changes: 7 additions & 0 deletions client/src/http/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ export class CollectionHttp extends BaseModel implements CollectionView {
});
}

static count(collectionName: string) {
return super.search({
path: `${this.COLLECTIONS_URL}/${collectionName}/count`,
params: {},
});
}

static getQSegments(collectionName: string) {
return super.search({
path: `${this.COLLECTIONS_URL}/${collectionName}/qsegments`,
Expand Down
38 changes: 28 additions & 10 deletions client/src/pages/overview/collectionCard/CollectionCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ import {
Card,
CardContent,
} from '@material-ui/core';
import { FC, useContext } from 'react';
import { FC, useContext, useEffect, useState, useCallback } from 'react';
import CustomButton from '@/components/customButton/CustomButton';
import icons from '@/components/icons/Icons';
import Status from '@/components/status/Status';
import { useTranslation } from 'react-i18next';
import CustomIconButton from '@/components/customButton/CustomIconButton';
import { useNavigate, Link } from 'react-router-dom';
import { LOADING_STATE } from '@/consts';
import { rootContext } from '@/context';
import { rootContext, dataContext } from '@/context';
import ReleaseCollectionDialog from '../../dialogs/ReleaseCollectionDialog';
import { CollectionCardProps } from './Types';
import { CollectionHttp } from '@/http';
import { CollectionData } from '@/pages/collections/Types';

const useStyles = makeStyles((theme: Theme) => ({
wrapper: {
Expand Down Expand Up @@ -86,16 +88,13 @@ const CollectionCard: FC<CollectionCardProps> = ({
onRelease,
wrapperClass = '',
}) => {
const { database } = useContext(dataContext);
const [loading, setLoading] = useState(false);
const [count, setCount] = useState<string>('');
const classes = useStyles();
const { setDialog } = useContext(rootContext);

const {
_name: name,
_status: status,
_rowCount: rowCount,
_loadedPercentage,
_replicas,
} = data;
const { _name: name, _status: status, _loadedPercentage, _replicas } = data;
const navigate = useNavigate();
// icons
const RightArrowIcon = icons.rightArrow;
Expand All @@ -122,6 +121,21 @@ const CollectionCard: FC<CollectionCardProps> = ({
navigate({ pathname: '/search', search: `?collectionName=${name}` });
};

const fetchData = useCallback(async () => {
try {
setLoading(true);
const data = (await CollectionHttp.count(name)) as CollectionData;
setCount(data._rowCount);
} catch (e) {
} finally {
setLoading(false);
}
}, [status]);

useEffect(() => {
fetchData();
}, [fetchData, database]);

return (
<Card
className={`card-wrapper ${classes.wrapper} ${wrapperClass} ${
Expand All @@ -147,7 +161,11 @@ const CollectionCard: FC<CollectionCardProps> = ({
) : null}
<li>
<Typography>{collectionTrans('count')}</Typography>:
<Typography className={classes.rowCount}>{rowCount}</Typography>
{loading ? (
`...`
) : (
<Typography className={classes.rowCount}>{count}</Typography>
)}
</li>
</ul>
<Divider classes={{ root: classes.divider }} />
Expand Down
14 changes: 14 additions & 0 deletions server/src/collections/collections.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class CollectionController {
);
this.router.delete('/:name/alias/:alias', this.dropAlias.bind(this));
this.router.get('/:name', this.describeCollection.bind(this));
this.router.get('/:name/count', this.count.bind(this));

// load / release
this.router.put('/:name/load', this.loadCollection.bind(this));
Expand Down Expand Up @@ -370,4 +371,17 @@ export class CollectionController {
next(error);
}
}

async count(req: Request, res: Response, next: NextFunction) {
const name = req.params?.name;
try {
const result = await this.collectionsService.count({
collection_name: name,
});

res.send({ collection_name: name, rowCount: result });
} catch (error) {
next(error);
}
}
}
49 changes: 21 additions & 28 deletions server/src/collections/collections.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,15 @@ export class CollectionsService {
}

async count(data: CountReq) {
const res = await this.milvusService.client.count(data);
throwErrorFromSDK(res.status);
return res;
let count = 0;
try {
const countRes = await this.milvusService.client.count(data);
count = countRes.data;
} catch (error) {
const collectionStatisticsRes = await this.getCollectionStatistics(data);
count = collectionStatisticsRes.data.row_count;
}
return count;
}

async insert(data: InsertReq) {
Expand Down Expand Up @@ -172,17 +178,17 @@ export class CollectionsService {

let count: number | string;

try {
const countRes = await this.count({
collection_name: name,
});
count = countRes.data;
} catch (error) {
const collectionStatisticsRes = await this.getCollectionStatistics({
collection_name: name,
});
count = collectionStatisticsRes.data.row_count;
}
const collectionStatisticsRes = await this.getCollectionStatistics({
collection_name: name,
});
count = collectionStatisticsRes.data.row_count;
// try {
// const countRes = await this.count({
// collection_name: name,
// });
// count = countRes.data;
// } catch (error) {
// }

const indexRes = await this.getIndexInfo({
collection_name: item.name,
Expand Down Expand Up @@ -241,20 +247,7 @@ export class CollectionsService {
for (const item of res.data) {
const { id, name } = item;

let count: number | string;

try {
const countRes = await this.count({
collection_name: name,
});
count = countRes.data;
} catch (error) {
const collectionStatisticsRes = await this.getCollectionStatistics({
collection_name: name,
});
count = collectionStatisticsRes.data.row_count;
}

const count = this.count({ collection_name: name });
data.push({
id,
collection_name: name,
Expand Down

0 comments on commit 01ba68e

Please sign in to comment.