Skip to content

Commit

Permalink
Merge branch 'dev' into feature/mooclet-backend-create-delete-experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
danoswaltCL authored Dec 10, 2024
2 parents e227781 + 43d74e9 commit b3901f2
Show file tree
Hide file tree
Showing 21 changed files with 1,719 additions and 2,897 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ export class ExperimentController {
* @swagger
* /experiments/{id}:
* put:
* description: Create New Experiment
* description: Update Experiment
* consumes:
* - application/json
* parameters:
Expand Down
47 changes: 47 additions & 0 deletions backend/packages/Upgrade/src/api/controllers/SegmentController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Segment } from '../models/Segment';
import { AppRequest } from '../../types';
import {
IdValidator,
ListInputValidator,
SegmentFile,
SegmentIds,
SegmentIdValidator,
Expand Down Expand Up @@ -60,6 +61,17 @@ export interface getSegmentData {
* items:
* type: string
* example: '5812a759-1dcf-47a8-b0ba-26c89092863e'
* ListInput:
* allOf:
* - $ref: '#/definitions/Segment'
* required:
* - parentSegmentId
* - listType
* properties:
* parentSegmentId:
* type: string
* listType:
* type: string
* segmentResponse:
* description: ''
* type: object
Expand Down Expand Up @@ -343,6 +355,41 @@ export class SegmentController {
return this.segmentService.upsertSegment(segment, request.logger);
}

/**
* @swagger
* /list:
* post:
* description: Create a new list
* tags:
* - Segment
* produces:
* - application/json
* parameters:
* - in: body
* name: list
* description: List object
* required: true
* schema:
* type: object
* $ref: '#/definitions/ListInput'
* responses:
* '200':
* description: Create a new list
* schema:
* $ref: '#/definitions/segmentResponse'
* '401':
* description: Authorization Required Error
* '500':
* description: Internal Server Error, Insert Error in database, SegmentId is not valid, JSON format is not valid
*/
@Post('/list')
public addList(
@Body({ validate: true }) listInput: ListInputValidator,
@Req() request: AppRequest
): Promise<Segment> {
return this.segmentService.addList(listInput, request.logger);
}

/**
* @swagger
* /segments/{segmentId}:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export class SegmentInputValidator {
@IsOptional()
public description?: string;

@IsString()
@IsOptional()
public listType?: string;

@IsNotEmpty()
@IsString()
public context: string;
Expand All @@ -46,6 +50,12 @@ export class SegmentInputValidator {
public subSegmentIds: string[];
}

export class ListInputValidator extends SegmentInputValidator {
@IsNotEmpty()
@IsUUID()
public parentSegmentId: string;
}

export class IdValidator {
@IsNotEmpty()
@IsUUID()
Expand Down
5 changes: 5 additions & 0 deletions backend/packages/Upgrade/src/api/models/Segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export class Segment extends BaseModel {
})
public description: string;

@Column({
nullable: true,
})
public listType: string;

@Column()
public context: string;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { GroupExclusionRepository } from './GroupExclusionRepository';
import { DecisionPoint } from '../models/DecisionPoint';
import { MonitoredDecisionPointLog } from '../models/MonitoredDecisionPointLog';
import { ExperimentCondition } from '../models/ExperimentCondition';
import { MonitoredDecisionPointRepository } from './MonitoredDecisionPointRepository';
import { UserStratificationFactorRepository } from './UserStratificationRepository';
import _ from 'lodash';
import { Repository } from 'typeorm';
Expand Down Expand Up @@ -448,6 +447,7 @@ export class AnalyticsRepository extends Repository<AnalyticsRepository> {
.createQueryBuilder('individualEnrollment')
.select([
'"individualEnrollment"."userId" as "userId"',
'"individualEnrollment"."createdAt" as "createdAt"',
'"individualEnrollment"."groupId" as "enrollmentGroupId"',
'"individualEnrollment"."enrollmentCode" as "enrollmentCode"',
'"individualEnrollment"."experimentId" as "experimentId"',
Expand All @@ -467,39 +467,13 @@ export class AnalyticsRepository extends Repository<AnalyticsRepository> {
.addGroupBy('individualEnrollment.experimentId')
.addGroupBy('individualEnrollment.conditionId')
.addGroupBy('individualEnrollment.partitionId')
.addGroupBy('individualEnrollment.createdAt')
.addGroupBy('decisionPointData.site')
.addGroupBy('decisionPointData.target')
.orderBy('individualEnrollment.userId', 'ASC')
.where('individualEnrollment.experimentId = :experimentId::uuid', { experimentId });

const monitoredDecisionPointRepository = Container.getCustomRepository(MonitoredDecisionPointRepository, 'export');
const monitoredDecisionPointQuery = monitoredDecisionPointRepository
.createQueryBuilder('monitoredDecisionPoint')
.select([
'monitoredDecisionPoint.site as "site"',
'monitoredDecisionPoint.target as "target"',
'monitoredDecisionPoint.userId as "userId"',
'"monitoredPointLogs"."createdAt" as "markExperimentPointTime"',
])
.leftJoin('monitoredDecisionPoint.monitoredPointLogs', 'monitoredPointLogs')
.orderBy('monitoredDecisionPoint.userId', 'ASC')
.where('monitoredDecisionPoint.experimentId = :experimentId', { experimentId })
.andWhere(
'monitoredDecisionPoint.userId IN (' +
individualEnrollmentRepository
.createQueryBuilder('individualEnrollment')
.select('DISTINCT "individualEnrollment"."userId"')
.where('"individualEnrollment"."experimentId" = :experimentId::uuid', { experimentId })
.getQuery() +
')'
)
.setParameters(individualEnrollmentQuery.getParameters());

const [individualEnrollmentQueryResults, monitoredDecisionPointQueryResults] = await Promise.all([
individualEnrollmentQuery.getRawMany(),
monitoredDecisionPointQuery.getRawMany(),
]);

const individualEnrollmentQueryResults = await individualEnrollmentQuery.getRawMany();
const userStratificationFactorUserList = [];

const individualEnrollmentExperimentData = [];
Expand All @@ -526,6 +500,7 @@ export class AnalyticsRepository extends Repository<AnalyticsRepository> {
expConditionId: individualEnrollmentQueryResult.conditionId,
site: individualEnrollmentQueryResult.site,
target: individualEnrollmentQueryResult.target,
markExperimentPointTime: individualEnrollmentQueryResult.createdAt,
});
}
});
Expand All @@ -552,32 +527,14 @@ export class AnalyticsRepository extends Repository<AnalyticsRepository> {
userStratificationFactorQueryResult = await userStratificationFactorQuery.getRawMany();
}

// Group monitoredDecisionPointQueryResults by userId
const groupedMonitoredDecisionPointQueryResults = _.groupBy(monitoredDecisionPointQueryResults, 'userId');

// Combine data in a single flatMap step
const combinedCSVExportData = _.flatMap(individualEnrollmentExperimentData, (individualEnrollmentExperiment) => {
const userMonitoredResults =
groupedMonitoredDecisionPointQueryResults[individualEnrollmentExperiment.userId] || [];

return userMonitoredResults
.filter(
(monitoredDecisionPointQueryResult) =>
monitoredDecisionPointQueryResult.site === individualEnrollmentExperiment.site &&
monitoredDecisionPointQueryResult.target === individualEnrollmentExperiment.target
)
.map((monitoredDecisionPointQueryResult) => ({
...individualEnrollmentExperiment,
site: monitoredDecisionPointQueryResult.site,
target: monitoredDecisionPointQueryResult.target,
markExperimentPointTime: monitoredDecisionPointQueryResult.markExperimentPointTime,
stratificationValue: experimentsData.stratification
? userStratificationFactorQueryResult.find((user) => user.userId === individualEnrollmentExperiment.userId)
?.stratificationFactorValue
: null,
}));
});
return combinedCSVExportData;
return individualEnrollmentExperimentData.map((individualEnrollmentExperiment) => ({
...individualEnrollmentExperiment,
enrollmentGroupId: individualEnrollmentExperiment.groupId,
stratificationValue: experimentsData.stratification
? userStratificationFactorQueryResult.find((user) => user.userId === individualEnrollmentExperiment.userId)
?.stratificationFactorValue
: null,
}));
}

public async getCSVDataForWithInSubExport(experimentId: string): Promise<CSVExportDataRow[]> {
Expand Down
Loading

0 comments on commit b3901f2

Please sign in to comment.