Cancels a search job.
Cancels a search job.
Name | Type | Description |
---|---|---|
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let job = service.jobs().item("mysid");
await job.cancel();
console.log("CANCELLED");
cancel: function (response_timeout) {
let req = this.post("control", {action: "cancel"}, response_timeout);
return req;
},
Disables preview generation for a search job.
Name | Type | Description |
---|---|---|
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let job = service.jobs().item("mysid");
let res = await job.disablePreview();
console.log("PREVIEW DISABLED");
disablePreview: function(response_timeout) {
var that = this;
let req = this.post("control", {action: "disablepreview"}, response_timeout).catch((err) => {
throw [err, that];
});
return req;
},
Enables preview generation for a search job.
Name | Type | Description |
---|---|---|
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let job = service.jobs().item("mysid");
let res = await job.disablePreview();
console.log("PREVIEW ENABLED");
enablePreview: function(response_timeout) {
var that = this;
let req = this.post("control", {action: "enablepreview"}, response_timeout).catch((err) => {
throw [err, that];
});
return req;
},
Returns the events of a search job with given parameters.
Name | Type | Description |
---|---|---|
params | Object | The parameters for retrieving events. For a list of available parameters, see the GET search/jobs/{search_id}/events endpoint in the REST API documentation. |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let job = service.jobs().item("mysid");
let events;
[events, job] = await job.events({count: 10});
console.log("Fields: ", events.fields);
events: function(params, response_timeout) {
if (utils.isNumber(params) && !response_timeout) {
response_timeout = params;
params = {};
}
params = params || {};
params.output_mode = params.output_mode || "json_rows";
var that = this;
// Default path to v2
let eventsPath = Paths.jobsV2 + "/" + encodeURIComponent(this.name) + "/events";
// Splunk version pre-9.0 doesn't support v2
// v1(GET), v2(POST)
if (this.disableV2SearchApi()) {
eventsPath = Paths.jobs + "/" + encodeURIComponent(this.name) + "/events";
return this.get(eventsPath, params, response_timeout).then((response) => {
return [response.data, that];
});
}
return this.post(eventsPath, params, response_timeout).then((response) => {
return [response.data, that];
});
},
Finalizes a search job.
Name | Type | Description |
---|---|---|
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let job = service.jobs().item("mysid");
let res = await job.finalize();
console.log("JOB FINALIZED");
finalize: function(response_timeout) {
var that = this;
let req = this.post("control", {action: "finalize"}, response_timeout).catch((err) => {
throw [err, that];
});
return req;
},
Constructor for splunkjs.Service.Job
.
Name | Type | Description |
---|---|---|
service | splunkjs.Service | A |
sid | String | The search ID for this search job. |
namespace | Object | Namespace information: |
A new splunkjs.Service.Job
instance.
init: function (service, sid, namespace) {
// Passing the service version and versionCompare to this.path() before instantiating splunkjs.Service.Entity.
this.version = service.version;
this.versionCompare = service.versionCompare;
this.disableV2SearchApi = service.disableV2SearchApi;
this.name = sid;
this._super(service, this.path(), namespace);
this.sid = sid;
// We perform the bindings so that every function works properly
this.cancel = utils.bind(this, this.cancel);
this.disablePreview = utils.bind(this, this.disablePreview);
this.enablePreview = utils.bind(this, this.enablePreview);
this.events = utils.bind(this, this.events);
this.finalize = utils.bind(this, this.finalize);
this.pause = utils.bind(this, this.pause);
this.preview = utils.bind(this, this.preview);
this.results = utils.bind(this, this.results);
this.searchlog = utils.bind(this, this.searchlog);
this.setPriority = utils.bind(this, this.setPriority);
this.setTTL = utils.bind(this, this.setTTL);
this.summary = utils.bind(this, this.summary);
this.timeline = utils.bind(this, this.timeline);
this.touch = utils.bind(this, this.touch);
this.unpause = utils.bind(this, this.unpause);
},
Returns an iterator over this search job's events or results.
Name | Type | Description |
---|---|---|
type | String | One of {"events", "preview", "results"}. |
params | Object | A dictionary of optional parameters: |
iterator: function(type, params) {
return new root.PaginatedEndpointIterator(this[type], params);
},
Retrieves the REST endpoint path for this resource (with no namespace).
path: function () {
// Pre-9.0 uses v1 endpoint
if (this.disableV2SearchApi()) {
return Paths.jobs + "/" + encodeURIComponent(this.name);
}
// Post-9.0 uses v2 endpoint
return Paths.jobsV2 + "/" + encodeURIComponent(this.name);
},
Pauses a search job.
Name | Type | Description |
---|---|---|
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let job = service.jobs().item("mysid");
let res = await job.pause();
console.log("JOB PAUSED");
pause: function(response_timeout) {
var that = this;
let req = this.post("control", {action: "pause"}, response_timeout).catch((err) => {
throw [err, that];
});
return req;
},
Gets the preview results for a search job with given parameters.
Name | Type | Description |
---|---|---|
params | Object | The parameters for retrieving preview results. For a list of available parameters, see the GET search/jobs/{search_id}/results_preview endpoint in the REST API documentation. |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let job = service.jobs().item("mysid");
let results;
[results, job] = await job.preview({count: 10});
console.log("Fields: ", results.fields);
preview: function(params, response_timeout) {
if (utils.isNumber(params) && !response_timeout) {
response_timeout = params;
params = {};
}
params = params || {};
params.output_mode = params.output_mode || "json_rows";
var that = this;
// Default path to v2
let resultsPreviewPath = Paths.jobsV2 + "/" + encodeURIComponent(this.name) + "/results_preview";
// Splunk version pre-9.0 doesn't support v2
// v1(GET), v2(POST)
if (this.disableV2SearchApi()) {
resultsPreviewPath = Paths.jobs + "/" + encodeURIComponent(this.name) + "/results_preview";
return this.get(resultsPreviewPath, params, response_timeout).then((response) => {
return [response.data, that];
});
}
return this.post(resultsPreviewPath, params, response_timeout).then((response) => {
return [response.data, that];
});
},
Gets the results for a search job with given parameters.
The response can be undefined
if the job is not yet done. To avoid this, use the Job.track()
method to wait until the job is complete prior to fetching the results with this method.
Name | Type | Description |
---|---|---|
params | Object | The parameters for retrieving search results. For a list of available parameters, see the GET search/jobs/{search_id}/results endpoint in the REST API documentation. |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let job = service.jobs().item("mysid");
let results;
[results, job] = await job.results({count: 10});
console.log("Fields: ", results.results);
results: function(params, response_timeout) {
if (utils.isNumber(params) && !response_timeout) {
response_timeout = params;
params = {};
}
params = params || {};
params.output_mode = params.output_mode || "json_rows";
var that = this;
// Default path to v2
let resultsPath = Paths.jobsV2 + "/" + encodeURIComponent(this.name) + "/results";
// Splunk version pre-9.0 doesn't support v2
// v1(GET), v2(POST)
if (this.disableV2SearchApi()) {
resultsPath = Paths.jobs + "/" + encodeURIComponent(this.name) + "/results";
return this.get(resultsPath, params, response_timeout).then((response) => {
return [response.data, that];
});
}
return this.post(resultsPath, params, response_timeout).then((response) => {
return [response.data, that];
});
},
Gets the search log for this search job.
Name | Type | Description |
---|---|---|
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let job = service.jobs().item("mysid");
let searchlog;
[searchlog, job] = await job.searchlog();
console.log(searchlog);
searchlog: function(response_timeout) {
var that = this;
return this.get("search.log", {}, response_timeout).then((response) => {
return [response.data, that];
});
},
Sets the priority for this search job.
Name | Type | Description |
---|---|---|
value | Number | The priority (an integer between 1-10). A higher value means a higher priority. |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let job = service.jobs().item("mysid");
let res = await job.setPriority(6);
console.log("JOB PRIORITY SET");
setPriority: function(value, response_timeout) {
var that = this;
let req = this.post("control", {action: "setpriority", priority: value}, response_timeout).catch((err) => {
throw [err, that];
});
return req;
},
Sets the time to live (TTL) for the search job, which is the time before the search job expires after it has been completed and is still available.
Name | Type | Description |
---|---|---|
value | Number | The time to live, in seconds. |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let job = service.jobs().item("mysid");
let res = await job.setTTL(1000);
console.log("JOB TTL SET");
setTTL: function(value, response_timeout) {
var that = this;
let req = this.post("control", {action: "setttl", ttl: value}, response_timeout).catch((err) => {
throw [err, that];
});
return req;
},
Gets the summary for this search job with the given parameters.
Name | Type | Description |
---|---|---|
params | Object | The parameters for retrieving the summary. For a list of available parameters, see the GET search/jobs/{search_id}/summary endpoint in the REST API documentation. |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let job = service.jobs().item("mysid");
let summary;
[summary, job] = await job.summary({top_count: 5});
console.log("Summary: ", summary);
summary: function(params, response_timeout) {
if (utils.isNumber(params) && !response_timeout) {
response_timeout = params;
params = {};
}
var that = this;
return this.get("summary", params, response_timeout).then((response) => {
return [response.data, that];
});
},
Gets the timeline for this search job.
Name | Type | Description |
---|---|---|
params | Object | The parameters for retrieving the timeline. For a list of available parameters, see the GET search/jobs/{search_id}/timeline endpoint in the REST API documentation. |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let job = service.jobs().item("mysid");
let timeline;
[timeline, job] = await job.timeline({time_format: "%c"});
console.log("Timeline: ", timeline);
timeline: function(params, response_timeout) {
if (utils.isNumber(params) && !response_timeout) {
response_timeout = params;
params = {};
}
var that = this;
return this.get("timeline", params, response_timeout).then((response) => {
return [response.data, that];
});
},
Touches a search job, which means extending the expiration time of the search to now plus the time to live (TTL).
Name | Type | Description |
---|---|---|
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let job = service.jobs().item("mysid");
await job.touch();
console.log("JOB TOUCHED");
touch: function(response_timeout) {
var that = this;
let req = this.post("control", {action: "touch"}, response_timeout).catch((err) => {
throw [err, that];
});
return req;
},
Starts polling the status of this search job, and fires callbacks upon each status change.
Name | Type | Description |
---|---|---|
options | Object | A dictionary of optional parameters: |
callbacks | Object,Function | A dictionary of optional callbacks: |
track: async function(options, callbacks) {
let period = options.period || 500; // ms
if (utils.isFunction(callbacks)) {
callbacks = {
done: callbacks
};
}
let noCallbacksAfterReady = (
!callbacks.progress &&
!callbacks.done &&
!callbacks.failed &&
!callbacks.error
);
callbacks.ready = callbacks.ready || function() {};
callbacks.progress = callbacks.progress || function() {};
callbacks.done = callbacks.done || function() {};
callbacks.failed = callbacks.failed || function() {};
callbacks.error = callbacks.error || function() {};
// For use by tests only
callbacks._preready = callbacks._preready || function() {};
callbacks._stoppedAfterReady = callbacks._stoppedAfterReady || function() {};
var that = this;
let emittedReady = false;
let doneLooping = false;
while(!doneLooping) {
try {
let job = await that.fetch();
let dispatchState = job.properties().dispatchState;
let notReady = dispatchState === "QUEUED" || dispatchState === "PARSING";
if (notReady) {
callbacks._preready(job);
} else {
if (!emittedReady) {
callbacks.ready(job);
emittedReady = true;
// Optimization: Don't keep polling the job if the
// caller only cares about the `ready` event.
if (noCallbacksAfterReady) {
doneLooping = true;
return callbacks._stoppedAfterReady(job);
}
}
callbacks.progress(job);
let props = job.properties();
if (dispatchState === "DONE" && props.isDone) {
doneLooping = true;
return callbacks.done(job);
}
else if (dispatchState === "FAILED" && props.isFailed) {
doneLooping = true;
return callbacks.failed(job);
}
}
await utils.sleep(period);
} catch (err) {
return callbacks.error(err);
}
}
},
Resumes a search job.
Name | Type | Description |
---|---|---|
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let job = service.jobs().item("mysid");
let res = await job.unpause();
console.log("JOB UNPAUSED");
unpause: function(response_timeout) {
var that = this;
let req = this.post("control", {action: "unpause"}, response_timeout).catch((err)=>{
throw [err, that];
});
return req;
}
});
Loads the entity and stores the properties.
Name | Type | Description |
---|---|---|
properties | Object | The properties for this entity. |
_load: function(properties) {
properties = utils.isArray(properties) ? properties[0] : properties;
// Initialize the properties to
// empty values
properties = properties || {
content: {},
fields: {},
acl: {},
links: {}
};
this._super(properties);
// Take out the entity-specific content
this._properties = properties.content || {};
this._fields = properties.fields || this._fields || {};
this._acl = properties.acl || {};
this._links = properties.links || {};
this._author = properties.author || null;
this._updated = properties.updated || null;
this._published = properties.published || null;
},
Retrieves the access control list (ACL) information for this entity, which contains the permissions for accessing the entity.
The ACL.
acl: function() {
return this._acl;
},
Update the access control list (ACL) information for this entity, which contains the permissions for accessing the entity.
Name | Type | Description |
---|---|---|
options | Object | Additional entity-specific arguments (required): |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
let savedSearches = svc.savedSearches({ owner: "owner-name", app: "app-name"});
let search = await searches.create({ search: "search * | head 1", name: "acl_test" });
search = await search.acl_update({sharing:"app",owner:"admin","perms.read":"admin"});
acl_update: function(options, response_timeout) {
if(!options.hasOwnProperty("sharing")) {
throw new Error("Required argument 'sharing' is missing.");
}
if(!options.hasOwnProperty("owner")) {
throw new Error("Required argument 'owner' is missing.");
}
return this.post("acl", options, response_timeout).then((res)=>{
return this.fetch({});
});
},
Retrieves the author information for this entity.
The author.
author: function() {
return this._author;
},
Create the URL for the get and post methods This is to allow v1 fallback if the service was instantiated with v2+ and a relpath v1 was provided
Name | Type | Description |
---|---|---|
qualifiedPath | String | A fully-qualified relative endpoint path (for example, "/services/search/jobs"). |
relpath | String | A relative path to append to the endpoint path. |
// Parameters
v2 example:
qualifiedPath = "/servicesNS/admin/foo/search/v2/jobs/id5_1649796951725"
qualifiedPath = "/services/search/v2/jobs/id5_1649796951725"
relpath = "search/v2/jobs/id5_1649796951725/events"
relpath = "events"
// Step 1:
Specifically for splunkjs.Service.Job method, the service endpoint may be provided
Retrieve the service prefix and suffix
servicesNS:
- servicePrefix = "/servicesNS/admin/foo"
- serviceSuffix = "foo/v2/jobs/id5_1649796951725"
services:
- servicePrefix = "/services"
- serviceSuffix = "search/v2/jobs/id5_1649796951725"
// Step 2:
Retrieve Service API version
If version can't be detected, default to 1 (v1)
qualifiedPathVersion = 2
// Step 3:
Retrieve relpath version
If version can't be detected, default to 1 (v1)
relpath = "search/v2/jobs/id5_1649796951725/events"
=> relPathVersion = 2
Check if relpath is a one segment relative path, if so, set to -1
relpath = "events"
=> relPathVersion = -1
// Step 4:
Create the URL based on set criteria
url = "/servicesNS/admin/foo/search/v2/jobs/id5_1649796951725/events"
url = "/services/search/v2/jobs/id5_1649796951725/events"
createUrl: function (qualifiedPath, relpath) {
var url = qualifiedPath,
servicePrefix = qualifiedPath.replace(/(\/services|\/servicesNS\/[^/]+\/[^/]+)\/(.*)/, "$1"),
serviceSuffix = qualifiedPath.replace(/(\/services|\/servicesNS\/[^/]+\/[^/]+)\/(.*)/, "$2"),
qualifiedPathVersionMatch = qualifiedPath.match(/\/(?:servicesNS\/[^/]+\/[^/]+|services)\/[^/]+\/v(\d+)\//),
qualifiedPathVersionMatch = qualifiedPathVersionMatch ? qualifiedPathVersionMatch.length : 0,
qualifiedPathVersion = qualifiedPathVersionMatch || 1,
relPathVersionMatch = relpath.match(/^[^/]+\/v(\d+)\//),
relPathVersionMatch = relPathVersionMatch ? relPathVersionMatch.length : 0,
relPathVersion = relPathVersionMatch || 1;
if (relpath.indexOf('/') == -1) {
relPathVersion = -1;
}
Performs a relative DELETE request on an endpoint's path, combined with the parameters and a relative path if specified.
Name | Type | Description |
---|---|---|
relpath | String | A relative path to append to the endpoint path. |
params | Object | A dictionary of entity-specific parameters to add to the query string. |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
// Will make a request to {service.prefix}/search/jobs/123456
let endpoint = new splunkjs.Service.Endpoint(service, "search/jobs/12345");
let res = await endpoint.delete("", {});
console.log("DELETED");
del: function(relpath, params, response_timeout) {
let url = this.qualifiedPath;
// If we have a relative path, we will append it with a preceding
// slash.
if (relpath) {
url = url + "/" + relpath;
}
return this.service.del(
url,
params,
response_timeout
);
}
});
Disables the entity on the server.
Name | Type | Description |
---|---|---|
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
disable: function(response_timeout) {
var that = this;
return this.post("disable", {}, response_timeout).then((response) => {
return that;
});;
},
Enables the entity on the server.
Name | Type | Description |
---|---|---|
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
enable: function(response_timeout) {
var that = this;
return this.post("enable", {}, response_timeout).then((response)=>{
return that;
});
},
Refreshes the entity by fetching the object from the server and loading it.
Name | Type | Description |
---|---|---|
options | Object | An optional dictionary of collection filtering and pagination options: |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
fetch: function(options, response_timeout) {
if (utils.isNumber(options) && !response_timeout) {
response_timeout = options;
options = {};
}
options = options || {};
var that = this;
return this.get("", options, response_timeout).then((res) => {
that._load(res.data ? res.data.entry : null);
return that;
});
},
A static property that indicates whether to call fetch
after an update to get the updated entity. By default, the entity is not fetched because the endpoint returns (echoes) the updated entity.
fetchOnUpdate: false,
Retrieves the fields information for this entity, indicating which fields are wildcards, required, and optional.
The fields information.
fields: function() {
return this._fields;
},
Performs a relative GET request on an endpoint's path, combined with the parameters and a relative path if specified.
Name | Type | Description |
---|---|---|
relpath | String | A relative path to append to the endpoint path. |
params | Object | A dictionary of entity-specific parameters to add to the query string. |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
// Will make a request to {service.prefix}/search/jobs/123456/results?offset=1
let endpoint = new splunkjs.Service.Endpoint(service, "search/jobs/12345");
let res = await endpoint.get("results", {offset: 1});
console.log("DONE");
get: function(relpath, params, response_timeout, isAsync) {
let url = this.createUrl(this.qualifiedPath, relpath);
return this.service.get(
url,
params,
response_timeout,
isAsync
);
},
Retrieves the links information for this entity, which is the URI of the entity relative to the management port of a Splunk instance.
The links information.
links: function() {
return this._links;
},
Performs a relative POST request on an endpoint's path, combined with the parameters and a relative path if specified.
Name | Type | Description |
---|---|---|
relpath | String | A relative path to append to the endpoint path. |
params | Object | A dictionary of entity-specific parameters to add to the body. |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
// Will make a request to {service.prefix}/search/jobs/123456/control
let endpoint = new splunkjs.Service.Endpoint(service, "search/jobs/12345");
let res = await endpoint.post("control", {action: "cancel"});
console.log("CANCELLED");
post: function(relpath, params, response_timeout) {
let url = this.createUrl(this.qualifiedPath, relpath);
return this.service.post(
url,
params,
response_timeout
);
},
Retrieves the current properties for this resource.
The properties.
properties: function() {
return this._properties;
},
Retrieves the published time for this entity.
The published time.
published: function() {
return this._published;
},
Reloads the entity on the server.
Name | Type | Description |
---|---|---|
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
reload: function(response_timeout) {
var that = this;
return this.post("_reload", {}, response_timeout)
.then((res) => {
return that;
});
}
});
Deletes the entity from the server.
Name | Type | Description |
---|---|---|
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
remove: function(response_timeout) {
return this.del("", {}, response_timeout);
},
Retrieves the current full state (properties and metadata) of this resource.
The current full state of this resource.
state: function() {
return this._state;
}
});
Updates the entity on the server.
Name | Type | Description |
---|---|---|
props | Object | The properties to update the object with. |
response_timeout | Number | A timeout period for aborting a request in milisecs (0 means no timeout). |
update: function(props, response_timeout) {
if (props.hasOwnProperty("name")) {
throw new Error("Cannot set 'name' field in 'update'");
}
var that = this;
let req = this.post("", props, response_timeout).then((response)=>{
if (!that.fetchOnUpdate){
that._load(response.data.entry);
return that;
}
else{
return that.fetch({}, response_timeout)
}
}).catch((err) => {
throw [err, that];
});
return req;
},
Retrieves the updated time for this entity.
The updated time.
updated: function() {
return this._updated;
},