2023-06-23 10:15:09 +03:00
|
|
|
import { ColumnsType } from 'antd/es/table';
|
|
|
|
|
import { ColumnType } from 'antd/lib/table';
|
2023-07-25 16:25:36 +03:00
|
|
|
import {
|
|
|
|
|
initialFormulaBuilderFormValues,
|
|
|
|
|
initialQueryBuilderFormValues,
|
|
|
|
|
} from 'constants/queryBuilder';
|
2023-06-23 10:15:09 +03:00
|
|
|
import { FORMULA_REGEXP } from 'constants/regExp';
|
|
|
|
|
import { QueryTableProps } from 'container/QueryTable/QueryTable.intefaces';
|
|
|
|
|
import { ReactNode } from 'react';
|
2023-07-17 15:46:41 +03:00
|
|
|
import {
|
|
|
|
|
IBuilderFormula,
|
|
|
|
|
IBuilderQuery,
|
|
|
|
|
Query,
|
|
|
|
|
} from 'types/api/queryBuilder/queryBuilderData';
|
2023-07-04 08:24:34 +03:00
|
|
|
import { ListItem, QueryDataV3, SeriesItem } from 'types/api/widgets/getQuery';
|
2023-07-17 15:46:41 +03:00
|
|
|
import { QueryBuilderData } from 'types/common/queryBuilder';
|
2023-06-23 10:15:09 +03:00
|
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
|
|
|
|
|
|
type CreateTableDataFromQueryParams = Pick<
|
|
|
|
|
QueryTableProps,
|
2023-07-25 15:45:51 +05:30
|
|
|
'queryTableData' | 'query' | 'renderActionCell' | 'renderColumnCell'
|
2023-06-23 10:15:09 +03:00
|
|
|
>;
|
|
|
|
|
|
|
|
|
|
export type RowData = {
|
|
|
|
|
timestamp: number;
|
|
|
|
|
key: string;
|
|
|
|
|
[key: string]: string | number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type DynamicColumn = {
|
2023-07-25 16:25:36 +03:00
|
|
|
query: IBuilderQuery | IBuilderFormula;
|
|
|
|
|
field: string;
|
|
|
|
|
dataIndex: string;
|
2023-07-17 15:46:41 +03:00
|
|
|
title: string;
|
2023-06-23 10:15:09 +03:00
|
|
|
data: (string | number)[];
|
2023-07-17 15:46:41 +03:00
|
|
|
type: 'field' | 'operator' | 'formula';
|
2023-07-05 10:21:22 +03:00
|
|
|
// sortable: boolean;
|
2023-06-23 10:15:09 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type DynamicColumns = DynamicColumn[];
|
|
|
|
|
|
|
|
|
|
type CreateTableDataFromQuery = (
|
|
|
|
|
params: CreateTableDataFromQueryParams,
|
|
|
|
|
) => {
|
|
|
|
|
columns: ColumnsType<RowData>;
|
|
|
|
|
dataSource: RowData[];
|
|
|
|
|
rowsLength: number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type FillColumnData = (
|
|
|
|
|
queryTableData: QueryDataV3[],
|
|
|
|
|
dynamicColumns: DynamicColumns,
|
|
|
|
|
) => { filledDynamicColumns: DynamicColumns; rowsLength: number };
|
|
|
|
|
|
|
|
|
|
type GetDynamicColumns = (
|
|
|
|
|
queryTableData: QueryDataV3[],
|
|
|
|
|
query: Query,
|
|
|
|
|
) => DynamicColumns;
|
|
|
|
|
|
2023-07-04 08:24:34 +03:00
|
|
|
type ListItemData = ListItem['data'];
|
|
|
|
|
type ListItemKey = keyof ListItemData;
|
|
|
|
|
|
2023-06-23 10:15:09 +03:00
|
|
|
const isFormula = (queryName: string): boolean =>
|
|
|
|
|
FORMULA_REGEXP.test(queryName);
|
|
|
|
|
|
2023-07-17 15:46:41 +03:00
|
|
|
const isValueExist = (
|
|
|
|
|
field: keyof DynamicColumn,
|
|
|
|
|
value: string,
|
2023-06-23 10:15:09 +03:00
|
|
|
columns: DynamicColumns,
|
|
|
|
|
): boolean => {
|
2023-07-17 15:46:41 +03:00
|
|
|
const existColumns = columns.find((item) => item[field] === value);
|
2023-06-23 10:15:09 +03:00
|
|
|
|
2023-07-17 15:46:41 +03:00
|
|
|
return !!existColumns;
|
2023-06-23 10:15:09 +03:00
|
|
|
};
|
|
|
|
|
|
2023-07-17 15:46:41 +03:00
|
|
|
const getQueryByName = <T extends keyof QueryBuilderData>(
|
|
|
|
|
builder: QueryBuilderData,
|
|
|
|
|
currentQueryName: string,
|
|
|
|
|
type: T,
|
2023-07-25 16:25:36 +03:00
|
|
|
): T extends 'queryData' ? IBuilderQuery : IBuilderFormula => {
|
2023-07-17 15:46:41 +03:00
|
|
|
const queryArray = builder[type];
|
2023-07-25 16:25:36 +03:00
|
|
|
const defaultValue =
|
|
|
|
|
type === 'queryData'
|
|
|
|
|
? initialQueryBuilderFormValues
|
|
|
|
|
: initialFormulaBuilderFormValues;
|
2023-06-23 10:15:09 +03:00
|
|
|
|
2023-07-17 15:46:41 +03:00
|
|
|
const currentQuery =
|
2023-07-25 16:25:36 +03:00
|
|
|
queryArray.find((q) => q.queryName === currentQueryName) || defaultValue;
|
2023-07-05 07:51:31 +03:00
|
|
|
|
2023-07-17 15:46:41 +03:00
|
|
|
return currentQuery as T extends 'queryData' ? IBuilderQuery : IBuilderFormula;
|
2023-07-05 07:51:31 +03:00
|
|
|
};
|
|
|
|
|
|
2023-07-25 16:25:36 +03:00
|
|
|
const addListLabels = (
|
|
|
|
|
query: IBuilderQuery | IBuilderFormula,
|
|
|
|
|
label: ListItemKey,
|
|
|
|
|
dynamicColumns: DynamicColumns,
|
|
|
|
|
): void => {
|
|
|
|
|
if (isValueExist('dataIndex', label, dynamicColumns)) return;
|
|
|
|
|
|
|
|
|
|
const fieldObj: DynamicColumn = {
|
|
|
|
|
query,
|
|
|
|
|
field: 'label',
|
|
|
|
|
dataIndex: label as string,
|
|
|
|
|
title: label as string,
|
|
|
|
|
data: [],
|
|
|
|
|
type: 'field',
|
|
|
|
|
// sortable: isNumber,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
dynamicColumns.push(fieldObj);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const addSeriaLabels = (
|
|
|
|
|
label: string,
|
2023-07-04 08:24:34 +03:00
|
|
|
dynamicColumns: DynamicColumns,
|
2023-07-25 16:25:36 +03:00
|
|
|
query: IBuilderQuery | IBuilderFormula,
|
2023-07-04 08:24:34 +03:00
|
|
|
): void => {
|
2023-07-25 16:25:36 +03:00
|
|
|
if (isValueExist('dataIndex', label, dynamicColumns)) return;
|
2023-07-04 08:24:34 +03:00
|
|
|
|
2023-07-05 10:21:22 +03:00
|
|
|
// const labelValue = labels[label];
|
2023-07-04 08:24:34 +03:00
|
|
|
|
2023-07-05 10:21:22 +03:00
|
|
|
// const isNumber = !Number.isNaN(parseFloat(String(labelValue)));
|
2023-07-04 08:24:34 +03:00
|
|
|
|
|
|
|
|
const fieldObj: DynamicColumn = {
|
2023-07-25 16:25:36 +03:00
|
|
|
query,
|
|
|
|
|
field: label as string,
|
|
|
|
|
dataIndex: label,
|
|
|
|
|
title: label,
|
2023-07-04 08:24:34 +03:00
|
|
|
data: [],
|
|
|
|
|
type: 'field',
|
2023-07-05 10:21:22 +03:00
|
|
|
// sortable: isNumber,
|
2023-07-04 08:24:34 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
dynamicColumns.push(fieldObj);
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-25 16:25:36 +03:00
|
|
|
const addOperatorFormulaColumns = (
|
|
|
|
|
query: IBuilderFormula | IBuilderQuery,
|
2023-07-17 15:46:41 +03:00
|
|
|
dynamicColumns: DynamicColumns,
|
2023-07-25 16:25:36 +03:00
|
|
|
customLabel?: string,
|
2023-07-17 15:46:41 +03:00
|
|
|
): void => {
|
2023-07-25 16:25:36 +03:00
|
|
|
if (isFormula(query.queryName)) {
|
|
|
|
|
const formulaQuery = query as IBuilderFormula;
|
|
|
|
|
let formulaLabel = `${formulaQuery.queryName}(${formulaQuery.expression})`;
|
2023-07-17 15:46:41 +03:00
|
|
|
|
2023-07-25 16:25:36 +03:00
|
|
|
if (formulaQuery.legend) {
|
2023-07-28 18:44:46 +03:00
|
|
|
formulaLabel = formulaQuery.legend;
|
2023-07-17 15:46:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const formulaColumn: DynamicColumn = {
|
2023-07-25 16:25:36 +03:00
|
|
|
query,
|
|
|
|
|
field: formulaQuery.queryName,
|
|
|
|
|
dataIndex: formulaQuery.queryName,
|
|
|
|
|
title: customLabel || formulaLabel,
|
2023-07-17 15:46:41 +03:00
|
|
|
data: [],
|
|
|
|
|
type: 'formula',
|
|
|
|
|
// sortable: isNumber,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
dynamicColumns.push(formulaColumn);
|
|
|
|
|
|
2023-07-25 16:25:36 +03:00
|
|
|
return;
|
|
|
|
|
}
|
2023-07-17 15:46:41 +03:00
|
|
|
|
2023-07-25 16:25:36 +03:00
|
|
|
const currentQueryData = query as IBuilderQuery;
|
2023-07-17 15:46:41 +03:00
|
|
|
|
|
|
|
|
let operatorLabel = `${currentQueryData.aggregateOperator}`;
|
|
|
|
|
if (currentQueryData.aggregateAttribute.key) {
|
|
|
|
|
operatorLabel += `(${currentQueryData.aggregateAttribute.key})`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentQueryData.legend) {
|
2023-07-28 18:44:46 +03:00
|
|
|
operatorLabel = currentQueryData.legend;
|
2023-07-17 15:46:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const operatorColumn: DynamicColumn = {
|
2023-07-25 16:25:36 +03:00
|
|
|
query,
|
|
|
|
|
field: currentQueryData.queryName,
|
|
|
|
|
dataIndex: currentQueryData.queryName,
|
2023-07-28 18:44:46 +03:00
|
|
|
title: customLabel || operatorLabel,
|
2023-07-17 15:46:41 +03:00
|
|
|
data: [],
|
|
|
|
|
type: 'operator',
|
|
|
|
|
// sortable: isNumber,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
dynamicColumns.push(operatorColumn);
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-25 16:25:36 +03:00
|
|
|
const transformColumnTitles = (
|
|
|
|
|
dynamicColumns: DynamicColumns,
|
|
|
|
|
): DynamicColumns =>
|
|
|
|
|
dynamicColumns.map((item) => {
|
|
|
|
|
if (isFormula(item.field as string)) {
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sameValues = dynamicColumns.filter(
|
|
|
|
|
(column) => column.title === item.title,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (sameValues.length > 1) {
|
|
|
|
|
return {
|
|
|
|
|
...item,
|
|
|
|
|
dataIndex: `${item.title} - ${item.query.queryName}`,
|
|
|
|
|
title: `${item.title} - ${item.query.queryName}`,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
});
|
|
|
|
|
|
2023-06-23 10:15:09 +03:00
|
|
|
const getDynamicColumns: GetDynamicColumns = (queryTableData, query) => {
|
|
|
|
|
const dynamicColumns: DynamicColumns = [];
|
|
|
|
|
|
|
|
|
|
queryTableData.forEach((currentQuery) => {
|
2023-07-28 18:44:46 +03:00
|
|
|
const { series, queryName, list } = currentQuery;
|
|
|
|
|
|
2023-07-25 16:25:36 +03:00
|
|
|
const currentStagedQuery = getQueryByName(
|
|
|
|
|
query.builder,
|
2023-07-28 18:44:46 +03:00
|
|
|
queryName,
|
|
|
|
|
isFormula(queryName) ? 'queryFormulas' : 'queryData',
|
2023-07-25 16:25:36 +03:00
|
|
|
);
|
2023-07-28 18:44:46 +03:00
|
|
|
if (list) {
|
|
|
|
|
list.forEach((listItem) => {
|
2023-07-04 08:24:34 +03:00
|
|
|
Object.keys(listItem.data).forEach((label) => {
|
2023-07-25 16:25:36 +03:00
|
|
|
addListLabels(currentStagedQuery, label as ListItemKey, dynamicColumns);
|
2023-07-04 08:24:34 +03:00
|
|
|
});
|
2023-06-23 10:15:09 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-28 18:44:46 +03:00
|
|
|
if (series) {
|
|
|
|
|
const isValuesColumnExist = series.some((item) => item.values.length > 0);
|
|
|
|
|
const isEveryValuesExist = series.every((item) => item.values.length > 0);
|
2023-07-25 16:25:36 +03:00
|
|
|
|
|
|
|
|
if (isValuesColumnExist) {
|
|
|
|
|
addOperatorFormulaColumns(
|
|
|
|
|
currentStagedQuery,
|
|
|
|
|
dynamicColumns,
|
|
|
|
|
isEveryValuesExist ? undefined : currentStagedQuery.queryName,
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-07-17 15:46:41 +03:00
|
|
|
|
2023-07-28 18:44:46 +03:00
|
|
|
series.forEach((seria) => {
|
2023-07-04 08:24:34 +03:00
|
|
|
Object.keys(seria.labels).forEach((label) => {
|
2023-07-25 16:25:36 +03:00
|
|
|
if (label === currentQuery?.queryName) return;
|
|
|
|
|
|
|
|
|
|
addSeriaLabels(label as string, dynamicColumns, currentStagedQuery);
|
2023-07-04 08:24:34 +03:00
|
|
|
});
|
2023-06-23 10:15:09 +03:00
|
|
|
});
|
2023-07-17 15:46:41 +03:00
|
|
|
}
|
|
|
|
|
});
|
2023-06-23 10:15:09 +03:00
|
|
|
|
2023-07-25 16:25:36 +03:00
|
|
|
return transformColumnTitles(dynamicColumns);
|
2023-06-23 10:15:09 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const fillEmptyRowCells = (
|
|
|
|
|
unusedColumnsKeys: Set<keyof RowData>,
|
|
|
|
|
sourceColumns: DynamicColumns,
|
|
|
|
|
currentColumn: DynamicColumn,
|
|
|
|
|
): void => {
|
|
|
|
|
unusedColumnsKeys.forEach((key) => {
|
2023-07-25 16:25:36 +03:00
|
|
|
if (key === currentColumn.field) {
|
|
|
|
|
const unusedCol = sourceColumns.find((item) => item.field === key);
|
2023-06-23 10:15:09 +03:00
|
|
|
|
|
|
|
|
if (unusedCol) {
|
|
|
|
|
unusedCol.data.push('N/A');
|
|
|
|
|
unusedColumnsKeys.delete(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-28 18:44:46 +03:00
|
|
|
const findSeriaValueFromAnotherQuery = (
|
|
|
|
|
currentLabels: Record<string, string>,
|
|
|
|
|
nextQuery: QueryDataV3 | null,
|
|
|
|
|
): SeriesItem | null => {
|
|
|
|
|
if (!nextQuery || !nextQuery.series) return null;
|
2023-06-23 10:15:09 +03:00
|
|
|
|
2023-07-28 18:44:46 +03:00
|
|
|
let value = null;
|
2023-06-23 10:15:09 +03:00
|
|
|
|
2023-07-28 18:44:46 +03:00
|
|
|
const labelEntries = Object.entries(currentLabels);
|
2023-06-23 10:15:09 +03:00
|
|
|
|
2023-07-28 18:44:46 +03:00
|
|
|
nextQuery.series.forEach((seria) => {
|
|
|
|
|
const localLabelEntries = Object.entries(seria.labels);
|
|
|
|
|
if (localLabelEntries.length !== labelEntries.length) return;
|
|
|
|
|
|
|
|
|
|
const isExistLabels = localLabelEntries.find(([key, value]) =>
|
|
|
|
|
labelEntries.find(
|
|
|
|
|
([currentKey, currentValue]) =>
|
|
|
|
|
currentKey === key && currentValue === value,
|
|
|
|
|
),
|
|
|
|
|
);
|
2023-06-23 10:15:09 +03:00
|
|
|
|
2023-07-28 18:44:46 +03:00
|
|
|
if (isExistLabels) {
|
|
|
|
|
value = seria;
|
|
|
|
|
}
|
2023-07-25 16:25:36 +03:00
|
|
|
});
|
2023-07-28 18:44:46 +03:00
|
|
|
|
|
|
|
|
return value;
|
2023-07-25 16:25:36 +03:00
|
|
|
};
|
2023-06-23 10:15:09 +03:00
|
|
|
|
2023-07-28 18:44:46 +03:00
|
|
|
const isEqualQueriesByLabel = (
|
|
|
|
|
equalQueries: string[],
|
2023-07-25 16:25:36 +03:00
|
|
|
queryName: string,
|
2023-07-28 18:44:46 +03:00
|
|
|
): boolean => equalQueries.includes(queryName);
|
|
|
|
|
|
|
|
|
|
const fillDataFromSeries = (
|
|
|
|
|
currentQuery: QueryDataV3,
|
|
|
|
|
queryTableData: QueryDataV3[],
|
|
|
|
|
columns: DynamicColumns,
|
|
|
|
|
equalQueriesByLabels: string[],
|
|
|
|
|
// TODO: fix it
|
|
|
|
|
// eslint-disable-next-line sonarjs/cognitive-complexity
|
2023-07-25 16:25:36 +03:00
|
|
|
): void => {
|
2023-07-28 18:44:46 +03:00
|
|
|
const { series, queryName } = currentQuery;
|
|
|
|
|
const isEqualQuery = isEqualQueriesByLabel(equalQueriesByLabels, queryName);
|
2023-07-25 16:25:36 +03:00
|
|
|
|
2023-07-28 18:44:46 +03:00
|
|
|
if (!series) return;
|
|
|
|
|
|
|
|
|
|
series.forEach((seria) => {
|
|
|
|
|
const labelEntries = Object.entries(seria.labels);
|
|
|
|
|
|
|
|
|
|
const unusedColumnsKeys = new Set<keyof RowData>(
|
|
|
|
|
columns.map((item) => item.field),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
columns.forEach((column) => {
|
|
|
|
|
if (queryName === column.field) {
|
|
|
|
|
if (seria.values.length === 0) return;
|
2023-07-25 16:25:36 +03:00
|
|
|
|
2023-07-28 18:44:46 +03:00
|
|
|
column.data.push(parseFloat(seria.values[0].value).toFixed(2));
|
|
|
|
|
unusedColumnsKeys.delete(column.field);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (column.type !== 'field' && column.field !== queryName) {
|
|
|
|
|
const nextQueryData =
|
|
|
|
|
queryTableData.find((q) => q.queryName === column.field) || null;
|
|
|
|
|
|
|
|
|
|
const targetSeria = findSeriaValueFromAnotherQuery(
|
|
|
|
|
seria.labels,
|
|
|
|
|
nextQueryData,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (targetSeria) {
|
|
|
|
|
const isEqual = isEqualQueriesByLabel(equalQueriesByLabels, column.field);
|
|
|
|
|
if (!isEqual) {
|
|
|
|
|
equalQueriesByLabels.push(column.field);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
column.data.push(parseFloat(targetSeria.values[0].value).toFixed(2));
|
|
|
|
|
} else {
|
|
|
|
|
column.data.push('N/A');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unusedColumnsKeys.delete(column.field);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isEqualQuery) return;
|
|
|
|
|
|
|
|
|
|
labelEntries.forEach(([key, currentValue]) => {
|
|
|
|
|
if (column.field === key) {
|
|
|
|
|
column.data.push(currentValue);
|
|
|
|
|
unusedColumnsKeys.delete(key);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
fillEmptyRowCells(unusedColumnsKeys, columns, column);
|
|
|
|
|
});
|
2023-06-23 10:15:09 +03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-04 08:24:34 +03:00
|
|
|
const fillDataFromList = (
|
|
|
|
|
listItem: ListItem,
|
|
|
|
|
columns: DynamicColumns,
|
|
|
|
|
): void => {
|
|
|
|
|
columns.forEach((column) => {
|
2023-07-25 16:25:36 +03:00
|
|
|
if (isFormula(column.field)) return;
|
2023-07-04 08:24:34 +03:00
|
|
|
|
|
|
|
|
Object.keys(listItem.data).forEach((label) => {
|
2023-07-25 16:25:36 +03:00
|
|
|
if (column.dataIndex === label) {
|
2023-07-05 10:21:22 +03:00
|
|
|
if (listItem.data[label as ListItemKey] !== '') {
|
|
|
|
|
column.data.push(listItem.data[label as ListItemKey].toString());
|
2023-07-04 08:24:34 +03:00
|
|
|
} else {
|
|
|
|
|
column.data.push('N/A');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-17 15:46:41 +03:00
|
|
|
const fillColumnsData: FillColumnData = (queryTableData, cols) => {
|
2023-06-23 10:15:09 +03:00
|
|
|
const fields = cols.filter((item) => item.type === 'field');
|
|
|
|
|
const operators = cols.filter((item) => item.type === 'operator');
|
2023-07-17 15:46:41 +03:00
|
|
|
const formulas = cols.filter((item) => item.type === 'formula');
|
|
|
|
|
const resultColumns = [...fields, ...operators, ...formulas];
|
2023-06-23 10:15:09 +03:00
|
|
|
|
2023-07-28 18:44:46 +03:00
|
|
|
const equalQueriesByLabels: string[] = [];
|
|
|
|
|
|
2023-06-23 10:15:09 +03:00
|
|
|
queryTableData.forEach((currentQuery) => {
|
2023-07-28 18:44:46 +03:00
|
|
|
const { list } = currentQuery;
|
|
|
|
|
|
|
|
|
|
fillDataFromSeries(
|
|
|
|
|
currentQuery,
|
|
|
|
|
queryTableData,
|
|
|
|
|
resultColumns,
|
|
|
|
|
equalQueriesByLabels,
|
|
|
|
|
);
|
2023-07-04 08:24:34 +03:00
|
|
|
|
2023-07-28 18:44:46 +03:00
|
|
|
if (list) {
|
|
|
|
|
list.forEach((listItem) => {
|
2023-07-04 08:24:34 +03:00
|
|
|
fillDataFromList(listItem, resultColumns);
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-06-23 10:15:09 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const rowsLength = resultColumns.length > 0 ? resultColumns[0].data.length : 0;
|
|
|
|
|
|
|
|
|
|
return { filledDynamicColumns: resultColumns, rowsLength };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const generateData = (
|
|
|
|
|
dynamicColumns: DynamicColumns,
|
|
|
|
|
rowsLength: number,
|
|
|
|
|
): RowData[] => {
|
|
|
|
|
const data: RowData[] = [];
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < rowsLength; i += 1) {
|
|
|
|
|
const rowData: RowData = dynamicColumns.reduce((acc, item) => {
|
2023-07-25 16:25:36 +03:00
|
|
|
const { dataIndex } = item;
|
2023-06-23 10:15:09 +03:00
|
|
|
|
2023-07-25 16:25:36 +03:00
|
|
|
acc[dataIndex] = item.data[i];
|
2023-06-23 10:15:09 +03:00
|
|
|
acc.key = uuid();
|
|
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
|
}, {} as RowData);
|
|
|
|
|
|
|
|
|
|
data.push(rowData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const generateTableColumns = (
|
|
|
|
|
dynamicColumns: DynamicColumns,
|
2023-07-25 15:45:51 +05:30
|
|
|
renderColumnCell?: QueryTableProps['renderColumnCell'],
|
2023-06-23 10:15:09 +03:00
|
|
|
): ColumnsType<RowData> => {
|
|
|
|
|
const columns: ColumnsType<RowData> = dynamicColumns.reduce<
|
|
|
|
|
ColumnsType<RowData>
|
|
|
|
|
>((acc, item) => {
|
|
|
|
|
const column: ColumnType<RowData> = {
|
2023-07-25 16:25:36 +03:00
|
|
|
dataIndex: item.dataIndex,
|
2023-07-17 15:46:41 +03:00
|
|
|
title: item.title,
|
2023-07-25 16:25:36 +03:00
|
|
|
render: renderColumnCell && renderColumnCell[item.dataIndex],
|
2023-07-05 10:21:22 +03:00
|
|
|
// sorter: item.sortable
|
|
|
|
|
// ? (a: RowData, b: RowData): number =>
|
|
|
|
|
// (a[item.key] as number) - (b[item.key] as number)
|
|
|
|
|
// : false,
|
2023-06-23 10:15:09 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return [...acc, column];
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return columns;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const createTableColumnsFromQuery: CreateTableDataFromQuery = ({
|
|
|
|
|
query,
|
|
|
|
|
queryTableData,
|
|
|
|
|
renderActionCell,
|
2023-07-25 15:45:51 +05:30
|
|
|
renderColumnCell,
|
2023-06-23 10:15:09 +03:00
|
|
|
}) => {
|
|
|
|
|
const dynamicColumns = getDynamicColumns(queryTableData, query);
|
|
|
|
|
|
|
|
|
|
const { filledDynamicColumns, rowsLength } = fillColumnsData(
|
|
|
|
|
queryTableData,
|
|
|
|
|
dynamicColumns,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const dataSource = generateData(filledDynamicColumns, rowsLength);
|
|
|
|
|
|
2023-07-25 15:45:51 +05:30
|
|
|
const columns = generateTableColumns(filledDynamicColumns, renderColumnCell);
|
2023-06-23 10:15:09 +03:00
|
|
|
|
|
|
|
|
const actionsCell: ColumnType<RowData> | null = renderActionCell
|
|
|
|
|
? {
|
|
|
|
|
key: 'actions',
|
|
|
|
|
title: 'Actions',
|
|
|
|
|
render: (_, record): ReactNode => renderActionCell(record),
|
|
|
|
|
}
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
if (actionsCell && dataSource.length > 0) {
|
|
|
|
|
columns.push(actionsCell);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { columns, dataSource, rowsLength };
|
|
|
|
|
};
|