< Summary

Information
Class: LGDXRobotCloud.API.Services.Automation.TriggerRetryService
Assembly: LGDXRobotCloud.API
File(s): /builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Services/Automation/TriggerRetryService.cs
Line coverage
72%
Covered lines: 61
Uncovered lines: 23
Coverable lines: 84
Total lines: 120
Line coverage: 72.6%
Branch coverage
50%
Covered branches: 9
Total branches: 18
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%44100%
GetTriggerRetriesAsync()100%11100%
GetTriggerRetryAsync()100%22100%
DeleteTriggerRetryAsync()100%210%
RetryTriggerRetryAsync()62.5%9880%
RetryAllFailedTriggerAsync()0%2040%

File(s)

/builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Services/Automation/TriggerRetryService.cs

#LineLine coverage
 1using LGDXRobotCloud.API.Exceptions;
 2using LGDXRobotCloud.Data.DbContexts;
 3using LGDXRobotCloud.Data.Entities;
 4using LGDXRobotCloud.Data.Models.Business.Automation;
 5using LGDXRobotCloud.Utilities.Helpers;
 6using Microsoft.EntityFrameworkCore;
 7
 8namespace LGDXRobotCloud.API.Services.Automation;
 9
 10public interface ITriggerRetryService
 11{
 12  Task<(IEnumerable<TriggerRetryListBusinessModel>, PaginationHelper)> GetTriggerRetriesAsync(int pageNumber, int pageSi
 13  Task<TriggerRetryBusinessModel> GetTriggerRetryAsync(int triggerRetryId);
 14  Task<bool> DeleteTriggerRetryAsync(int triggerRetryId);
 15  Task RetryTriggerRetryAsync(int triggerRetryId);
 16  Task RetryAllFailedTriggerAsync(int triggerId);
 17}
 18
 519public class TriggerRetryService (
 520  ITriggerService triggerService,
 521  LgdxContext context
 522) : ITriggerRetryService
 23{
 524  private readonly ITriggerService _triggerService = triggerService ?? throw new ArgumentNullException(nameof(triggerSer
 525  private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context));
 26
 27  public async Task<(IEnumerable<TriggerRetryListBusinessModel>, PaginationHelper)> GetTriggerRetriesAsync(int pageNumbe
 128  {
 129    var triggerRetries = await _context.TriggerRetries.AsNoTracking()
 130      .Include(tr => tr.Trigger)
 131      .Include(tr => tr.AutoTask)
 132      .AsSplitQuery()
 133      .Select(tr => new TriggerRetryListBusinessModel {
 134        Id = tr.Id,
 135        TriggerId = tr.TriggerId,
 136        TriggerName = tr.Trigger.Name,
 137        AutoTaskId = tr.AutoTaskId,
 138        AutoTaskName = tr.AutoTask.Name,
 139        CreatedAt = tr.CreatedAt
 140      })
 141      .OrderBy(t => t.Id)
 142      .Skip(pageSize * (pageNumber - 1))
 143      .Take(pageSize)
 144      .ToListAsync();
 145      var itemCount = triggerRetries.Count;
 146      var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize);
 147      return (triggerRetries, PaginationHelper);
 148  }
 49
 50  public async Task<TriggerRetryBusinessModel> GetTriggerRetryAsync(int triggerRetryId)
 251  {
 252    TriggerRetry triggerRetry = await _context.TriggerRetries.AsNoTracking()
 253      .Where(tr => tr.Id == triggerRetryId)
 254      .Include(tr => tr.Trigger)
 255      .Include(tr => tr.AutoTask)
 256      .FirstOrDefaultAsync() ?? throw new LgdxNotFound404Exception();
 57
 158    int SameTriggerFailed = await _context.TriggerRetries.AsNoTracking()
 159      .Where(tr => tr.TriggerId == triggerRetry.TriggerId)
 160      .CountAsync();
 61
 162    return new TriggerRetryBusinessModel {
 163      Id = triggerRetry.Id,
 164      TriggerId = triggerRetry.TriggerId,
 165      TriggerName = triggerRetry.Trigger.Name,
 166      TriggerUrl = triggerRetry.Trigger.Url,
 167      TriggerHttpMethodId = triggerRetry.Trigger.HttpMethodId,
 168      AutoTaskId = triggerRetry.AutoTaskId,
 169      AutoTaskName = triggerRetry.AutoTask.Name,
 170      Body = triggerRetry.Body,
 171      SameTriggerFailed = SameTriggerFailed,
 172      CreatedAt = triggerRetry.CreatedAt
 173    };
 174  }
 75
 76  public async Task<bool> DeleteTriggerRetryAsync(int triggerRetryId)
 077  {
 078    return await _context.TriggerRetries.Where(tr => tr.Id == triggerRetryId)
 079      .ExecuteDeleteAsync() >= 1;
 080  }
 81
 82  public async Task RetryTriggerRetryAsync(int triggerRetryId)
 283  {
 284    var triggerRetry = await _context.TriggerRetries.AsNoTracking()
 285      .Where(tr => tr.Id == triggerRetryId)
 286      .FirstOrDefaultAsync() ?? throw new LgdxNotFound404Exception();
 87
 188    var autoTask = await _context.AutoTasks.AsNoTracking()
 189      .Where(at => at.Id == triggerRetry.AutoTaskId)
 190      .FirstOrDefaultAsync() ?? throw new LgdxValidation400Expection(nameof(triggerRetry.AutoTaskId), "AutoTask ID is in
 91
 192    var trigger = await _context.Triggers.AsNoTracking()
 193      .Where(t => t.Id == triggerRetry.TriggerId)
 194      .FirstOrDefaultAsync() ?? throw new LgdxValidation400Expection(nameof(triggerRetry.TriggerId), "Trigger ID is inva
 95
 196    if (await _triggerService.RetryTriggerAsync(autoTask, trigger, triggerRetry.Body))
 097    {
 098      await DeleteTriggerRetryAsync(triggerRetryId);
 099    }
 1100  }
 101
 102  public async Task RetryAllFailedTriggerAsync(int triggerId)
 0103  {
 0104    var triggerRetries = await _context.TriggerRetries
 0105      .Where(tr => tr.TriggerId == triggerId)
 0106      .Include(tr => tr.Trigger)
 0107      .Include(tr => tr.AutoTask)
 0108      .AsSplitQuery()
 0109      .ToListAsync();
 110
 0111    foreach (var tr in triggerRetries)
 0112    {
 0113      if (await _triggerService.RetryTriggerAsync(tr.AutoTask, tr.Trigger, tr.Body))
 0114      {
 0115        _context.TriggerRetries.Remove(tr);
 0116      }
 0117    }
 0118    _context.SaveChanges();
 0119  }
 120}