Skip to content

Commit

Permalink
Added delete materialized run endpoints (#858)
Browse files Browse the repository at this point in the history
* Added endpoint for fetching all notebook jobs

* Refractored NotebookJobInfo to AgentNotebookInfo to make it more consistent with the rest of the codebase

* Added Notebook History endpoint in contracts.

* Added Create, Update, Delete notebook endpoints. Also added separate fetch template, materialized notebook endpoints. This will make the Notebook Request and Notebook History responses lighter.

* AgentNotebookInfo is now derived from AgentJobInfo

* added fetch noteook history endpoint

* Added fetching materialized notebook endpoint

* Added code for cleaning up the directory

* Added create notebook api

* Added Update and delete notebook job

* Fixed notebook history API

* Added last run info to the script and template folder

* Added execute database feature for notebook Jobs

* SQL commands are now using sqlparameters to prevent
any injection attacks

* Changed rundate and runtime to string to preserve
leading zeros

* integration test for agentnotebooks api

* Made some changes mentioned in PR

* Refactored the code, removed enpoint logic from the notebook handler and
wrote test cases

* changes select statements, fixed a bug in the test job cleanup
and fixed other stuff mentioned in the PR.

* added notebook_error column in notebook history select statement

* Added get template notebook endpoint

* Added renaming and pinning notebook runs

* made tables names caps to handle servers with case sensitive queries

* made some changes to the enpoint points paths

* Added delete materialized notebooks endpoints

* Fixed a bug in delete, rename and pin apis where
requests from multiple clients with materializedID = 0
could have resulted in multiple rows getting created
on notebooks.nb_materialized table.

* fixed a merge conflict

* Made some changes mentioned in the PR
  • Loading branch information
aasimkhan30 committed Sep 4, 2019
1 parent 89623c1 commit 8a12802
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 64 deletions.
49 changes: 42 additions & 7 deletions src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void InitializeService(ServiceHost serviceHost)
this.ServiceHost.SetRequestHandler(UpdateAgentNotebookRequest.Type, HandleUpdateAgentNotebookRequest);
this.ServiceHost.SetRequestHandler(UpdateAgentNotebookRunPinRequest.Type, HandleUpdateAgentNotebookRunPinRequest);
this.ServiceHost.SetRequestHandler(UpdateAgentNotebookRunNameRequest.Type, HandleUpdateAgentNotebookRunNameRequest);

this.ServiceHost.SetRequestHandler(DeleteNotebookMaterializedRequest.Type, HandleDeleteNotebookMaterializedRequest);

serviceHost.RegisterShutdownTask(async (shutdownParams, shutdownRequestContext) =>
{
Expand Down Expand Up @@ -1265,7 +1265,7 @@ internal async Task HandleAgentNotebooksRequest(AgentNotebooksParams parameters,
parameters.OwnerUri,
out connInfo);
result = GetAgentNotebookHistories(
result = await GetAgentNotebookHistories(
connInfo,
parameters.JobId,
parameters.JobName,
Expand Down Expand Up @@ -1320,7 +1320,7 @@ internal async Task HandleAgentNotebookTemplateRequest(AgentNotebookTemplatePara
ConnectionServiceInstance.TryFindConnection(
parameters.OwnerUri,
out connInfo);
result.NotebookTemplate = AgentNotebookHelper.GetTemplateNotebook(connInfo, parameters.JobId, parameters.TargetDatabase).Result;
result.NotebookTemplate = await AgentNotebookHelper.GetTemplateNotebook(connInfo, parameters.JobId, parameters.TargetDatabase);
result.Success = true;
}
catch (Exception e)
Expand Down Expand Up @@ -1426,7 +1426,7 @@ internal async Task HandleUpdateAgentNotebookRunNameRequest(UpdateAgentNotebookR
// Calling update helper function
await AgentNotebookHelper.UpdateMaterializedNotebookName(
connInfo,
parameters.MaterializedId,
parameters.agentNotebookHistory,
parameters.TargetDatabase,
parameters.MaterializedNotebookName);
result.Success = true;
Expand Down Expand Up @@ -1455,7 +1455,7 @@ internal async Task HandleUpdateAgentNotebookRunPinRequest(UpdateAgentNotebookRu
// Calling update helper function
await AgentNotebookHelper.UpdateMaterializedNotebookPin(
connInfo,
parameters.MaterializedId,
parameters.agentNotebookHistory,
parameters.TargetDatabase,
parameters.MaterializedNotebookPin);
result.Success = true;
Expand All @@ -1470,7 +1470,36 @@ internal async Task HandleUpdateAgentNotebookRunPinRequest(UpdateAgentNotebookRu
});
}

public AgentNotebookHistoryResult GetAgentNotebookHistories(
internal async Task HandleDeleteNotebookMaterializedRequest(DeleteNotebookMaterializedParams parameters, RequestContext<ResultStatus> requestContext)
{
await Task.Run(async () =>
{
var result = new ResultStatus();
try
{
ConnectionInfo connInfo;
ConnectionServiceInstance.TryFindConnection(
parameters.OwnerUri,
out connInfo);
// Calling update helper function
await AgentNotebookHelper.DeleteMaterializedNotebook(
connInfo,
parameters.agentNotebookHistory,
parameters.TargetDatabase);
result.Success = true;
}
catch (Exception e)
{
result.Success = false;
result.ErrorMessage = e.ToString();
}
await requestContext.SendResult(result);
});
}

public async Task<AgentNotebookHistoryResult> GetAgentNotebookHistories
(
ConnectionInfo connInfo,
string jobId,
string jobName,
Expand Down Expand Up @@ -1519,7 +1548,7 @@ string targetDatabase
var jobHistories = AgentUtilities.ConvertToAgentNotebookHistoryInfo(logEntries, job, steps);
// fetching notebook part of histories
Dictionary<string, DataRow> notebookHistoriesDict = new Dictionary<string, DataRow>();
DataTable materializedNotebookTable = AgentNotebookHelper.GetAgentNotebookHistories(connInfo, jobId, targetDatabase).Result;
DataTable materializedNotebookTable = await AgentNotebookHelper.GetAgentNotebookHistories(connInfo, jobId, targetDatabase);
foreach (DataRow materializedNotebookRow in materializedNotebookTable.Rows)
{
string materializedRunDateTime = materializedNotebookRow["run_date"].ToString() + materializedNotebookRow["run_time"].ToString();
Expand All @@ -1537,8 +1566,14 @@ string targetDatabase
notebookHistory.MaterializedNotebookErrorInfo = notebookHistoriesDict[jobRuntime]["notebook_error"] as string;
notebookHistory.MaterializedNotebookName = notebookHistoriesDict[jobRuntime]["notebook_name"] as string;
notebookHistory.MaterializedNotebookPin = (bool)notebookHistoriesDict[jobRuntime]["pin"];
notebookHistory.MaterializedNotebookDeleted = (bool)notebookHistoriesDict[jobRuntime]["is_deleted"];
}
if (notebookHistory.MaterializedNotebookDeleted)
{
continue;
}
notebookHistories.Add(notebookHistory);

}
result.Histories = notebookHistories.ToArray();
tlog.CloseReader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ public class AgentNotebookHistoryInfo : AgentJobHistoryInfo
public string MaterializedNotebookName { get; set; }
public int MaterializedNotebookErrorFlag { get; set; }
public string MaterializedNotebookErrorInfo { get; set; }
public bool MaterializedNotebookDeleted { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public static readonly
public class UpdateAgentNotebookRunNameParams : TaskRequestDetails
{
public string OwnerUri { get; set; }
public int MaterializedId { get; set; }
public AgentNotebookHistoryInfo agentNotebookHistory { get; set; }
public string MaterializedNotebookName { get; set; }
public string TargetDatabase { get; set; }

Expand All @@ -241,7 +241,7 @@ public static readonly
public class UpdateAgentNotebookRunPinParams : TaskRequestDetails
{
public string OwnerUri { get; set; }
public string MaterializedId { get; set; }
public AgentNotebookHistoryInfo agentNotebookHistory{ get; set; }
public bool MaterializedNotebookPin { get; set; }
public string TargetDatabase { get; set; }

Expand All @@ -256,4 +256,32 @@ public static readonly
RequestType<UpdateAgentNotebookRunPinParams, ResultStatus> Type =
RequestType<UpdateAgentNotebookRunPinParams, ResultStatus>.Create("agent/updatenotebookpin");
}

/// <summary>
/// SQL Agent Notebook materialized params
/// </summary>
public class DeleteNotebookMaterializedParams : TaskRequestDetails
{
public string OwnerUri { get; set; }
public string TargetDatabase { get; set; }
public AgentNotebookHistoryInfo agentNotebookHistory { get; set; }
}

/// <summary>
/// SQL Agent Notebook materialized result
/// </summary>
public class DeleteNotebookMaterializedResult : ResultStatus
{
public string NotebookMaterialized { get; set; }
}

/// <summary>
/// SQL Agent Notebook materialized request type
/// </summary>
public class DeleteNotebookMaterializedRequest
{
public static readonly
RequestType<DeleteNotebookMaterializedParams, ResultStatus> Type =
RequestType<DeleteNotebookMaterializedParams, ResultStatus>.Create("agent/deletenotebookmaterialized");
}
}

0 comments on commit 8a12802

Please sign in to comment.