Skip to content

Commit

Permalink
fix: add preset values to variableModel stat results & apply cost-d…
Browse files Browse the repository at this point in the history
…ata-source presetValues to data-table filter (#4625) (#4627)

* fix: add preset values to variableModel stat results



* fix: apply cost-data-source presetValues to data-table filter



---------

Signed-off-by: samuel.park <[email protected]>
  • Loading branch information
piggggggggy authored Sep 2, 2024
1 parent 9e826c5 commit 6a97f6f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ const costFilterState = reactive({
return sortBy(Object.entries(additionalInfo).map(([k]) => ({
name: `additional_info.${k}`,
label: k,
presetKeys: additionalInfo[k].enums,
})), 'label');
}
return dataSource ? sortBy((dataSource.data?.cost_additional_info_keys ?? [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ const state = reactive({
loading: false,
proxyFilter: useProxyValue('filter', props, emit),
filterItems: computed(() => props.filterItems),
selectedItems: [] as MenuItem[],
selectedItems: [] as any[],
handlerMap: computed(() => {
const handlerMaps = {};
if (props.sourceType === DATA_SOURCE_DOMAIN.COST) {
state.selectedItems.forEach(({ name }) => {
handlerMaps[name] = getCostMenuHandler(name, {}, state.primaryCostStatOptions);
state.selectedItems.forEach(({ name, presetKeys }) => {
handlerMaps[name] = getCostMenuHandler(name, {}, state.primaryCostStatOptions, { presetKeys });
});
} else {
assetFilterState.refinedLabelKeys.forEach((labelInfo) => {
Expand Down Expand Up @@ -193,7 +193,12 @@ const resetFilterByKey = (key: string) => {
state.proxyFilter = { ...state.proxyFilter };
state.selectedItemsMap = { ...state.selectedItemsMap };
};
const getCostMenuHandler = (groupBy: string, listQueryOptions: Partial<Record<ManagedVariableModelKey, any>>, primaryOptions: Record<string, any>): AutocompleteHandler => {
const getCostMenuHandler = (
groupBy: string,
listQueryOptions: Partial<Record<ManagedVariableModelKey, any>>,
primaryOptions: Record<string, any>,
modelOptions?: Record<string, any>,
): AutocompleteHandler => {
try {
let variableModelInfo: VariableModelMenuHandlerInfo;
const _variableOption = GROUP_BY_TO_VAR_MODELS[groupBy];
Expand All @@ -204,7 +209,7 @@ const getCostMenuHandler = (groupBy: string, listQueryOptions: Partial<Record<Ma
};
} else {
const CostVariableModel = new VariableModelFactory({ type: 'MANAGED', managedModelKey: MANAGED_VARIABLE_MODEL_KEY_MAP.cost });
CostVariableModel[groupBy] = CostVariableModel.generateProperty({ key: groupBy });
CostVariableModel[groupBy] = CostVariableModel.generateProperty({ key: groupBy, presetValues: modelOptions?.presetKeys });
variableModelInfo = {
variableModel: CostVariableModel,
dataKey: groupBy,
Expand Down
19 changes: 15 additions & 4 deletions apps/web/src/lib/variable-models/_base/resource-variable-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default class ResourceVariableModel<T=any> implements IResourceVariableMo
name: options.name,
isDataKey: _isDataKey,
isFilter: _isFilter,
values: _isDataKey ? this.stat(options.key as string) : undefined,
values: _isDataKey ? this.stat(options.key as string, options.presetValues) : undefined,
};

// TODO: keys method binding
Expand Down Expand Up @@ -114,6 +114,10 @@ export default class ResourceVariableModel<T=any> implements IResourceVariableMo
apiQueryHelper.addFilter({ k: dataKey, v: query.filters, o: '=' });
}

if (query.start !== undefined && query.limit !== undefined) {
apiQueryHelper.setPage(query.start, query.limit);
}

this.#properties?.forEach((key) => {
if (this[key]?.fixedValue) {
apiQueryHelper.addFilter({ k: key, v: this[key].fixedValue, o: '=' });
Expand Down Expand Up @@ -186,7 +190,7 @@ export default class ResourceVariableModel<T=any> implements IResourceVariableMo
}
}

stat(dataKey: string): (query?: ListQuery) => Promise<ListResponse> {
stat(dataKey: string, presetValues?: string[]): (query?: ListQuery) => Promise<ListResponse> {
const _dataKey = dataKey;
return async (query: ListQuery = {}) => {
try {
Expand All @@ -200,10 +204,17 @@ export default class ResourceVariableModel<T=any> implements IResourceVariableMo
if (status === 'succeed') {
let more = false;
if (query.start !== undefined && query.limit !== undefined && response.total_count !== undefined) {
more = (query.start * query.limit) < response.total_count;
more = (query.start - 1 + query.limit) < response.total_count;
}

const _presetValues = presetValues;
const _resultsWithPreset = [
...(query.start === 1 ? (_presetValues ?? []).map((d) => ({ name: d, key: d })) : []),
...(response.results.filter((d) => !_presetValues?.includes(d)).map((d) => ({ key: d, name: d })) ?? []),
];

this.#response = {
results: response.results ? response.results.map((d) => ({ key: d, name: d })) : [],
results: _presetValues ? _resultsWithPreset : (response.results ?? []).map((d) => ({ key: d, name: d })),
more,
};
}
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/lib/variable-models/_base/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface PropertyOptions<T> {
name?: string;
isDataKey?: boolean;
isFilter?: boolean;
presetValues?: string[];
}
export interface PropertyObject<T> {
key: keyof T;
Expand Down

0 comments on commit 6a97f6f

Please sign in to comment.