Skip to content

Commit

Permalink
hotfix for condition payload data eport issue and adding types
Browse files Browse the repository at this point in the history
  • Loading branch information
ppratikcr7 committed Nov 6, 2024
1 parent 2c3e708 commit ddb1a1d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ export interface CSVExportDataRow {
}

export interface ConditionDecisionPointData {
revertTo: string;
payload: string;
excludeIfReached: boolean;
expDecisionPointId: string;
expConditionId: string;
conditionName: string;
revertTo?: string;
payload?: string;
excludeIfReached?: boolean;
expDecisionPointId?: string;
expConditionId?: string;
conditionName?: string;
}

export interface ExperimentCSVData {
export interface ExperimentDetailsForCSVData extends ConditionDecisionPointData {
experimentId: string;
experimentName: string;
context: string[];
Expand All @@ -99,12 +99,6 @@ export interface ExperimentCSVData {
postRule: string;
enrollmentStartDate: string;
enrollmentCompleteDate: string;
revertTo: string;
payload: string;
excludeIfReached: boolean;
expDecisionPointId: string;
expConditionId: string;
conditionName: string;
details: ConditionDecisionPointData[];
}

Expand Down Expand Up @@ -445,7 +439,7 @@ export class AnalyticsRepository extends Repository<AnalyticsRepository> {
}

public async getCSVDataForSimpleExport(
experimentsData: ExperimentCSVData,
experimentsData: ExperimentDetailsForCSVData,
experimentId: string
): Promise<CSVExportDataRow[]> {
// Get the individual enrollment-related data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Experiment } from '../models/Experiment';
import repositoryError from './utils/repositoryError';
import { UpgradeLogger } from 'src/lib/logger/UpgradeLogger';
import { createGlobalExcludeSegment } from '../../../src/init/seed/globalExcludeSegment';
import { ExperimentCSVData } from './AnalyticsRepository';
import { ExperimentDetailsForCSVData } from './AnalyticsRepository';
import { StateTimeLog } from '../models/StateTimeLogs';
import { ConditionPayload } from '../models/ConditionPayload';
import { DecisionPoint } from '../models/DecisionPoint';
Expand Down Expand Up @@ -472,7 +472,7 @@ export class ExperimentRepository extends Repository<Experiment> {
return experiment;
}

public async getExperimentCSVDataExport(experimentId: string): Promise<ExperimentCSVData[]> {
public async fetchExperimentDetailsForCSVDataExport(experimentId: string): Promise<ExperimentDetailsForCSVData[]> {
// Get the experiment details
const experimentQuery = await this.createBaseQueryBuilder()
.select([
Expand Down
33 changes: 21 additions & 12 deletions backend/packages/Upgrade/src/api/services/AnalyticsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
SERVER_ERROR,
IExperimentEnrollmentStats,
} from 'upgrade_types';
import { AnalyticsRepository, CSVExportDataRow } from '../repositories/AnalyticsRepository';
import { AnalyticsRepository, CSVExportDataRow, ExperimentDetailsForCSVData } from '../repositories/AnalyticsRepository';
import { Experiment } from '../models/Experiment';
import ObjectsToCsv from 'objects-to-csv';
import fs from 'fs';
Expand All @@ -30,6 +30,7 @@ import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
import { ExperimentService } from './ExperimentService';
import { QueryService } from './QueryService';
import { HttpError } from 'routing-controllers';

dayjs.extend(utc);
dayjs.extend(timezone);
Expand Down Expand Up @@ -180,11 +181,17 @@ export class AnalyticsService {
const userRepository: UserRepository = Container.getCustomRepository(UserRepository, 'export');
const user = await userRepository.findOneBy({ email });

const experimentQueryResult = await this.experimentService.getExperimentDetailsForCSVDataExport(experimentId);
const formattedExperiments = experimentQueryResult.reduce((acc, item) => {
let experiment = acc.find((e) => e.experimentId === item.experimentId);
if (!experiment) {
experiment = {
const experimentDetails: ExperimentDetailsForCSVData[] = await this.experimentService.getExperimentDetailsForCSVDataExport(experimentId);
if (!experimentDetails || experimentDetails.length === 0) {
throw new HttpError(404, `Experiment not found for id: ${experimentId}`);
}

const experimentMap = new Map<string, ExperimentDetailsForCSVData>();

const formattedExperiment: ExperimentDetailsForCSVData[] = experimentDetails.reduce((acc, item) => {
// if the experiment is not in the map, add it
if (!experimentMap.has(item.experimentId)) {
const experiment: ExperimentDetailsForCSVData = {
experimentId: item.experimentId,
experimentName: item.experimentName,
context: item.context,
Expand All @@ -199,24 +206,26 @@ export class AnalyticsService {
enrollmentCompleteDate: item.enrollmentCompleteDate,
details: [],
};
experimentMap.set(item.experimentId, experiment);
acc.push(experiment);
}
experiment.details.push({
// add the design details for the experiment for each experimentQueryResult item
experimentMap.get(item.experimentId)?.details.push({
expConditionId: item.expConditionId,
conditionName: item.conditionName,
revertTo: item.revertTo,
payloadValue: item.payload,
payload: item.payload,
excludeIfReached: item.excludeIfReached,
expDecisionPointId: item.expDecisionPointId,
});
return acc;
}, []);

let csvExportData: CSVExportDataRow[];
if (experimentQueryResult[0].assignmentUnit === ASSIGNMENT_UNIT.WITHIN_SUBJECTS) {
if (experimentDetails[0].assignmentUnit === ASSIGNMENT_UNIT.WITHIN_SUBJECTS) {
csvExportData = await this.analyticsRepository.getCSVDataForWithInSubExport(experimentId);
} else {
csvExportData = await this.analyticsRepository.getCSVDataForSimpleExport(formattedExperiments[0], experimentId);
csvExportData = await this.analyticsRepository.getCSVDataForSimpleExport(formattedExperiment[0], experimentId);
}

const queryData = await this.logRepository.getLogPerExperimentQuery(experimentId);
Expand Down Expand Up @@ -447,7 +456,7 @@ export class AnalyticsService {
<br>
<a href="${signedURLMonitored}">Monitored Experiment Data</a>`;

const emailSubject = `Exported Data for the experiment: ${experimentQueryResult[0].experimentName}`;
const emailSubject = `Exported Data for the experiment: ${experimentDetails[0].experimentName}`;
// send email to the user
logger.info({ message: `Sending export data email to ${email}` });
try {
Expand All @@ -463,7 +472,7 @@ export class AnalyticsService {
}
await this.experimentAuditLogRepository.saveRawJson(
LOG_TYPE.EXPERIMENT_DATA_EXPORTED,
{ experimentName: experimentQueryResult[0].experimentName },
{ experimentName: experimentDetails[0].experimentName },
user
);
logger.info({ message: `Exported Data emailed successfully to ${email}` });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ import { ArchivedStatsRepository } from '../repositories/ArchivedStatsRepository
import { validate } from 'class-validator';
import { plainToClass } from 'class-transformer';
import { StratificationFactorRepository } from '../repositories/StratificationFactorRepository';
import { ExperimentCSVData } from '../repositories/AnalyticsRepository';
import { ExperimentDetailsForCSVData } from '../repositories/AnalyticsRepository';

const errorRemovePart = 'instance of ExperimentDTO has failed the validation:\n - ';
const stratificationErrorMessage =
Expand Down Expand Up @@ -225,8 +225,8 @@ export class ExperimentService {
return this.formatingConditionPayload(experiment);
}

public async getExperimentDetailsForCSVDataExport(experimentId: string): Promise<ExperimentCSVData[]> {
return await this.experimentRepository.getExperimentCSVDataExport(experimentId);
public async getExperimentDetailsForCSVDataExport(experimentId: string): Promise<ExperimentDetailsForCSVData[]> {
return await this.experimentRepository.fetchExperimentDetailsForCSVDataExport(experimentId);
}

public getTotalCount(): Promise<number> {
Expand Down

0 comments on commit ddb1a1d

Please sign in to comment.