public void Execute(FireTime fireTime) { // Todo add week number // don`t run job, if it too late //todo enable it in prod var now = DateTime.Now; // check by day if ((long)now.DayOfWeek != fireTime.NumberOfDay) { _logger.LogWarning($"Job {fireTime.Time} {fireTime.NumberOfDay} {fireTime.NumberOfWeek } runs late, stop job"); return; } // check by time if (now.TimeOfDay>fireTime.Time.Add(TimeSpan.FromMinutes(10)) || now.TimeOfDay<fireTime.Time.Subtract(TimeSpan.FromMinutes(10))) { _logger.LogWarning($"Job {fireTime.Time} {fireTime.NumberOfDay} {fireTime.NumberOfWeek } runs late, stop job"); return; } // todo check by week var notifications = _repository.GetAllNotificationsByThisTime(fireTime).ToList(); var pushNotifications = notifications.Where(n => n.Type == "Push").ToList(); var telegramNotifications = notifications.Where(n => n.Type == "Telegram").ToList(); // This method cause null exception // I can`t properly debug it // But anyway, it works, // So if AutomaticRetry is selected, everything goes fine _telegramNotifications.SendNotifications(telegramNotifications); }
// time of lesson in format "8:30:00" public IEnumerable <Notification> GetAllNotificationsByThisTime(FireTime fireTime) { // get all notifications // foreach student in notifications get all lessons // if lesson is next lesson, return it var notificationsSettings = _context.NotificationsSettings.ToList(); var notifications = new List <Notification>(); foreach (var notificationsSetting in notificationsSettings) { if (!notificationsSetting.IsNotificationsOn) { continue; } var lessons = this.GetLessonsForStudentSync(notificationsSetting.StudentId); notifications.AddRange(from lesson in lessons where lesson.Week == fireTime.NumberOfWeek && lesson.DayOfWeek == fireTime.NumberOfDay && // check if time format is ok lesson.TimeStart == fireTime.LessonTime.ToString() select new Notification { Lesson = lesson, StudentId = notificationsSetting.StudentId, Type = notificationsSetting.NotificationType }); } return(notifications); }
public async Task Execute(FireTime fireTime) { // don`t run job, if it too late //todo enable it in prod var now = DateTime.Now; // check by day // if ((long)now.DayOfWeek != fireTime.NumberOfDay) // { // _logger.LogWarning($"Job {fireTime.Time} {fireTime.NumberOfDay} {fireTime.NumberOfWeek } runs late, stop job"); // return; // } // // check by time // if (now.TimeOfDay>fireTime.Time.Add(TimeSpan.FromMinutes(10)) || now.TimeOfDay<fireTime.Time.Subtract(TimeSpan.FromMinutes(10))) // { // _logger.LogWarning($"Job {fireTime.Time} {fireTime.NumberOfDay} {fireTime.NumberOfWeek } runs late, stop job"); // return; // } var notifications = (await _repository.GetAllNotificationsByThisTime(fireTime)) .GroupBy(n => n.StudentId) .SelectMany(g => g.DistinctBy(n => n.Lesson.Id)) .ToList(); var pushNotifications = notifications.Where(n => n.Type == "Push").ToList(); var telegramNotifications = notifications.Where(n => n.Type == "Telegram").ToList(); _telegramNotifications.SendNotifications(telegramNotifications); }
// public async Task<IEnumerable<NotificationsSettings>> GetAllNotificationsSettings() // { // return await _context.NotificationsSettings.ToListAsync(); // } public async Task <IEnumerable <FireTime> > GetFireTimesForStudent(Guid studentId) { var notificationsSettings = (await _context.Students.FirstOrDefaultAsync(s => s.Id == studentId)).NotificationsSettings; var firetimes = new List <FireTime>(); var lessons = await GetLessonsForStudent(notificationsSettings.StudentId); foreach (var lesson in lessons) { var notificationTime = DateTime.Parse(lesson.TimeStart) .AddMinutes(-notificationsSettings.TimeBeforeLesson).TimeOfDay; var firetime = new FireTime { Time = notificationTime, NumberOfDay = lesson.DayOfWeek, NumberOfWeek = lesson.Week, LessonTime = TimeSpan.Parse(lesson.TimeStart) }; if (!firetimes.Contains(firetime)) { firetimes.Add(firetime); } } return(firetimes); }
private static string calculateCronExpresion(FireTime fireTime) { // generate expressions // {seconds} {minutes} {hours} ? {DAY}#{numbersOfDayAtMonth} var dayName = fireTime.NumberOfDay switch { 1 => "MON", 2 => "TUE", 3 => "WED", 4 => "THU", 5 => "FRI", 6 => "SAT", _ => "" }; string cronString; if (NotificationsConfig.IsFirstWeekEven) { cronString = fireTime.NumberOfWeek == 1 ? $"{fireTime.Time.Minutes} {fireTime.Time.Hours} 1-7,15-21,29-31 * {dayName}" : $"{fireTime.Time.Minutes} {fireTime.Time.Hours} 8-14,22-28 * {dayName}"; } else { cronString = fireTime.NumberOfWeek == 2 ? $"{fireTime.Time.Minutes} {fireTime.Time.Hours} 1-7,15-21,29-31 * {dayName}" : $"{fireTime.Time.Minutes} {fireTime.Time.Hours} 8-14,22-28 * {dayName}"; } return(cronString); } }
public virtual void RegisterAnimation(string name, SpriteAnimation_Base animation, string cousinAnimation, FireTime fireTime) { animation.FireTime = fireTime; if (!_Animations.ContainsKey(name) && _Animations.ContainsKey(cousinAnimation)) { _Animations[cousinAnimation].AddSibling(animation); _Animations.Add(name, animation); } }
protected virtual void StartSiblingAnimations(FireTime fireTime) { foreach (SpriteAnimation_Base animation in _SiblingAnimation) { if (animation._FireTime == fireTime) { if (animation.AnimationState == AnimationState.End) animation.Reset(); if (animation.AnimationState == AnimationState.Waiting) animation.AnimationState = AnimationState.Animate; } } }
protected virtual void StartSiblingAnimations(FireTime fireTime) { foreach (SpriteAnimation_Base animation in _SiblingAnimation) { if (animation._FireTime == fireTime) { if (animation.AnimationState == AnimationState.End) { animation.Reset(); } if (animation.AnimationState == AnimationState.Waiting) { animation.AnimationState = AnimationState.Animate; } } } }
private static string calculateCronExpresion(FireTime fireTime) { // generate expressions // {seconds} {minutes} {hours} ? {DAY}#{numbersOfDayAtMonth} var dayName = fireTime.NumberOfDay switch { 1 => "MON", 2 => "TUE", 3 => "WED", 4 => "THU", 5 => "FRI", 6 => "SAT", _ => "" }; var cronString = $"{fireTime.Time.Minutes} {fireTime.Time.Hours} * * {dayName}"; return(cronString); } }
public async Task <IEnumerable <FireTime> > GetAllNotificationsFireTimes() { // get all notifications // foreach student in notifications get all lessons // for timetable calculate notificationsTime // select all unique notification times var notificationsSettings = await _context.NotificationsSettings.ToListAsync(); var fireTimes = new List <FireTime>(); foreach (var notificationsSetting in notificationsSettings.Where(notificationsSetting => notificationsSetting.IsNotificationsOn)) { var lessons = await GetLessonsForStudent(notificationsSetting.StudentId); foreach (var lesson in lessons) { var notificationTime = DateTime.Parse(lesson.TimeStart) .AddMinutes(-notificationsSetting.TimeBeforeLesson).TimeOfDay; var firetime = new FireTime { Time = notificationTime, NumberOfDay = lesson.DayOfWeek, NumberOfWeek = lesson.Week, LessonTime = TimeSpan.Parse(lesson.TimeStart) }; // get notification time if (!fireTimes.Contains(firetime)) { fireTimes.Add(firetime); } } } return(fireTimes); }
public async Task <IEnumerable <Notification> > GetAllNotificationsByThisTime(FireTime fireTime) { // it makes only one big request to database var students = await _context.Students .Include(s => s.NotificationsSettings) .Include(s => s.Group) .ThenInclude(g => g.Subjects) .ThenInclude(s => s.Lessons) .ThenInclude(l => l.Subject) .Include(s => s.DisabledSubjects) .Include(s => s.MutedSubjects) .Where(s => s.NotificationsSettings.IsNotificationsOn).ToListAsync(); var lessons = students.SelectMany(s => s.Group.Subjects).SelectMany(s => s.Lessons).ToList(); var notifications = new List <Notification>(); foreach (var student in students) { var groupId = student.Group.Id; var studentLessons = lessons .Where(l => l.Subject.GroupId == groupId) .Where(l => l.Week == fireTime.NumberOfWeek && l.DayOfWeek == fireTime.NumberOfDay && l.TimeStart == fireTime.LessonTime.ToString()) // remove all subjects, disabled by this student .Where(l => student.DisabledSubjects.All(ds => ds.SubjectId != l.SubjectId)) .Where(l => student.MutedSubjects.All(ms => ms.SubjectId != l.SubjectId)); notifications.AddRange(studentLessons.Select(l => new Notification { Lesson = l, Type = student.NotificationsSettings.NotificationType, StudentId = student.Id })); } return(notifications); }
public async void CanGetNotifications() { // Arrange var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(databaseName: "Rozklad2") .Options; using (var context = new ApplicationDbContext(options)) { context.Groups.Add(new Group { Id = Guid.Parse("3bb26431-a7ba-4b55-970a-c8544cb920c8"), Group_Name = "Test" }); context.Students.Add(new Student { Id = Guid.Parse("fb484eee-dea4-409b-b6df-778cfd82d6a1"), GroupId = Guid.Parse("3bb26431-a7ba-4b55-970a-c8544cb920c8"), }); // context.NotificationsSettings.Add(new NotificationsSettings // { // Id = Guid.Parse("3c1ea52f-3610-4ddc-a8b7-9b87e167eeff"), // NotificationType = "Telegram", // StudentId = Guid.Parse("fb484eee-dea4-409b-b6df-778cfd82d6a1"), // IsNotificationsOn = true, // TimeBeforeLesson = 15 // }); context.Subjects.Add(new Subject { Id = Guid.Parse("e4d55a2a-cb04-4a0d-a355-d99130ff5986"), Name = "First", Teachers = "some teacher", GroupId = Guid.Parse("3bb26431-a7ba-4b55-970a-c8544cb920c8"), LabsZoom = "", LessonsZoom = "", LabsAccessCode = "", LessonsAccessCode = "" }); context.Subjects.Add(new Subject { Id = Guid.Parse("f30c42db-7bee-4d3d-beec-2bc39e797e6c"), Name = "Second", Teachers = "some teacher", GroupId = Guid.Parse("3bb26431-a7ba-4b55-970a-c8544cb920c8"), LabsZoom = "", LessonsZoom = "", LabsAccessCode = "", LessonsAccessCode = "" }); context.MutedSubjects.Add( new MutedSubject { Id = Guid.NewGuid(), StudentId = Guid.Parse("fb484eee-dea4-409b-b6df-778cfd82d6a1"), SubjectId = Guid.Parse("f30c42db-7bee-4d3d-beec-2bc39e797e6c") }); context.Lessons.Add(new Lesson { Id = Guid.Parse("3692e169-1ea6-45fa-8dc3-9738eb0a3a8b"), SubjectId = Guid.Parse("e4d55a2a-cb04-4a0d-a355-d99130ff5986"), Type = "Лек", Week = 2, TimeStart = "10:25:00", DayOfWeek = 1 }); context.Lessons.Add(new Lesson { Id = Guid.Parse("1ca4899a-01c5-43c6-bc4f-6c2cce53c8de"), SubjectId = Guid.Parse("e4d55a2a-cb04-4a0d-a355-d99130ff5986"), Type = "Лек", Week = 2, TimeStart = "12:20:00", DayOfWeek = 1 }); context.Lessons.Add(new Lesson { Id = Guid.Parse("e6aa81c4-d7c7-4d54-acf5-c372884fd6da"), SubjectId = Guid.Parse("f30c42db-7bee-4d3d-beec-2bc39e797e6c"), Type = "Лек", Week = 1, TimeStart = "12:20:00", DayOfWeek = 1 }); context.SaveChanges(); } var result1 = new List <Notification>(); // Act using (var context = new ApplicationDbContext(options)) { var repository = new RozkladRepository(context); var fireTime1 = new FireTime { Time = new TimeSpan(10, 10, 00), LessonTime = new TimeSpan(12, 20, 00), NumberOfDay = 1, NumberOfWeek = 1 }; // result1 = (repository.GetAllNotificationsByThisTime(fireTime1)).ToList(); } //Assert // Assert.True(result1.Count() == 1); // Assert.True(result1[0].StudentId == Guid.Parse("fb484eee-dea4-409b-b6df-778cfd82d6a1")); Assert.True(!result1.Select(r => r.Lesson.Subject.Id).Contains(Guid.Parse("f30c42db-7bee-4d3d-beec-2bc39e797e6c"))); }