Skip to content

Commit

Permalink
Some client code refactoring (#3077)
Browse files Browse the repository at this point in the history
* Some refactoring, fixed unnesasary rerenderss

* Refactored filter

* Refactored filter

* Removed extra call of queryString() method
  • Loading branch information
Boris Sekachev authored Apr 8, 2021
1 parent fad8612 commit b0b3631
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 104 deletions.
56 changes: 37 additions & 19 deletions cvat-core/src/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
}
data.task_subsets = Array.from(subsetsSet);
}
if (initialData.training_project) {
data.training_project = JSON.parse(JSON.stringify(initialData.training_project));
if (typeof initialData.training_project === 'object') {
data.training_project = { ...initialData.training_project };
}

Object.defineProperties(
Expand Down Expand Up @@ -222,21 +222,34 @@
subsets: {
get: () => [...data.task_subsets],
},

_internalData: {
get: () => data,
},

training_project: {
get: () => data.training_project,
set: (training) => {
if (training) {
data.training_project = JSON.parse(JSON.stringify(training));
/**
* Training project associated with this annotation project
* This is a simple object which contains
* keys like host, username, password, enabled, project_class
* @name trainingProject
* @type {object}
* @memberof module:API.cvat.classes.Project
* @readonly
* @instance
*/
trainingProject: {
get: () => {
if (typeof data.training_project === 'object') {
return { ...data.training_project };
}
return data.training_project;
},
set: (updatedProject) => {
if (typeof training === 'object') {
data.training_project = { ...updatedProject };
} else {
data.training_project = training;
data.training_project = updatedProject;
}
},
},
_internalData: {
get: () => data,
},
}),
);
}
Expand Down Expand Up @@ -278,33 +291,38 @@
};

Project.prototype.save.implementation = async function () {
let trainingProject;
if (this.training_project) {
trainingProject = JSON.parse(JSON.stringify(this.training_project));
}
const trainingProjectCopy = this.trainingProject;
if (typeof this.id !== 'undefined') {
// project has been already created, need to update some data
const projectData = {
name: this.name,
assignee_id: this.assignee ? this.assignee.id : null,
bug_tracker: this.bugTracker,
labels: [...this._internalData.labels.map((el) => el.toJSON())],
training_project: trainingProject,
};

if (trainingProjectCopy) {
projectData.training_project = trainingProjectCopy;
}

await serverProxy.projects.save(this.id, projectData);
return this;
}

// initial creating
const projectSpec = {
name: this.name,
labels: [...this.labels.map((el) => el.toJSON())],
training_project: trainingProject,
};

if (this.bugTracker) {
projectSpec.bug_tracker = this.bugTracker;
}

if (trainingProjectCopy) {
projectSpec.training_project = trainingProjectCopy;
}

const project = await serverProxy.projects.create(projectSpec);
return new Project(project);
};
Expand Down
5 changes: 4 additions & 1 deletion cvat-ui/src/actions/annotation-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,10 @@ export function getJobAsync(tid: number, jid: number, initialFrame: number, init
setTimeout(updatePredictorStatus, 20 * 1000);
}
};
updatePredictorStatus();

if (state.plugins.list.PREDICT && job.task.projectId !== null) {
updatePredictorStatus();
}

dispatch(changeFrameAsync(frameNumber, false));
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions cvat-ui/src/components/annotation-page/annotation-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import SubmitReviewModal from 'components/annotation-page/review/submit-review-m
import StandardWorkspaceComponent from 'components/annotation-page/standard-workspace/standard-workspace';
import StandardWorkspace3DComponent from 'components/annotation-page/standard3D-workspace/standard3D-workspace';
import TagAnnotationWorkspace from 'components/annotation-page/tag-annotation-workspace/tag-annotation-workspace';
import FiltersModalContainer from 'containers/annotation-page/top-bar/filters-modal';
import FiltersModalComponent from 'components/annotation-page/top-bar/filters-modal';
import StatisticsModalContainer from 'containers/annotation-page/top-bar/statistics-modal';
import AnnotationTopBarContainer from 'containers/annotation-page/top-bar/top-bar';
import { Workspace } from 'reducers/interfaces';
Expand Down Expand Up @@ -131,7 +131,7 @@ export default function AnnotationPageComponent(props: Props): JSX.Element {
<ReviewAnnotationsWorkspace />
</Layout.Content>
)}
<FiltersModalContainer visible={false} />
<FiltersModalComponent />
<StatisticsModalContainer />
<SubmitAnnotationsModal />
<SubmitReviewModal />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ import GlobalHotKeys from 'utils/mousetrap-react';

function LabelsListComponent(): JSX.Element {
const dispatch = useDispatch();
const {
annotation: {
job: { labels },
tabContentHeight: listHeight,
annotations: { activatedStateID, states },
},
shortcuts: { keyMap },
} = useSelector((state: CombinedState) => state);
const labels = useSelector((state: CombinedState) => state.annotation.job.labels);
const listHeight = useSelector((state: CombinedState) => state.annotation.tabContentHeight);
const activatedStateID = useSelector((state: CombinedState) => state.annotation.annotations.activatedStateID);
const states = useSelector((state: CombinedState) => state.annotation.annotations.states);
const keyMap = useSelector((state: CombinedState) => state.shortcuts.keyMap);

const labelIDs = labels.map((label: any): number => label.id);

const [keyToLabelMapping, setKeyToLabelMapping] = useState<Record<string, number>>(
Expand Down
39 changes: 21 additions & 18 deletions cvat-ui/src/components/annotation-page/top-bar/filters-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,15 @@ const { FieldDropdown } = AntdWidgets;

const FILTERS_HISTORY = 'annotationFiltersHistory';

interface Props {
visible: boolean;
}

interface StoredFilter {
id: string;
logic: JsonLogicTree;
}

export default function FiltersModalComponent(props: Props): JSX.Element {
const { visible } = props;
const { labels } = useSelector((state: CombinedState) => state.annotation.job);
const { filters: activeFilters } = useSelector((state: CombinedState) => state.annotation.annotations);
function FiltersModalComponent(): JSX.Element {
const labels = useSelector((state: CombinedState) => state.annotation.job.labels);
const activeFilters = useSelector((state: CombinedState) => state.annotation.annotations.filters);
const visible = useSelector((state: CombinedState) => state.annotation.filtersPanelVisible);

const getConvertedInputType = (inputType: string): string => {
switch (inputType) {
Expand Down Expand Up @@ -234,18 +230,23 @@ export default function FiltersModalComponent(props: Props): JSX.Element {
const menu = (
<Menu>
{filters
.filter((filter: StoredFilter) => {
const tree = QbUtils.loadFromJsonLogic(filter.logic, config);
return !!QbUtils.queryString(tree, config);
})
.map((filter: StoredFilter) => {
// if a logic received from local storage does not correspond to current config
// which depends on label specification
// (it can be when history from another task with another specification or when label was removed)
// loadFromJsonLogic() prints a warning to console
// the are not ways to configure this behaviour

const tree = QbUtils.loadFromJsonLogic(filter.logic, config);
return (
<Menu.Item key={filter.id} onClick={() => setState({ tree, config })}>
{QbUtils.queryString(tree, config)}
</Menu.Item>
);
})}
const queryString = QbUtils.queryString(tree, config);
return { tree, queryString, filter };
})
.filter(({ queryString }) => !!queryString)
.map(({ filter, tree, queryString }) => (
<Menu.Item key={filter.id} onClick={() => setState({ tree, config })}>
{queryString}
</Menu.Item>
))}
</Menu>
);

Expand Down Expand Up @@ -286,3 +287,5 @@ export default function FiltersModalComponent(props: Props): JSX.Element {
</Modal>
);
}

export default React.memo(FiltersModalComponent);
12 changes: 6 additions & 6 deletions cvat-ui/src/components/annotation-page/top-bar/right-group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ function RightGroup(props: Props): JSX.Element {
isTrainingActive,
showFilters,
} = props;
predictor.annotationAmount = predictor.annotationAmount ? predictor.annotationAmount : 0;
predictor.mediaAmount = predictor.mediaAmount ? predictor.mediaAmount : 0;
const annotationAmount = predictor.annotationAmount || 0;
const mediaAmount = predictor.mediaAmount || 0;
const formattedScore = `${(predictor.projectScore * 100).toFixed(0)}%`;
const predictorTooltip = (
<div className='cvat-predictor-tooltip'>
Expand All @@ -65,15 +65,15 @@ function RightGroup(props: Props): JSX.Element {
<br />
<span>
Annotations amount:
{predictor.annotationAmount}
{annotationAmount}
</span>
<br />
<span>
Media amount:
{predictor.mediaAmount}
{mediaAmount}
</span>
<br />
{predictor.annotationAmount > 0 ? (
{annotationAmount > 0 ? (
<span>
Model mAP is
{' '}
Expand Down Expand Up @@ -139,7 +139,7 @@ function RightGroup(props: Props): JSX.Element {
<Tooltip title={predictorTooltip}>
<Icon component={BrainIcon} />
</Tooltip>
{predictor.annotationAmount ? `mAP ${formattedScore}` : 'not trained'}
{annotationAmount ? `mAP ${formattedScore}` : 'not trained'}
</Button>
)}
<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,26 +154,19 @@ export default function CreateProjectContent(): JSX.Element {
}, [newProjectId]);

const onSumbit = async (): Promise<void> => {
interface Project {
[key: string]: any;
}

const projectData: Project = {};
let projectData: Record<string, any> = {};
if (nameFormRef.current && advancedFormRef.current) {
const basicValues = await nameFormRef.current.validateFields();
const advancedValues = await advancedFormRef.current.validateFields();
const adaptiveAutoAnnotationValues = await adaptiveAutoAnnotationFormRef.current?.validateFields();
projectData.name = basicValues.name;
projectData.training_project = null;
if (adaptiveAutoAnnotationValues) {
projectData.training_project = {};
for (const [field, value] of Object.entries(adaptiveAutoAnnotationValues)) {
projectData.training_project[field] = value;
}
}
projectData = {
...projectData,
...advancedValues,
name: basicValues.name,
};

for (const [field, value] of Object.entries(advancedValues)) {
projectData[field] = value;
if (adaptiveAutoAnnotationValues) {
projectData.training_project = { ...adaptiveAutoAnnotationValues };
}
}

Expand Down
9 changes: 5 additions & 4 deletions cvat-ui/src/components/cvat-app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
//
// SPDX-License-Identifier: MIT

import 'antd/dist/antd.css';
import React from 'react';
import { Redirect, Route, Switch } from 'react-router';
import { RouteComponentProps, withRouter } from 'react-router-dom';
import { Col, Row } from 'antd/lib/grid';
import Layout from 'antd/lib/layout';
import Modal from 'antd/lib/modal';
import notification from 'antd/lib/notification';
import Spin from 'antd/lib/spin';
import Text from 'antd/lib/typography/Text';
import 'antd/dist/antd.css';

import GlobalErrorBoundary from 'components/global-error-boundary/global-error-boundary';
import Header from 'components/header/header';
import ResetPasswordPageConfirmComponent from 'components/reset-password-confirm-page/reset-password-confirm-page';
Expand All @@ -26,10 +30,7 @@ import AnnotationPageContainer from 'containers/annotation-page/annotation-page'
import LoginPageContainer from 'containers/login-page/login-page';
import RegisterPageContainer from 'containers/register-page/register-page';
import getCore from 'cvat-core-wrapper';
import React from 'react';
import GlobalHotKeys, { KeyMap } from 'utils/mousetrap-react';
import { Redirect, Route, Switch } from 'react-router';
import { RouteComponentProps, withRouter } from 'react-router-dom';
import { NotificationsState } from 'reducers/interfaces';
import { customWaViewHit } from 'utils/enviroment';
import showPlatformNotification, { platformInfo, stopNotifications } from 'utils/platform-checker';
Expand Down
3 changes: 2 additions & 1 deletion cvat-ui/src/components/layout-grid/layout-grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

import React, { useCallback, useState } from 'react';
import ReactDOM from 'react-dom';
import GlobalHotKeys from 'utils/mousetrap-react';
import { useSelector } from 'react-redux';

import GlobalHotKeys from 'utils/mousetrap-react';
import { CombinedState } from 'reducers/interfaces';
import './styles.scss';

Expand Down
26 changes: 0 additions & 26 deletions cvat-ui/src/containers/annotation-page/top-bar/filters-modal.tsx

This file was deleted.

9 changes: 5 additions & 4 deletions cvat-ui/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
//
// SPDX-License-Identifier: MIT

import React from 'react';
import ReactDOM from 'react-dom';
import { connect, Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';

import { getAboutAsync } from 'actions/about-actions';
import { authorizedAsync, loadAuthActionsAsync } from 'actions/auth-actions';
import { getFormatsAsync } from 'actions/formats-actions';
Expand All @@ -14,11 +19,7 @@ import CVATApplication from 'components/cvat-app';
import LayoutGrid from 'components/layout-grid/layout-grid';
import logger, { LogType } from 'cvat-logger';
import createCVATStore, { getCVATStore } from 'cvat-store';
import React from 'react';
import ReactDOM from 'react-dom';
import { KeyMap } from 'utils/mousetrap-react';
import { connect, Provider } from 'react-redux';
import { BrowserRouter } from 'react-router-dom';
import createRootReducer from 'reducers/root-reducer';
import { resetErrors, resetMessages } from './actions/notification-actions';
import { CombinedState, NotificationsState } from './reducers/interfaces';
Expand Down
4 changes: 4 additions & 0 deletions cvat-ui/src/reducers/annotation-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ const defaultState: AnnotationState = {
projectScore: 0,
fetching: false,
annotatedFrames: [],
timeRemaining: 0,
progress: 0,
annotationAmount: 0,
mediaAmount: 0,
},
workspace: Workspace.STANDARD,
};
Expand Down

0 comments on commit b0b3631

Please sign in to comment.