buster/apps/web/src/controllers/MetricController/MetricViewSQL/useMetricRunSQL/helpers.ts

94 lines
3.0 KiB
TypeScript
Raw Normal View History

2025-03-08 07:02:56 +08:00
import type {
ColumnMetaData,
BusterMetric,
BusterMetricChartConfig
2025-03-08 07:02:56 +08:00
} from '@/api/asset_interfaces/metric';
import type {
BusterChartConfigProps,
IColumnLabelFormat
2025-03-08 07:02:56 +08:00
} from '@/api/asset_interfaces/metric/charts';
2025-03-20 02:03:54 +08:00
import { createDefaultChartConfig } from '@/lib/metrics/messageAutoChartHandler';
export const didColumnDataChange = (
oldColumnData: ColumnMetaData[] | undefined,
newColumnData: ColumnMetaData[] | undefined
) => {
if (!oldColumnData || !newColumnData) return true;
const createRecordOfColumnMetaData = (columnData: ColumnMetaData[]) => {
return columnData.reduce<
Record<
string,
{
name: string;
simple_type: string;
}
>
>((acc, x) => {
acc[x.name] = {
name: x.name,
simple_type: x.simple_type
};
return acc;
}, {});
};
const oldColumnDataRecord = createRecordOfColumnMetaData(oldColumnData);
const newColumnDataRecord = createRecordOfColumnMetaData(newColumnData);
const oldKeys = Object.keys(oldColumnDataRecord);
const newKeys = Object.keys(newColumnDataRecord);
if (oldKeys.length !== newKeys.length) return true;
return oldKeys.some((key) => {
const oldCol = oldColumnDataRecord[key];
const newCol = newColumnDataRecord[key];
return !newCol || oldCol.name !== newCol.name || oldCol.simple_type !== newCol.simple_type;
});
};
2025-03-22 13:00:15 +08:00
/**
* Simplifies the chart configuration when SQL query changes by preserving column formatting
* that's still valid for the new data structure and resetting formatting for changed columns.
*
* @param chartConfig - The current chart configuration to simplify
* @param data_metadata - Metadata about the new data structure from the metric
* @returns A new chart configuration suitable for the changed SQL data
*/
2025-02-06 03:54:06 +08:00
export const simplifyChatConfigForSQLChange = (
chartConfig: BusterMetricChartConfig,
data_metadata: BusterMetric['data_metadata']
): BusterMetricChartConfig => {
2025-03-22 13:00:15 +08:00
// Create a new mapping of column name to format settings
// This preserves existing format settings only when the column type hasn't changed
const columnLabelFormats = data_metadata?.column_metadata?.reduce<
2025-04-04 23:52:16 +08:00
NonNullable<BusterChartConfigProps['columnLabelFormats']>
>((acc, x) => {
2025-03-22 13:00:15 +08:00
// Get the existing format for this column (if any)
const oldFormat: undefined | Required<IColumnLabelFormat> =
chartConfig.columnLabelFormats?.[x.name];
2025-03-22 13:00:15 +08:00
// Check if the column type has changed
const didTypeChange = oldFormat?.columnType !== x.simple_type;
2025-03-22 13:00:15 +08:00
// If type changed, reset format (undefined), otherwise keep existing format
const value = didTypeChange ? undefined : oldFormat;
2025-03-22 13:00:15 +08:00
// Add this column's format to our accumulated result
2025-04-04 23:52:16 +08:00
acc[x.name] = value;
return acc;
}, {});
2025-03-22 13:00:15 +08:00
// Generate a new default chart configuration using the preserved formats
// and the new data structure's metadata
const result = createDefaultChartConfig({
chart_config: {
columnLabelFormats
} as BusterChartConfigProps,
data_metadata
});
return result;
};