Skip to content

Commit

Permalink
Doing code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
renemadsen committed Nov 26, 2023
1 parent 2dce063 commit 84935a2
Showing 1 changed file with 12 additions and 164 deletions.
176 changes: 12 additions & 164 deletions ServiceItemsPlanningPlugin/Scheduler/Jobs/SearchListJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,141 +129,22 @@ private async Task ExecuteDeploy()
Log.LogEvent("SearchListJob.Task: SearchListJob.Execute got called");
var now = DateTime.UtcNow;
now = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
// var lastDayOfMonth = new DateTime(now.Year, now.Month, 1).AddMonths(1).AddDays(-1).Day;
// var firstDayOfMonth = new DateTime(now.Year, now.Month, 1).Day;


// var baseQuery = _dbContext.Plannings
// .Where(x =>
// (x.RepeatUntil == null || DateTime.UtcNow <= x.RepeatUntil)
// &&
// (DateTime.UtcNow >= x.StartDate)
// &&
// x.WorkflowState != Constants.WorkflowStates.Removed);
var baseQuery = _dbContext.Plannings
.Where(x => x.WorkflowState != Constants.WorkflowStates.Removed);

// var dailyListsQuery = baseQuery
// .Where(x => x.RepeatType == RepeatType.Day
// && (x.LastExecutedTime == null ||
// now.AddDays(-x.RepeatEvery) >= x.LastExecutedTime)).
// Where(x => x.RepeatEvery > 0);
//
// var weeklyListsQuery = now.DayOfWeek == 0 ? baseQuery
// .Where(x => x.RepeatType == RepeatType.Week
// && ((x.LastExecutedTime == null && x.DayOfWeek - 7 == 0) ||
// (now.AddDays(-x.RepeatEvery * 7) >= x.LastExecutedTime &&
// x.DayOfWeek - 7 == 0))) : baseQuery
// .Where(x => x.RepeatType == RepeatType.Week
// && ((x.LastExecutedTime == null && x.DayOfWeek == now.DayOfWeek) ||
// (now.AddDays(-x.RepeatEvery * 7) >= x.LastExecutedTime &&
// x.DayOfWeek == now.DayOfWeek)));

// if (now is { Month: < 2, Year: 2023 })
// {
// // find all plannings which are repeated every day where the repeatEvery is bigger than 1
// var dailyListsQuery = baseQuery
// .Where(x => x.RepeatType == RepeatType.Day)
// .Where(x => x.RepeatEvery > 1);
//
// // loop through them all and set nextExecutionTime to a multiple of repeatEvery days calculated from 1. january of current year
// foreach (var planning in dailyListsQuery)
// {
// var diff = (now - new DateTime(now.Year, 1, 1)).TotalDays;
// var multiplier = (int)(diff / planning.RepeatEvery);
// var nextExecutionTime = new DateTime(now.Year, 1, 1).AddDays(multiplier * planning.RepeatEvery);
//
// planning.NextExecutionTime = nextExecutionTime;
// await planning.Update(_dbContext).ConfigureAwait(false);
// }
//
//
// // find all plannings which are repeated every week
// var weeklyListsQuery = baseQuery
// .Where(x => x.RepeatType == RepeatType.Week);
// // loop through them all and set nextExecutionTime to a multiple of repeatEvery week calculated from 1. january of current year
// foreach (var planning in weeklyListsQuery)
// {
// var diff = (now - new DateTime(now.Year, 1, 1)).TotalDays;
// var multiplier = (int)(diff / (planning.RepeatEvery * 7));
// var dayOfWeek = (int)now.DayOfWeek;
// if (dayOfWeek == 0)
// {
// dayOfWeek = 7;
// }
//
// var nextExecutionTime =
// new DateTime(now.Year, 1, dayOfWeek).AddDays(multiplier * planning.RepeatEvery * 7);
//
// planning.NextExecutionTime = nextExecutionTime;
// await planning.Update(_dbContext).ConfigureAwait(false);
// }
//
// // find all plannings which are repeated every month
// var monthlyListsQuery = baseQuery
// .Where(x => x.RepeatType == RepeatType.Month);
// // loop through them all and set nextExecutionTime to a multiple of repeatEvery month calculated from 1. january of current year
// foreach (var planning in monthlyListsQuery)
// {
// if (planning.DayOfMonth == 0)
// {
// planning.DayOfMonth = 1;
// }
//
// if (planning.RepeatEvery == 1)
// {
// planning.NextExecutionTime =
// new DateTime(now.Year, now.Month + 1, (int)planning.DayOfMonth!, 0, 0, 0);
// }
// else
// {
// if (planning.RepeatEvery == 12)
// {
// if (planning.LastExecutedTime is { Month: 1 } &&
// planning.LastExecutedTime.Value.Year == now.Year)
// {
// planning.NextExecutionTime =
// new DateTime(now.Year + 1, 1, (int)planning.DayOfMonth!, 0,
// 0, 0);
// }
// else
// {
// planning.NextExecutionTime =
// new DateTime(now.Year, 1, (int)planning.DayOfMonth!, 0, 0,
// 0);
// }
// }
// else
// {
// planning.NextExecutionTime =
// new DateTime(now.Year, planning.RepeatEvery + 1, (int)planning.DayOfMonth!, 0, 0, 0);
// }
// }
//
// await planning.Update(_dbContext).ConfigureAwait(false);
// }
// }

// repeat == 2, lastExecutedTime = 1.1.2020, now = 1.3.2020
// repeat == 3, lastExecutedTime = 1.1.2020, now = 1.4.2020
// repeat == 4, lastExecutedTime = 1.1.2020, now = 1.5.2020
// repeat == 5, lastExecutedTime = 1.1.2020, now = 1.6.2020
// repeat == 6, lastExecutedTime = 1.1.2020, now = 1.7.2020
// repeat == 7, lastExecutedTime = 1.1.2020, now = 1.8.2020
// repeat == 8, lastExecutedTime = 1.1.2020, now = 1.9.2020
// repeat == 9, lastExecutedTime = 1.1.2020, now = 1.10.2020
// repeat == 10, lastExecutedTime = 1.1.2020, now = 1.11.2020
// repeat == 11, lastExecutedTime = 1.1.2020, now = 1.12.2020
// repeat == 12, lastExecutedTime = 1.1.2020, now = 1.1.2021


var planningForCorrectingNextExecutionTime = await baseQuery
.Where(x => x.NextExecutionTime != null)
.Where(x => x.RepeatType != RepeatType.Day)
.Where(x => x.RepeatEvery != 0)
.Where(x => x.StartDate > now)
.Where(x => x.Enabled)
.ToListAsync();

// var monthlyListsQuery = baseQuery
// .Where(x => x.RepeatType == RepeatType.Month
// && (x.LastExecutedTime == null ||
// ((x.DayOfMonth <= now.Day || now.Day == firstDayOfMonth) &&
// ((now.Month - x.LastExecutedTime.Value.Month) +
// 12 * (now.Year - x.LastExecutedTime.Value.Year) >= x.RepeatEvery))));
foreach (var planning in planningForCorrectingNextExecutionTime)
{
planning.NextExecutionTime = null;
await planning.Update(_dbContext);
}

var planningsForExecution = await baseQuery
.Where(x => x.NextExecutionTime <= now || x.NextExecutionTime == null)
Expand All @@ -272,43 +153,10 @@ private async Task ExecuteDeploy()
.Where(x => x.Enabled)
.ToListAsync();

// Console.WriteLine($"Daily lists query: {dailyListsQuery.ToSql()}");
// Console.WriteLine($"Weekly lists query: {weeklyListsQuery.ToSql()}");
// Console.WriteLine($"Monthly lists query: {monthlyListsQuery.ToSql()}");

// var pushReady = baseQuery.Where(x => !x.DoneInPeriod).Where(x => x.NextExecutionTime > now)
// .Where(x => x.RepeatType != RepeatType.Day).Where(x => !x.PushMessageSent);

//var dailyPlannings = await dailyListsQuery.ToListAsync();
//var weeklyPlannings = await weeklyListsQuery.ToListAsync();
//var monthlyPlannings = await monthlyListsQuery.ToListAsync();
// var pushReadyPlannings = await pushReady.ToListAsync();
//
// foreach (Planning planning in pushReadyPlannings)
// {
// if ((((DateTime) planning.NextExecutionTime).Date - now.Date).Days ==
// planning.DaysBeforeRedeploymentPushMessage)
// {
// await _bus.SendLocal(new PushMessage(planning.Id));
// }
// }

// Find plannings where site not executed
// var newPlanningSites = await baseQuery
// .Where(x => x.LastExecutedTime != null)
// .Where(x => x.PlanningSites.Any(y => y.LastExecutedTime == null))
// .ToListAsync();

Log.LogEvent($"SearchListJob.Task: Found {planningsForExecution.Count} plannings");
// Log.LogEvent($"SearchListJob.Task: Found {dailyPlannings.Count} daily plannings");
// Log.LogEvent($"SearchListJob.Task: Found {weeklyPlannings.Count} weekly plannings");
// Log.LogEvent($"SearchListJob.Task: Found {monthlyPlannings.Count} monthly plannings");

var scheduledItemPlannings = new List<Planning>();
scheduledItemPlannings.AddRange(planningsForExecution);
// scheduledItemPlannings.AddRange(dailyPlannings);
// scheduledItemPlannings.AddRange(weeklyPlannings);
// scheduledItemPlannings.AddRange(monthlyPlannings);

await using var sdkDbContext = _sdkCore.DbContextHelper.GetDbContext();

Expand Down

0 comments on commit 84935a2

Please sign in to comment.