Skip to content

Commit

Permalink
adds policy parameters to single experiment fetch (#2197)
Browse files Browse the repository at this point in the history
* adds policy parameters to single experiment fetch

* add missed await, clean up imports
  • Loading branch information
danoswaltCL authored Feb 10, 2025
1 parent 29c5fb3 commit 2eb265d
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 8 deletions.
2 changes: 1 addition & 1 deletion backend/packages/Upgrade/.env.docker.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ EMAIL_BUCKET="s3_bucket"
# Mooclets
#

MOOCLETS_ENABLED = true/false
MOOCLETS_ENABLED = false
MOOCLETS_HOST_URL = mooclet_host_url
MOOCLETS_API_ROUTE = /engine/api/v1
MOOCLETS_API_TOKEN = some_token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { MoocletExperimentService } from '../services/MoocletExperimentService';
import { env } from '../../env';
import { NotFoundException } from '@nestjs/common/exceptions';
import { ExperimentIdValidator } from '../DTO/ExperimentDTO';
import { SUPPORTED_MOOCLET_ALGORITHMS } from 'upgrade_types';

interface ExperimentPaginationInfo extends PaginationResponse {
nodes: Experiment[];
Expand Down Expand Up @@ -828,11 +829,24 @@ export class ExperimentController {
*/
@Get('/single/:id')
@OnUndefined(ExperimentNotFoundError)
public one(
public async one(
@Params({ validate: true }) { id }: ExperimentIdValidator,
@Req() request: AppRequest
): Promise<ExperimentDTO> {
return this.experimentService.getSingleExperiment(id, request.logger);
const experiment = await this.experimentService.getSingleExperiment(id, request.logger);

if (SUPPORTED_MOOCLET_ALGORITHMS.includes(experiment.assignmentAlgorithm)) {
if (!env.mooclets?.enabled) {
throw new BadRequestError(
'MoocletPolicyParameters are present in the experiment but Mooclet is not enabled in the environment'
);
} else {
const policyParametersResponse = await this.moocletExperimentService.getPolicyParametersByExperimentId(id);
experiment.moocletPolicyParameters = policyParametersResponse.parameters;
}
}

return experiment;
}

/**
Expand Down
13 changes: 13 additions & 0 deletions backend/packages/Upgrade/src/api/services/MoocletDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ export class MoocletDataService {
return response;
}

public async getPolicyParameters(policyParametersId: number): Promise<MoocletPolicyParametersResponseDetails> {
const endpoint = `/policyparameters/${policyParametersId}`;
const requestParams: MoocletProxyRequestParams = {
method: 'GET',
url: this.apiUrl + endpoint,
apiToken: this.apiToken,
};

const response = await this.fetchExternalMoocletsData(requestParams);

return response;
}

public async deletePolicyParameters(policyParametersId: number): Promise<any> {
const endpoint = `/policyparameters/${policyParametersId}`;
const requestParams: MoocletProxyRequestParams = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import { ConditionValidator, ExperimentDTO } from '../DTO/ExperimentDTO';
import { UserDTO } from '../DTO/UserDTO';
import { Experiment } from '../models/Experiment';
import { UpgradeLogger } from '../../lib/logger/UpgradeLogger';
import { ASSIGNMENT_ALGORITHM, MOOCLET_POLICY_SCHEMA_MAP, MoocletPolicyParametersDTO } from 'upgrade_types';
import { ASSIGNMENT_ALGORITHM, MoocletPolicyParametersDTO, SUPPORTED_MOOCLET_ALGORITHMS } from 'upgrade_types';
import { ExperimentCondition } from '../models/ExperimentCondition';

export interface SyncCreateParams {
Expand Down Expand Up @@ -409,6 +409,17 @@ export class MoocletExperimentService extends ExperimentService {
}
}

public async getPolicyParametersByExperimentId(
experimentId: string
): Promise<MoocletPolicyParametersResponseDetails> {
try {
const moocletExperimentRef = await this.getMoocletExperimentRefByUpgradeExperimentId(experimentId);
return await this.moocletDataService.getPolicyParameters(moocletExperimentRef.policyParametersId);
} catch (err) {
throw new Error(`Failed to get Mooclet policy parameters: ${err}`);
}
}

private async createMooclet(newMoocletRequest: MoocletRequestBody): Promise<MoocletResponseDetails> {
try {
return await this.moocletDataService.postNewMooclet(newMoocletRequest);
Expand Down Expand Up @@ -551,6 +562,6 @@ export class MoocletExperimentService extends ExperimentService {
}

public isMoocletExperiment(assignmentAlgorithm: ASSIGNMENT_ALGORITHM): boolean {
return Object.keys(MOOCLET_POLICY_SCHEMA_MAP).includes(assignmentAlgorithm);
return SUPPORTED_MOOCLET_ALGORITHMS.includes(assignmentAlgorithm);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { AppState } from '../core.state';
import { ASSIGNMENT_ALGORITHM, ASSIGNMENT_UNIT, MOOCLET_POLICY_SCHEMA_MAP } from 'upgrade_types';
import { ASSIGNMENT_ALGORITHM, ASSIGNMENT_UNIT, SUPPORTED_MOOCLET_ALGORITHMS } from 'upgrade_types';
import {
ExperimentDecisionPoint,
ExperimentCondition,
Expand Down Expand Up @@ -87,7 +87,7 @@ export class ExperimentDesignStepperService {
currentAssignmentAlgorithm$ = new BehaviorSubject<ASSIGNMENT_ALGORITHM>(ASSIGNMENT_ALGORITHM.RANDOM);
isMoocletExperimentDesign$ = this.currentAssignmentAlgorithm$.pipe(
map((algorithm) => {
return environment.moocletToggle && Object.keys(MOOCLET_POLICY_SCHEMA_MAP).includes(algorithm);
return environment.moocletToggle && SUPPORTED_MOOCLET_ALGORITHMS.includes(algorithm);
})
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
EXPERIMENT_STATE,
EXPERIMENT_TYPE,
MOOCLET_POLICY_SCHEMA_MAP,
SUPPORTED_MOOCLET_ALGORITHMS,
} from 'upgrade_types';
import {
NewExperimentDialogEvents,
Expand Down Expand Up @@ -106,7 +107,7 @@ export class ExperimentOverviewComponent implements OnInit, OnDestroy {
this.unitOfAssignments.push({ value: ASSIGNMENT_UNIT.WITHIN_SUBJECTS });
}
if (this.environment.moocletToggle) {
const supportedMoocletAlgorithms = Object.keys(MOOCLET_POLICY_SCHEMA_MAP) as ASSIGNMENT_ALGORITHM[];
const supportedMoocletAlgorithms = SUPPORTED_MOOCLET_ALGORITHMS as ASSIGNMENT_ALGORITHM[];
supportedMoocletAlgorithms.forEach((algorithmName) => {
this.assignmentAlgorithms.push({ value: algorithmName });
});
Expand Down
3 changes: 3 additions & 0 deletions types/src/Mooclet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ const MOOCLET_POLICY_SCHEMA_MAP = {
[ASSIGNMENT_ALGORITHM.MOOCLET_TS_CONFIGURABLE]: MoocletTSConfigurablePolicyParametersDTO,
};

const SUPPORTED_MOOCLET_ALGORITHMS = Object.keys(MOOCLET_POLICY_SCHEMA_MAP);

export {
MOOCLET_POLICY_SCHEMA_MAP,
SUPPORTED_MOOCLET_ALGORITHMS,
Prior,
CurrentPosteriors,
MoocletPolicyParametersDTO,
Expand Down
1 change: 1 addition & 0 deletions types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,5 @@ export {
MoocletPolicyParametersDTO,
MoocletTSConfigurablePolicyParametersDTO,
MOOCLET_POLICY_SCHEMA_MAP,
SUPPORTED_MOOCLET_ALGORITHMS,
} from './Mooclet';

0 comments on commit 2eb265d

Please sign in to comment.