Skip to content

Commit

Permalink
Agent: Edit Job backend (#700)
Browse files Browse the repository at this point in the history
* added stepInfo for editing jobs/steps

* added schedules to history requests

* added alerts and schedules to history

* formatting

* code review comments

* removed smo import
  • Loading branch information
Aditya Bist committed Oct 4, 2018
1 parent f990618 commit 6f69f7e
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 29 deletions.
19 changes: 18 additions & 1 deletion src/Microsoft.SqlTools.ServiceLayer/Agent/AgentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ await Task.Run(async () =>
out connInfo);
if (connInfo != null)
{
ConnectionServiceInstance.TryFindConnection(parameters.OwnerUri, out connInfo);
CDataContainer dataContainer = CDataContainer.CreateDataContainer(connInfo, databaseExists: true);
var jobs = dataContainer.Server.JobServer.Jobs;
Tuple<SqlConnectionInfo, DataTable, ServerConnection> tuple = CreateSqlConnection(connInfo, parameters.JobId);
SqlConnectionInfo sqlConnInfo = tuple.Item1;
DataTable dt = tuple.Item2;
Expand All @@ -202,7 +205,19 @@ await Task.Run(async () =>
var tlog = t as ILogSource;
tlog.Initialize();
var logEntries = t.LogEntries;
jobHistories = AgentUtilities.ConvertToAgentJobHistoryInfo(logEntries, job);
// Send Steps, Alerts and Schedules with job history in background
JobStepCollection steps = jobs[jobName].JobSteps;
JobScheduleCollection schedules = jobs[jobName].JobSchedules;
List<Alert> alerts = new List<Alert>();
foreach (Alert alert in dataContainer.Server.JobServer.Alerts)
{
if (alert.JobID == jobId && alert.JobName == jobName)
{
alerts.Add(alert);
}
}
jobHistories = AgentUtilities.ConvertToAgentJobHistoryInfo(logEntries, job, steps, schedules, alerts);
tlog.CloseReader();
}
result.Jobs = jobHistories.ToArray();
Expand Down Expand Up @@ -739,6 +754,7 @@ await Task.Run(async () =>
for (int i = 0; i < scheduleCount; ++i)
{
var schedule = dataContainer.Server.JobServer.SharedSchedules[i];
var scheduleData = new JobScheduleData(schedule);
schedules[i] = new AgentScheduleInfo();
schedules[i].Id = schedule.ID;
schedules[i].Name = schedule.Name;
Expand All @@ -756,6 +772,7 @@ await Task.Run(async () =>
schedules[i].JobCount = schedule.JobCount;
schedules[i].ActiveEndDate = schedule.ActiveEndDate;
schedules[i].ScheduleUid = schedule.ScheduleUid;
schedules[i].Description = scheduleData.Description;
}
result.Schedules = schedules;
result.Success = true;
Expand Down
124 changes: 104 additions & 20 deletions src/Microsoft.SqlTools.ServiceLayer/Agent/Common/AgentUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,93 @@ public static AgentJobInfo ConvertToAgentJobInfo(JobProperties job)
};
}

public static AgentJobStep ConvertToAgentJobStep(ILogEntry logEntry, DataRow jobRow)
internal static AgentJobStep ConvertToAgentJobStepInfo(JobStep step, LogSourceJobHistory.LogEntryJobHistory logEntry, string jobId)
{
var entry = logEntry as LogSourceJobHistory.LogEntryJobHistory;
string stepId = entry.StepID;
string stepName = entry.StepName;
string message = entry.Message;
DateTime runDate = logEntry.PointInTime;
int runStatus = logEntry.Severity == SeverityClass.Success ? 1 : 0;
AgentJobStep step = new AgentJobStep();
step.StepId = stepId;
step.StepName = stepName;
step.Message = message;
step.RunDate = runDate;
step.RunStatus = runStatus;
return step;
AgentJobStepInfo stepInfo = new AgentJobStepInfo();
stepInfo.JobId = jobId;
stepInfo.JobName = logEntry.JobName;
stepInfo.StepName = step.Name;
stepInfo.SubSystem = step.SubSystem.ToString();
stepInfo.Id = step.ID;
stepInfo.FailureAction = step.OnFailAction.ToString();
stepInfo.SuccessAction = step.OnSuccessAction.ToString();
stepInfo.FailStepId = step.OnFailStep;
stepInfo.SuccessStepId = step.OnSuccessStep;
stepInfo.Command = step.Command;
stepInfo.CommandExecutionSuccessCode = step.CommandExecutionSuccessCode;
stepInfo.DatabaseName = step.DatabaseName;
stepInfo.DatabaseUserName = step.DatabaseUserName;
stepInfo.Server = step.Server;
stepInfo.OutputFileName = step.OutputFileName;
stepInfo.RetryAttempts = step.RetryAttempts;
stepInfo.RetryInterval = step.RetryInterval;
stepInfo.ProxyName = step.ProxyName;
AgentJobStep jobStep = new AgentJobStep();
jobStep.stepId = logEntry.StepID;
jobStep.stepName = logEntry.StepName;
jobStep.stepDetails = stepInfo;
jobStep.message = logEntry.Message;
jobStep.runDate = step.LastRunDate.ToString();
jobStep.runStatus = (Contracts.CompletionResult) step.LastRunOutcome;
return jobStep;
}

public static List<AgentJobHistoryInfo> ConvertToAgentJobHistoryInfo(List<ILogEntry> logEntries, DataRow jobRow)
internal static AgentScheduleInfo ConvertToAgentScheduleInfo(JobSchedule schedule)
{
AgentScheduleInfo scheduleInfo = new AgentScheduleInfo();
scheduleInfo.Id = schedule.ID;
scheduleInfo.Name = schedule.Name;
scheduleInfo.JobName = " ";
scheduleInfo.IsEnabled = schedule.IsEnabled;
scheduleInfo.FrequencyTypes = (Contracts.FrequencyTypes) schedule.FrequencyTypes;
scheduleInfo.FrequencySubDayTypes = (Contracts.FrequencySubDayTypes) schedule.FrequencySubDayTypes;
scheduleInfo.FrequencySubDayInterval = schedule.FrequencySubDayInterval;
scheduleInfo.FrequencyRelativeIntervals = (Contracts.FrequencyRelativeIntervals) schedule.FrequencyRelativeIntervals;
scheduleInfo.FrequencyRecurrenceFactor = schedule.FrequencyRecurrenceFactor;
scheduleInfo.FrequencyInterval = schedule.FrequencyInterval;
scheduleInfo.DateCreated = schedule.DateCreated;
scheduleInfo.ActiveStartTimeOfDay = schedule.ActiveStartTimeOfDay;
scheduleInfo.ActiveStartDate = schedule.ActiveStartDate;
scheduleInfo.ActiveEndTimeOfDay = schedule.ActiveEndTimeOfDay;
scheduleInfo.ActiveEndDate = schedule.ActiveEndDate;
scheduleInfo.JobCount = schedule.JobCount;
scheduleInfo.ScheduleUid = schedule.ScheduleUid;
var scheduleData = new JobScheduleData(schedule);
scheduleInfo.Description = scheduleData.Description;
return scheduleInfo;
}

internal static AgentAlertInfo ConvertToAgentAlertInfo(Alert alert)
{
AgentAlertInfo alertInfo = new AgentAlertInfo();
alertInfo.Id = alert.ID;
alertInfo.Name = alert.Name;
alertInfo.DelayBetweenResponses = alert.DelayBetweenResponses;
alertInfo.EventDescriptionKeyword = alert.EventDescriptionKeyword;
alertInfo.EventSource = alert.EventSource;
alertInfo.HasNotification = alert.HasNotification;
alertInfo.IncludeEventDescription = (Contracts.NotifyMethods) alert.IncludeEventDescription;
alertInfo.IsEnabled = alert.IsEnabled;
alertInfo.JobId = alert.JobID.ToString();
alertInfo.JobName = alert.JobName;
alertInfo.LastOccurrenceDate = alert.LastOccurrenceDate.ToString();
alertInfo.LastResponseDate = alert.LastResponseDate.ToString();
alertInfo.MessageId = alert.MessageID;
alertInfo.NotificationMessage = alert.NotificationMessage;
alertInfo.OccurrenceCount = alert.OccurrenceCount;
alertInfo.PerformanceCondition = alert.PerformanceCondition;
alertInfo.Severity = alert.Severity;
alertInfo.DatabaseName = alert.DatabaseName;
alertInfo.CountResetDate = alert.CountResetDate.ToString();
alertInfo.CategoryName = alert.CategoryName;
alertInfo.AlertType = (Contracts.AlertType) alert.AlertType;
alertInfo.WmiEventNamespace = alert.WmiEventNamespace;
alertInfo.WmiEventQuery = alert.WmiEventQuery;
return alertInfo;
}

public static List<AgentJobHistoryInfo> ConvertToAgentJobHistoryInfo(List<ILogEntry> logEntries,
DataRow jobRow, JobStepCollection steps, JobScheduleCollection schedules, List<Alert> alerts)
{
List<AgentJobHistoryInfo> jobs = new List<AgentJobHistoryInfo>();
// get all the values for a job history
Expand Down Expand Up @@ -102,14 +171,29 @@ public static List<AgentJobHistoryInfo> ConvertToAgentJobHistoryInfo(List<ILogEn

// Add steps to the job if any
var jobSteps = new List<AgentJobStep>();
if (entry.CanLoadSubEntries)
foreach (JobStep step in steps)
{
foreach (ILogEntry step in entry.SubEntries)
{
jobSteps.Add(AgentUtilities.ConvertToAgentJobStep(step, jobRow));
}
var jobId = jobRow[UrnJobId].ToString();
jobSteps.Add(AgentUtilities.ConvertToAgentJobStepInfo(step, logEntry, jobId));
}

jobHistoryInfo.Steps = jobSteps.ToArray();

// Add schedules to the job if any
var jobSchedules = new List<AgentScheduleInfo>();
foreach (JobSchedule schedule in schedules)
{
jobSchedules.Add(AgentUtilities.ConvertToAgentScheduleInfo(schedule));
}
jobHistoryInfo.Schedules = jobSchedules.ToArray();

// Add alerts to the job if any
var jobAlerts = new List<AgentAlertInfo>();
foreach (Alert alert in alerts)
{
jobAlerts.Add(AgentUtilities.ConvertToAgentAlertInfo(alert));
}
jobHistoryInfo.Alerts = jobAlerts.ToArray();
jobs.Add(jobHistoryInfo);
}
return jobs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,28 @@ public class AgentJobHistoryInfo
public string RetriesAttempted { get; set; }
public string Server { get; set; }
public AgentJobStep[] Steps { get; set; }
public AgentScheduleInfo[] Schedules { get; set; }
public AgentAlertInfo[] Alerts { get; set; }
}

public class AgentJobStep
public enum CompletionResult
{
public string StepId { get; set; }
public string StepName { get; set; }
public string Message { get; set; }
public DateTime RunDate { get; set; }
public int RunStatus { get; set; }

Failed = 0,
Succeeded = 1,
Retry = 2,
Cancelled = 3,
InProgress = 4,
Unknown = 5
}


public class AgentJobStep
{
public string jobId;
public string stepId;
public string stepName;
public string message;
public string runDate;
public CompletionResult runStatus;
public AgentJobStepInfo stepDetails;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@ public class AgentScheduleInfo
public int JobCount { get; set; }
public DateTime ActiveEndDate { get; set; }
public Guid ScheduleUid { get; set; }
public string Description { get; set; }
}
}
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


0 comments on commit 6f69f7e

Please sign in to comment.