2017-10-08 20:07:54 +00:00
|
|
|
using System.Collections.Generic;
|
2020-08-18 20:11:44 +00:00
|
|
|
using System.Data;
|
2016-09-22 17:57:12 +00:00
|
|
|
using System.Linq;
|
2020-08-18 20:11:44 +00:00
|
|
|
using Dapper;
|
2022-01-26 00:08:27 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
2024-04-07 23:22:21 +00:00
|
|
|
using NzbDrone.Core.AutoTagging;
|
|
|
|
using NzbDrone.Core.AutoTagging.Specifications;
|
2016-09-22 17:57:12 +00:00
|
|
|
using NzbDrone.Core.Datastore;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Housekeeping.Housekeepers
|
|
|
|
{
|
|
|
|
public class CleanupUnusedTags : IHousekeepingTask
|
|
|
|
{
|
|
|
|
private readonly IMainDatabase _database;
|
2024-04-07 23:22:21 +00:00
|
|
|
private readonly IAutoTaggingRepository _autoTaggingRepository;
|
2016-09-22 17:57:12 +00:00
|
|
|
|
2024-04-07 23:22:21 +00:00
|
|
|
public CleanupUnusedTags(IMainDatabase database, IAutoTaggingRepository autoTaggingRepository)
|
2016-09-22 17:57:12 +00:00
|
|
|
{
|
|
|
|
_database = database;
|
2024-04-07 23:22:21 +00:00
|
|
|
_autoTaggingRepository = autoTaggingRepository;
|
2016-09-22 17:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Clean()
|
|
|
|
{
|
2023-06-21 02:47:20 +00:00
|
|
|
using var mapper = _database.OpenConnection();
|
2024-04-07 23:22:21 +00:00
|
|
|
var usedTags = new[]
|
|
|
|
{
|
|
|
|
"Artists", "Notifications", "DelayProfiles", "ReleaseProfiles", "ImportLists", "Indexers",
|
|
|
|
"AutoTagging", "DownloadClients"
|
|
|
|
}
|
2016-09-22 17:57:12 +00:00
|
|
|
.SelectMany(v => GetUsedTags(v, mapper))
|
2024-04-07 23:22:21 +00:00
|
|
|
.Concat(GetAutoTaggingTagSpecificationTags(mapper))
|
2023-06-21 02:47:20 +00:00
|
|
|
.Distinct()
|
|
|
|
.ToArray();
|
2016-09-22 17:57:12 +00:00
|
|
|
|
2023-06-21 02:47:20 +00:00
|
|
|
if (usedTags.Any())
|
|
|
|
{
|
|
|
|
var usedTagsList = usedTags.Select(d => d.ToString()).Join(",");
|
2016-09-22 17:57:12 +00:00
|
|
|
|
2023-06-21 02:47:20 +00:00
|
|
|
if (_database.DatabaseType == DatabaseType.PostgreSQL)
|
|
|
|
{
|
|
|
|
mapper.Execute($"DELETE FROM \"Tags\" WHERE NOT \"Id\" = ANY (\'{{{usedTagsList}}}\'::int[])");
|
2022-01-26 00:08:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-06-21 02:47:20 +00:00
|
|
|
mapper.Execute($"DELETE FROM \"Tags\" WHERE NOT \"Id\" IN ({usedTagsList})");
|
2022-01-26 00:08:27 +00:00
|
|
|
}
|
2020-08-18 20:11:44 +00:00
|
|
|
}
|
2023-06-21 02:47:20 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
mapper.Execute("DELETE FROM \"Tags\"");
|
|
|
|
}
|
2016-09-22 17:57:12 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 20:11:44 +00:00
|
|
|
private int[] GetUsedTags(string table, IDbConnection mapper)
|
2016-09-22 17:57:12 +00:00
|
|
|
{
|
2024-04-07 23:22:21 +00:00
|
|
|
return mapper
|
|
|
|
.Query<List<int>>(
|
|
|
|
$"SELECT DISTINCT \"Tags\" FROM \"{table}\" WHERE NOT \"Tags\" = '[]' AND NOT \"Tags\" IS NULL")
|
2020-08-18 20:11:44 +00:00
|
|
|
.SelectMany(x => x)
|
|
|
|
.Distinct()
|
|
|
|
.ToArray();
|
2016-09-22 17:57:12 +00:00
|
|
|
}
|
2024-04-07 23:22:21 +00:00
|
|
|
|
|
|
|
private List<int> GetAutoTaggingTagSpecificationTags(IDbConnection mapper)
|
|
|
|
{
|
|
|
|
var tags = new List<int>();
|
|
|
|
var autoTags = _autoTaggingRepository.All();
|
|
|
|
|
|
|
|
foreach (var autoTag in autoTags)
|
|
|
|
{
|
|
|
|
foreach (var specification in autoTag.Specifications)
|
|
|
|
{
|
|
|
|
if (specification is TagSpecification tagSpec)
|
|
|
|
{
|
|
|
|
tags.Add(tagSpec.Value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tags;
|
|
|
|
}
|
2016-09-22 17:57:12 +00:00
|
|
|
}
|
|
|
|
}
|