114 lines
3.2 KiB
JavaScript
114 lines
3.2 KiB
JavaScript
'use strict'
|
|
|
|
function buildXpackMlGetRecords (opts) {
|
|
// eslint-disable-next-line no-unused-vars
|
|
const { makeRequest, ConfigurationError, result } = opts
|
|
/**
|
|
* Perform a [xpack.ml.get_records](http://www.elastic.co/guide/en/elasticsearch/reference/current/ml-get-record.html) request
|
|
*
|
|
* @param {string} job_id -
|
|
* @param {boolean} exclude_interim - Exclude interim results
|
|
* @param {int} from - skips a number of records
|
|
* @param {int} size - specifies a max number of records to get
|
|
* @param {string} start - Start time filter for records
|
|
* @param {string} end - End time filter for records
|
|
* @param {double} record_score -
|
|
* @param {string} sort - Sort records by a particular field
|
|
* @param {boolean} desc - Set the sort direction
|
|
* @param {object} body - Record selection criteria
|
|
*/
|
|
return function xpackMlGetRecords (params, callback) {
|
|
if (typeof params === 'function' || params == null) {
|
|
callback = params
|
|
params = {}
|
|
}
|
|
// promises support
|
|
if (callback == null) {
|
|
return new Promise((resolve, reject) => {
|
|
xpackMlGetRecords(params, (err, body) => {
|
|
err ? reject(err) : resolve(body)
|
|
})
|
|
})
|
|
}
|
|
|
|
// check required parameters
|
|
if (params['job_id'] == null && params['jobId'] == null) {
|
|
return callback(
|
|
new ConfigurationError('Missing required parameter: job_id or jobId'),
|
|
result
|
|
)
|
|
}
|
|
|
|
// build querystring object
|
|
const querystring = {}
|
|
const keys = Object.keys(params)
|
|
const acceptedQuerystring = [
|
|
'exclude_interim',
|
|
'from',
|
|
'size',
|
|
'start',
|
|
'end',
|
|
'record_score',
|
|
'sort',
|
|
'desc'
|
|
]
|
|
const acceptedQuerystringCamelCased = [
|
|
'excludeInterim',
|
|
'from',
|
|
'size',
|
|
'start',
|
|
'end',
|
|
'recordScore',
|
|
'sort',
|
|
'desc'
|
|
]
|
|
|
|
for (var i = 0, len = keys.length; i < len; i++) {
|
|
var key = keys[i]
|
|
if (acceptedQuerystring.indexOf(key) !== -1) {
|
|
querystring[key] = params[key]
|
|
} else {
|
|
var camelIndex = acceptedQuerystringCamelCased.indexOf(key)
|
|
if (camelIndex !== -1) {
|
|
querystring[acceptedQuerystring[camelIndex]] = params[key]
|
|
}
|
|
}
|
|
}
|
|
|
|
// configure http method
|
|
var method = params.method
|
|
if (method == null) {
|
|
method = params.body == null ? 'GET' : 'POST'
|
|
}
|
|
|
|
// validate headers object
|
|
if (params.headers != null && typeof params.headers !== 'object') {
|
|
return callback(
|
|
new ConfigurationError(`Headers should be an object, instead got: ${typeof params.headers}`),
|
|
result
|
|
)
|
|
}
|
|
|
|
var ignore = params.ignore || null
|
|
if (typeof ignore === 'number') {
|
|
ignore = [ignore]
|
|
}
|
|
|
|
// build request object
|
|
const parts = ['_xpack', 'ml', 'anomaly_detectors', params['job_id'] || params['jobId'], 'results', 'records']
|
|
const request = {
|
|
method,
|
|
path: '/' + parts.filter(Boolean).map(encodeURIComponent).join('/'),
|
|
querystring,
|
|
body: params.body || '',
|
|
headers: params.headers || null,
|
|
ignore,
|
|
requestTimeout: params.requestTimeout || null
|
|
}
|
|
|
|
return makeRequest(request, callback)
|
|
}
|
|
}
|
|
|
|
module.exports = buildXpackMlGetRecords
|