This repository has been archived by the owner on Apr 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
pm_client.js
246 lines (230 loc) · 7.54 KB
/
pm_client.js
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
/* eslint-env node es6
Copyright 2016 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict';
var request = require('request');
var parseString = require('xml2js').parseString;
var log4js = require('../app/utils/log4js-logger-util');
var logger = log4js.getLogger('server/pm_client');
var PMClient = module.exports = function (pmService) {
if (pmService) {
let {url, access_key} = pmService.credentials;
let v1Url = '/pm/v1';
this.url = url.includes(v1Url) ? url : url + v1Url;
this.access_key = access_key;
}
};
var FAKE_MODELS = [
{
id: '123',
stream: 'test.sev',
tableData: {
dataTable1: {
'ID': 'INTEGER',
'Age': 'INTEGER',
'Sex': 'STRING'
},
dataTable2: {
'ID': 'INTEGER',
'Age': 'INTEGER',
'Sex': 'STRING',
'res': 'INTEGER'
}
}
},
{
id: '212',
stream: 'test2.sev',
tableData: {
dataTable2: {
'ID': 'INTEGER',
'Value': 'INTEGER'
}
}
},
{
id: 'drug1N',
stream: 'drug1N.mocked.str',
tableData: {
trainingData: {
'Age': 'INTEGER',
'Sex': 'STRING',
'BP': 'STRING',
'Cholesterol': 'STRING',
'Na': 'FLOAT',
'K': 'FLOAT',
'res': 'FLOAT'
},
scoreInput: {
'Age': 'INTEGER',
'Sex': 'STRING',
'BP': 'STRING',
'Cholesterol': 'STRING',
'Na': 'FLOAT',
'K': 'FLOAT'
}
}
}
];
PMClient.prototype = {
getModel: function (contextId, callback) {
logger.enter('getModel()', contextId);
var modelUri = this.url + '/metadata/' + contextId +
'?accesskey="' + this.access_key + '"';
request.get(modelUri, function (error, response, body) {
if (!error && response.statusCode === 200) {
var metadata = JSON.parse(body);
var flag = metadata.flag;
var message = metadata.message;
if (flag) {
parseModelMetadata(message, function (error, model) {
if (error) {
return callback(error);
} else {
model.id = contextId;
logger.return('getModel()', 'model description for contextId=' +
contextId + ' with info about following tables: ' +
Object.keys(model.tableData));
return callback(null, model);
}
});
} else {
error = new Error('PA service error: ' + message);
logger.error('getModel()', 'stream with id=' + contextId, message);
return callback(error);
}
} else if (error) {
logger.error('getModel()', 'stream with id=' + contextId, error);
return callback(error);
} else {
error = new Error('Service error code: ' + response.statusCode);
logger.error('getModel()', 'stream with id=' + contextId,
'statusCode: ' + response.statusCode);
return callback(error);
}
});
},
getScore: function (contextId, scoreParam, callback) {
logger.enter('getScore()',
'contextId: ' + contextId + ', scoreParam: ' + scoreParam);
var scoreUri = this.url + '/score/' + contextId +
'?accesskey="' + this.access_key + '"';
var body = JSON.stringify(scoreParam);
logger.debug('getScore()', 'contextId=' + contextId +
', url: ' + this.url + '/score/' + contextId + '?accesskey=xxx');
logger.debug('getScore()',
'contextId=' + contextId + ', body: ' + body);
request.post({
headers: {'content-type': 'application/json'},
url: scoreUri,
body: body
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
var scoreResponse = JSON.parse(body);
if (scoreResponse.flag === false) {
logger.error('getScore()', 'error during scoring stream \
with contextId=' + contextId + ', msg: ' +
scoreResponse.message.substring(0, scoreResponse.message.indexOf('\n')));
return callback(new Error('PA service error: ' + scoreResponse.message));
} else {
logger.info('getScore()', 'successfully finished scoring for \
stream with contextId=' + contextId);
logger.return('getScore()',
scoreResponse.length + ' row(s) of result');
return callback(null, scoreResponse);
}
} else if (error) {
logger.error(error);
return callback(error);
} else {
error = new Error('Service error code: ' + response.statusCode);
logger.error('getScore()', 'error during scoring stream \
with contextId=' + contextId + ', msg: ' + error);
return callback(error);
}
});
},
getModels: function (callback) {
logger.enter('getModels()');
var client = this;
if (!this.access_key) {
logger.info('getModels()', 'Using FAKE_MODELS');
return callback(null, FAKE_MODELS);
} else {
var modelsUri = this.url + '/model/' + '?accesskey="' +
this.access_key + '"';
request.get(modelsUri, function (error, response, body) {
if (!error && response.statusCode === 200) {
let models = JSON.parse(body);
if (models.flag === false) {
logger.error('getModels() error ' + models.message);
return callback(new Error('PA service error: ' + models.message));
}
logger.debug(`There are ${models.length} models uploaded`);
let modelsData = models.map((model) => {
return new Promise((resolve, reject) => {
client.getModel(model.id, function (error, modelMetadata) {
if (error) {
reject(error);
} else {
model.tableData = modelMetadata.tableData;
resolve();
}
});
});
});
Promise.all(modelsData)
.then(() => {
logger.return('getModels()');
return callback(null, models);
}
)
.catch((err) => {
logger.error('geModels()', err);
return callback(error);
});
} else if (error) {
logger.error('getModels()', error);
return callback(error);
} else {
error = new Error('Service error code: ' + response.statusCode);
logger.error('getModels()', error);
return callback(error);
}
});
}
}
};
function parseModelMetadata(metadata, callback) {
parseString(metadata, {
trim: true
}, function (error, result) {
if (!error) {
var scoringInput = {
'tableData': {}
};
result['metadata']['table'].forEach(function (tableEntry) {
var fields = tableEntry['field'];
var fieldsNames = {};
for (var item in fields) {
fieldsNames[fields[item]['$']['name']] =
fields[item]['$']['storageType'];
}
scoringInput.tableData[tableEntry['$']['name']] = fieldsNames;
});
return callback(null, scoringInput);
} else {
logger.error('parseModelMetadata()', error);
return callback(error);
}
});
}