justinpark commented on code in PR #32548: URL: https://github.com/apache/superset/pull/32548#discussion_r1996251757
########## superset-frontend/src/explore/components/DatasourcePanel/transformDatasourceFolders.ts: ########## @@ -0,0 +1,155 @@ +/** + * 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 { Metric, t } from '@superset-ui/core'; +import { + ColumnItem, + DatasourceFolder, + DatasourcePanelColumn, + Folder, + MetricItem, +} from './types'; + +const transformToFolderStructure = ( + metrics: MetricItem[], + columns: ColumnItem[], + folderConfig: DatasourceFolder[] | undefined, +): Folder[] => { + const metricsMap = new Map<string, MetricItem>(); + const columnsMap = new Map<string, ColumnItem>(); + + const assignedMetricUuids = new Set<string>(); + const assignedColumnUuids = new Set<string>(); + + metrics.forEach(metric => { + metricsMap.set(metric.uuid, metric); + }); + + columns.forEach(column => { + columnsMap.set(column.uuid, column); + }); + + const processFolder = ( + datasourceFolder: DatasourceFolder, + parentId?: string, + ): Folder => { + const folder: Folder = { + id: datasourceFolder.uuid, + name: datasourceFolder.name, + description: datasourceFolder.description, + isCollapsed: false, + items: [], + parentId, + }; + + if (datasourceFolder.children && datasourceFolder.children.length > 0) { + if (!folder.subFolders) { + folder.subFolders = []; + } + + datasourceFolder.children.forEach(child => { + if (child.type === 'folder') { + folder.subFolders!.push( + processFolder(child as DatasourceFolder, folder.id), + ); + } else if (child.type === 'metric') { + const metric = metricsMap.get(child.uuid); + if (metric) { + folder.items.push(metric); + assignedMetricUuids.add(metric.uuid); + } + } else if (child.type === 'column') { + const column = columnsMap.get(child.uuid); + if (column) { + folder.items.push(column); + assignedColumnUuids.add(column.uuid); + } + } + }); + } Review Comment: Instead of keeping track of `assignedMetricUuids` separately, I think we can assign them from the existing metricsMap to folder items, and after that, delete them. This way, the metricsMap will only contain unassigned metrics, allowing us to skip this additional process. ```suggestion datasourceFolder.children.forEach(child => { if (child.type === 'folder') { folder.subFolders!.push( processFolder(child as DatasourceFolder, folder.id), ); } else if (child.type === 'metric') { const metric = metricsMap.get(child.uuid); if (metric) { folder.items.push(metric); metricsMap.delete(metric.uuid); } } else if (child.type === 'column') { const column = columnsMap.get(child.uuid); if (column) { folder.items.push(column); columnsMap.delete(column.uuid); } } }); } ``` -- 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]
