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

Make event-related identifiers in cvat-core consistent #7482

Merged
merged 2 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions cvat-core/src/annotations-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { omit, throttle } from 'lodash';
import { ArgumentError } from './exceptions';
import { SerializedCollection } from './server-response-types';
import { Job, Task } from './session';
import { LogType, ObjectType } from './enums';
import { EventScope, ObjectType } from './enums';
import ObjectState from './object-state';
import { getAnnotations, getCollection } from './annotations';

Expand Down Expand Up @@ -114,7 +114,7 @@ async function runSingleFrameChain(
cancelled: () => boolean,
): Promise<void> {
type IDsToHandle = { shapes: number[] };
const event = await instance.logger.log(LogType.annotationsAction, {
const event = await instance.logger.log(EventScope.annotationsAction, {
from: frameFrom,
to: frameTo,
chain: actionsChain.map((action) => action.name).join(' => '),
Expand Down
8 changes: 4 additions & 4 deletions cvat-core/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
// SPDX-License-Identifier: MIT

import PluginRegistry from './plugins';
import loggerStorage from './logger-storage';
import { EventLogger } from './log';
import logger from './logger';
import { Event } from './event';
import ObjectState from './object-state';
import Statistics from './statistics';
import Comment from './comment';
Expand Down Expand Up @@ -246,7 +246,7 @@ function build(): CVATCore {
return result;
},
},
logger: loggerStorage,
logger,
config: {
get backendAPI() {
return config.backendAPI;
Expand Down Expand Up @@ -371,7 +371,7 @@ function build(): CVATCore {
Project: implementProject(Project),
Task: implementTask(Task),
Job: implementJob(Job),
EventLogger,
Event,
Attribute,
Label,
Statistics,
Expand Down
2 changes: 1 addition & 1 deletion cvat-core/src/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export enum Source {
GT = 'Ground truth',
}

export enum LogType {
export enum EventScope {
loadTool = 'load:cvat',

loadJob = 'load:job',
Expand Down
60 changes: 30 additions & 30 deletions cvat-core/src/log.ts → cvat-core/src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@

import { detect } from 'detect-browser';
import PluginRegistry from './plugins';
import { LogType } from './enums';
import { EventScope } from './enums';
import { ArgumentError } from './exceptions';

export class EventLogger {
export class Event {
public readonly id: number;
public readonly scope: LogType;
public readonly time: Date;
public readonly scope: EventScope;
public readonly timestamp: Date;

public payload: any;

protected onCloseCallback: (() => void) | null;

constructor(logType: LogType, payload: any) {
constructor(scope: EventScope, payload: any) {
this.onCloseCallback = null;

this.scope = logType;
this.scope = scope;
this.payload = { ...payload };
this.time = new Date();
this.timestamp = new Date();
}

public onClose(callback: () => void): void {
Expand All @@ -46,7 +46,7 @@ export class EventLogger {
const payload = { ...this.payload };
const body = {
scope: this.scope,
timestamp: this.time.toISOString(),
timestamp: this.timestamp.toISOString(),
};

for (const field of [
Expand Down Expand Up @@ -81,17 +81,17 @@ export class EventLogger {
// Log duration will be computed based on the latest call
// All payloads will be shallowly combined (all top level properties will exist)
public async close(payload = {}): Promise<void> {
const result = await PluginRegistry.apiWrapper.call(this, EventLogger.prototype.close, payload);
const result = await PluginRegistry.apiWrapper.call(this, Event.prototype.close, payload);
return result;
}
}

Object.defineProperties(EventLogger.prototype.close, {
Object.defineProperties(Event.prototype.close, {
implementation: {
writable: false,
enumerable: false,
value: async function implementation(payload: any) {
this.payload.duration = Date.now() - this.time.getTime();
value: async function implementation(this: Event, payload: any) {
this.payload.duration = Date.now() - this.timestamp.getTime();
this.payload = { ...this.payload, ...payload };
if (this.onCloseCallback) {
this.onCloseCallback();
Expand All @@ -100,7 +100,7 @@ Object.defineProperties(EventLogger.prototype.close, {
},
});

class LogWithCount extends EventLogger {
class EventWithCount extends Event {
public validatePayload(): void {
super.validatePayload.call(this);
if (!Number.isInteger(this.payload.count) || this.payload.count < 1) {
Expand All @@ -110,7 +110,7 @@ class LogWithCount extends EventLogger {
}
}

class LogWithExceptionInfo extends EventLogger {
class EventWithExceptionInfo extends Event {
public validatePayload(): void {
super.validatePayload.call(this);

Expand Down Expand Up @@ -154,7 +154,7 @@ class LogWithExceptionInfo extends EventLogger {
}
}

class LogWithControlsInfo extends EventLogger {
class EventWithControlsInfo extends Event {
public dump(): any {
this.payload = {
obj_val: this.payload?.text,
Expand All @@ -164,27 +164,27 @@ class LogWithControlsInfo extends EventLogger {
}
}

export default function logFactory(logType: LogType, payload: any): EventLogger {
const logsWithCount = [
LogType.deleteObject,
LogType.mergeObjects,
LogType.copyObject,
LogType.undoAction,
LogType.redoAction,
LogType.changeFrame,
export default function makeEvent(scope: EventScope, payload: any): Event {
const eventsWithCount = [
EventScope.deleteObject,
EventScope.mergeObjects,
EventScope.copyObject,
EventScope.undoAction,
EventScope.redoAction,
EventScope.changeFrame,
];

if (logsWithCount.includes(logType)) {
return new LogWithCount(logType, payload);
if (eventsWithCount.includes(scope)) {
return new EventWithCount(scope, payload);
}

if (logType === LogType.exception) {
return new LogWithExceptionInfo(logType, payload);
if (scope === EventScope.exception) {
return new EventWithExceptionInfo(scope, payload);
}

if (logType === LogType.clickElement) {
return new LogWithControlsInfo(logType, payload);
if (scope === EventScope.clickElement) {
return new EventWithControlsInfo(scope, payload);
}

return new EventLogger(logType, payload);
return new Event(scope, payload);
}
8 changes: 4 additions & 4 deletions cvat-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import PluginRegistry from './plugins';
import serverProxy from './server-proxy';
import lambdaManager from './lambda-manager';
import { AnnotationFormats } from './annotation-formats';
import loggerStorage from './logger-storage';
import logger from './logger';
import * as enums from './enums';
import config from './config';
import { mask2Rle, rle2Mask } from './object-utils';
import User from './user';
import Project from './project';
import { Job, Task } from './session';
import { EventLogger } from './log';
import { Event } from './event';
import { Attribute, Label } from './labels';
import Statistics from './statistics';
import ObjectState from './object-state';
Expand Down Expand Up @@ -151,7 +151,7 @@ export default interface CVATCore {
register: typeof registerAction;
run: typeof runActions;
};
logger: typeof loggerStorage;
logger: typeof logger;
config: {
backendAPI: typeof config.backendAPI;
origin: typeof config.origin;
Expand Down Expand Up @@ -179,7 +179,7 @@ export default interface CVATCore {
Project: typeof Project;
Task: typeof Task;
Job: typeof Job;
EventLogger: typeof EventLogger;
Event: typeof Event;
Attribute: typeof Attribute;
Label: typeof Label;
Statistics: typeof Statistics;
Expand Down
Loading
Loading