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

Centralize demo mode DB clearing logic in ExperimentUserService #1845

Merged
merged 3 commits into from
Aug 22, 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
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { JsonController, Post, Body, UseBefore, Req, InternalServerError, Delete } from 'routing-controllers';
import {
JsonController,
Post,
Body,
UseBefore,
Req,
InternalServerError,
Delete,
Authorized,
} from 'routing-controllers';
import { ExperimentService } from '../services/ExperimentService';
import { ExperimentAssignmentService } from '../services/ExperimentAssignmentService';
import { MarkExperimentValidator } from './validators/MarkExperimentValidator';
Expand All @@ -19,7 +28,6 @@ import { ExperimentUserAliasesValidator } from './validators/ExperimentUserAlias
import { Metric } from '../models/Metric';
import * as express from 'express';
import { AppRequest } from '../../types';
import { env } from '../../env';
import flatten from 'lodash.flatten';
import { CaliperLogEnvelope } from './validators/CaliperLogEnvelope';
import { ExperimentUserValidator } from './validators/ExperimentUserValidator';
Expand Down Expand Up @@ -1090,13 +1098,9 @@ export class ExperimentClientController {
* '500':
* description: DEMO mode is disabled
*/
@Authorized()
@Delete('clearDB')
public async clearDB(@Req() request: AppRequest): Promise<string> {
// if DEMO mode is enabled, then clear the database:
if (env.app.demo) {
await this.experimentUserService.clearDB(request.logger);
return 'DB truncate successful';
}
return Promise.resolve('DEMO mode is disabled. You cannot clear DB.');
return this.experimentUserService.clearDB(request.logger);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
InternalServerError,
Delete,
Patch,
OnUndefined,
Authorized,
} from 'routing-controllers';
import { ExperimentService } from '../services/ExperimentService';
import { ExperimentAssignmentService } from '../services/ExperimentAssignmentService';
Expand All @@ -27,7 +27,6 @@ import { ExperimentUserAliasesValidator } from './validators/ExperimentUserAlias
import { Metric } from '../models/Metric';
import * as express from 'express';
import { AppRequest } from '../../types';
import { env } from '../../env';
import { MonitoredDecisionPointLog } from '../models/MonitoredDecisionPointLog';
import { Log } from '../models/Log';
import flatten from 'lodash.flatten';
Expand Down Expand Up @@ -1023,15 +1022,9 @@ export class ExperimentClientController {
* '500':
* description: DEMO mode is disabled
*/
@Authorized()
@Delete('clearDB')
@OnUndefined(204)
public clearDB(@Req() request: AppRequest) {
// if DEMO mode is enabled, then clear the database:
if (!env.app.demo) {
this.experimentUserService.clearDB(request.logger);
} else {
request.logger.error({ message: 'DEMO mode is disabled. You cannot clear DB.' });
}
return;
public async clearDB(@Req() request: AppRequest): Promise<string> {
return this.experimentUserService.clearDB(request.logger);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
InternalServerError,
Delete,
Patch,
OnUndefined,
Authorized,
} from 'routing-controllers';
import { ExperimentService } from '../services/ExperimentService';
import { ExperimentAssignmentService } from '../services/ExperimentAssignmentService';
Expand All @@ -32,7 +32,6 @@ import { ExperimentUserAliasesValidator } from './validators/ExperimentUserAlias
import { Metric } from '../models/Metric';
import * as express from 'express';
import { AppRequest } from '../../types';
import { env } from '../../env';
import { MonitoredDecisionPointLog } from '../models/MonitoredDecisionPointLog';
import { MarkExperimentValidatorv4 } from './validators/MarkExperimentValidator.v4';
import { Log } from '../models/Log';
Expand Down Expand Up @@ -979,15 +978,9 @@ export class ExperimentClientController {
* '500':
* description: DEMO mode is disabled
*/
@Authorized()
@Delete('clearDB')
@OnUndefined(204)
public clearDB(@Req() request: AppRequest) {
// if DEMO mode is enabled, then clear the database:
if (!env.app.demo) {
this.experimentUserService.clearDB(request.logger);
} else {
request.logger.error({ message: 'DEMO mode is disabled. You cannot clear DB.' });
}
return;
public async clearDB(@Req() request: AppRequest): Promise<string> {
return this.experimentUserService.clearDB(request.logger);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
InternalServerError,
Delete,
Patch,
OnUndefined,
Authorized,
} from 'routing-controllers';
import { ExperimentService } from '../services/ExperimentService';
import { ExperimentAssignmentService } from '../services/ExperimentAssignmentService';
Expand All @@ -31,7 +31,6 @@ import { MetricService } from '../services/MetricService';
import { ExperimentUserAliasesValidator } from './validators/ExperimentUserAliasesValidator';
import * as express from 'express';
import { AppRequest } from '../../types';
import { env } from '../../env';
import { MonitoredDecisionPointLog } from '../models/MonitoredDecisionPointLog';
import { MarkExperimentValidatorv5 } from './validators/MarkExperimentValidator.v5';
import { Log } from '../models/Log';
Expand Down Expand Up @@ -888,15 +887,9 @@ export class ExperimentClientController {
* '500':
* description: DEMO mode is disabled
*/
@Authorized()
@Delete('clearDB')
@OnUndefined(204)
public clearDB(@Req() request: AppRequest) {
// if DEMO mode is enabled, then clear the database:
if (!env.app.demo) {
this.experimentUserService.clearDB(request.logger);
} else {
request.logger.error({ message: 'DEMO mode is disabled. You cannot clear DB.' });
}
return;
public async clearDB(@Req() request: AppRequest): Promise<string> {
return this.experimentUserService.clearDB(request.logger);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { GroupExclusionRepository } from '../repositories/GroupExclusionReposito
import { Experiment } from '../models/Experiment';
import isEqual from 'lodash/isEqual';
import { RequestedExperimentUser } from '../controllers/validators/ExperimentUserValidator';
import { env } from '../../env';

@Service()
export class ExperimentUserService {
Expand Down Expand Up @@ -345,10 +346,16 @@ export class ExperimentUserService {
}
}

public async clearDB(logger: UpgradeLogger): Promise<void> {
public async clearDB(logger: UpgradeLogger): Promise<string> {
if (!env.app.demo) {
return 'DEMO mode is disabled. You cannot clear DB.';
}

await getConnection().transaction(async (transactionalEntityManager) => {
await this.experimentRepository.clearDB(transactionalEntityManager, logger);
});

return 'DB truncate successful';
}

/**
Expand Down
Loading