private ScheduledExecution GetScheduledExecution(DateTime executionDateTime, Job job,
                                                  IList <SystemJobSchedule> systemJobSchedules, List <sysjobhistory> workingDurations)
 {
     if (!workingDurations.Any() ||
         !systemJobSchedules.Any(
             schedule => schedule.JobId == job.JobId && schedule.NextRunDate >= executionDateTime))
     {
         return(new ScheduledExecution(job.JobId, job.Name, executionDateTime));
     }
     else
     {
         var standardDeviation = StandardDeviation(workingDurations, sysjobhistory => sysjobhistory.run_duration);
         if (!standardDeviation.HasValue)
         {
             return(new ScheduledExecution(job.JobId, job.Name,
                                           executionDateTime,
                                           SqlConverter.GetRunDurationFromInt(
                                               (int)workingDurations.Average(sysjobhistory => sysjobhistory.run_duration))));
         }
         return(new ScheduledExecution(job.JobId, job.Name,
                                       executionDateTime,
                                       SqlConverter.GetRunDurationFromInt(
                                           (int)workingDurations.Average(sysjobhistory => sysjobhistory.run_duration)),
                                       SqlConverter.GetRunDurationFromInt(
                                           (int)
                                           standardDeviation
                                           .Value)));
     }
 }
        public IList <RunTimeStatistics> GetRunTimeStatistics(IList <Job> jobs, DateTime minDateTime)
        {
            var jobRunDuration       = _jobRepository.GetJobHistory(jobs.Select(job => job.JobId));
            var jobRunTimeStatistics = new List <RunTimeStatistics>();
            var systemJobSchedules   =
                _dataMapper.Map <IList <SystemJobSchedule> >(
                    _jobRepository.GetJobSchedules()
                    .Where(sysjobschedule => sysjobschedule.job_id != null && sysjobschedule.schedule_id != null));

            foreach (var job in jobs)
            {
                var workingDurations = jobRunDuration.Where(sysjobhistory => sysjobhistory.job_id == job.JobId);
                if (!workingDurations.Any() ||
                    !systemJobSchedules.Any(
                        schedule => schedule.JobId == job.JobId && schedule.NextRunDate >= minDateTime))
                {
                    continue;
                }
                var statistics = new RunTimeStatistics
                {
                    JobId           = job.JobId,
                    JobName         = job.Name,
                    AverageDuration =
                        SqlConverter.GetRunDurationFromInt(
                            (int)workingDurations.Average(sysjobhistory => sysjobhistory.run_duration)),
                    MaxDuration =
                        SqlConverter.GetRunDurationFromInt(
                            workingDurations.Max(sysjobhistory => sysjobhistory.run_duration)),
                    NextRunDate = systemJobSchedules.Single(sysschedule => sysschedule.JobId == job.JobId).NextRunDate
                };
                var runDurationStandardDeviation = StandardDeviation(workingDurations,
                                                                     sysjobhistory => sysjobhistory.run_duration);
                if (runDurationStandardDeviation.HasValue)
                {
                    statistics.RunDurationStandardDeviation =
                        SqlConverter.GetRunDurationFromInt((int)runDurationStandardDeviation.Value);
                }

                jobRunTimeStatistics.Add(statistics);
            }

            return(jobRunTimeStatistics);
        }