public async Task SavedSearchHistory()
        {
            using (var service = await SdkHelper.CreateService())
            {
                const string name   = "sdk-test_SavedSearchHistory";
                const string search = "search index=sdk-tests * earliest=-1m";

                SavedSearchCollection savedSearches = service.SavedSearches;
                await savedSearches.GetSliceAsync(new SavedSearchCollection.Filter {
                    Count = 0, SortDirection = SortDirection.Descending
                });

                SavedSearch savedSearch = savedSearches.SingleOrDefault(ss => ss.Name == name);

                if (savedSearch != null)
                {
                    await savedSearch.RemoveAsync();
                }

                // Create a saved search
                savedSearch = await savedSearches.CreateAsync(name, search);

                // Clear the history - even though we have a newly create saved search
                // it's possible there was a previous saved search with the same name
                // that had a matching history.

                JobCollection history = await savedSearch.GetHistoryAsync();

                foreach (Job job in history)
                {
                    await job.CancelAsync();
                }

                history = await savedSearch.GetHistoryAsync();

                Assert.Equal(0, history.Count);

                Job job1 = await savedSearch.DispatchAsync();

                history = await savedSearch.GetHistoryAsync();

                Assert.Equal(1, history.Count);
                Assert.True(history.Any(a => a.Sid == job1.Sid)); // this.Contains(history, job1.Sid));

                Job job2 = await savedSearch.DispatchAsync();

                history = await savedSearch.GetHistoryAsync();

                Assert.Equal(2, history.Count);
                Assert.True(history.Any(a => a.Sid == job1.Sid));
                Assert.True(history.Any(a => a.Sid == job2.Sid));

                await job1.CancelAsync();

                history = await savedSearch.GetHistoryAsync();

                Assert.Equal(1, history.Count);
                Assert.True(history.Any(a => a.Sid == job2.Sid));

                await job2.CancelAsync();

                history = await savedSearch.GetHistoryAsync();

                Assert.Equal(0, history.Count);

                //// Delete the saved search
                await savedSearches.GetSliceAsync(new SavedSearchCollection.Filter {
                    Count = 0, SortDirection = SortDirection.Descending
                });

                savedSearch = savedSearches.SingleOrDefault(ss => ss.Name == name);
                Assert.NotNull(savedSearch);

                await savedSearch.RemoveAsync();

                savedSearch = await savedSearches.GetOrNullAsync(savedSearch.Name);

                Assert.Null(savedSearch);
            }
        }
 /// <summary>
 /// Asynchronously retrieves the collection of jobs created from a
 /// saved search.
 /// </summary>
 /// <param name="name">
 /// Name of a saved search.
 /// </param>
 /// <returns>
 /// An object representing the collection of jobs created from the
 /// saved search identified by <see cref="name"/>.
 /// </returns>
 /// <remarks>
 /// This method uses the <a href="http://goo.gl/kv9L1l">GET 
 /// saved/searches/{name}/history</a> endpoint to get the collection of
 /// jobs created from the <see cref="SavedSearch"/> identified by <see 
 /// cref="name"/>.
 /// </remarks>
 public async Task<JobCollection> GetSavedSearchHistoryAsync(string name)
 {
     var savedSearch = new SavedSearch(this.Context, this.Namespace, name);
     var jobs = await savedSearch.GetHistoryAsync();
     return jobs;
 }
        public async Task SavedSearchHistory()
        {
            using (var service = this.Connect())
            {
                string savedSearchTitle = "sdk-test1";

                SavedSearchCollection savedSearches = await service.GetSavedSearchesAsync(
                    new SavedSearchCollectionArgs { Count = 0 });

                // Ensure test starts in a known good state

                if (savedSearches.Any(a => a.Name == savedSearchTitle))
                {
                    await service.RemoveSavedSearchAsync(savedSearchTitle);

                    await savedSearches.GetAsync();
                }

                Assert.False(savedSearches.Any(a => a.Name == savedSearchTitle));
                string search = "search index=sdk-tests * earliest=-1m";

                // Create a saved search

                SavedSearch savedSearch = await service.CreateSavedSearchAsync(savedSearchTitle,
                                                                               new SavedSearchAttributes()
                {
                    Search = search
                });

                // Clear the history - even though we have a newly create saved search
                // it's possible there was a previous saved search with the same name
                // that had a matching history.

                JobCollection history = await savedSearch.GetHistoryAsync();

                foreach (Job job in history)
                {
                    await job.CancelAsync();
                }

                history = await savedSearch.GetHistoryAsync();

                Assert.Equal(0, history.Count);

                Job job1 = await savedSearch.DispatchAsync();

                this.Ready(job1);
                history = await savedSearch.GetHistoryAsync();

                Assert.Equal(1, history.Count);
                Assert.True(history.Any(a => a.Sid == job1.Sid)); // this.Contains(history, job1.Sid));

                Job job2 = await savedSearch.DispatchAsync();

                this.Ready(job2);
                history = await savedSearch.GetHistoryAsync();

                Assert.Equal(2, history.Count);
                Assert.True(history.Any(a => a.Sid == job1.Sid));
                Assert.True(history.Any(a => a.Sid == job2.Sid));

                await job1.CancelAsync();

                history = await savedSearch.GetHistoryAsync();

                Assert.Equal(1, history.Count);
                Assert.True(history.Any(a => a.Sid == job2.Sid));

                await job2.CancelAsync();

                history = await savedSearch.GetHistoryAsync();

                Assert.Equal(0, history.Count);

                //// Delete the saved search
                await service.RemoveSavedSearchAsync("sdk-test1");

                await savedSearches.GetAsync();

                Assert.False(savedSearches.Any(a => a.Name == "sdk-test1"));
            }
        }