Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added endpoint for fetching all notebook jobs #834

Merged
merged 23 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
0548b14
Added endpoint for fetching all notebook jobs
Jul 10, 2019
e93875b
Refractored NotebookJobInfo to AgentNotebookInfo to make it more cons…
Jul 10, 2019
f712b14
Added Notebook History endpoint in contracts.
Jul 11, 2019
16ff3e6
Added Create, Update, Delete notebook endpoints. Also added separate …
Jul 12, 2019
598b91e
AgentNotebookInfo is now derived from AgentJobInfo
Jul 13, 2019
e05e3a3
added fetch noteook history endpoint
aasimkhan30 Jul 15, 2019
b81d721
Added fetching materialized notebook endpoint
aasimkhan30 Jul 18, 2019
32be5ac
Added code for cleaning up the directory
aasimkhan30 Jul 18, 2019
b0a9fbf
Added create notebook api
aasimkhan30 Aug 2, 2019
05503eb
Added Update and delete notebook job
aasimkhan30 Aug 3, 2019
d06d027
Fixed notebook history API
aasimkhan30 Aug 6, 2019
ff74e7b
Added last run info to the script and template folder
aasimkhan30 Aug 6, 2019
7602f7e
Added execute database feature for notebook Jobs
aasimkhan30 Aug 10, 2019
0fa8005
Merge branch 'master' of https://github.com/microsoft/sqltoolsservice…
aasimkhan30 Aug 12, 2019
98ef5c8
SQL commands are now using sqlparameters to prevent
aasimkhan30 Aug 13, 2019
e025516
Changed rundate and runtime to string to preserve
aasimkhan30 Aug 13, 2019
42651dd
integration test for agentnotebooks api
aasimkhan30 Aug 13, 2019
d56a0ea
Made some changes mentioned in PR
aasimkhan30 Aug 13, 2019
fc71b39
Refactored the code, removed enpoint logic from the notebook handler and
aasimkhan30 Aug 16, 2019
2e2da19
Merge branch 'master' of https://github.com/microsoft/sqltoolsservice…
aasimkhan30 Aug 16, 2019
cf95619
changes select statements, fixed a bug in the test job cleanup
aasimkhan30 Aug 16, 2019
256e483
added notebook_error column in notebook history select statement
aasimkhan30 Aug 19, 2019
19442dc
Added get template notebook endpoint
aasimkhan30 Aug 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Data;
using System.IO;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Xml;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
Expand Down Expand Up @@ -122,6 +123,8 @@ public void InitializeService(ServiceHost serviceHost)
this.ServiceHost.SetRequestHandler(UpdateAgentScheduleRequest.Type, HandleUpdateAgentScheduleRequest);
this.ServiceHost.SetRequestHandler(DeleteAgentScheduleRequest.Type, HandleDeleteAgentScheduleRequest);

// Notebook request handlers
this.ServiceHost.SetRequestHandler(AgentNotebooksRequest.Type, HandleAgentNotebooksRequest);
}

#region "Jobs Handlers"
Expand Down Expand Up @@ -1211,6 +1214,88 @@ private Tuple<SqlConnectionInfo, DataTable, ServerConnection> CreateSqlConnectio
return new Tuple<SqlConnectionInfo, DataTable, ServerConnection>(sqlConnInfo, dt, serverConnection);
}

internal async Task HandleAgentNotebooksRequest(AgentNotebooksParams parameters, RequestContext<AgentNotebooksResult> requestContext)
{
await Task.Run(async () =>
{
try
{
var result = new AgentNotebooksResult();
ConnectionInfo connInfo;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactor these large methods into several small methods. And create a object model of some type to organize the functionality.

ConnectionServiceInstance.TryFindConnection(
parameters.OwnerUri,
out connInfo);

if (connInfo != null)
{
aasimkhan30 marked this conversation as resolved.
Show resolved Hide resolved
using (SqlConnection connection = new SqlConnection(ConnectionService.BuildConnectionString(connInfo.ConnectionDetails)))
aasimkhan30 marked this conversation as resolved.
Show resolved Hide resolved
{
connection.Open();
/*
This query fetches all the notebook Jobs across all the databases accessible by the user.
aasimkhan30 marked this conversation as resolved.
Show resolved Hide resolved
*/
string getJobIdsFromDatabaseQueryString =
"DECLARE @script as varchar(MAX)\n" +
"SET @script =\n" +
"'USE [?];\n" +
aasimkhan30 marked this conversation as resolved.
Show resolved Hide resolved
"IF EXISTS\n" +
"(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N''nb_template'')\n"+
aasimkhan30 marked this conversation as resolved.
Show resolved Hide resolved
"BEGIN\n" +
"select dbo.nb_template.*,\n" +
aasimkhan30 marked this conversation as resolved.
Show resolved Hide resolved
aasimkhan30 marked this conversation as resolved.
Show resolved Hide resolved
"DB_NAME() as db_name,\n" +
"msdb.dbo.sysjobs.*\n" +
aasimkhan30 marked this conversation as resolved.
Show resolved Hide resolved
"from [?].dbo.nb_template\n" +
"LEFT JOIN\n" +
"msdb.dbo.sysjobs \n" +
"ON \n" +
"[?].dbo.nb_template.job_id = msdb.dbo.sysjobs.job_id\n" +
aasimkhan30 marked this conversation as resolved.
Show resolved Hide resolved
"END '\n" +
"EXEC sp_MSforeachdb @script\n";
var agentnotebooks = new List<NotebookJobInfo>();
aasimkhan30 marked this conversation as resolved.
Show resolved Hide resolved
using( SqlCommand getJobIdsFromDatabaseQueryCommand = new SqlCommand(getJobIdsFromDatabaseQueryString, connection))
{
SqlDataAdapter jobIdsAdapter = new SqlDataAdapter(getJobIdsFromDatabaseQueryCommand);
DataSet jobIdsDataSet = new DataSet();
jobIdsAdapter.Fill(jobIdsDataSet);
var serverConnection = ConnectionService.OpenServerConnection(connInfo);
var fetcher = new JobFetcher(serverConnection);
var filter = new JobActivityFilter();
// Fetching all the jobs
aasimkhan30 marked this conversation as resolved.
Show resolved Hide resolved
var jobs = fetcher.FetchJobs(filter);
// Creating Dictionary of jobs for effcient access later on.
Dictionary<Guid, JobProperties> allJobsHashTable = new Dictionary<Guid, JobProperties>();
if(jobs != null)
{
foreach (var job in jobs.Values)
{
allJobsHashTable.Add(job.JobID, job);
}
}
foreach (DataTable templateTable in jobIdsDataSet.Tables)
{
foreach (DataRow templateRow in templateTable.Rows){
aasimkhan30 marked this conversation as resolved.
Show resolved Hide resolved
NotebookJobInfo notebookJob = new NotebookJobInfo();
notebookJob.Template = (string)templateRow["json"];
aasimkhan30 marked this conversation as resolved.
Show resolved Hide resolved
notebookJob.TargetDatabase = (string)templateRow["db_name"];
aasimkhan30 marked this conversation as resolved.
Show resolved Hide resolved
// Setting Job using the already fetched Jobs
notebookJob.Job = AgentUtilities.ConvertToAgentJobInfo(allJobsHashTable[(Guid)templateRow["job_id"]]);
agentnotebooks.Add(notebookJob);
}
}
}
result.Success = true;
result.Notebooks = agentnotebooks.ToArray();
}
}
await requestContext.SendResult(result);
}
catch (Exception e)
{
await requestContext.SendError(e);
}
});
}

#endregion // "Helpers"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using Microsoft.SqlTools.Hosting.Protocol.Contracts;
using Microsoft.SqlTools.ServiceLayer.TaskServices;
using Microsoft.SqlTools.ServiceLayer.Utility;
using Microsoft.SqlTools.Utility;

namespace Microsoft.SqlTools.ServiceLayer.Agent.Contracts
{
/// <summary>
aasimkhan30 marked this conversation as resolved.
Show resolved Hide resolved
/// SQL Agent Notebooks activity parameters
/// </summary>
public class AgentNotebooksParams : GeneralRequestDetails
{
public string OwnerUri {get; set;}

}
/// <summary>
/// SQL Agent Notebook activity result
/// </summary>
public class AgentNotebooksResult : ResultStatus
{
public NotebookJobInfo[] Notebooks {get; set;}
}
/// <summary>
/// SQL Agent Notebook request type
/// </summary>
public class AgentNotebooksRequest
{
/// <summary>
/// Request definition
/// </summary>
public static readonly
RequestType<AgentNotebooksParams, AgentNotebooksResult> Type =
RequestType<AgentNotebooksParams, AgentNotebooksResult>.Create("agent/notebooks");
}
/// <summary>
/// SQL Agent Notebook Job Info Class
/// </summary>
public class NotebookJobInfo
aasimkhan30 marked this conversation as resolved.
Show resolved Hide resolved
{
public AgentJobInfo Job {get; set;}
public string Template {get; set;}
public string TargetDatabase {get; set;}
}


}