Skip to content

Commit

Permalink
feat(): cmdb详情构件支持proxy数据源接口
Browse files Browse the repository at this point in the history
Closes CMDB_CONSUME-436
  • Loading branch information
panzekun committed Nov 5, 2024
1 parent 34683f1 commit 8b9722d
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 31 deletions.
35 changes: 28 additions & 7 deletions libs/cmdb-instances/src/data-providers/fetchCmdbInstanceDetail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,43 @@ import {
InstanceApi_getDetail,
InstanceApi_GetDetailRequestParams,
} from "@next-sdk/cmdb-sdk";
import { http } from "@next-core/brick-http";

export function fetchCmdbInstanceDetail(
objectId: string,
instanceId: string
instanceId: string,
sourceId?: string
): Promise<Partial<InstanceApi_GetDetailResponseBody>> {
return InstanceApi_getDetail(objectId, instanceId, {});
return sourceId
? http.post(
"api/gateway/easyops.api.cmdb.topo_center.ProxyGetInstanceDetail@1.0.0/api/v1/proxy-get-instance-detail",
{
objectId,
sourceId,
}
)
: InstanceApi_getDetail(objectId, instanceId, {});
}

export function fetchCmdbInstanceDetailByFields(
objectId: string,
instanceId: string,
fields: string,
relation_limit?: number
relation_limit?: number,
sourceId?: string
): Promise<Partial<InstanceApi_GetDetailResponseBody>> {
return InstanceApi_getDetail(objectId, instanceId, {
fields,
relation_limit,
} as InstanceApi_GetDetailRequestParams);
return sourceId
? http.post(
"api/gateway/easyops.api.cmdb.topo_center.ProxyGetInstanceDetail@1.0.0/api/v1/proxy-get-instance-detail",
{
objectId,
sourceId,
fields,
relation_limit,
}
)
: InstanceApi_getDetail(objectId, instanceId, {
fields,
relation_limit,
} as InstanceApi_GetDetailRequestParams);
}
16 changes: 14 additions & 2 deletions libs/cmdb-instances/src/data-providers/fetchCmdbInstanceSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@ import {
InstanceApi_postSearchV3,
} from "@next-sdk/cmdb-sdk";

import { http } from "@next-core/brick-http";

export function fetchCmdbInstanceSearch(
objectId: string,
params: InstanceApi_PostSearchV3RequestBody
params: InstanceApi_PostSearchV3RequestBody,
sourceId?: string
): Promise<Partial<InstanceApi_PostSearchV3ResponseBody>> {
return InstanceApi_postSearchV3(objectId, params);
return sourceId
? http.post(
"api/gateway/easyops.api.cmdb.topo_center.ProxyPostSearchV3@1.0.1/api/v1/proxy-post-search-v3",
{
...params,
objectId,
sourceId,
}
)
: InstanceApi_postSearchV3(objectId, params);
}
14 changes: 12 additions & 2 deletions libs/cmdb-instances/src/data-providers/fetchCmdbObjectRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@ import {
CmdbObjectApi_GetObjectRefResponseBody,
CmdbObjectApi_getObjectRef,
} from "@next-sdk/cmdb-sdk";
import { http } from "@next-core/brick-http";

export function fetchCmdbObjectRef(
objectId: string
objectId: string,
sourceId?: string
): Promise<CmdbObjectApi_GetObjectRefResponseBody> {
return CmdbObjectApi_getObjectRef({ ref_object: objectId });
return sourceId
? http.post(
"api/gateway/easyops.api.cmdb.topo_center.ProxyGetObjectRef@1.0.0/api/v1/proxy-get-object-ref",
{
ref_object: objectId,
sourceId,
}
)
: CmdbObjectApi_getObjectRef({ ref_object: objectId });
}
78 changes: 60 additions & 18 deletions libs/cmdb-instances/src/instance-detail/InstanceDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ interface LegacyInstanceDetailProps extends WithTranslation {
useAnchor?: boolean;
ignorePermission?: boolean;
useHttpErrorString?: boolean;
externalSourceId?: string;
}

interface LegacyInstanceDetailState {
Expand All @@ -200,6 +201,7 @@ interface LegacyInstanceDetailState {
filterObjectId?: string;
scrollContainer?: HTMLElement;
httpErrorSting?: string;
externalSourceId?: string;
}
function getHref(hash: string) {
const isHash = (hash || "").startsWith("#");
Expand Down Expand Up @@ -257,13 +259,18 @@ export class LegacyInstanceDetail extends React.Component<
searchValue: "",
scrollContainer: null,
httpErrorSting: "",
externalSourceId: "",
};
}

render(): React.ReactNode {
const { loaded, currentAttr, instanceRelationModalData, httpErrorSting } =
this.state;
const { showCard = true, useHttpErrorString = false } = this.props;
const {
showCard = true,
useHttpErrorString = false,
externalSourceId,
} = this.props;
return (
// React Fragments
<Spin spinning={!loaded}>
Expand Down Expand Up @@ -321,7 +328,8 @@ export class LegacyInstanceDetail extends React.Component<
1,
this.state.relationTablePagination.pageSize,
this.state.currentAttr,
value
value,
externalSourceId
);
}}
enterButton
Expand All @@ -343,14 +351,16 @@ export class LegacyInstanceDetail extends React.Component<
1,
this.state.relationTablePagination.pageSize,
this.state.currentAttr,
undefined
undefined,
externalSourceId
);
});
}
}}
relationTablePagination={this.state.relationTablePagination}
relationFieldUrlTemplate={this.props.relationFieldUrlTemplate}
isPagination={true}
externalSourceId={externalSourceId}
total={instanceRelationModalData?.total || 0}
paginationChange={(page, pageSize, relationData) => {
if (
Expand All @@ -360,7 +370,9 @@ export class LegacyInstanceDetail extends React.Component<
this.searchInstanceRelationData(
page,
pageSize,
relationData
relationData,
undefined,
externalSourceId
);
}
}}
Expand Down Expand Up @@ -685,7 +697,8 @@ export class LegacyInstanceDetail extends React.Component<
isJson,
isAttachment,
} = this;
const { modelDataMap, modelData, instanceData } = this.state;
const { modelDataMap, modelData, instanceData, externalSourceId } =
this.state;
let config;
let isComponentMode = false;
let attrCustomConfig: LegacyCustomComponent;
Expand Down Expand Up @@ -805,12 +818,15 @@ export class LegacyInstanceDetail extends React.Component<
value={instanceData[attr.__id]?.slice(0, 10)}
relationFieldUrlTemplate={this.props.relationFieldUrlTemplate}
filterInstanceSourceDisabled={true}
externalSourceId={externalSourceId}
/>
{instanceData[attr.__id]?.length >= 10 && (
<Button
data-testid={"view-more-" + attr.__id}
type="link"
onClick={() => this.handleRelationModal(attr)}
onClick={() =>
this.handleRelationModal(attr, externalSourceId)
}
>
{i18n.t(`${NS_LIBS_CMDB_INSTANCES}:${K.VIEW_MORE}`)}
</Button>
Expand Down Expand Up @@ -894,16 +910,26 @@ export class LegacyInstanceDetail extends React.Component<
);
}
// istanbul ignore next
async handleRelationModal(attr: any): Promise<void> {
await this.searchInstanceRelationData(1, 10, attr);
async handleRelationModal(
attr: any,
externalSourceId?: string
): Promise<void> {
await this.searchInstanceRelationData(
1,
10,
attr,
undefined,
externalSourceId
);
}

// istanbul ignore next
async searchInstanceRelationData(
page: number,
pageSize: number,
attr: any,
value?: string
value?: string,
externalSourceId?: string
): Promise<void> {
const { right_id, left_object_id, right_object_id, left_id } = attr;
let objectId, queryId, oppositeId;
Expand Down Expand Up @@ -950,7 +976,10 @@ export class LegacyInstanceDetail extends React.Component<
if (value !== undefined ? value : this.state.searchValue) {
let oppositeModelDataMap = this.state.oppositeModelDataMap;
if (this.state.oppositeObjectId !== objectId) {
const oppositeModelList = await fetchCmdbObjectRef(objectId);
const oppositeModelList = await fetchCmdbObjectRef(
objectId,
externalSourceId
);
oppositeModelDataMap = keyBy(oppositeModelList.data, "objectId");
this.setState({
oppositeObjectId: objectId,
Expand Down Expand Up @@ -997,7 +1026,8 @@ export class LegacyInstanceDetail extends React.Component<
} else {
instanceRelationModalData = await fetchCmdbInstanceSearch(
objectId,
params
params,
externalSourceId
);
}

Expand Down Expand Up @@ -1251,6 +1281,7 @@ export class LegacyInstanceDetail extends React.Component<
async fetchData(props: LegacyInstanceDetailProps): Promise<void> {
let modelListData, modelDataMap, instanceData;
const relationLimit = 10;
const externalSourceId = props.externalSourceId;
try {
if (props.modelDataList) {
modelDataMap = keyBy(props.modelDataList, "objectId");
Expand All @@ -1260,17 +1291,22 @@ export class LegacyInstanceDetail extends React.Component<
props.objectId,
props.instanceId,
fields,
relationLimit
relationLimit,
externalSourceId
);
} else {
instanceData = await fetchCmdbInstanceDetail(
props.objectId,
props.instanceId
props.instanceId,
externalSourceId
);
}
} else {
if (props.showFields) {
modelListData = await fetchCmdbObjectRef(props.objectId);
modelListData = await fetchCmdbObjectRef(
props.objectId,
externalSourceId
);
modelDataMap = keyBy(modelListData.data, "objectId") as {
[objectId: string]: CmdbModels.ModelCmdbObject;
};
Expand All @@ -1279,21 +1315,26 @@ export class LegacyInstanceDetail extends React.Component<
props.objectId,
props.instanceId,
fields,
relationLimit
relationLimit,
externalSourceId
);
} else {
if (props.ignorePermission) {
[modelListData, { data: instanceData }] = await Promise.all([
fetchCmdbObjectRef(props.objectId),
fetchCmdbObjectRef(props.objectId, externalSourceId),
http.get(
`api/gateway/easyops.api.cmdb.instance.GetDetailWithAdmin/object/${props.objectId}/instance/${props.instanceId}`,
{}
) as any,
]);
} else {
[modelListData, instanceData] = await Promise.all([
fetchCmdbObjectRef(props.objectId),
fetchCmdbInstanceDetail(props.objectId, props.instanceId),
fetchCmdbObjectRef(props.objectId, externalSourceId),
fetchCmdbInstanceDetail(
props.objectId,
props.instanceId,
externalSourceId
),
]);
}
modelDataMap = keyBy(modelListData.data, "objectId") as {
Expand Down Expand Up @@ -1324,6 +1365,7 @@ export class LegacyInstanceDetail extends React.Component<
loaded: true,
basicInfoGroupListShow: [],
basicInfoGroupList: [],
externalSourceId,
});
this.setBasicInfoGroupList(this.state);
// this.setFormattedInstanceData(this.state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface InstanceRelationTableShowProps {
hideRelationLink?: boolean;
onInstanceSourceChange?: (instanceSource: string) => void;
filterInstanceSourceDisabled?: boolean;
externalSourceId?: string;
}
export function reOrderAttrs(fields: string[], fieldOrder: string[] = []) {
return sortBy(fields, (field) => {
Expand Down Expand Up @@ -67,6 +68,7 @@ export function InstanceRelationTableShow(
paginationChange,
total,
relationTablePagination,
externalSourceId,
} = props;
const [oppositeModelDataMap, setOppositeModelDataMap] = useState({});
/**
Expand All @@ -77,14 +79,15 @@ export function InstanceRelationTableShow(
useEffect(() => {
const fetchOppositeModelData = async () => {
const modelListData = await fetchCmdbObjectRef(
relationData.right_object_id
relationData.right_object_id,
externalSourceId
);
setOppositeModelDataMap(keyBy(modelListData.data, "objectId"));
};
fetchOppositeModelData().catch((error) => {
handleHttpError(error);
});
}, [relationData]);
}, [relationData, externalSourceId]);
const [instanceSourceQuery, setInstanceSourceQuery] = useState(null);
const modelData = modelDataMap[relationData.left_object_id];
let oppositeModelData = modifyModelData(
Expand Down Expand Up @@ -169,6 +172,8 @@ export function InstanceRelationTableShow(
: false,
}}
target={"_blank"}
externalSourceId={externalSourceId}
useExternalCmdbApi={externalSourceId ? true : false}
></InstanceListTable>
</div>
);
Expand Down

0 comments on commit 8b9722d

Please sign in to comment.