Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds policy parameters to single experiment fetch #2197

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Collaborator

@VivekFitkariwala VivekFitkariwala Feb 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if check should be a util function which should throw an error if the experiment contains a valid mooclet algorithm and mooclet is not enabled, otherwise return true.
It is getting in the create function as well and even Ben is using this in his PR

Might be in a separate PR or refactoring.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure that makes sense, let me make a separate ticket for that for follow-up

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';
Loading