From 234773e99218ccc16aefc10a07a10917dced1a6b Mon Sep 17 00:00:00 2001 From: Luke Gumbley Date: Tue, 31 Mar 2020 19:43:49 +1300 Subject: [PATCH 1/3] Add pagination support to history calls Although the REST docs don't mention it, the history call accepts pagination parameters. Without passing parameters, a default of only 30 results will be returned. --- lib/service.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/service.js b/lib/service.js index 299c2a022..b3767ea22 100644 --- a/lib/service.js +++ b/lib/service.js @@ -1704,22 +1704,29 @@ * @example * * var savedSearch = service.savedSearches().item("MySavedSearch"); - * savedSearch.history(function(err, jobs, search) { + * savedSearch.history({count: 10}, function(err, jobs, search) { * for(var i = 0; i < jobs.length; i++) { * console.log("Job", i, ":", jobs[i].sid); * } * }); * + * @param {Object} options Options for retrieving history. For a full list, see the Pagination and Filtering options in the REST API documentation. * @param {Function} callback A function to call when the history is retrieved: `(err, job, savedSearch)`. * * @endpoint saved/searches/{name}/history * @method splunkjs.Service.SavedSearch */ - history: function(callback) { + history: function(options, callback) { + if (!callback && utils.isFunction(options)) { + callback = options; + options = {}; + } + callback = callback || function() {}; + options = options || {}; var that = this; - return this.get("history", {}, function(err, response) { + return this.get("history", options, function(err, response) { if (err) { callback(err); return; From 8ce8c392ecba86333b8643afb7cf0036c6b23b91 Mon Sep 17 00:00:00 2001 From: vmalaviya Date: Mon, 30 Aug 2021 11:34:39 +0530 Subject: [PATCH 2/3] Changes for savedsearch history pagination --- lib/service.js | 25 +++++++++++++++--- tests/service_tests/savedsearch.js | 41 ++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/lib/service.js b/lib/service.js index 299c2a022..bd7837f6c 100644 --- a/lib/service.js +++ b/lib/service.js @@ -1702,24 +1702,43 @@ * `splunkjs.Service.Job` instances. * * @example - * + * With callback + * * var savedSearch = service.savedSearches().item("MySavedSearch"); * savedSearch.history(function(err, jobs, search) { * for(var i = 0; i < jobs.length; i++) { * console.log("Job", i, ":", jobs[i].sid); * } * }); + * + * @example + * With calback and optional parameters. + * Pass { count: 0 } to fetch all results. + * + * var savedSearch = service.savedSearches().item("MySavedSearch"); + * savedSearch.history({count: 1}, function(err, jobs, search) { + * for(var i = 0; i < jobs.length; i++) { + * console.log("Job", i, ":", jobs[i].sid); + * } + * }); * + * @param {Object} options Additional parameters. * @param {Function} callback A function to call when the history is retrieved: `(err, job, savedSearch)`. * * @endpoint saved/searches/{name}/history * @method splunkjs.Service.SavedSearch */ - history: function(callback) { + history: function(options, callback) { + if (!callback && utils.isFunction(options)) { + callback = options; + options = {}; + } + callback = callback || function() {}; + options = options || {}; var that = this; - return this.get("history", {}, function(err, response) { + return this.get("history", options, function(err, response) { if (err) { callback(err); return; diff --git a/tests/service_tests/savedsearch.js b/tests/service_tests/savedsearch.js index 5430806ed..9676e3a46 100644 --- a/tests/service_tests/savedsearch.js +++ b/tests/service_tests/savedsearch.js @@ -195,6 +195,47 @@ exports.setup = function (svc, loggedOutSvc) { done(); }) + it("Callback#history with pagination", function (done) { + var name = "jssdk_savedsearch_" + getNextId(); + var originalSearch = "search index=_internal | head 1"; + var searches = this.service.savedSearches({ owner: this.service.username, app: "sdkappcollection" }); + + Async.chain([ + function (done) { + searches.create({ search: originalSearch, name: name }, done); + }, + function (search, done) { + assert.ok(search); + search.dispatch(done); + }, + function (job, search, done) { + assert.ok(job); + assert.ok(search); + search.dispatch(done); + }, + function (job, search, done) { + assert.ok(job); + assert.ok(search); + + tutils.pollUntil( + job, () => job.properties()["isDone"], 10, Async.augment(done, search) + ); + }, + function (job, search, done) { + search.history({ count: 1 }, Async.augment(done, job)); + }, + function (jobs, search, originalJob, done) { + assert.ok(jobs.length > 0); + assert.equal(jobs.length, 1); + done(); + }], + function (err) { + assert.ok(!err); + done(); + } + ); + }); + it("Callback#history error", function (done) { var name = "jssdk_savedsearch_" + getNextId(); var originalSearch = "search index=_internal | head 1"; From 971d655d16269c5a59da7cf7d99921f99a3f3bd0 Mon Sep 17 00:00:00 2001 From: vmalaviya Date: Tue, 31 Aug 2021 14:27:02 +0530 Subject: [PATCH 3/3] Update service.js --- lib/service.js | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/lib/service.js b/lib/service.js index bd7837f6c..b506d7eba 100644 --- a/lib/service.js +++ b/lib/service.js @@ -1702,43 +1702,24 @@ * `splunkjs.Service.Job` instances. * * @example - * With callback - * + * * var savedSearch = service.savedSearches().item("MySavedSearch"); * savedSearch.history(function(err, jobs, search) { * for(var i = 0; i < jobs.length; i++) { * console.log("Job", i, ":", jobs[i].sid); * } * }); - * - * @example - * With calback and optional parameters. - * Pass { count: 0 } to fetch all results. - * - * var savedSearch = service.savedSearches().item("MySavedSearch"); - * savedSearch.history({count: 1}, function(err, jobs, search) { - * for(var i = 0; i < jobs.length; i++) { - * console.log("Job", i, ":", jobs[i].sid); - * } - * }); * - * @param {Object} options Additional parameters. * @param {Function} callback A function to call when the history is retrieved: `(err, job, savedSearch)`. * * @endpoint saved/searches/{name}/history * @method splunkjs.Service.SavedSearch */ - history: function(options, callback) { - if (!callback && utils.isFunction(options)) { - callback = options; - options = {}; - } - + history: function(callback) { callback = callback || function() {}; - options = options || {}; var that = this; - return this.get("history", options, function(err, response) { + return this.get("history", {}, function(err, response) { if (err) { callback(err); return; @@ -5694,4 +5675,4 @@ }); } }); -})(); +})(); \ No newline at end of file