Skip to content

Commit

Permalink
Fixed sending /events requests from logged out users (cvat-ai#7608)
Browse files Browse the repository at this point in the history
<!-- Raise an issue to propose your change
(https://github.com/opencv/cvat/issues).
It helps to avoid duplication of efforts from multiple independent
contributors.
Discuss your ideas with maintainers to be sure that changes will be
approved and merged.
Read the [Contribution
guide](https://opencv.github.io/cvat/docs/contributing/). -->

<!-- Provide a general summary of your changes in the Title above -->

### Motivation and context
<!-- Why is this change required? What problem does it solve? If it
fixes an open
issue, please link to the issue here. Describe your changes in detail,
add
screenshots. -->
This PR fixes the following problem:
- When user is logged-out, the /events request is still sent. Its
failing due with unauthorized request creating lots of error events on
the server. They are quite useless.

### How has this been tested?
<!-- Please describe in detail how you tested your changes.
Include details of your testing environment, and the tests you ran to
see how your change affects other areas of the code, etc. -->

### Checklist
<!-- Go over all the following points, and put an `x` in all the boxes
that apply.
If an item isn't applicable for some reason, then ~~explicitly
strikethrough~~ the whole
line. If you don't do that, GitHub will show incorrect progress for the
pull request.
If you're unsure about any of these, don't hesitate to ask. We're here
to help! -->
- [x] I submit my changes into the `develop` branch
- [x] I have created a changelog fragment <!-- see top comment in
CHANGELOG.md -->
- ~~[ ] I have updated the documentation accordingly~~
- [ ] I have added tests to cover my changes
- ~~[ ] I have linked related issues (see [GitHub docs](

https://help.github.com/en/github/managing-your-work-on-github/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword))~~
- [x] I have increased versions of npm packages if it is necessary

([cvat-canvas](https://github.com/opencv/cvat/tree/develop/cvat-canvas#versioning),

[cvat-core](https://github.com/opencv/cvat/tree/develop/cvat-core#versioning),

[cvat-data](https://github.com/opencv/cvat/tree/develop/cvat-data#versioning)
and

[cvat-ui](https://github.com/opencv/cvat/tree/develop/cvat-ui#versioning))

### License

- [x] I submit _my code changes_ under the same [MIT License](
https://github.com/opencv/cvat/blob/develop/LICENSE) that covers the
project.
  Feel free to contact the maintainers if that's a concern.

---------

Co-authored-by: Boris Sekachev <[email protected]>
(cherry picked from commit 02889ee)
  • Loading branch information
klakhov authored and Viditagarwal7479 committed Mar 30, 2024
1 parent 3e0679f commit 2aa6c41
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- Sending `/events` request from logged-out user (<https://github.com/opencv/cvat/pull/7608>)
2 changes: 1 addition & 1 deletion cvat-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cvat-core",
"version": "15.0.1",
"version": "15.0.2",
"type": "module",
"description": "Part of Computer Vision Tool which presents an interface for client-side integration",
"main": "src/api.ts",
Expand Down
2 changes: 1 addition & 1 deletion cvat-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export default interface CVATCore {
enabled: boolean;
onEmptyMaskOccurrence: () => void | null;
};
onOrganizationChange: typeof config.onOrganizationChange;
onOrganizationChange: (newOrgId: number | null) => void | null;
globalObjectsCounter: typeof config.globalObjectsCounter;
},
client: {
Expand Down
2 changes: 2 additions & 0 deletions cvat-core/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ Object.defineProperties(Logger.prototype.save, {
// potentially new events may be generated during saving
// that is why we add this.collection
this.collection = [...collectionToSend, ...this.collection];

throw error;
} finally {
this.saving = false;
}
Expand Down
15 changes: 11 additions & 4 deletions cvat-ui/src/components/cvat-app.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2020-2022 Intel Corporation
// Copyright (C) 2022-2023 CVAT.ai Corporation
// Copyright (C) 2022-2024 CVAT.ai Corporation
//
// SPDX-License-Identifier: MIT

Expand Down Expand Up @@ -71,7 +71,7 @@ import showPlatformNotification, {
} from 'utils/platform-checker';
import '../styles.scss';
import appConfig from 'config';
import EventRecorder from 'utils/controls-logger';
import EventRecorder from 'utils/event-recorder';
import { authQuery } from 'utils/auth-query';
import EmailConfirmationPage from './email-confirmation-pages/email-confirmed';
import EmailVerificationSentPage from './email-confirmation-pages/email-verification-sent';
Expand Down Expand Up @@ -149,7 +149,6 @@ class CVATApplication extends React.PureComponent<CVATAppProps & RouteComponentP
});

core.logger.configure(() => window.document.hasFocus, userActivityCallback);
EventRecorder.initSave();

core.config.onOrganizationChange = (newOrgId: number | null) => {
if (newOrgId === null) {
Expand Down Expand Up @@ -254,7 +253,7 @@ class CVATApplication extends React.PureComponent<CVATAppProps & RouteComponentP
}
}

public componentDidUpdate(): void {
public componentDidUpdate(prevProps: CVATAppProps): void {
const {
verifyAuthorized,
loadFormats,
Expand Down Expand Up @@ -302,6 +301,14 @@ class CVATApplication extends React.PureComponent<CVATAppProps & RouteComponentP
return;
}

if (user !== prevProps.user) {
if (user) {
EventRecorder.initSave();
} else {
EventRecorder.cancelSave();
}
}

if (!userAgreementsInitialized && !userAgreementsFetching) {
loadUserAgreements();
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2023 CVAT.ai Corporation
// Copyright (C) 2023-2024 CVAT.ai Corporation
//
// SPDX-License-Identifier: MIT

Expand All @@ -15,6 +15,7 @@ const parentClassFilter = ['ant-btn'];

class EventRecorder {
#savingTimeout: number | null;

public constructor() {
this.#savingTimeout = null;
core.logger.log(EventScope.loadTool, {
Expand Down Expand Up @@ -52,13 +53,29 @@ class EventRecorder {
public initSave(): void {
if (this.#savingTimeout) return;
this.#savingTimeout = window.setTimeout(() => {
core.logger.save().finally(() => {
const scheduleSave = (): void => {
this.#savingTimeout = null;
this.initSave();
});
};
core.logger.save()
.then(scheduleSave)
.catch((error) => {
if (error?.code === 401) {
this.cancelSave();
} else {
scheduleSave();
}
});
}, CONTROLS_LOGS_INTERVAL);
}

public cancelSave(): void {
if (this.#savingTimeout) {
window.clearTimeout(this.#savingTimeout);
this.#savingTimeout = null;
}
}

private filterClassName(cls: string): string {
if (typeof cls === 'string') {
return cls.split(' ').filter((_cls: string) => _cls.startsWith('cvat')).join(' ');
Expand Down

0 comments on commit 2aa6c41

Please sign in to comment.