Xiao-zhen-Liu commented on code in PR #5043: URL: https://github.com/apache/texera/pull/5043#discussion_r3276402907
########## frontend/src/app/workspace/component/ui-udf-parameters/ui-udf-parameters.component.ts: ########## @@ -0,0 +1,82 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { Component, OnInit } from "@angular/core"; +import { NgFor, NgIf } from "@angular/common"; +import { FieldArrayType, FormlyFieldConfig, FormlyModule } from "@ngx-formly/core"; + +type UiUdfParameterColumn = Readonly<{ label: string; key: string; parentKey?: string; disabled: boolean }>; + +@Component({ + selector: "texera-ui-udf-parameters", + templateUrl: "./ui-udf-parameters.component.html", + styleUrls: ["./ui-udf-parameters.component.scss"], + imports: [NgIf, NgFor, FormlyModule], +}) +export class UiUdfParametersComponent extends FieldArrayType implements OnInit { + readonly fieldColumns: UiUdfParameterColumn[] = [ + { label: "Value", key: "value", disabled: false }, + { label: "Name", key: "attributeName", parentKey: "attribute", disabled: true }, + { label: "Type", key: "attributeType", parentKey: "attribute", disabled: true }, + ]; + + ngOnInit(): void { + this.field.fieldGroup?.forEach(rowField => { + this.fieldColumns.forEach(column => { + this.configureDisabledState(this.getColumnField(rowField, column), column.disabled); + }); + }); + } Review Comment: The disable logic runs in `ngOnInit`, which only iterates the rows that exist at component init time. When PR #2 wires the `uiParametersChanged$` subscriber and writes back into `operatorProperties.uiParameters`, `FieldArrayType` will generate new rows from `this.field.fieldArray` (the row template). Those new rows do not go through `ngOnInit`, and the `onInit` hooks registered inside `configureDisabledState` are attached to the existing field configs, not to the template. New rows would then render with Name and Type editable. This is not visible today because no subscriber exists in this PR. But it will be a regression as soon as PR #2 lands. Two cleaner options: (1) set `disabled: true` directly on `field.fieldArray.fieldGroup` entries for Name and Type so every new row inherits the state from the template; or (2) override `FieldArrayType.onPopulate` so the disable logic runs whenever rows are populated. The component spec also only constructs a static `field.fieldGroup` and never adds rows, so this case is not exercised. Please either pick a fix now or add a spec that adds a row after `ngOnInit` and asserts the new row's Name and Type are disabled, so this regression is caught when PR #2 lands. ########## frontend/src/app/workspace/service/code-editor/ui-udf-parameters-sync.service.ts: ########## @@ -0,0 +1,115 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { Injectable } from "@angular/core"; +import { isEqual } from "lodash-es"; +import { ReplaySubject, Subject } from "rxjs"; +import { debounceTime } from "rxjs/operators"; +import { WorkflowActionService } from "../workflow-graph/model/workflow-action.service"; +import { UiUdfParametersParseError, UiUdfParametersParserService } from "./ui-udf-parameters-parser.service"; +import type { UiUdfParameter } from "./ui-udf-parameters-parser.service"; +import { isDefined } from "../../../common/util/predicate"; +import { isPythonUdf } from "../workflow-graph/model/workflow-graph"; +import type { Text as YText } from "yjs"; + +const UI_PARAMETER_SYNC_DEBOUNCE_TIME_MS = 200; + +@Injectable({ providedIn: "root" }) +export class UiUdfParametersSyncService { + private readonly uiParametersChangedSubject = new ReplaySubject<{ operatorId: string; parameters: UiUdfParameter[] }>( + 1 + ); + private readonly uiParametersParseErrorSubject = new ReplaySubject<{ operatorId: string; message?: string }>(1); + + readonly uiParametersChanged$ = this.uiParametersChangedSubject.asObservable(); + readonly uiParametersParseError$ = this.uiParametersParseErrorSubject.asObservable(); + + constructor( + private workflowActionService: WorkflowActionService, + private uiUdfParametersParserService: UiUdfParametersParserService + ) {} + + attachToYCode(operatorId: string, yCode: YText): () => void { + const codeChanges = new Subject<string>(); + const subscription = codeChanges + .pipe(debounceTime(UI_PARAMETER_SYNC_DEBOUNCE_TIME_MS)) + .subscribe(latestCode => this.syncStructureFromCode(operatorId, latestCode)); + const handler = () => codeChanges.next(yCode.toString()); + + yCode.observe(handler); + this.syncStructureFromCode(operatorId, yCode.toString()); + + return () => { + yCode.unobserve(handler); + subscription.unsubscribe(); + codeChanges.complete(); + }; + } + + syncStructureFromCode(operatorId: string, codeFromEditor?: string): void { + const operator = this.workflowActionService.getTexeraGraph().getOperator(operatorId); + + if (!operator || !isPythonUdf(operator)) return; + + const code = codeFromEditor ?? this.getSharedCode(operatorId); + if (!isDefined(code)) return; + + const existingParameters = (operator.operatorProperties?.uiParameters ?? []) as UiUdfParameter[]; + let mergedParameters: UiUdfParameter[]; + + try { + mergedParameters = this.buildParsedShapeWithPreservedValues(code, existingParameters); + } catch (error) { + if (error instanceof UiUdfParametersParseError) { + this.uiParametersParseErrorSubject.next({ operatorId, message: error.message }); + return; + } + throw error; + } + + this.uiParametersParseErrorSubject.next({ operatorId }); Review Comment: This emits `{ operatorId, message: undefined }` to clear errors, so subscribers have to check `message` to distinguish "no error" from "error". This works, but reads a bit awkwardly. When PR #2 consumes this, a `clearParseError(operatorId)` method (or a separate `uiParametersParseErrorCleared$` observable) would express the intent more directly. ########## frontend/src/app/workspace/service/code-editor/ui-udf-parameters-sync.service.ts: ########## @@ -0,0 +1,115 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { Injectable } from "@angular/core"; +import { isEqual } from "lodash-es"; +import { ReplaySubject, Subject } from "rxjs"; +import { debounceTime } from "rxjs/operators"; +import { WorkflowActionService } from "../workflow-graph/model/workflow-action.service"; +import { UiUdfParametersParseError, UiUdfParametersParserService } from "./ui-udf-parameters-parser.service"; +import type { UiUdfParameter } from "./ui-udf-parameters-parser.service"; +import { isDefined } from "../../../common/util/predicate"; +import { isPythonUdf } from "../workflow-graph/model/workflow-graph"; +import type { Text as YText } from "yjs"; + +const UI_PARAMETER_SYNC_DEBOUNCE_TIME_MS = 200; + +@Injectable({ providedIn: "root" }) +export class UiUdfParametersSyncService { + private readonly uiParametersChangedSubject = new ReplaySubject<{ operatorId: string; parameters: UiUdfParameter[] }>( + 1 + ); + private readonly uiParametersParseErrorSubject = new ReplaySubject<{ operatorId: string; message?: string }>(1); + + readonly uiParametersChanged$ = this.uiParametersChangedSubject.asObservable(); + readonly uiParametersParseError$ = this.uiParametersParseErrorSubject.asObservable(); + + constructor( + private workflowActionService: WorkflowActionService, + private uiUdfParametersParserService: UiUdfParametersParserService + ) {} + + attachToYCode(operatorId: string, yCode: YText): () => void { + const codeChanges = new Subject<string>(); + const subscription = codeChanges + .pipe(debounceTime(UI_PARAMETER_SYNC_DEBOUNCE_TIME_MS)) + .subscribe(latestCode => this.syncStructureFromCode(operatorId, latestCode)); + const handler = () => codeChanges.next(yCode.toString()); + + yCode.observe(handler); + this.syncStructureFromCode(operatorId, yCode.toString()); + + return () => { + yCode.unobserve(handler); + subscription.unsubscribe(); + codeChanges.complete(); + }; + } + + syncStructureFromCode(operatorId: string, codeFromEditor?: string): void { + const operator = this.workflowActionService.getTexeraGraph().getOperator(operatorId); + + if (!operator || !isPythonUdf(operator)) return; + + const code = codeFromEditor ?? this.getSharedCode(operatorId); + if (!isDefined(code)) return; + + const existingParameters = (operator.operatorProperties?.uiParameters ?? []) as UiUdfParameter[]; + let mergedParameters: UiUdfParameter[]; + + try { + mergedParameters = this.buildParsedShapeWithPreservedValues(code, existingParameters); + } catch (error) { + if (error instanceof UiUdfParametersParseError) { + this.uiParametersParseErrorSubject.next({ operatorId, message: error.message }); + return; + } + throw error; + } + + this.uiParametersParseErrorSubject.next({ operatorId }); + + if (isEqual(existingParameters, mergedParameters)) return; + + this.uiParametersChangedSubject.next({ operatorId, parameters: mergedParameters }); + } + + private buildParsedShapeWithPreservedValues(code: string, existingParameters: UiUdfParameter[]): UiUdfParameter[] { + const parsedParameters = this.uiUdfParametersParserService.parse(code); + const existingValues = new Map( + existingParameters.map(parameter => [parameter.attribute.attributeName, parameter.value] as const) + ); + + return parsedParameters.map(parameter => ({ + ...parameter, + value: existingValues.get(parameter.attribute.attributeName) ?? "", + })); + } + + private getSharedCode(operatorId: string): string | undefined { + try { + const sharedOperatorType = this.workflowActionService.getTexeraGraph().getSharedOperatorType(operatorId); + + const operatorProperties = sharedOperatorType.get("operatorProperties") as any; + const yCode = operatorProperties.get("code") as YText; + return yCode?.toString(); + } catch { + return undefined; + } Review Comment: Two small things in this helper. (1) The `catch {}` block silently returns `undefined` for every error type, which could hide real bugs during shared-editing debugging. A `console.warn` on the caught error would help. (2) `sharedOperatorType.get("operatorProperties") as any` was previously typed as `as YType<Readonly<{ [key: string]: any }>>`. Restoring that (or a tighter type) keeps the editor-to-graph boundary type-safe. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
