/**
 * 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 { t } from '../translation' ; import { removeDuplicates } from '../utils' ; import { DTTM_ALIAS } from './constants' ; import getColumnLabel from './getColumnLabel' ; import getMetricLabel from './getMetricLabel' ; import { QueryFields , QueryFormColumn , QueryFormMetric , QueryFormOrderBy , QueryFieldAliases , FormDataResidual , QueryMode , } from './types/QueryFormData' ; param formData param aliases export default function extractQueryFields ( formData , aliases ? ) { const queryFieldAliases = { /** These are predefined for backward compatibility */ metric : 'metrics' , metric_2 : 'metrics' , secondary_metric : 'metrics' , x : 'metrics' , y : 'metrics' , size : 'metrics' , all_columns : 'columns' , series : 'groupby' , order_by_cols : 'orderby' , ... aliases , } ; const { query_mode : queryMode , include_time : includeTime , ... restFormData } = formData ; let columns = [ ] ; let metrics = [ ] ; let orderby = [ ] ; Object . entries ( restFormData ) . forEach ( ( [ key , value ] ) => { if ( value == null ) { return ; } let normalizedKey = queryFieldAliases [ key ] || key ; if ( queryMode === QueryMode . aggregate && normalizedKey === 'columns' ) { return ; } if ( queryMode === QueryMode . raw && ( normalizedKey === 'groupby' || normalizedKey === 'metrics' ) ) { return ; } if ( normalizedKey === 'groupby' ) { normalizedKey = 'columns' ; } if ( normalizedKey === 'metrics' ) { metrics = metrics . concat ( value ) ; } else if ( normalizedKey === 'columns' ) { columns = columns . concat ( value ) ; } else if ( normalizedKey === 'orderby' ) { orderby = orderby . concat ( value ) ; } } ) ; if ( includeTime && ! columns . includes ( DTTM_ALIAS ) ) { columns . unshift ( DTTM_ALIAS ) ; } return { columns : removeDuplicates ( columns . filter ( col => col !== '' ) , getColumnLabel ) , metrics : queryMode === QueryMode . raw ? undefined : removeDuplicates ( metrics , getMetricLabel ) , orderby : orderby . length > 0 ? orderby . map ( item => { if ( typeof item === 'string' ) { try { return JSON . parse ( item ) ; } catch ( error ) { throw new Error ( t ( 'Found invalid orderby options' ) ) ; } } return item ; } ) : undefined , } ; }