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

Persist code changes in condition table when factor update #1376

Merged
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 @@ -35,8 +35,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 +98,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.isAnyRowRemoved && !this.formInitialized) {
this.handleInitializeExistingTableData();
} else if (!this.experimentInfo && this.formInitialized && !this.isAnyRowRemoved) {
} else if (!this.experimentInfo && !this.isAnyRowRemoved && (!this.formInitialized || !isDesignDataValid)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we still need isAnyRowRemoved? I think isDesignDataValid can replace it. And it doesn't go stale after multiple back-and-forth edits.

Copy link
Collaborator Author

@Yagnik56 Yagnik56 Mar 20, 2024

Choose a reason for hiding this comment

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

done, I have run few test to check if it's working properly but it will be good if you can verify same.

this.handleInitializeNewNewTableData(designData);
} else {
Yagnik56 marked this conversation as resolved.
Show resolved Hide resolved
// 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 +125,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 +187,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 +214,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
Loading