forked from rinafcode/teachLink_backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathincident-management.controller.ts
More file actions
250 lines (233 loc) · 7.41 KB
/
Copy pathincident-management.controller.ts
File metadata and controls
250 lines (233 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import {
Controller,
Get,
Post,
Put,
Body,
Param,
Query,
HttpCode,
HttpStatus,
Logger,
} from '@nestjs/common';
import { IncidentManagementService } from './incident-management.service';
import {
CreateIncidentDto,
UpdateIncidentDto,
ResolveIncidentDto,
EscalateIncidentDto,
GetIncidentsQueryDto,
IncidentResponseDto,
CreateRemediationActionDto,
RemediationActionResponseDto,
CreateRunbookExecutionDto,
RunbookExecutionResponseDto,
} from './dto';
import { Incident } from './entities/incident.entity';
import { RemediationAction } from './entities/remediation-action.entity';
import { RunbookExecution } from './entities/runbook-execution.entity';
@Controller('incidents')
export class IncidentManagementController {
private readonly logger = new Logger(IncidentManagementController.name);
constructor(private incidentManagementService: IncidentManagementService) {}
/**
* Create a new incident manually
*/
@Post()
@HttpCode(HttpStatus.CREATED)
async createIncident(@Body() createIncidentDto: CreateIncidentDto): Promise<IncidentResponseDto> {
this.logger.log(`Creating incident: ${createIncidentDto.title}`);
const incident = await this.incidentManagementService.createIncident(createIncidentDto);
return this.mapIncidentToDto(incident);
}
/**
* Get all incidents
*/
@Get()
async getIncidents(
@Query() query: GetIncidentsQueryDto,
): Promise<{ data: IncidentResponseDto[]; total: number }> {
const result = await this.incidentManagementService.getIncidents(query);
return {
data: result.data.map((incident) => this.mapIncidentToDto(incident)),
total: result.total,
};
}
/**
* Get incident by ID
*/
@Get(':incidentId')
async getIncidentById(@Param('incidentId') incidentId: string): Promise<IncidentResponseDto> {
const incident = await this.incidentManagementService.getIncidentById(incidentId);
if (!incident) {
throw new Error(`Incident not found: ${incidentId}`);
}
return this.mapIncidentToDto(incident);
}
/**
* Update incident
*/
@Put(':incidentId')
async updateIncident(
@Param('incidentId') incidentId: string,
@Body() updateIncidentDto: UpdateIncidentDto,
): Promise<IncidentResponseDto> {
const incident = await this.incidentManagementService.updateIncident(
incidentId,
updateIncidentDto,
);
return this.mapIncidentToDto(incident);
}
/**
* Resolve incident
*/
@Post(':incidentId/resolve')
async resolveIncident(
@Param('incidentId') incidentId: string,
@Body() resolveIncidentDto: ResolveIncidentDto,
): Promise<IncidentResponseDto> {
this.logger.log(`Resolving incident: ${incidentId}`);
const incident = await this.incidentManagementService.resolveIncident(
incidentId,
resolveIncidentDto.resolutionNotes,
);
return this.mapIncidentToDto(incident);
}
/**
* Escalate incident
*/
@Post(':incidentId/escalate')
async escalateIncident(
@Param('incidentId') incidentId: string,
@Body() escalateIncidentDto: EscalateIncidentDto,
): Promise<IncidentResponseDto> {
this.logger.log(`Escalating incident: ${incidentId}`);
const incident = await this.incidentManagementService.escalateIncident(
incidentId,
escalateIncidentDto.escalatedTo,
escalateIncidentDto.reason,
);
return this.mapIncidentToDto(incident);
}
/**
* Create remediation action
*/
@Post(':incidentId/remediation-actions')
@HttpCode(HttpStatus.CREATED)
async createRemediationAction(
@Param('incidentId') incidentId: string,
@Body() createDto: CreateRemediationActionDto,
): Promise<RemediationActionResponseDto> {
this.logger.log(`Creating remediation action for incident: ${incidentId}`);
const remediationAction = await this.incidentManagementService.createRemediationAction({
...createDto,
incidentId,
});
return this.mapRemediationActionToDto(remediationAction);
}
/**
* Get remediation actions for incident
*/
@Get(':incidentId/remediation-actions')
async getRemediationActions(
@Param('incidentId') incidentId: string,
): Promise<RemediationActionResponseDto[]> {
const actions =
await this.incidentManagementService.getRemediationActionsForIncident(incidentId);
return actions.map((action) => this.mapRemediationActionToDto(action));
}
/**
* Execute runbook for incident
*/
@Post(':incidentId/runbook-executions')
@HttpCode(HttpStatus.CREATED)
async executeRunbook(
@Param('incidentId') incidentId: string,
@Body() createDto: CreateRunbookExecutionDto,
): Promise<RunbookExecutionResponseDto> {
this.logger.log(`Executing runbook for incident: ${incidentId}`);
const execution = await this.incidentManagementService.executeRunbookForIncident(
incidentId,
createDto.runbookName,
);
return this.mapRunbookExecutionToDto(execution);
}
/**
* Get runbook executions for incident
*/
@Get(':incidentId/runbook-executions')
async getRunbookExecutions(
@Param('incidentId') incidentId: string,
): Promise<RunbookExecutionResponseDto[]> {
const executions =
await this.incidentManagementService.getRunbookExecutionsForIncident(incidentId);
return executions.map((execution) => this.mapRunbookExecutionToDto(execution));
}
/**
* List available runbooks
*/
@Get('runbooks/available')
async listAvailableRunbooks(): Promise<string[]> {
return this.incidentManagementService.listAvailableRunbooks();
}
/**
* Get incident management statistics
*/
@Get('statistics/overview')
async getStatistics() {
return this.incidentManagementService.getStatistics();
}
/**
* Mapper functions
*/
private mapIncidentToDto(incident: Incident): IncidentResponseDto {
return {
id: incident.id,
title: incident.title,
description: incident.description,
status: incident.status,
severity: incident.severity,
triggerMetrics: incident.triggerMetrics,
runbookId: incident.runbookId,
remediationActionIds: incident.remediationActionIds,
escalatedTo: incident.escalatedTo,
resolvedAt: incident.resolvedAt,
resolutionNotes: incident.resolutionNotes,
detectedAt: incident.detectedAt,
updatedAt: incident.updatedAt,
};
}
private mapRemediationActionToDto(action: RemediationAction): RemediationActionResponseDto {
return {
id: action.id,
incidentId: action.incidentId,
actionType: action.actionType,
description: action.description,
status: action.status,
parameters: action.parameters,
executedAt: action.executedAt,
executionOutput: action.executionOutput,
errorMessage: action.errorMessage,
autoRollback: action.autoRollback,
rolledBackAt: action.rolledBackAt,
createdAt: action.createdAt,
updatedAt: action.updatedAt,
};
}
private mapRunbookExecutionToDto(execution: RunbookExecution): RunbookExecutionResponseDto {
return {
id: execution.id,
incidentId: execution.incidentId,
runbookName: execution.runbookName,
runbookPath: execution.runbookPath,
status: execution.status,
startedAt: execution.startedAt,
completedAt: execution.completedAt,
stepExecutions: execution.stepExecutions,
executionSummary: execution.executionSummary,
errorDetails: execution.errorDetails,
createdAt: execution.createdAt,
updatedAt: execution.updatedAt,
};
}
}