Skip to content

Commit

Permalink
Merge pull request #2142 from CarnegieLearningWeb/bugfix/enrollmentDa…
Browse files Browse the repository at this point in the history
…te-issue-2099

Resolved enrollmentDate missing issue in export data and experiment details state time logs
  • Loading branch information
ppratikcr7 authored Dec 8, 2024
2 parents 753a085 + 5bc1f6e commit 9888aaf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ export class ExperimentController {
* @swagger
* /experiments/{id}:
* put:
* description: Create New Experiment
* description: Update Experiment
* consumes:
* - application/json
* parameters:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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 @@ -1067,17 +1066,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
39 changes: 31 additions & 8 deletions backend/packages/Upgrade/src/api/services/ExperimentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,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 @@ -459,14 +473,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 @@ -785,6 +792,13 @@ export class ExperimentService {
let experimentDoc: Experiment;
try {
experimentDoc = await transactionalEntityManager.getRepository(Experiment).save(expDoc);
// 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 @@ -1228,6 +1242,15 @@ export class ExperimentService {
let experimentDoc: Experiment;
try {
experimentDoc = await transactionalEntityManager.getRepository(Experiment).save(expDoc);
// 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;
error.details = 'Error in adding experiment in DB';
Expand Down

0 comments on commit 9888aaf

Please sign in to comment.