Added the ability to purge/remove issues

pull/2542/head
TidusJar 6 years ago
parent 6f02c38a20
commit dffd27d804

@ -191,6 +191,7 @@ namespace Ombi.DependencyInjection
services.AddTransient<ILidarrAlbumSync, LidarrAlbumSync>(); services.AddTransient<ILidarrAlbumSync, LidarrAlbumSync>();
services.AddTransient<ILidarrArtistSync, LidarrArtistSync>(); services.AddTransient<ILidarrArtistSync, LidarrArtistSync>();
services.AddTransient<ILidarrAvailabilityChecker, LidarrAvailabilityChecker>(); services.AddTransient<ILidarrAvailabilityChecker, LidarrAvailabilityChecker>();
services.AddTransient<IIssuesPurge, IssuesPurge>();
} }
} }
} }

@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Moq;
using NUnit.Framework;
using Ombi.Core.Settings;
using Ombi.Schedule.Jobs.Ombi;
using Ombi.Settings.Settings.Models;
using Ombi.Store.Entities.Requests;
using Ombi.Store.Repository;
using System.Threading.Tasks;
namespace Ombi.Schedule.Tests
{
[TestFixture]
public class IssuesPurgeTests
{
[SetUp]
public void Setup()
{
Repo = new Mock<IRepository<Issues>>();
Settings = new Mock<ISettingsService<IssueSettings>>();
Settings.Setup(x => x.GetSettingsAsync()).ReturnsAsync(new IssueSettings());
Job = new IssuesPurge(Repo.Object, Settings.Object);
}
public Mock<IRepository<Issues>> Repo { get; set; }
public Mock<ISettingsService<IssueSettings>> Settings { get; set; }
public IssuesPurge Job { get; set; }
[Test]
public async Task DoesNotRun_WhenDisabled()
{
await Job.Start();
Repo.Verify(x => x.GetAll(),Times.Never);
}
[Test]
public async Task Deletes_Correct_Issue()
{
var issues = new List<Issues>()
{
new Issues
{
Status = IssueStatus.Resolved,
ResovledDate = DateTime.Now.AddDays(-5).AddHours(-1)
}
};
Settings.Setup(x => x.GetSettingsAsync()).ReturnsAsync(new IssueSettings { DeleteIssues = true, DaysAfterResolvedToDelete = 5 });
Repo.Setup(x => x.GetAll()).Returns(new EnumerableQuery<Issues>(issues));
await Job.Start();
Assert.That(issues.First().Status, Is.EqualTo(IssueStatus.Deleted));
Repo.Verify(x => x.SaveChangesAsync(), Times.Once);
}
[Test]
public async Task DoesNot_Delete_AnyIssues()
{
var issues = new List<Issues>()
{
new Issues
{
Status = IssueStatus.Resolved,
ResovledDate = DateTime.Now.AddDays(-2)
},
new Issues
{
Status = IssueStatus.Resolved,
ResovledDate = DateTime.Now.AddDays(-6)
}
};
Settings.Setup(x => x.GetSettingsAsync()).ReturnsAsync(new IssueSettings { DeleteIssues = true, DaysAfterResolvedToDelete = 5 });
Repo.Setup(x => x.GetAll()).Returns(new EnumerableQuery<Issues>(issues));
await Job.Start();
Assert.That(issues[0].Status, Is.Not.EqualTo(IssueStatus.Deleted));
Assert.That(issues[1].Status, Is.EqualTo(IssueStatus.Deleted));
Repo.Verify(x => x.SaveChangesAsync(), Times.Once);
}
}
}

@ -0,0 +1,9 @@
using System.Threading.Tasks;
namespace Ombi.Schedule.Jobs.Ombi
{
public interface IIssuesPurge : IBaseJob
{
Task Start();
}
}

@ -0,0 +1,63 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Ombi.Core.Settings;
using Ombi.Settings.Settings.Models;
using Ombi.Store.Entities.Requests;
using Ombi.Store.Repository;
namespace Ombi.Schedule.Jobs.Ombi
{
public class IssuesPurge : IIssuesPurge
{
public IssuesPurge(IRepository<Issues> issuesRepo, ISettingsService<IssueSettings> issueSettings)
{
_issuesRepository = issuesRepo;
_settings = issueSettings;
_settings.ClearCache();
}
private readonly IRepository<Issues> _issuesRepository;
private readonly ISettingsService<IssueSettings> _settings;
public async Task Start()
{
var settings = await _settings.GetSettingsAsync();
if (!settings.DeleteIssues)
{
return;
}
var now = DateTime.Now.AddDays(-settings.DaysAfterResolvedToDelete).Date;
var resolved = _issuesRepository.GetAll().Where(x => x.Status == IssueStatus.Resolved);
var toDelete = resolved.Where(x => x.ResovledDate.HasValue && x.ResovledDate.Value.Date <= now);
foreach (var d in toDelete)
{
d.Status = IssueStatus.Deleted;
}
await _issuesRepository.SaveChangesAsync();
}
private bool _disposed;
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
_issuesRepository?.Dispose();
_settings?.Dispose();
}
_disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}

@ -4,5 +4,8 @@
{ {
public bool Enabled { get; set; } public bool Enabled { get; set; }
public bool EnableInProgress { get; set; } public bool EnableInProgress { get; set; }
public bool DeleteIssues { get; set; }
public int DaysAfterResolvedToDelete { get; set; }
} }
} }

@ -14,5 +14,6 @@
public string RefreshMetadata { get; set; } public string RefreshMetadata { get; set; }
public string Newsletter { get; set; } public string Newsletter { get; set; }
public string LidarrArtistSync { get; set; } public string LidarrArtistSync { get; set; }
public string IssuesPurge { get; set; }
} }
} }

@ -57,6 +57,11 @@ namespace Ombi.Settings.Settings.Models
return Get(s.LidarrArtistSync, Cron.Hourly(40)); return Get(s.LidarrArtistSync, Cron.Hourly(40));
} }
public static string IssuePurge(JobSettings s)
{
return Get(s.IssuesPurge, Cron.Daily());
}
private static string Get(string settings, string defaultCron) private static string Get(string settings, string defaultCron)
{ {
return settings.HasValue() ? settings : defaultCron; return settings.HasValue() ? settings : defaultCron;

@ -29,5 +29,6 @@ namespace Ombi.Store.Entities.Requests
Pending = 0, Pending = 0,
InProgress = 1, InProgress = 1,
Resolved = 2, Resolved = 2,
Deleted = 3,
} }
} }

@ -144,11 +144,14 @@ export interface IJobSettings {
newsletter: string; newsletter: string;
plexRecentlyAddedSync: string; plexRecentlyAddedSync: string;
lidarrArtistSync: string; lidarrArtistSync: string;
issuesPurge: string;
} }
export interface IIssueSettings extends ISettings { export interface IIssueSettings extends ISettings {
enabled: boolean; enabled: boolean;
enableInProgress: boolean; enableInProgress: boolean;
deleteIssues: boolean;
daysAfterResolvedToDelete: number;
} }
export interface IAuthenticationSettings extends ISettings { export interface IAuthenticationSettings extends ISettings {

@ -18,6 +18,20 @@
</div> </div>
</div> </div>
<div class="form-group">
<div class="checkbox">
<input type="checkbox" id="deleteIssues" formControlName="deleteIssues" ng-checked="form.deleteIssues">
<label for="deleteIssues">Delete issues after they have been resolved</label>
</div>
</div>
<div class="form-group" *ngIf="form.controls.deleteIssues.value">
<div class="checkbox">
<input type="checkbox" id="daysAfterResolvedToDelete" formControlName="daysAfterResolvedToDelete" ng-checked="form.daysAfterResolvedToDelete">
<label for="daysAfterResolvedToDelete">Leave the resolved issue for X amount of days before deletion</label>
</div>
</div>
<div class="form-group"> <div class="form-group">
<div> <div>
<button type="submit" [disabled]="form.invalid" class="btn btn-primary-outline ">Submit</button> <button type="submit" [disabled]="form.invalid" class="btn btn-primary-outline ">Submit</button>

@ -21,8 +21,10 @@ export class IssuesComponent implements OnInit {
public ngOnInit() { public ngOnInit() {
this.settingsService.getIssueSettings().subscribe(x => { this.settingsService.getIssueSettings().subscribe(x => {
this.form = this.fb.group({ this.form = this.fb.group({
enabled: [x.enabled], enabled: [x.enabled],
enableInProgress: [x.enableInProgress], enableInProgress: [x.enableInProgress],
deleteIssues: [x.deleteIssues],
daysAfterResolvedToDelete: [x.daysAfterResolvedToDelete],
}); });
}); });
this.getCategories(); this.getCategories();

@ -85,12 +85,19 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="userImporter" class="control-label">Newsletter</label> <label for="userImporter" class="control-label">Newsletter</label>
<input type="text" class="form-control form-control-custom" [ngClass]="{'form-error': form.get('newsletter').hasError('required')}" id="newsletter" name="newsletter" formControlName="newsletter"> <input type="text" class="form-control form-control-custom" [ngClass]="{'form-error': form.get('newsletter').hasError('required')}" id="newsletter" name="newsletter" formControlName="newsletter">
<small *ngIf="form.get('newsletter').hasError('required')" class="error-text">The Newsletter is required</small> <small *ngIf="form.get('newsletter').hasError('required')" class="error-text">The Newsletter is required</small>
<button type="button" class="btn btn-sm btn-primary-outline" (click)="testCron(form.get('newsletter')?.value)">Test</button> <button type="button" class="btn btn-sm btn-primary-outline" (click)="testCron(form.get('newsletter')?.value)">Test</button>
</div> </div>
<div class="form-group">
<label for="userImporter" class="control-label">Issue Purge/Delete</label>
<input type="text" class="form-control form-control-custom" [ngClass]="{'form-error': form.get('issuesPurge').hasError('required')}" id="issuesPurge" name="issuesPurge" formControlName="issuesPurge">
<small *ngIf="form.get('issuesPurge').hasError('required')" class="error-text">The Issues Purge is required</small>
<button type="button" class="btn btn-sm btn-primary-outline" (click)="testCron(form.get('issuesPurge')?.value)">Test</button>
</div>
</div> </div>
<div class="form-group"> <div class="form-group">
<div> <div>

@ -35,6 +35,7 @@ export class JobsComponent implements OnInit {
newsletter: [x.newsletter, Validators.required], newsletter: [x.newsletter, Validators.required],
plexRecentlyAddedSync: [x.plexRecentlyAddedSync, Validators.required], plexRecentlyAddedSync: [x.plexRecentlyAddedSync, Validators.required],
lidarrArtistSync: [x.lidarrArtistSync, Validators.required], lidarrArtistSync: [x.lidarrArtistSync, Validators.required],
issuesPurge: [x.issuesPurge, Validators.required],
}); });
}); });
} }

@ -519,7 +519,8 @@ namespace Ombi.Controllers
j.PlexRecentlyAddedSync = j.PlexRecentlyAddedSync.HasValue() ? j.PlexRecentlyAddedSync : JobSettingsHelper.PlexRecentlyAdded(j); j.PlexRecentlyAddedSync = j.PlexRecentlyAddedSync.HasValue() ? j.PlexRecentlyAddedSync : JobSettingsHelper.PlexRecentlyAdded(j);
j.Newsletter = j.Newsletter.HasValue() ? j.Newsletter : JobSettingsHelper.Newsletter(j); j.Newsletter = j.Newsletter.HasValue() ? j.Newsletter : JobSettingsHelper.Newsletter(j);
j.LidarrArtistSync = j.LidarrArtistSync.HasValue() ? j.LidarrArtistSync : JobSettingsHelper.LidarrArtistSync(j); j.LidarrArtistSync = j.LidarrArtistSync.HasValue() ? j.LidarrArtistSync : JobSettingsHelper.LidarrArtistSync(j);
j.IssuesPurge = j.IssuesPurge.HasValue() ? j.IssuesPurge : JobSettingsHelper.IssuePurge(j);
return j; return j;
} }

Loading…
Cancel
Save