由于我对Angular(v6.0),RxJs(v6.0)和可观察对象的总体经验不足,在过去的一周中引起了相当大的头痛。
组件使用的服务需要输出数据模型集合,但这样做需要四个单独的api调用,而且我遇到了相关数据的问题,可以选择阻止数据模型的返回。
基本流量
设计
期望
问题
已知
尝试
代码
我希望将其简化,但在这里我们调用api的方式很重要。
return this.agentService.GetByFilter(query)
.pipe(
map(agents => {
let deployments : AgentDeployment[] = [];
for(let i = 0; i < agents.length; ++i) {
deployments.push(new AgentDeployment(agents[i].id, agents[i].name, agents[i].tags));
};
return deployments;
}),
map(deployments => {
// Getting related tags assoiciated with the agent
let tagIds : Array<number> = [];
// Get tag id's
for(let i = 0; i < deployments.length; ++i) {
if (deployments[i].tagIds) {
for(let y = 0; y < deployments[i].tagIds.length; ++y) {
if (!tagIds.includes(deployments[i].tagIds[y])) {
tagIds.push(deployments[i].tagIds[y]);
}
};
}
}
// Get the tags associated with the agents
let filter = new Filter();
filter.Field = 'ID';
filter.Value = tagIds.join(',');
// Get tags using list of tag id's
return this.tagService.GetByFilter(filter).pipe(
map(tags => {
if (tags.length) {
for(let i = 0; i < tags.length; ++i) {
// Find the agent
deployments.forEach(deployment => {
if (deployment.tagIds && deployment.tagIds.includes(tags[i].id)) {
deployment.tags.push(tags[i]);
}
});
}
}
return deployments;
}));
}),
mergeMap(deployments => deployments),
map(deployments => {
// Getting deployments related to the agent, an agent may not have any deployment packages associated
let agentIds : Array<number> = [];
// Get tags
for(let i = 0; i < deployments.length; ++i) {
if (deployments[i].id) {
agentIds.push(deployments[i].id);
}
}
// Get the deployment packages for the agents
let filter = new Filter();
filter.Field = 'AgentId';
filter.Value = agentIds.join(',');
// Get agent deployment packages using list of agent id's
return this.agentDeploymentPkgService.GetByFilter(filter)
.mergeMap(packages => packages)
.groupBy(pkg => pkg.id)
.map(pkg$ => {
if (pkg$) {
// Add packages to agent deployment
pkg$.map(pkg => {
// Find the agent
let i : number = deployments.findIndex(agent => agent.id == pkg$.key);
if (pkg.deploymentPackageIds) {
// Keep last deployment ID handy
deployments[i].lastDeploymentId = Math.max.apply(null, pkg.deploymentPackageIds);;
deployments[i].deployments.push(new Deployment(pkg.id, pkg.name))
}
});
}
return deployments;
});
}),
mergeMap(deployments => deployments),
map(deployments => {
// Get the logs for the most recent deployment package for the agent, may not exist if no recent deployment package
let deploymentPackageIds : Array<number> = [];
// Get tags
for(let i = 0; i < deployments.length; ++i) {
if (deployments[i].id) {
deploymentPackageIds.push(deployments[i].lastDeploymentId);
}
}
// Get the deployment packages for the agents
let filter = new Filter();
filter.Field = 'DeploymentPackageId';
filter.Value = deploymentPackageIds.join(',');
// Get agent deployment log based on the last
this.logService.GetByFilter(filter)
.map(logs => {
logs.forEach(log => {
// Find the agent
let i : number = deployments.findIndex(agent => agent.lastDeploymentId == log.deploymentPackageId);
deployments[i].logId = log.id;
deployments[i].status = log.lastReportedStatusMessage;
});
return deployments;
});
return deployments;
})
);