From d14df62c84324424589986f11b834030ef4f0352 Mon Sep 17 00:00:00 2001 From: Yagnik Date: Wed, 11 Oct 2023 15:04:38 +0530 Subject: [PATCH 1/3] subsegments of used segment will show as used in segments list --- .../Upgrade/src/api/services/SegmentService.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/backend/packages/Upgrade/src/api/services/SegmentService.ts b/backend/packages/Upgrade/src/api/services/SegmentService.ts index 14ceb02b78..89469a980d 100644 --- a/backend/packages/Upgrade/src/api/services/SegmentService.ts +++ b/backend/packages/Upgrade/src/api/services/SegmentService.ts @@ -110,6 +110,24 @@ export class SegmentService { }); } + segmentsData.forEach((segment) => { + segmentsUsedLockedList.forEach((usedSegment) => { + if (segment.id === usedSegment) { + segment.subSegments.forEach((subSegment) => { + segmentsUsedLockedList.push(subSegment.id); + }); + } + }); + + segmentsUsedUnlockedList.forEach((usedSegment) => { + if (segment.id === usedSegment) { + segment.subSegments.forEach((subSegment) => { + segmentsUsedUnlockedList.push(subSegment.id); + }); + } + }); + }); + const segmentsDataWithStatus = segmentsData.map((segment) => { if (segment.id === globalExcludeSegment.id) { return { ...segment, status: SEGMENT_STATUS.GLOBAL }; From 6d80f8f7412752b524f1e844da24cdf2a8d7a69a Mon Sep 17 00:00:00 2001 From: Yagnik Date: Thu, 12 Oct 2023 13:32:36 +0530 Subject: [PATCH 2/3] code refactoring and removing locked unclocked status conditions --- .../src/api/services/SegmentService.ts | 37 ++++--------------- 1 file changed, 8 insertions(+), 29 deletions(-) diff --git a/backend/packages/Upgrade/src/api/services/SegmentService.ts b/backend/packages/Upgrade/src/api/services/SegmentService.ts index 89469a980d..6ced284076 100644 --- a/backend/packages/Upgrade/src/api/services/SegmentService.ts +++ b/backend/packages/Upgrade/src/api/services/SegmentService.ts @@ -5,7 +5,7 @@ import { IndividualForSegmentRepository } from '../repositories/IndividualForSeg import { GroupForSegmentRepository } from '../repositories/GroupForSegmentRepository'; import { Segment } from '../models/Segment'; import { UpgradeLogger } from '../../lib/logger/UpgradeLogger'; -import { EXPERIMENT_STATE, SEGMENT_TYPE, SERVER_ERROR, SEGMENT_STATUS } from 'upgrade_types'; +import { SEGMENT_TYPE, SERVER_ERROR, SEGMENT_STATUS } from 'upgrade_types'; import { getConnection } from 'typeorm'; import uuid from 'uuid'; import { ErrorWithType } from '../errors/ErrorWithType'; @@ -85,45 +85,26 @@ export class SegmentService { const allExperimentSegmentsInclusion = await this.getExperimentSegmenInclusionData(); const allExperimentSegmentsExclusion = await this.getExperimentSegmenExclusionData(); - const segmentsUsedLockedList = []; - const segmentsUsedUnlockedList = []; + const segmentsUsedList = []; if (allExperimentSegmentsInclusion) { allExperimentSegmentsInclusion.forEach((ele) => { const subSegments = ele.segment.subSegments; - subSegments.forEach((subSegment) => { - ele.experiment.state === EXPERIMENT_STATE.ENROLLING - ? segmentsUsedLockedList.push(subSegment.id) - : segmentsUsedUnlockedList.push(subSegment.id); - }); + segmentsUsedList.push(...subSegments.map((subSegment) => subSegment.id)); }); } if (allExperimentSegmentsExclusion) { allExperimentSegmentsExclusion.forEach((ele) => { const subSegments = ele.segment.subSegments; - subSegments.forEach((subSegment) => { - ele.experiment.state === EXPERIMENT_STATE.ENROLLING - ? segmentsUsedLockedList.push(subSegment.id) - : segmentsUsedUnlockedList.push(subSegment.id); - }); + segmentsUsedList.push(...subSegments.map((subSegment) => subSegment.id)); }); } segmentsData.forEach((segment) => { - segmentsUsedLockedList.forEach((usedSegment) => { + segmentsUsedList.forEach((usedSegment) => { if (segment.id === usedSegment) { - segment.subSegments.forEach((subSegment) => { - segmentsUsedLockedList.push(subSegment.id); - }); - } - }); - - segmentsUsedUnlockedList.forEach((usedSegment) => { - if (segment.id === usedSegment) { - segment.subSegments.forEach((subSegment) => { - segmentsUsedUnlockedList.push(subSegment.id); - }); + segmentsUsedList.push(...segment.subSegments.map((subSegment) => subSegment.id)); } }); }); @@ -131,10 +112,8 @@ export class SegmentService { const segmentsDataWithStatus = segmentsData.map((segment) => { if (segment.id === globalExcludeSegment.id) { return { ...segment, status: SEGMENT_STATUS.GLOBAL }; - } else if (segmentsUsedLockedList.find((segmentId) => segmentId === segment.id)) { - return { ...segment, status: SEGMENT_STATUS.USED }; // TODO change to locked - } else if (segmentsUsedUnlockedList.find((segmentId) => segmentId === segment.id)) { - return { ...segment, status: SEGMENT_STATUS.USED }; // TODO change to unlocked + } else if (segmentsUsedList.find((segmentId) => segmentId === segment.id)) { + return { ...segment, status: SEGMENT_STATUS.USED }; } else { return { ...segment, status: SEGMENT_STATUS.UNUSED }; } From c2ab0b484dd3ac8f3731535ea57db05f34bb6425 Mon Sep 17 00:00:00 2001 From: Yagnik Date: Fri, 13 Oct 2023 12:39:03 +0530 Subject: [PATCH 3/3] Resolved review comments --- .../Upgrade/src/api/services/SegmentService.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/backend/packages/Upgrade/src/api/services/SegmentService.ts b/backend/packages/Upgrade/src/api/services/SegmentService.ts index 6ced284076..b75e5f2c32 100644 --- a/backend/packages/Upgrade/src/api/services/SegmentService.ts +++ b/backend/packages/Upgrade/src/api/services/SegmentService.ts @@ -81,9 +81,11 @@ export class SegmentService { public async getAllSegmentWithStatus(logger: UpgradeLogger): Promise { const segmentsData = await getConnection().transaction(async () => { - const segmentsData = await this.getAllSegments(logger); - const allExperimentSegmentsInclusion = await this.getExperimentSegmenInclusionData(); - const allExperimentSegmentsExclusion = await this.getExperimentSegmenExclusionData(); + const [segmentsData, allExperimentSegmentsInclusion, allExperimentSegmentsExclusion] = await Promise.all([ + this.getAllSegments(logger), + this.getExperimentSegmenInclusionData(), + this.getExperimentSegmenExclusionData(), + ]); const segmentsUsedList = []; @@ -102,17 +104,15 @@ export class SegmentService { } segmentsData.forEach((segment) => { - segmentsUsedList.forEach((usedSegment) => { - if (segment.id === usedSegment) { - segmentsUsedList.push(...segment.subSegments.map((subSegment) => subSegment.id)); - } - }); + if (segmentsUsedList.includes(segment.id)) { + segmentsUsedList.push(...segment.subSegments.map((subSegment) => subSegment.id)); + } }); const segmentsDataWithStatus = segmentsData.map((segment) => { if (segment.id === globalExcludeSegment.id) { return { ...segment, status: SEGMENT_STATUS.GLOBAL }; - } else if (segmentsUsedList.find((segmentId) => segmentId === segment.id)) { + } else if (segmentsUsedList.includes(segment.id)) { return { ...segment, status: SEGMENT_STATUS.USED }; } else { return { ...segment, status: SEGMENT_STATUS.UNUSED };