feat(agent): 接入有界只读运维查询
- 使用真实模型工具调用查询项目、部署与 Jenkins 构建和日志,返回可信来源。 - 限制并发、调用轮次、日志及响应大小,隔离个人历史并脱敏审计。 - 增加只读查询和协议回归,不新增依赖、进程或数据库迁移。
This commit is contained in:
@@ -69,6 +69,34 @@ type RunStepMetadata = Prisma.InputJsonObject & {
|
||||
logExcerpt?: string;
|
||||
};
|
||||
|
||||
export type AgentRunQuery = {
|
||||
projectKeys: string[];
|
||||
environment?: DeployRunSummary['environment'];
|
||||
status?: DeployRunStatus;
|
||||
since?: string;
|
||||
until?: string;
|
||||
limit: number;
|
||||
offset: number;
|
||||
};
|
||||
const agentRunSelect = {
|
||||
id: true,
|
||||
version: true,
|
||||
ref: true,
|
||||
status: true,
|
||||
trigger: true,
|
||||
operator: true,
|
||||
jenkinsBuildNumber: true,
|
||||
failureSummary: true,
|
||||
createdAt: true,
|
||||
startedAt: true,
|
||||
finishedAt: true,
|
||||
project: { select: { key: true } },
|
||||
environment: { select: { name: true } },
|
||||
} satisfies Prisma.DeployRunSelect;
|
||||
type AgentRunRecord = Prisma.DeployRunGetPayload<{
|
||||
select: typeof agentRunSelect;
|
||||
}>;
|
||||
|
||||
const terminalRunStatuses = new Set<DeployRunStatus>([
|
||||
'success',
|
||||
'failed',
|
||||
@@ -116,6 +144,10 @@ export class DeployRunRepository {
|
||||
private readonly prisma: PrismaService,
|
||||
) {}
|
||||
|
||||
agentDataSource(): 'database' | 'memory' {
|
||||
return this.useDatabase() ? 'database' : 'memory';
|
||||
}
|
||||
|
||||
async findMany(): Promise<DeployRunSummary[]> {
|
||||
if (!this.useDatabase()) {
|
||||
return [...this.memoryRuns].sort((left, right) =>
|
||||
@@ -142,6 +174,135 @@ export class DeployRunRepository {
|
||||
return runs.map((run) => this.toSummary(run));
|
||||
}
|
||||
|
||||
/** Agent 列表在数据库侧限量与过滤;不读取每次部署的步骤和日志 JSON。 */
|
||||
async findAgentRuns(
|
||||
query: AgentRunQuery,
|
||||
): Promise<{ runs: DeployRunSummary[]; hasMore: boolean }> {
|
||||
const limit = Math.max(1, Math.min(10, query.limit));
|
||||
const offset = Math.max(0, Math.min(200, query.offset));
|
||||
if (!query.projectKeys.length) return { runs: [], hasMore: false };
|
||||
if (!this.useDatabase()) {
|
||||
const runs = this.memoryRuns
|
||||
.filter(
|
||||
(run) =>
|
||||
query.projectKeys.includes(run.projectKey) &&
|
||||
(!query.environment || run.environment === query.environment) &&
|
||||
(!query.status || run.status === query.status) &&
|
||||
(!query.since ||
|
||||
Date.parse(run.createdAt) >= Date.parse(query.since)) &&
|
||||
(!query.until ||
|
||||
Date.parse(run.createdAt) < Date.parse(query.until)),
|
||||
)
|
||||
.sort((a, b) => b.createdAt.localeCompare(a.createdAt))
|
||||
.slice(offset, offset + limit + 1);
|
||||
return {
|
||||
runs: runs.slice(0, limit).map((run) => ({
|
||||
...run,
|
||||
steps: [],
|
||||
failureSummary: run.failureSummary?.slice(0, 800),
|
||||
})),
|
||||
hasMore: runs.length > limit,
|
||||
};
|
||||
}
|
||||
const records = await this.prisma.deployRun.findMany({
|
||||
where: {
|
||||
project: { key: { in: query.projectKeys } },
|
||||
...(query.environment
|
||||
? {
|
||||
environment: {
|
||||
name:
|
||||
query.environment === 'test'
|
||||
? EnvironmentKind.TEST
|
||||
: EnvironmentKind.PRODUCTION,
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
...(query.status ? { status: runStatusToPrisma[query.status] } : {}),
|
||||
createdAt: {
|
||||
...(query.since ? { gte: new Date(query.since) } : {}),
|
||||
...(query.until ? { lt: new Date(query.until) } : {}),
|
||||
},
|
||||
},
|
||||
select: agentRunSelect,
|
||||
orderBy: [{ createdAt: 'desc' }, { id: 'desc' }],
|
||||
skip: offset,
|
||||
take: limit + 1,
|
||||
});
|
||||
return {
|
||||
runs: records
|
||||
.slice(0, limit)
|
||||
.map((record) => this.toAgentSummary(record)),
|
||||
hasMore: records.length > limit,
|
||||
};
|
||||
}
|
||||
|
||||
async findAgentRun(id: string): Promise<DeployRunSummary | null> {
|
||||
if (!this.useDatabase()) {
|
||||
const run = this.memoryRuns.find((item) => item.id === id);
|
||||
return run
|
||||
? {
|
||||
...run,
|
||||
failureSummary: run.failureSummary?.slice(0, 800),
|
||||
steps: run.steps.slice(0, 12).map((step) => ({
|
||||
...step,
|
||||
message: step.message?.slice(0, 800),
|
||||
errorSummary: step.errorSummary?.slice(0, 800),
|
||||
logExcerpt: undefined,
|
||||
})),
|
||||
}
|
||||
: null;
|
||||
}
|
||||
const record = await this.prisma.deployRun.findUnique({
|
||||
where: { id },
|
||||
select: {
|
||||
...agentRunSelect,
|
||||
steps: {
|
||||
take: 12,
|
||||
orderBy: { order: 'asc' },
|
||||
select: {
|
||||
id: true,
|
||||
bpmnNodeId: true,
|
||||
name: true,
|
||||
status: true,
|
||||
order: true,
|
||||
errorSummary: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
if (!record) return null;
|
||||
return {
|
||||
...this.toAgentSummary(record),
|
||||
steps: record.steps.map((step) => ({
|
||||
...step,
|
||||
status: step.status.toLowerCase() as DeployRunStepStatus,
|
||||
errorSummary: step.errorSummary?.slice(0, 800),
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
private toAgentSummary(record: AgentRunRecord): DeployRunSummary {
|
||||
return {
|
||||
id: record.id,
|
||||
version: record.version,
|
||||
projectKey: record.project.key,
|
||||
environment:
|
||||
record.environment.name === EnvironmentKind.TEST
|
||||
? 'test'
|
||||
: 'production',
|
||||
ref: record.ref,
|
||||
status: record.status.toLowerCase() as DeployRunStatus,
|
||||
trigger: record.trigger.toLowerCase() as DeployRunSummary['trigger'],
|
||||
operator: record.operator,
|
||||
jenkinsBuildNumber: record.jenkinsBuildNumber ?? undefined,
|
||||
failureSummary: record.failureSummary?.slice(0, 800),
|
||||
createdAt: record.createdAt.toISOString(),
|
||||
startedAt: record.startedAt?.toISOString(),
|
||||
finishedAt: record.finishedAt?.toISOString(),
|
||||
steps: [],
|
||||
};
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<DeployRunSummary | null> {
|
||||
if (!this.useDatabase()) {
|
||||
return this.memoryRuns.find((run) => run.id === id) ?? null;
|
||||
@@ -844,7 +1005,9 @@ export class DeployRunRepository {
|
||||
|
||||
private toSummary(run: DeployRunRecord): DeployRunSummary {
|
||||
const metadata = this.objectMetadata<DeployRunMetadata>(run.metadata);
|
||||
const sortedSteps = [...run.steps].sort((left, right) => left.order - right.order);
|
||||
const sortedSteps = [...run.steps].sort(
|
||||
(left, right) => left.order - right.order,
|
||||
);
|
||||
|
||||
return {
|
||||
id: run.id,
|
||||
|
||||
Reference in New Issue
Block a user