Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: delete unused widget helper and validation code #5288

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import type { TranslateResult } from 'vue-i18n';

import { i18n } from '@/translations';

import type { GroupByValue, GroupByOptions } from '@/common/modules/widgets/_widget-fields/group-by/type';
import type { TableDataFieldValue } from '@/common/modules/widgets/_widget-fields/table-data-field/type';
import type { XAxisValue } from '@/common/modules/widgets/_widget-fields/x-axis/type';
import type { YAxisValue } from '@/common/modules/widgets/_widget-fields/y-axis/type';
import type { WidgetFieldValue } from '@/common/modules/widgets/_widget-field-value-manager/type';
import type { GroupByOptions, GroupByValue } from '@/common/modules/widgets/_widget-fields/group-by/type';
import type { WidgetConfig } from '@/common/modules/widgets/types/widget-config-type';
import type {
WidgetFieldValues,
} from '@/common/modules/widgets/types/widget-field-value-type';


type OptionsValueMap = Record<string, WidgetFieldValues|undefined>;
type OptionsValueMap = Record<string, WidgetFieldValue<WidgetFieldValues>|undefined>;
interface WidgetOptionValidationProps {
optionValueMap: Ref<OptionsValueMap>;
widgetConfig: Ref<WidgetConfig>;
Expand All @@ -43,7 +41,7 @@ export const useWidgetOptionsComplexValidation = ({
invalidText: computed(() => {
if (_state.widgetConfig.widgetName === 'geoMap') {
const fixedVal = (_state.widgetConfig.requiredFieldsSchema.groupBy?.options as GroupByOptions)?.fixedValue;
const val = (_state.valueMap?.groupBy as GroupByValue)?.value;
const val = (_state.valueMap?.groupBy?.value as GroupByValue)?.data;
if (fixedVal) {
if ((Array.isArray(val) && !val.includes(fixedVal)) || val !== fixedVal) {
return i18n.t('COMMON.WIDGETS.FORM.WIDGET_VALIDATION_WARNING_DESC_GEO_MAP');
Expand All @@ -57,7 +55,7 @@ export const useWidgetOptionsComplexValidation = ({
const getRequiredFieldValidation = (valueMap: OptionsValueMap, config: WidgetConfig): boolean => {
if (config.widgetName === 'geoMap') {
const fixedVal = (config.requiredFieldsSchema.groupBy?.options as GroupByOptions)?.fixedValue;
const val = (valueMap?.groupBy as GroupByValue)?.value;
const val = (valueMap?.groupBy?.value as GroupByValue)?.data;
if (fixedVal) {
if (Array.isArray(val)) return val.includes(fixedVal);
return val === fixedVal;
Expand All @@ -70,23 +68,9 @@ export const useWidgetOptionsComplexValidation = ({
// Label Info Fields Value Duplicate Validation (Table Widget)
if (config.widgetName === 'table') {
const groupByField = 'groupBy';
const tableDataField = 'tableDataField';
const groupByFieldValue = valueMap[groupByField] as GroupByValue;
const tableDataFieldValue = valueMap[tableDataField] as TableDataFieldValue;
const allValueExist = groupByFieldValue?.value && !!groupByFieldValue.value.length
&& (tableDataFieldValue?.staticFieldInfo?.fieldValue || tableDataFieldValue?.dynamicFieldInfo?.fieldValue);
if (tableDataFieldValue?.fieldType === 'dynamicField' && allValueExist) {
isValid = !(groupByFieldValue?.value ?? []).includes(tableDataFieldValue.dynamicFieldInfo?.fieldValue as string);
}
} else if (['clusteredColumnChart', 'lineChart', 'stackedAreaChart', 'stackedColumnChart', 'stackedHorizontalBarChart', 'heatmap', 'colorCodedTableHeatmap'].includes(config.widgetName)) {
let fieldValue = valueMap.xAxis as XAxisValue;
if (config.widgetName === 'stackedHorizontalBarChart') {
fieldValue = valueMap.yAxis as YAxisValue;
}
const tableDataFieldValue = valueMap.tableDataField as TableDataFieldValue;
if (tableDataFieldValue?.fieldType === 'dynamicField') {
isValid = fieldValue?.value !== tableDataFieldValue?.dynamicFieldInfo?.fieldValue;
}
const groupByFieldValue = valueMap[groupByField]?.value as GroupByValue;
const allValueExist = groupByFieldValue?.data && !!groupByFieldValue.data.length;
isValid = !!allValueExist;
} else {
// Label Info Fields Value Duplicate Validation (Except Table Widget)
const allFields = [..._state.requiredFields, ..._state.optionalFields];
Expand Down
104 changes: 1 addition & 103 deletions apps/web/src/common/modules/widgets/_helpers/widget-date-helper.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import type { ManipulateType } from 'dayjs';
import dayjs from 'dayjs';
import { orderBy, sum } from 'lodash';

import { DATE_FORMAT } from '@/common/modules/widgets/_constants/widget-field-constant';
import { isDateField } from '@/common/modules/widgets/_helpers/widget-field-helper';
import type { DateFormat } from '@/common/modules/widgets/_widget-fields/date-format/type';
import type { TableDataFieldValue } from '@/common/modules/widgets/_widget-fields/table-data-field/type';
import type { DateRange, DynamicFieldData } from '@/common/modules/widgets/types/widget-data-type';
import type { DateRange } from '@/common/modules/widgets/types/widget-data-type';

import type { AllReferenceTypeInfo } from '@/services/dashboards/stores/all-reference-type-info-store';

Expand Down Expand Up @@ -158,102 +155,3 @@ export const getFormattedDate = (date: string, dateFormat: string): string => {
if (dateFormatsWithMMM.includes(dateFormat)) return dayjs.utc(date).locale('en').format(dateFormat);
return dayjs.utc(date).format(dateFormat);
};


export const getRefinedDynamicFieldData = (rawData: DynamicFieldData, dynamicFieldInfo: TableDataFieldValue['dynamicFieldInfo'], xAxisField: string): [any[], string[]] => {
if (!rawData?.results?.length) return [[], []];

const valueType = dynamicFieldInfo?.valueType;
const valueCount = dynamicFieldInfo?.count || 0;
const criteria = dynamicFieldInfo?.criteria as string;
const dataField = dynamicFieldInfo?.fieldValue as string;
const dynamicFieldValue = dynamicFieldInfo?.fixedValue || [];

const _refinedResults: any[] = [];
let _seriesFields: string[] = [];
if (valueType === 'fixed') {
let _etcExists = false;
rawData.results.forEach((result) => {
const _filteredData = (result[criteria] || []).filter((d) => dynamicFieldValue.includes(d[dataField]));

// etc data
const _etcData = (result[criteria] || []).filter((d) => !dynamicFieldValue.includes(d[dataField]));
const _etcValueSum = sum(_etcData.map((v) => v.value || 0));
if (_etcValueSum > 0) _etcExists = true;

_refinedResults.push({
...result,
[criteria]: [
..._filteredData,
{ [dataField]: 'etc', value: _etcValueSum },
],
});
});
_seriesFields = [...dynamicFieldValue];
if (_etcExists) _seriesFields.push('etc');
} else {
let _etcExists = false;
const _seriesFieldsSet = new Set<string>();

rawData.results?.forEach((result) => {
let _refinedData: any[] = [];
if (isDateField(dataField)) {
_refinedData = orderBy(result[criteria], dataField, 'desc') ?? [];
} else {
_refinedData = orderBy(result[criteria], 'value', 'desc') ?? [];
}
_refinedData = _refinedData.slice(0, valueCount);
_refinedData.forEach((v) => {
_seriesFieldsSet.add(v[dataField]);
});
_seriesFields = Array.from(_seriesFieldsSet);
if (isDateField(dataField)) _seriesFields.sort();

// etc data
const _etcData = (result[criteria] || []).filter((d) => !_seriesFields.includes(d[dataField]));
const _etcValueSum = sum(_etcData.map((v) => v.value || 0));
if (_etcValueSum > 0) _etcExists = true;

_refinedResults.push({
[criteria]: [
..._refinedData,
{ [dataField]: 'etc', value: _etcValueSum },
],
[xAxisField]: result[xAxisField],
});
});


if (!isDateField(dataField) && _etcExists) _seriesFields.push('etc');
}

return [_refinedResults, _seriesFields];
};
export const getRefinedHeatmapDynamicFieldData = (rawData: DynamicFieldData, dynamicFieldInfo: TableDataFieldValue['dynamicFieldInfo']): string[] => {
if (!rawData?.results?.length) return [];

const valueType = dynamicFieldInfo?.valueType;
const valueCount = dynamicFieldInfo?.count || 0;
const criteria = dynamicFieldInfo?.criteria as string;
const dataField = dynamicFieldInfo?.fieldValue as string;
const dynamicFieldValue = dynamicFieldInfo?.fixedValue || [];

let _seriesFields: string[] = [];
if (valueType === 'fixed') {
_seriesFields = [...dynamicFieldValue];
} else {
const _subTotalResults: Record<string, number> = {};
rawData.results.forEach((result) => {
if (!Array.isArray(result[criteria])) return;
result[criteria]?.forEach((d) => {
if (d[dataField] in _subTotalResults) {
_subTotalResults[d[dataField]] += d.value;
} else {
_subTotalResults[d[dataField]] = d.value;
}
});
});
_seriesFields = orderBy(Object.entries(_subTotalResults), 1, 'desc').slice(0, valueCount).map(([k]) => k);
}
return _seriesFields;
};
42 changes: 4 additions & 38 deletions apps/web/src/common/modules/widgets/_helpers/widget-load-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import dayjs from 'dayjs';
import type { Query } from '@cloudforet/core-lib/space-connector/type';

import { getDateFormat, getTimeUnit } from '@/common/modules/widgets/_helpers/widget-date-helper';
import { isDateField } from '@/common/modules/widgets/_helpers/widget-field-helper';
import type { TableDataFieldValue } from '@/common/modules/widgets/_widget-fields/table-data-field/type';
import type { DateRange } from '@/common/modules/widgets/types/widget-data-type';


Expand Down Expand Up @@ -38,41 +36,9 @@ export const getWidgetLoadApiQueryDateRange = (granularity: string, dateRange: D
return dateRange;
};

export const getWidgetLoadApiQuery = (dataFieldInfo: TableDataFieldValue, xAxisField: string): Record<string, any> => {
const _dataFieldType = dataFieldInfo?.fieldType;
const _dataField = _dataFieldType === 'staticField' ? dataFieldInfo?.staticFieldInfo?.fieldValue : dataFieldInfo?.dynamicFieldInfo?.fieldValue;
const _criteria = dataFieldInfo?.dynamicFieldInfo?.criteria;
const _dynamicFixedFieldValue = dataFieldInfo?.dynamicFieldInfo?.fixedValue;

const _fields = {};
let _groupBy: string[] = [xAxisField];
let _field_group: string[] = [];
let _sort: Query['sort'] = [];
let _filter: Query['filter'] = [];
if (_dataFieldType === 'staticField') {
_dataField?.forEach((field) => {
_fields[field] = { key: field, operator: 'sum' };
});
_sort = _groupBy.includes('Date') ? [{ key: 'Date', desc: false }] : _dataField?.map((field) => ({ key: field, desc: true }));
} else {
_fields[_criteria] = { key: _criteria, operator: 'sum' };
_field_group = [_dataField];
_groupBy = [..._groupBy, _dataField];
_sort = _groupBy.includes('Date') && !_field_group.includes('Date') ? [{ key: 'Date', desc: false }] : [{ key: `_total_${_criteria}`, desc: true }];
}
if (isDateField(_dataField) && _dataFieldType === 'dynamicField' && _dynamicFixedFieldValue?.length) {
_dynamicFixedFieldValue.sort();
_filter = [{
k: _dataField,
v: _dynamicFixedFieldValue,
o: 'in',
}];
export const getWidgetLoadApiQuerySort = (xAxisField: string, dataField: string[]): Query['sort'] => {
if (xAxisField === 'Date') {
return [{ key: 'Date', desc: false }];
}
return {
fields: _fields,
groupBy: _groupBy,
field_group: _field_group,
sort: _sort,
filter: _filter,
};
return dataField.map((field) => ({ key: field, desc: true }));
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface PrivateWidgetLoadParameters {
granularity: string;
start: string;
end: string;
group_by?: string[];
sort?: Sort[];
page?: Page
vars?: DashboardVars;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface PublicWidgetLoadParameters {
granularity: string;
start: string;
end: string;
group_by?: string[];
sort?: Sort[];
page?: Page
vars?: DashboardVars;
Expand Down
Loading