-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding anomaly detector and anomaly scores REST API methods
- Loading branch information
Showing
12 changed files
with
649 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/** | ||
* Copyright 2014 BigML | ||
* | ||
* 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 BigML = require('./BigML'); | ||
var Resource = require('./Resource'); | ||
var constants = require('./constants'); | ||
var logger = require('./logger'); | ||
var utils = require('./utils'); | ||
var async = require('async'); | ||
|
||
function Anomaly(connection) { | ||
Resource.call(this, connection); | ||
} | ||
|
||
Anomaly.prototype = new Resource(); | ||
|
||
Anomaly.prototype.parent = Resource.prototype; | ||
|
||
Anomaly.prototype.create = function (datasets, args, retry, cb) { | ||
/** | ||
* Creates an anomaly detector and builds customized error and resource info | ||
* | ||
* Uses HTTP POST to send dataset content. | ||
* | ||
* Returns a BigML resource wrapped in an object that includes | ||
* code: HTTP status code | ||
* resource: The resource/id | ||
* location: Remote location of the resource | ||
* object: The resource itself | ||
* error: An error code and message | ||
* | ||
* @param {string|object|array} datasets Dataset id or array of dataset ids | ||
* (or objects) | ||
* @param {object} args Arguments that should be used in the call. For | ||
* example {name: "my_name"} | ||
* @param {boolean} retry Turns on/off the retries if a resumable | ||
* condition happens | ||
* @param {function} cb Callback function | ||
*/ | ||
|
||
|
||
var self = this, options, resource, resources, datasetsArray = false, | ||
message = 'Failed to create the anomaly. First parameter must be' + | ||
' a dataset or datasets array.', | ||
resourceId, | ||
reqOptions = { | ||
method: 'POST', | ||
resourceType: 'anomaly', | ||
endpoint: '/' | ||
}; | ||
|
||
if (arguments.length > 0) { | ||
datasetsArray = utils.isArray(datasets); | ||
resource = (datasetsArray) ? datasets[0] : datasets; | ||
resourceId = utils.getResource(resource); | ||
options = utils.optionalCUParams(arguments, message); | ||
|
||
if (datasetsArray && resourceId.type === 'dataset') { | ||
resources = datasets; | ||
options.args['datasets'] = datasets; | ||
} else if (resourceId.type === 'dataset') { | ||
resources = [datasets]; | ||
options.args['dataset'] = resourceId.resource; | ||
} else { | ||
throw new Error(message); | ||
} | ||
options = utils.setRetries(options); | ||
options.type = reqOptions.resourceType; | ||
options.operation = 'create'; | ||
this.resources = resources; | ||
this.options = options | ||
} else { | ||
if (!this.options || !this.resources) { | ||
logger.error("Create has been called with no parameters."); | ||
return; | ||
} | ||
options = this.options; | ||
datasetsArray = this.resources.length > 1; | ||
resource = this.resources[0]; | ||
resources = this.resources; | ||
resourceId = utils.getResource(resource); | ||
} | ||
reqOptions.body = options.args; | ||
|
||
function sendRequest(error) { | ||
if (error) { | ||
logger.error("Origin resources could not be retrieved: " + error); | ||
} else { | ||
self.connection.request(reqOptions, | ||
function processResponse(error, data, response) { | ||
var code = constants.HTTP_INTERNAL_SERVER_ERROR, | ||
result = utils.makeEmptyResult('resource', | ||
code, | ||
'The resource couldn\'t be created'); | ||
return utils.requestResponse('resource', self, | ||
constants.HTTP_CREATED, | ||
constants.HTTP_CREATE_ERRORS, | ||
error, data, response, result, | ||
options.cb); | ||
}); | ||
} | ||
} | ||
// The origin resources must be retrieved in a finished state before | ||
// create starts. This is only done when create is called by the user, not | ||
// internally to retry creation. | ||
if (arguments.length > 0) { | ||
async.each(resources, function(resource, done) { | ||
var id = utils.getResource(resource); | ||
new Resource(self.connection).get(id.resource, true, done); | ||
}, | ||
sendRequest); | ||
} else { | ||
sendRequest(null); | ||
} | ||
|
||
}; | ||
|
||
Anomaly.prototype.list = function (query, cb) { | ||
this.parent.list.call(this, 'anomaly', query, cb); | ||
}; | ||
|
||
|
||
module.exports = Anomaly; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* Copyright 2014 BigML | ||
* | ||
* 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 BigML = require('./BigML'); | ||
var Resource = require('./Resource'); | ||
var constants = require('./constants'); | ||
var logger = require('./logger'); | ||
var utils = require('./utils'); | ||
|
||
function optionalCUParams(argsArray, message) { | ||
/** | ||
* Checks the arguments given to the create method and | ||
* sets their default values if absent. | ||
* | ||
* @param {array} arguments Arguments array | ||
* @param {string} message Resource-customized error message | ||
*/ | ||
|
||
var options, newArgs; | ||
newArgs = [argsArray[0]].concat([].slice.call(argsArray, 2)); | ||
options = utils.optionalCUParams(newArgs, message); | ||
options.inputData = argsArray[1]; | ||
return options; | ||
} | ||
|
||
function AnomalyScore(connection) { | ||
Resource.call(this, connection); | ||
} | ||
|
||
AnomalyScore.prototype = new Resource(); | ||
|
||
AnomalyScore.prototype.parent = Resource.prototype; | ||
|
||
AnomalyScore.prototype.create = function (anomaly, inputData, args, | ||
retry, cb) { | ||
/** | ||
* Creates an anomaly score and builds customized error and resource info | ||
* | ||
* Uses HTTP POST to send dataset content. | ||
* | ||
* Returns a BigML resource wrapped in an object that includes | ||
* code: HTTP status code | ||
* resource: The resource/id | ||
* location: Remote location of the resource | ||
* object: The resource itself | ||
* error: An error code and message | ||
* | ||
* @param {string} anomaly Anomaly id | ||
* @param {object} inputData Input Data to predict from | ||
* @param {object} args Arguments that should be used in the call. For | ||
* example {name: "my_name"} | ||
* @param {boolean} retry Turns on/off the retries if a resumable | ||
* condition happens | ||
* @param {function} cb Callback function | ||
*/ | ||
|
||
var options, | ||
message = 'Failed to create the anomaly score. First parameter must be' + | ||
' an anomaly detector id.', | ||
resourceId = utils.getResource(anomaly), | ||
self = this; | ||
|
||
options = optionalCUParams(arguments, message); | ||
|
||
options.args['input_data'] = options.inputData; | ||
self.parent.create.call(self, 'anomalyscore', ['anomaly'], message, | ||
resourceId.resource, | ||
options.args, options.retry, options.cb); | ||
}; | ||
|
||
AnomalyScore.prototype.list = function (query, cb) { | ||
this.parent.list.call(this, 'anomalyscore', query, cb); | ||
}; | ||
|
||
|
||
module.exports = AnomalyScore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.