Skip to content

Commit

Permalink
Merge branch 'develop' into f-4088/simplify-link-navgitaion
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalesi authored Jul 25, 2023
2 parents 5ab9d98 + 578e2f5 commit 96bf940
Showing 1 changed file with 97 additions and 52 deletions.
149 changes: 97 additions & 52 deletions src/subapps/admin/components/Settings/ViewsSubView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { useHistory, useRouteMatch } from 'react-router';
import { AccessControl, useNexusContext } from '@bbp/react-nexus';
import { useMutation, useQuery } from 'react-query';
import { Table, Button, Row, Col, notification, Tooltip } from 'antd';
import { isArray, isString } from 'lodash';
import { isArray, isString, orderBy } from 'lodash';
import { ColumnsType } from 'antd/es/table';
import { NexusClient } from '@bbp/nexus-sdk';
import { PromisePool } from '@supercharge/promise-pool';
import { useSelector } from 'react-redux';
import * as Sentry from '@sentry/browser';
import { getOrgAndProjectFromProjectId } from '../../../../shared/utils';
import { RootState } from '../../../../shared/store/reducers';
import HasNoPermission from '../../../../shared/components/Icons/HasNoPermission';
Expand All @@ -21,18 +22,27 @@ type TViewType = {
status: string;
orgLabel: string;
projectLabel: string;
isAggregateView: boolean;
};
const AggregateViews = ['AggregateElasticSearchView', 'AggregateSparqlView'];
const aggregateFilterPredicate = (type?: string | string[]) => {
if (type) {
if (isArray(type)) {
return type.some(i => AggregateViews.includes(i));
}
return AggregateViews.includes(type);
}
return false;
};

const fetchViewsList = async ({
nexus,
orgLabel,
projectLabel,
apiRoot,
}: {
nexus: NexusClient;
orgLabel: string;
projectLabel: string;
apiRoot: string;
}) => {
try {
const views = await nexus.View.list(orgLabel, projectLabel, {});
Expand All @@ -47,33 +57,48 @@ const fetchViewsList = async ({
key: item['@id'] as string,
name: (item['@id'] as string).split('/').pop() as string,
type: item['@type'],
isAggregateView: aggregateFilterPredicate(item['@type']),
status: '100%',
};
});
const { results, errors } = await PromisePool.withConcurrency(4)
.for(result!)
.process(async view => {
const iViewStats = await nexus.View.statistics(
orgLabel,
projectLabel,
encodeURIComponent(view.key)
);
// TODO: we should update the type in nexus-sdk! as the response is not the same from delta!
// @ts-ignore
const percentage = iViewStats.totalEvents
? // @ts-ignore
iViewStats.processedEvents / iViewStats.totalEvents
: 0;
if (!view.isAggregateView) {
const iViewStats = await nexus.View.statistics(
orgLabel,
projectLabel,
encodeURIComponent(view.key)
);
// TODO: we should update the type in nexus-sdk! as the response is not the same from delta!
// @ts-ignore
const percentage = iViewStats.totalEvents
? // @ts-ignore
iViewStats.processedEvents / iViewStats.totalEvents
: 0;
return {
...view,
status: percentage ? `${(percentage * 100).toFixed(0)}%` : '0%',
};
}
return {
...view,
status: percentage ? `${(percentage * 100).toFixed(0)}%` : '0%',
status: 'N/A',
};
});
return {
results,
errors,
results: orderBy(results, ['isAggregateView', 'name'], ['asc', 'asc']),
};
} catch (error) {
console.error('@@error', error);
Sentry.captureException('Error loading and filtering the views', {
extra: {
orgLabel,
projectLabel,
error,
},
});
// @ts-ignore
throw new Error('Can not fetch views', { cause: error });
}
Expand All @@ -91,11 +116,23 @@ const restartIndexOneView = async ({
projectLabel: string;
viewId: string;
}) => {
return await nexus.httpDelete({
path: `${apiEndpoint}/views/${orgLabel}/${projectLabel}/${encodeURIComponent(
viewId
)}/offset`,
});
try {
return await nexus.httpDelete({
path: `${apiEndpoint}/views/${orgLabel}/${projectLabel}/${encodeURIComponent(
viewId
)}/offset`,
});
} catch (error) {
console.log('@@error', error);
Sentry.captureException('Error restarting one view', {
extra: {
orgLabel,
projectLabel,
viewId,
error,
},
});
}
};
const restartIndexingAllViews = async ({
nexus,
Expand All @@ -118,6 +155,11 @@ const restartIndexingAllViews = async ({
});
});
if (errors.length) {
Sentry.captureException('Error restarting views', {
extra: {
views,
},
});
// @ts-ignore
throw new Error('Error captured when reindexing the views', {
cause: errors,
Expand All @@ -144,8 +186,7 @@ const ViewsSubView = () => {
};
const { data: views, status } = useQuery({
queryKey: [`views-${orgLabel}-${projectLabel}`],
queryFn: () =>
fetchViewsList({ nexus, orgLabel, projectLabel, apiRoot: apiEndpoint }),
queryFn: () => fetchViewsList({ nexus, orgLabel, projectLabel }),
refetchInterval: 30 * 1000, // 30s
});
const { mutateAsync: handleReindexingOneView } = useMutation(
Expand Down Expand Up @@ -199,7 +240,7 @@ const ViewsSubView = () => {
dataIndex: 'actions',
title: 'Actions',
align: 'center',
render: (_, { id, key, orgLabel, projectLabel }) => {
render: (_, { id, key, orgLabel, projectLabel, isAggregateView }) => {
const editURI = `/${orgLabel}/${projectLabel}/resources/${encodeURIComponent(
`${key}`
)}`;
Expand All @@ -222,34 +263,36 @@ const ViewsSubView = () => {
>
Query
</Button>
<AccessControl
permissions={['views/query', 'views/write']}
path={[`${orgLabel}/${projectLabel}`]}
noAccessComponent={() => (
<Tooltip title="You have no permissions to re-index this view">
<Button disabled type="link">
<span style={{ marginRight: 5 }}>Re-index</span>
<HasNoPermission />
</Button>
</Tooltip>
)}
>
<Button
type="link"
htmlType="button"
onClick={() =>
handleReindexingOneView({
nexus,
apiEndpoint,
orgLabel,
projectLabel,
viewId: id,
})
}
{!isAggregateView && (
<AccessControl
permissions={['views/query', 'views/write']}
path={[`${orgLabel}/${projectLabel}`]}
noAccessComponent={() => (
<Tooltip title="You have no permissions to re-index this view">
<Button disabled type="link">
<span style={{ marginRight: 5 }}>Re-index</span>
<HasNoPermission />
</Button>
</Tooltip>
)}
>
Re-index
</Button>
</AccessControl>
<Button
type="link"
htmlType="button"
onClick={() =>
handleReindexingOneView({
nexus,
apiEndpoint,
orgLabel,
projectLabel,
viewId: id,
})
}
>
Re-index
</Button>
</AccessControl>
)}
</div>
);
},
Expand Down Expand Up @@ -305,7 +348,9 @@ const ViewsSubView = () => {
handleReindexingAllViews({
nexus,
apiEndpoint,
views: views?.results || [],
views:
views?.results.filter(item => !item.isAggregateView) ||
[],
});
}}
>
Expand Down

0 comments on commit 96bf940

Please sign in to comment.