beenhead commented on code in PR #19547: URL: https://github.com/apache/druid/pull/19547#discussion_r3394745600
########## web-console/src/dialogs/supervisor-to-sql-dialog/supervisor-to-sql-dialog.tsx: ########## @@ -0,0 +1,323 @@ +/* + * 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 { + Button, + Classes, + Dialog, + FormGroup, + InputGroup, + Intent, + Radio, + RadioGroup, + TextArea, +} from '@blueprintjs/core'; +import { IconNames } from '@blueprintjs/icons'; +import React, { useState } from 'react'; + +import { ExternalLink } from '../../components'; +import { convertSupervisorToSql } from '../../helpers/supervisor-conversion'; +import { Api, AppToaster } from '../../singletons'; +import { deepGet } from '../../utils'; + +import './supervisor-to-sql-dialog.scss'; + +interface SupervisorSpec { + type: string; + spec: { + dataSchema: { + dataSource: string; + timestampSpec: { + column: string; + format: string; + }; + dimensionsSpec: { + dimensions: (string | { name: string; type: string })[]; + }; + metricsSpec: { name?: string; fieldName?: string; type: string }[]; + }; + ioConfig?: { + topic?: string; + inputSource?: { + type: string; + uris?: string[]; + baseDir?: string; + }; + }; + }; +} + +export interface SupervisorToSqlDialogProps { + onConvert(converted: { queryString: string; queryContext: any }, datasource?: string): void; + onClose(): void; +} + +export const SupervisorToSqlDialog = React.memo(function SupervisorToSqlDialog( + props: SupervisorToSqlDialogProps, +) { + const { onConvert, onClose } = props; + + const [supervisorSource, setSupervisorSource] = useState<'select' | 'paste'>('select'); + const [selectedSupervisor, setSelectedSupervisor] = useState<string>(''); + const [pastedSupervisor, setPastedSupervisor] = useState<string>(''); + const [availableSupervisors, setAvailableSupervisors] = useState<string[]>([]); + const [supervisorSpec, setSupervisorSpec] = useState<SupervisorSpec | undefined>(); + + const [fileLocation, setFileLocation] = useState<string>(''); + const [fileType, setFileType] = useState<string>('json'); + + const [loading, setLoading] = useState(false); + const [error, setError] = useState<string | undefined>(); + + React.useEffect(() => { + void loadSupervisors(); + }, []); + + async function loadSupervisors() { + try { + const supervisors = await Api.instance.get<string[]>('/druid/indexer/v1/supervisor'); + setAvailableSupervisors(supervisors.data); + if (supervisors.data.length > 0) { + setSelectedSupervisor(supervisors.data[0]); + } + } catch (e) { + setError(`Failed to load supervisors: ${e.message}`); + } + } + + async function loadSupervisorSpec(supervisorId: string) { + if (!supervisorId) return; + + setLoading(true); + setError(undefined); + + try { + const resp = await Api.instance.get<SupervisorSpec>( + `/druid/indexer/v1/supervisor/${Api.encodePath(supervisorId)}`, + ); + setSupervisorSpec(resp.data); + + // Auto-populate file location from ioConfig if available + const ioConfig = deepGet(resp.data, 'spec.ioConfig'); + if (ioConfig?.inputSource?.uris) { + setFileLocation(ioConfig.inputSource.uris[0] || ''); + } else if (ioConfig?.inputSource?.baseDir) { + setFileLocation(ioConfig.inputSource.baseDir); + } + } catch (e) { + setError(`Failed to load supervisor spec: ${e.message}`); + } finally { + setLoading(false); + } + } + + function parsePastedSupervisor() { + if (!pastedSupervisor.trim()) { + // Clear any previously parsed spec so a blank/cleared paste can't submit a stale supervisor + setSupervisorSpec(undefined); + setError(undefined); + return; + } + + try { + const parsed = JSON.parse(pastedSupervisor); + setSupervisorSpec(parsed); + setError(undefined); + + // Auto-populate file location from ioConfig if available + const ioConfig = deepGet(parsed, 'spec.ioConfig'); + if (ioConfig?.inputSource?.uris) { + setFileLocation(ioConfig.inputSource.uris[0] || ''); + } else if (ioConfig?.inputSource?.baseDir) { + setFileLocation(ioConfig.inputSource.baseDir); + } + } catch (e) { + setError(`Invalid JSON: ${e.message}`); + setSupervisorSpec(undefined); + } + } + + function handleConvert() { + if (!supervisorSpec) { + AppToaster.show({ + message: 'No supervisor spec loaded', + intent: Intent.DANGER, + }); + return; + } + + if (!fileLocation) { + AppToaster.show({ + message: 'Please specify a file location', + intent: Intent.DANGER, + }); + return; + } + + let converted: { queryString: string; queryContext: any }; + try { + converted = convertSupervisorToSql(supervisorSpec, { + fileLocation, + fileType, + }); + } catch (e) { + AppToaster.show({ + message: `Could not convert supervisor: ${e.message}`, + intent: Intent.DANGER, + }); + return; + } + + AppToaster.show({ + message: 'Supervisor converted to SQL, please review', + intent: Intent.SUCCESS, + }); + + onConvert(converted, deepGet(supervisorSpec, 'spec.dataSchema.dataSource')); + } + + React.useEffect(() => { + if (supervisorSource !== 'select') return; + if (selectedSupervisor) { + void loadSupervisorSpec(selectedSupervisor); + } else { + // No supervisor selected (e.g. none available); don't keep a spec from paste mode around + setSupervisorSpec(undefined); + } + }, [selectedSupervisor, supervisorSource]); + + React.useEffect(() => { + if (supervisorSource !== 'paste') return; + // Always reparse on entering paste mode or editing the text so a stale select-mode spec is + // dropped and a cleared paste disables Generate SQL + parsePastedSupervisor(); + }, [pastedSupervisor, supervisorSource]); + + return ( + <Dialog + className="supervisor-to-sql-dialog" + isOpen + onClose={onClose} + title="Convert supervisor to SQL" + canOutsideClickClose={false} + > + <div className={Classes.DIALOG_BODY}> + <p> + Convert a streaming supervisor specification to an MSQ (Multi-Stage Query) ingestion SQL + statement.{' '} Review Comment: Reworded the intro: it now states this "generates a one-time batch ingestion that reads the supplied files — it does not start a streaming ingestion and will not continuously ingest new data." -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
