Skip to content

Commit

Permalink
Persist code changes in condition table when factor update (#1376)
Browse files Browse the repository at this point in the history
* Persist code changes in condition table which aren't related to factor table update

* Code clean up

* resolved issue of condition values not persisting while creating new experiment

* resolved review comment to remove isAnyRowRemoved

* resolved review comment to remove isAnyRowRemoved in factorial design stepper comp
  • Loading branch information
Yagnik56 authored Mar 20, 2024
1 parent 17cef93 commit 04e2cd5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,38 @@ export class ExperimentDesignStepperService {
return tableData;
}

editFactorialConditionTableData(
designData: ExperimentFactorialDesignData,
oldTableData: FactorialConditionTableRowData[]
): FactorialConditionTableRowData[] {
const tableData: FactorialConditionTableRowData[] = [];
const requiredFactorialTableData = this.factorDataToConditions(designData.factors);

requiredFactorialTableData.map((conditionData) => {
const conditionLevelsData = this.filterLevelsData(conditionData);
const conditionstring = this.createConditionString(conditionData);
const condition = oldTableData.filter((conditionData) => {
return conditionData.condition === conditionstring;
});

if (condition.length) {
tableData.push({ ...condition[0], id: uuidv4() });
} else {
const tableRow: FactorialConditionTableRowData = {
id: uuidv4(), // TODO: maybe not the right place?
levels: conditionLevelsData,
condition: conditionstring,
payload: conditionstring,
weight: '0.0',
include: true,
};
tableData.push(tableRow);
}
});

return tableData;
}

factorDataToConditions(factorsData: ExperimentFactorData[], levelsCombinationData: FactorLevelData[] = []) {
// return if no data in factors
if (factorsData.length === 0) {
Expand Down Expand Up @@ -485,7 +517,7 @@ export class ExperimentDesignStepperService {
let conditionIndex = 1;
tableData.forEach((factorialConditionTableRow) => {
factorialConditionsRequestObject.push({
id: factorialConditionTableRow.id,
id: factorialConditionTableRow.id || uuidv4(),
name: factorialConditionTableRow.condition,
conditionCode: factorialConditionTableRow.condition,
assignmentWeight: parseFloat(factorialConditionTableRow.weight),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@
></mat-row>
</mat-table>
<div class="validation-container">
<span class="ft-14-600 validation-message">{{ conditionweightSumError }}</span>
<span class="ft-14-600 validation-message">{{ conditionnegativeweightError }}</span>
<span class="ft-14-600 validation-message">{{ conditionWeightSumError }}</span>
<span class="ft-14-600 validation-message">{{ conditionNegativeWeightError }}</span>
</div>
</form>
</section>
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { ExperimentVM } from '../../../../../../core/experiments/store/experimen
})
export class ConditionsTableComponent implements OnInit, OnDestroy {
@Input() experimentInfo: ExperimentVM;
@Input() isAnyRowRemoved: boolean;
@Input() isExperimentEditable: boolean;

subscriptions: Subscription;
Expand All @@ -35,8 +34,8 @@ export class ConditionsTableComponent implements OnInit, OnDestroy {
useEllipsis = false;

// Condition Errors
conditionweightSumError: string = null;
conditionnegativeweightError: string = null;
conditionWeightSumError: string = null;
conditionNegativeWeightError: string = null;

constructor(
private experimentDesignStepperService: ExperimentDesignStepperService,
Expand Down Expand Up @@ -98,15 +97,15 @@ export class ConditionsTableComponent implements OnInit, OnDestroy {
}

handleDesignDataChanges(designData: ExperimentFactorialDesignData) {
if (this.experimentInfo?.partitions.length && !this.formInitialized && !this.isAnyRowRemoved) {
const isDesignDataValid = this.checkDesignDataValidity(designData);
if (this.experimentInfo?.partitions.length && (!this.formInitialized || !isDesignDataValid)) {
this.handleInitializeExistingTableData();
} else if (!this.experimentInfo && this.formInitialized && !this.isAnyRowRemoved) {
} else if (!this.experimentInfo && (!this.formInitialized || !isDesignDataValid)) {
this.handleInitializeNewNewTableData(designData);
} else {
// if new exp and form initialized and you move back and forth
// if edit exp and form already initialized
this.handleInitializeNewNewTableData(designData); // <---- be careful doing this! if you see bugs, it may be because this is not the intended place for this function
// this.handleUpdateDesignDataTableChanges(designData);
this.handleUpdateDesignDataTableChanges(designData);
}
}

Expand All @@ -125,9 +124,25 @@ export class ConditionsTableComponent implements OnInit, OnDestroy {
this.initializeForm(newTableData);
}

// handleUpdateDesignDataTableChanges(designData: ExperimentFactorialDesignData) {
// // TODO: intelligently handle updates to design data without triggering complete table re-creation
// }
handleUpdateDesignDataTableChanges(designData: ExperimentFactorialDesignData) {
const newTableData = this.experimentDesignStepperService.editFactorialConditionTableData(
designData,
this.tableData$.value
);
this.initializeForm(newTableData);
}

checkDesignDataValidity(designData: ExperimentFactorialDesignData) {
let isDesignDataValid = true;
if (designData.factors.length > 1) {
designData.factors.forEach((factor) => {
if (!factor.levels[0].name) {
isDesignDataValid = false;
}
});
}
return isDesignDataValid;
}

initializeForm(tableData: FactorialConditionTableRowData[]) {
this.createFormControls(tableData);
Expand Down Expand Up @@ -171,18 +186,18 @@ export class ConditionsTableComponent implements OnInit, OnDestroy {
factorialConditions.forEach((condition) => (sumOfAssignmentWeights += parseFloat(condition.weight)));
// checking if sum is not equal to 100
if (Math.round(sumOfAssignmentWeights) !== 100.0) {
this.conditionweightSumError = weightSumNot100ErrorMsg;
this.conditionWeightSumError = weightSumNot100ErrorMsg;
} else {
this.conditionweightSumError = null;
this.conditionWeightSumError = null;
}
}

validateWeightsNotNegative(factorialConditions: FactorialConditionTableRowData[]) {
this.conditionnegativeweightError = null;
const negativeweightErrorMsg = this.translate.instant('home.new-experiment.design.assignment-weight-negative.text');
this.conditionNegativeWeightError = null;
const negativeWeightErrorMsg = this.translate.instant('home.new-experiment.design.assignment-weight-negative.text');
// handling sum of decimal values for assignment weights:
factorialConditions.forEach((condition) =>
parseFloat(condition.weight) < 0 ? (this.conditionnegativeweightError = negativeweightErrorMsg) : null
parseFloat(condition.weight) < 0 ? (this.conditionNegativeWeightError = negativeWeightErrorMsg) : null
);
}

Expand All @@ -198,7 +213,7 @@ export class ConditionsTableComponent implements OnInit, OnDestroy {
this.equalWeightFlag = !this.equalWeightFlag;

if (this.equalWeightFlag) {
const newTableData = this.applyEqualWeights();
const newTableData = this.applyEqualWeights(this.factorialConditionTableForm.value.factorialConditions);
this.experimentDesignStepperService.updateFactorialConditionTableData(newTableData);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,6 @@
<div *ngIf="this.factor.length >= 2">
<br />
<app-conditions-table
[isAnyRowRemoved]="isAnyRowRemoved"
[experimentInfo]="experimentInfo"
[isExperimentEditable]="isExperimentEditable"
></app-conditions-table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ export class FactorialExperimentDesignComponent implements OnInit, OnChanges, On

conditionTableDataUpToDate = true;
isExperimentEditable = true;
isAnyRowRemoved = false;
// common lock variable for all tables:
isFormLockedForEdit$ = this.experimentDesignStepperService.isFormLockedForEdit$;

Expand Down Expand Up @@ -370,7 +369,6 @@ export class FactorialExperimentDesignComponent implements OnInit, OnChanges, On
removeFactor(groupIndex: number) {
this.factor.removeAt(groupIndex);
this.handleConditionsButtonClick();
this.isAnyRowRemoved = true;
this.experimentDesignStepperService.experimentStepperDataChanged();
this.experimentDesignStepperService.clearFactorialFactorTableEditModeDetails();
this.updateView('factorTable');
Expand All @@ -382,7 +380,6 @@ export class FactorialExperimentDesignComponent implements OnInit, OnChanges, On
removeLevel(factorIndex: number, levelIndex: number) {
this.getFactorialLevelsAt(factorIndex).removeAt(levelIndex);
this.handleConditionsButtonClick();
this.isAnyRowRemoved = true;
this.experimentDesignStepperService.experimentStepperDataChanged();
this.experimentDesignStepperService.clearFactorialLevelTableEditModeDetails();
this.updateView('levelTable');
Expand Down Expand Up @@ -777,7 +774,6 @@ export class FactorialExperimentDesignComponent implements OnInit, OnChanges, On

if (eventType == NewExperimentDialogEvents.SAVE_DATA) {
this.experimentDesignStepperService.experimentStepperDataReset();
this.isAnyRowRemoved = false;
this.factorialExperimentDesignForm.markAsPristine();
}
}
Expand Down

0 comments on commit 04e2cd5

Please sign in to comment.