From ddb1a1d882c52f80bd7a2f19a18fb18b98ec218a Mon Sep 17 00:00:00 2001 From: pratik Date: Wed, 6 Nov 2024 19:37:15 +0530 Subject: [PATCH] hotfix for condition payload data eport issue and adding types --- .../api/repositories/AnalyticsRepository.ts | 22 +++++-------- .../api/repositories/ExperimentRepository.ts | 4 +-- .../src/api/services/AnalyticsService.ts | 33 ++++++++++++------- .../src/api/services/ExperimentService.ts | 6 ++-- 4 files changed, 34 insertions(+), 31 deletions(-) diff --git a/backend/packages/Upgrade/src/api/repositories/AnalyticsRepository.ts b/backend/packages/Upgrade/src/api/repositories/AnalyticsRepository.ts index b0c8f34b0d..2265c823df 100644 --- a/backend/packages/Upgrade/src/api/repositories/AnalyticsRepository.ts +++ b/backend/packages/Upgrade/src/api/repositories/AnalyticsRepository.ts @@ -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[]; @@ -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[]; } @@ -445,7 +439,7 @@ export class AnalyticsRepository extends Repository { } public async getCSVDataForSimpleExport( - experimentsData: ExperimentCSVData, + experimentsData: ExperimentDetailsForCSVData, experimentId: string ): Promise { // Get the individual enrollment-related data diff --git a/backend/packages/Upgrade/src/api/repositories/ExperimentRepository.ts b/backend/packages/Upgrade/src/api/repositories/ExperimentRepository.ts index 645e5725b0..414299a5cc 100644 --- a/backend/packages/Upgrade/src/api/repositories/ExperimentRepository.ts +++ b/backend/packages/Upgrade/src/api/repositories/ExperimentRepository.ts @@ -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'; @@ -472,7 +472,7 @@ export class ExperimentRepository extends Repository { return experiment; } - public async getExperimentCSVDataExport(experimentId: string): Promise { + public async fetchExperimentDetailsForCSVDataExport(experimentId: string): Promise { // Get the experiment details const experimentQuery = await this.createBaseQueryBuilder() .select([ diff --git a/backend/packages/Upgrade/src/api/services/AnalyticsService.ts b/backend/packages/Upgrade/src/api/services/AnalyticsService.ts index 886955d457..8b3e977d8a 100644 --- a/backend/packages/Upgrade/src/api/services/AnalyticsService.ts +++ b/backend/packages/Upgrade/src/api/services/AnalyticsService.ts @@ -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'; @@ -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); @@ -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(); + + 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, @@ -199,13 +206,15 @@ 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, }); @@ -213,10 +222,10 @@ export class AnalyticsService { }, []); 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); @@ -447,7 +456,7 @@ export class AnalyticsService {
Monitored Experiment Data`; - 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 { @@ -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}` }); diff --git a/backend/packages/Upgrade/src/api/services/ExperimentService.ts b/backend/packages/Upgrade/src/api/services/ExperimentService.ts index 217851fd6c..0ffaf9ffb9 100644 --- a/backend/packages/Upgrade/src/api/services/ExperimentService.ts +++ b/backend/packages/Upgrade/src/api/services/ExperimentService.ts @@ -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 = @@ -225,8 +225,8 @@ export class ExperimentService { return this.formatingConditionPayload(experiment); } - public async getExperimentDetailsForCSVDataExport(experimentId: string): Promise { - return await this.experimentRepository.getExperimentCSVDataExport(experimentId); + public async getExperimentDetailsForCSVDataExport(experimentId: string): Promise { + return await this.experimentRepository.fetchExperimentDetailsForCSVDataExport(experimentId); } public getTotalCount(): Promise {