/
SituationFeedbackDAO.ts
151 lines (138 loc) · 4.72 KB
/
SituationFeedbackDAO.ts
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
import {BaseDAO} from './BaseDAO';
import {IHasHTTP} from '../api/IHasHTTP';
import {IOnmsHTTP} from '../api/IOnmsHTTP';
import {OnmsError} from '../api/OnmsError';
import {OnmsHTTPOptions} from '../api/OnmsHTTPOptions';
import {OnmsSituationFeedback} from '../model/OnmsSituationFeedback';
import {OnmsSituationFeedbackType} from '../model/OnmsSituationFeedbackType';
/**
* Data access for [[OnmsSituationFeedback]] objects.
* @category DAO
*/
export class SituationFeedbackDAO extends BaseDAO {
constructor(impl: IHasHTTP | IOnmsHTTP) {
super(impl);
}
/**
* Retrieve feedback.
*
* @version ReST v1
* @param {number} situationId - The alarmId of the Situation to use when querying.
* @return An array of [[OnmsSituationFeedback]] objects.
*/
public async getFeedback(situationId: number): Promise<OnmsSituationFeedback[]> {
const builder = OnmsHTTPOptions.newBuilder().setHeader('Accept', 'application/json');
return this.http.get(this.pathToEndpoint() + '/' + situationId, builder.build()).then((result) => {
const data = this.getData(result);
if (!Array.isArray(data)) {
if (!data) {
return [] as OnmsSituationFeedback[];
}
throw new OnmsError('Expected an array of feedback but got "' + (typeof data) + '" instead.');
}
return data.map((feedbackData) => {
return this.fromData(feedbackData);
});
});
}
/**
* Retrieve the tags for a given prefix
* @param prefix the prefix associated with the tags
*/
public async getTags(prefix: string): Promise<string[]> {
const builder = OnmsHTTPOptions.newBuilder().setHeader('Accept', 'application/json');
return this.http.get(this.pathToEndpoint() + '/tags?prefix=' + prefix, builder.build()).then((result) => {
const data = result.data;
if (!Array.isArray(data)) {
if (!data) {
return [] as string[];
}
throw new OnmsError('Expected an array of tags but got "' + (typeof data) + '" instead.');
}
return data;
});
}
/**
* Submit Correlation Feedback for a Situation.
*
* @version ReST v1
* @param {number} situationId - The alarmId of the Situation to use when querying.
* @param {OnmsSituationFeedback[]} feedback - The [[OnmsSituationFeedback]].
*/
public async saveFeedback(feedback: OnmsSituationFeedback[], situationId: number): Promise<void> {
return this.post(this.pathToEndpoint() + '/' + situationId, this.serializeFeedback(feedback));
}
/**
* Extracts the data from an HTTP Request result.
*
* @param result the HTTP Request result.
* @returns An array of [[OnmsSituationFeedback]] objects.
*/
public getData(result: any): OnmsSituationFeedback[] {
const data = result.data;
if (!Array.isArray(data)) {
throw new OnmsError('Expected an array of situationFeedback but got "' + (typeof data) + '" instead.');
}
return data;
}
/**
* Generate a feedback object from the given dictionary.
* @hidden
*/
public fromData(data: any) {
const feedback = new OnmsSituationFeedback();
feedback.situationKey = data.situationKey;
feedback.fingerprint = data.situationFingerprint;
feedback.alarmKey = data.alarmKey;
feedback.reason = data.reason;
feedback.rootCause = data.rootCause;
feedback.tags = data.tags;
feedback.user = data.user;
if (data.feedbackType) {
const fbt = data.feedbackType;
feedback.feedbackType = OnmsSituationFeedbackType.forId(fbt);
}
feedback.timestamp = this.toNumber(data.timestamp);
return feedback;
}
/**
* Serialize the feedbackType as a string.
* @hidden
*/
public serializeFeedback(feedback: OnmsSituationFeedback[]): any[] {
const serializeFeedback = [] as any[];
feedback.forEach((fb) => {
// Create a shallow clone
const sfb = Object.assign({}, fb) as any;
// Set the type to the id, to avoid serializing it as an object
if (sfb.feedbackType !== null) {
sfb.feedbackType = sfb.feedbackType.id;
}
serializeFeedback.push(sfb);
});
return serializeFeedback;
}
/**
* Call a POST request in the format the SituationFeedback API expects.
* @hidden
*/
private async post(url: string, data: any): Promise<void> {
const builder = OnmsHTTPOptions.newBuilder()
.setHeader('Content-Type', 'application/json')
.setHeader('Accept', 'application/json')
.setData(data);
return this.http.post(url, builder.build()).then((result) => {
if (!result.isSuccess) {
throw result;
}
return;
});
}
/**
* Get the path to the SituationFeedback endpoint.
* @hidden
*/
private pathToEndpoint() {
return 'rest/situation-feedback';
}
}