From 2d4918ad83de8f966916f2fe672daddac492e24d Mon Sep 17 00:00:00 2001 From: Aditya Bist Date: Wed, 4 Apr 2018 17:12:40 -0700 Subject: [PATCH] Agent work (WIP) (#597) * Initial SQL Agent WIP * Fix test build break * wip * wip2 * additonal SQL Agent history parsing code * Fix namespace sorting * Hook up agent\jobs method * fixed agent service tests * added job history handler * Fix broken integration test build * code review refactoring * Added handler for job history request (#586) * fixed agent service tests * added job history handler * code review refactoring * Turn off another failing test * Disable failing test * refactored code * small refactor * code review changes * Feature/agent1 adbist (#592) * fixed agent service tests * added job history handler * code review refactoring * refactored code * small refactor * code review changes * changed constant casing * added handler for job actions * Remove unused code * Remove unused test file * Feature/agent1 adbist (#593) * fixed agent service tests * added job history handler * code review refactoring * refactored code * small refactor * code review changes * changed constant casing * added handler for job actions * Reenable disabled test * cleaned up code * added steps to job history * sort the job history steps * changed error handling for job action --- .../Agent/AgentService.cs | 37 +++++++++++++++++-- .../Agent/Contracts/AgentJobHistoryInfo.cs | 10 +++++ .../Agent/JobUtilities.cs | 21 ++++++++++- 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs b/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs index 5b627578..b2e3dfbd 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs @@ -147,12 +147,41 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent DataTable dt = tuple.Item2; int count = dt.Rows.Count; var agentJobs = new List(); + + var agentStepMap = new Dictionary>(); for (int i = 0; i < count; ++i) { var job = dt.Rows[i]; - agentJobs.Add(JobUtilities.ConvertToAgentJobHistoryInfo(job, sqlConnInfo)); + if (JobUtilities.IsStep(job, sqlConnInfo)) + { + var agentJobStep = JobUtilities.ConvertToAgentJobStep(job, sqlConnInfo); + if (agentStepMap.ContainsKey(agentJobStep.RunDate)) + { + agentStepMap[agentJobStep.RunDate].Add(agentJobStep); + } + else + { + var agentJobSteps = new List(); + agentJobSteps.Add(agentJobStep); + agentStepMap[agentJobStep.RunDate] = agentJobSteps; + } + } + else + { + var agentJobHistoryInfo = JobUtilities.ConvertToAgentJobHistoryInfo(job, sqlConnInfo); + agentJobs.Add(agentJobHistoryInfo); + } } result.Succeeded = true; + foreach (AgentJobHistoryInfo agentJobHistoryInfo in agentJobs) + { + if (agentStepMap.ContainsKey(agentJobHistoryInfo.RunDate)) + { + var agentStepList = agentStepMap[agentJobHistoryInfo.RunDate].ToList(); + agentStepList.Sort(delegate (AgentJobStep s1, AgentJobStep s2) { return s1.StepId.CompareTo(s2.StepId); }); + agentJobHistoryInfo.Steps = agentStepList.ToArray(); + } + } result.Jobs = agentJobs.ToArray(); await requestContext.SendResult(result); } @@ -168,9 +197,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent /// internal async Task HandleJobActionRequest(AgentJobActionParams parameters, RequestContext requestContext) { + var result = new AgentJobActionResult(); try { - var result = new AgentJobActionResult(); ConnectionInfo connInfo; ConnectionServiceInstance.TryFindConnection( parameters.OwnerUri, @@ -207,7 +236,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent } catch (Exception e) { - await requestContext.SendError(e); + result.Succeeded = false; + result.ErrorMessage = e.Message; + await requestContext.SendResult(result); } } diff --git a/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentJobHistoryInfo.cs b/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentJobHistoryInfo.cs index 2b322942..cc4e7b6c 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentJobHistoryInfo.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Agent/Contracts/AgentJobHistoryInfo.cs @@ -33,6 +33,16 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent.Contracts public string OperatorPaged { get; set; } public int RetriesAttempted { get; set; } public string Server { get; set; } + public AgentJobStep[] Steps { get; set; } + } + + public class AgentJobStep + { + public int StepId { get; set; } + public string StepName { get; set; } + public string Message { get; set; } + public DateTime RunDate { get; set; } + } } diff --git a/src/Microsoft.SqlTools.ServiceLayer/Agent/JobUtilities.cs b/src/Microsoft.SqlTools.ServiceLayer/Agent/JobUtilities.cs index 2e2fb7bb..ebab1728 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Agent/JobUtilities.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Agent/JobUtilities.cs @@ -56,6 +56,12 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent }; } + public static bool IsStep(DataRow row, SqlConnectionInfo sqlConnectionInfo) + { + int stepId = Convert.ToInt32(row[UrnStepID], System.Globalization.CultureInfo.InvariantCulture); + return stepId != 0; + } + public static AgentJobHistoryInfo ConvertToAgentJobHistoryInfo(DataRow jobRow, SqlConnectionInfo sqlConnInfo) { // get all the values for a job history @@ -99,8 +105,21 @@ namespace Microsoft.SqlTools.ServiceLayer.Agent jobHistoryInfo.OperatorPaged = operatorPaged; jobHistoryInfo.RetriesAttempted = retriesAttempted; jobHistoryInfo.Server = server; - return jobHistoryInfo; } + + public static AgentJobStep ConvertToAgentJobStep(DataRow jobRow, SqlConnectionInfo sqlConnInfo) + { + int stepId = Convert.ToInt32(jobRow[UrnStepID], System.Globalization.CultureInfo.InvariantCulture); + string stepName = Convert.ToString(jobRow[UrnStepName], System.Globalization.CultureInfo.InvariantCulture); + string message = Convert.ToString(jobRow[UrnMessage], System.Globalization.CultureInfo.InvariantCulture); + DateTime runDate = Convert.ToDateTime(jobRow[UrnRunDate], System.Globalization.CultureInfo.InvariantCulture); + AgentJobStep step = new AgentJobStep(); + step.StepId = stepId; + step.StepName = stepName; + step.Message = message; + step.RunDate = runDate; + return step; + } } } \ No newline at end of file