2025-03-08 07:02:56 +08:00
|
|
|
import type {
|
|
|
|
ColumnMetaData,
|
2025-07-04 05:54:31 +08:00
|
|
|
BusterMetric,
|
|
|
|
BusterMetricChartConfig
|
2025-03-08 07:02:56 +08:00
|
|
|
} from '@/api/asset_interfaces/metric';
|
2025-01-07 02:29:29 +08:00
|
|
|
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';
|
2025-01-07 02:29:29 +08:00
|
|
|
|
|
|
|
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 = (
|
2025-07-04 05:54:31 +08:00
|
|
|
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
|
2025-01-07 02:29:29 +08:00
|
|
|
const columnLabelFormats = data_metadata?.column_metadata?.reduce<
|
2025-04-04 23:52:16 +08:00
|
|
|
NonNullable<BusterChartConfigProps['columnLabelFormats']>
|
2025-01-07 02:29:29 +08:00
|
|
|
>((acc, x) => {
|
2025-03-22 13:00:15 +08:00
|
|
|
// Get the existing format for this column (if any)
|
2025-01-07 02:29:29 +08:00
|
|
|
const oldFormat: undefined | Required<IColumnLabelFormat> =
|
|
|
|
chartConfig.columnLabelFormats?.[x.name];
|
2025-03-22 13:00:15 +08:00
|
|
|
|
|
|
|
// Check if the column type has changed
|
2025-01-07 02:29:29 +08:00
|
|
|
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
|
2025-01-07 02:29:29 +08:00
|
|
|
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;
|
2025-01-07 02:29:29 +08:00
|
|
|
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
|
2025-01-07 02:29:29 +08:00
|
|
|
const result = createDefaultChartConfig({
|
|
|
|
chart_config: {
|
|
|
|
columnLabelFormats
|
|
|
|
} as BusterChartConfigProps,
|
|
|
|
data_metadata
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|