Skip to content

Commit

Permalink
feat: delete unused widget helper
Browse files Browse the repository at this point in the history
Signed-off-by: yuda <[email protected]>
  • Loading branch information
yuda110 committed Dec 22, 2024
1 parent 2d52a6e commit 60485cd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 141 deletions.
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 }));
};

0 comments on commit 60485cd

Please sign in to comment.