Skip to content

Commit

Permalink
having a common method to define statetimelogs and proper fromState v…
Browse files Browse the repository at this point in the history
…alue stored
  • Loading branch information
ppratikcr7 committed Dec 4, 2024
1 parent 18331c8 commit 1d66793
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import isequal from 'lodash.isequal';
import flatten from 'lodash.flatten';
import { ILogInput, ENROLLMENT_CODE } from 'upgrade_types';
import { StateTimeLogsRepository } from '../repositories/StateTimeLogsRepository';
import { StateTimeLog } from '../models/StateTimeLogs';
import { UpgradeLogger } from '../../lib/logger/UpgradeLogger';
import { SegmentService } from './SegmentService';
import { MonitoredDecisionPointLogRepository } from '../repositories/MonitoredDecisionPointLogRepository';
Expand Down Expand Up @@ -1069,17 +1068,14 @@ export class ExperimentAssignmentService {
private async checkEnrollmentEndingCriteriaForCount(experiment: Experiment, logger: UpgradeLogger): Promise<void> {
const { enrollmentCompleteCondition } = experiment;
const { groupCount, userCount } = enrollmentCompleteCondition;

const timeLogDate = new Date();
/**
* Create stateTimeLog document which will be inserted if ending criteria is met
*/
const stateTimeLogDoc = new StateTimeLog();
stateTimeLogDoc.id = uuid();
stateTimeLogDoc.fromState = experiment.state;
stateTimeLogDoc.toState = EXPERIMENT_STATE.ENROLLMENT_COMPLETE;
stateTimeLogDoc.timeLog = timeLogDate;
stateTimeLogDoc.experiment = experiment;
const stateTimeLogDoc = await this.experimentService.prepareStateTimeLogDoc(
experiment,
experiment.state,
EXPERIMENT_STATE.ENROLLMENT_COMPLETE
);

if (groupCount && userCount && experiment.assignmentUnit === ASSIGNMENT_UNIT.GROUP) {
const groupSatisfied: number = await this.getGroupAssignmentStatus(experiment.id, logger);
Expand Down
55 changes: 30 additions & 25 deletions backend/packages/Upgrade/src/api/services/ExperimentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,20 @@ export class ExperimentService {
return [...conditionIds, ...decisionPointsIds];
}

public async prepareStateTimeLogDoc(
experimentDoc: Experiment,
fromState: EXPERIMENT_STATE,
toState: EXPERIMENT_STATE
) {
const stateTimeLogDoc = new StateTimeLog();
stateTimeLogDoc.id = uuid();
stateTimeLogDoc.fromState = fromState;
stateTimeLogDoc.toState = toState;
stateTimeLogDoc.timeLog = new Date();
stateTimeLogDoc.experiment = experimentDoc;
return stateTimeLogDoc;
}

public async updateState(
experimentId: string,
state: EXPERIMENT_STATE,
Expand Down Expand Up @@ -456,14 +470,7 @@ export class ExperimentService {
// add experiment audit logs
await this.experimentAuditLogRepository.saveRawJson(LOG_TYPE.EXPERIMENT_STATE_CHANGED, data, user, entityManager);

const timeLogDate = new Date();

const stateTimeLogDoc = new StateTimeLog();
stateTimeLogDoc.id = uuid();
stateTimeLogDoc.fromState = oldExperiment.state;
stateTimeLogDoc.toState = state;
stateTimeLogDoc.timeLog = timeLogDate;
stateTimeLogDoc.experiment = oldExperiment;
const stateTimeLogDoc = await this.prepareStateTimeLogDoc(oldExperiment, oldExperiment.state, state);

// updating the experiment and stateTimeLog
const stateTimeLogRepo = entityManager ? entityManager.getRepository(StateTimeLog) : this.stateTimeLogsRepository;
Expand Down Expand Up @@ -782,10 +789,13 @@ export class ExperimentService {
let experimentDoc: Experiment;
try {
experimentDoc = await transactionalEntityManager.getRepository(Experiment).save(expDoc);
// Store state time logs for the experiment in enrolling state.
if (experimentDoc.state === EXPERIMENT_STATE.ENROLLING) {
this.updateStateTimeLogs(experimentDoc, transactionalEntityManager);
}
// Store state time log for the experiment
const stateTimeLogDoc = await this.prepareStateTimeLogDoc(
experimentDoc,
oldExperiment.state,
experimentDoc.state
);
await transactionalEntityManager.getRepository(StateTimeLog).save(stateTimeLogDoc);
} catch (err) {
const error = err as ErrorWithType;
error.details = `Error in updating experiment document "updateExperimentInDB"`;
Expand Down Expand Up @@ -1138,16 +1148,6 @@ export class ExperimentService {
});
}

async updateStateTimeLogs(experimentDoc: Experiment, transactionalEntityManager: EntityManager) {
const stateTimeLogDoc = new StateTimeLog();
stateTimeLogDoc.id = uuid();
stateTimeLogDoc.fromState = EXPERIMENT_STATE.INACTIVE;
stateTimeLogDoc.toState = experimentDoc.state;
stateTimeLogDoc.timeLog = new Date();
stateTimeLogDoc.experiment = experimentDoc;
await transactionalEntityManager.getRepository(StateTimeLog).save(stateTimeLogDoc);
}

// private async cleanLogsForQuery(query: Query[]): Promise<void> {
// const result = await Promise.all(
// query.map(({ metric: { key } }) => {
Expand Down Expand Up @@ -1239,9 +1239,14 @@ export class ExperimentService {
let experimentDoc: Experiment;
try {
experimentDoc = await transactionalEntityManager.getRepository(Experiment).save(expDoc);
// Store state time logs for the experiment in enrolling state.
if (experimentDoc.state === EXPERIMENT_STATE.ENROLLING) {
this.updateStateTimeLogs(experimentDoc, transactionalEntityManager);
// Store state time log for the experiment in enrolling state.
if (experimentDoc.state !== EXPERIMENT_STATE.INACTIVE) {
const stateTimeLogDoc = await this.prepareStateTimeLogDoc(
experimentDoc,
EXPERIMENT_STATE.INACTIVE,
experimentDoc.state
);
await transactionalEntityManager.getRepository(StateTimeLog).save(stateTimeLogDoc);
}
} catch (err) {
const error = err as ErrorWithType;
Expand Down

0 comments on commit 1d66793

Please sign in to comment.