New: Auto tag artists based on tags present/absent on artists

(cherry picked from commit f4c19a384bd9bb4e35c9fa0ca5d9a448c04e409e)

Closes #4742
This commit is contained in:
Mark McDowall 2024-04-07 16:22:21 -07:00 committed by Bogdan
parent 8c09c0cb5c
commit b14e2bb618
11 changed files with 188 additions and 7 deletions

View File

@ -0,0 +1,53 @@
import React, { useCallback } from 'react';
import TagInputConnector from './TagInputConnector';
interface ArtistTagInputProps {
name: string;
value: number | number[];
onChange: ({
name,
value,
}: {
name: string;
value: number | number[];
}) => void;
}
export default function ArtistTagInput(props: ArtistTagInputProps) {
const { value, onChange, ...otherProps } = props;
const isArray = Array.isArray(value);
const handleChange = useCallback(
({ name, value: newValue }: { name: string; value: number[] }) => {
if (isArray) {
onChange({ name, value: newValue });
} else {
onChange({
name,
value: newValue.length ? newValue[newValue.length - 1] : 0,
});
}
},
[isArray, onChange]
);
let finalValue: number[] = [];
if (isArray) {
finalValue = value;
} else if (value === 0) {
finalValue = [];
} else {
finalValue = [value];
}
return (
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore 2786 'TagInputConnector' isn't typed yet
<TagInputConnector
{...otherProps}
value={finalValue}
onChange={handleChange}
/>
);
}

View File

@ -4,6 +4,7 @@ import Link from 'Components/Link/Link';
import { inputTypes, kinds } from 'Helpers/Props'; import { inputTypes, kinds } from 'Helpers/Props';
import translate from 'Utilities/String/translate'; import translate from 'Utilities/String/translate';
import AlbumReleaseSelectInputConnector from './AlbumReleaseSelectInputConnector'; import AlbumReleaseSelectInputConnector from './AlbumReleaseSelectInputConnector';
import ArtistTagInput from './ArtistTagInput';
import AutoCompleteInput from './AutoCompleteInput'; import AutoCompleteInput from './AutoCompleteInput';
import CaptchaInputConnector from './CaptchaInputConnector'; import CaptchaInputConnector from './CaptchaInputConnector';
import CheckInput from './CheckInput'; import CheckInput from './CheckInput';
@ -99,6 +100,9 @@ function getComponent(type) {
case inputTypes.DYNAMIC_SELECT: case inputTypes.DYNAMIC_SELECT:
return EnhancedSelectInputConnector; return EnhancedSelectInputConnector;
case inputTypes.ARTIST_TAG:
return ArtistTagInput;
case inputTypes.SERIES_TYPE_SELECT: case inputTypes.SERIES_TYPE_SELECT:
return SeriesTypeSelectInput; return SeriesTypeSelectInput;

View File

@ -29,6 +29,8 @@ function getType({ type, selectOptionsProviderAction }) {
return inputTypes.DYNAMIC_SELECT; return inputTypes.DYNAMIC_SELECT;
} }
return inputTypes.SELECT; return inputTypes.SELECT;
case 'artistTag':
return inputTypes.ARTIST_TAG;
case 'tag': case 'tag':
return inputTypes.TEXT_TAG; return inputTypes.TEXT_TAG;
case 'tagSelect': case 'tagSelect':

View File

@ -20,6 +20,7 @@ export const DOWNLOAD_CLIENT_SELECT = 'downloadClientSelect';
export const ROOT_FOLDER_SELECT = 'rootFolderSelect'; export const ROOT_FOLDER_SELECT = 'rootFolderSelect';
export const SELECT = 'select'; export const SELECT = 'select';
export const SERIES_TYPE_SELECT = 'artistTypeSelect'; export const SERIES_TYPE_SELECT = 'artistTypeSelect';
export const ARTIST_TAG = 'artistTag';
export const DYNAMIC_SELECT = 'dynamicSelect'; export const DYNAMIC_SELECT = 'dynamicSelect';
export const TAG = 'tag'; export const TAG = 'tag';
export const TAG_SELECT = 'tagSelect'; export const TAG_SELECT = 'tagSelect';
@ -49,6 +50,7 @@ export const all = [
DOWNLOAD_CLIENT_SELECT, DOWNLOAD_CLIENT_SELECT,
ROOT_FOLDER_SELECT, ROOT_FOLDER_SELECT,
SELECT, SELECT,
ARTIST_TAG,
DYNAMIC_SELECT, DYNAMIC_SELECT,
SERIES_TYPE_SELECT, SERIES_TYPE_SELECT,
TAG, TAG,

View File

@ -12,7 +12,7 @@ export default function TagInUse(props) {
return null; return null;
} }
if (count > 1 && labelPlural ) { if (count > 1 && labelPlural) {
return ( return (
<div> <div>
{count} {labelPlural.toLowerCase()} {count} {labelPlural.toLowerCase()}

View File

@ -1,6 +1,9 @@
using System.Collections.Generic;
using FizzWare.NBuilder; using FizzWare.NBuilder;
using FluentAssertions; using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Core.AutoTagging;
using NzbDrone.Core.AutoTagging.Specifications;
using NzbDrone.Core.Housekeeping.Housekeepers; using NzbDrone.Core.Housekeeping.Housekeepers;
using NzbDrone.Core.Profiles.Releases; using NzbDrone.Core.Profiles.Releases;
using NzbDrone.Core.Tags; using NzbDrone.Core.Tags;
@ -46,5 +49,35 @@ namespace NzbDrone.Core.Test.Housekeeping.Housekeepers
Subject.Clean(); Subject.Clean();
AllStoredModels.Should().HaveCount(1); AllStoredModels.Should().HaveCount(1);
} }
[Test]
public void should_not_delete_used_auto_tagging_tag_specification_tags()
{
var tags = Builder<Tag>
.CreateListOfSize(2)
.All()
.With(x => x.Id = 0)
.BuildList();
Db.InsertMany(tags);
var autoTags = Builder<AutoTag>.CreateListOfSize(1)
.All()
.With(x => x.Id = 0)
.With(x => x.Specifications = new List<IAutoTaggingSpecification>
{
new TagSpecification
{
Name = "Test",
Value = tags[0].Id
}
})
.BuildList();
Mocker.GetMock<IAutoTaggingRepository>().Setup(s => s.All())
.Returns(autoTags);
Subject.Clean();
AllStoredModels.Should().HaveCount(1);
}
} }
} }

View File

@ -86,7 +86,8 @@ namespace NzbDrone.Core.Annotations
TagSelect, TagSelect,
RootFolder, RootFolder,
QualityProfile, QualityProfile,
MetadataProfile MetadataProfile,
ArtistTag
} }
public enum HiddenType public enum HiddenType

View File

@ -0,0 +1,36 @@
using FluentValidation;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.Music;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.AutoTagging.Specifications
{
public class TagSpecificationValidator : AbstractValidator<TagSpecification>
{
public TagSpecificationValidator()
{
RuleFor(c => c.Value).GreaterThan(0);
}
}
public class TagSpecification : AutoTaggingSpecificationBase
{
private static readonly TagSpecificationValidator Validator = new ();
public override int Order => 1;
public override string ImplementationName => "Tag";
[FieldDefinition(1, Label = "AutoTaggingSpecificationTag", Type = FieldType.ArtistTag)]
public int Value { get; set; }
protected override bool IsSatisfiedByWithoutNegate(Artist artist)
{
return artist.Tags.Contains(Value);
}
public override NzbDroneValidationResult Validate()
{
return new NzbDroneValidationResult(Validator.Validate(this));
}
}
}

View File

@ -3,6 +3,8 @@ using System.Data;
using System.Linq; using System.Linq;
using Dapper; using Dapper;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.AutoTagging;
using NzbDrone.Core.AutoTagging.Specifications;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
namespace NzbDrone.Core.Housekeeping.Housekeepers namespace NzbDrone.Core.Housekeeping.Housekeepers
@ -10,17 +12,24 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
public class CleanupUnusedTags : IHousekeepingTask public class CleanupUnusedTags : IHousekeepingTask
{ {
private readonly IMainDatabase _database; private readonly IMainDatabase _database;
private readonly IAutoTaggingRepository _autoTaggingRepository;
public CleanupUnusedTags(IMainDatabase database) public CleanupUnusedTags(IMainDatabase database, IAutoTaggingRepository autoTaggingRepository)
{ {
_database = database; _database = database;
_autoTaggingRepository = autoTaggingRepository;
} }
public void Clean() public void Clean()
{ {
using var mapper = _database.OpenConnection(); using var mapper = _database.OpenConnection();
var usedTags = new[] { "Artists", "Notifications", "DelayProfiles", "ReleaseProfiles", "ImportLists", "Indexers", "AutoTagging", "DownloadClients" } var usedTags = new[]
{
"Artists", "Notifications", "DelayProfiles", "ReleaseProfiles", "ImportLists", "Indexers",
"AutoTagging", "DownloadClients"
}
.SelectMany(v => GetUsedTags(v, mapper)) .SelectMany(v => GetUsedTags(v, mapper))
.Concat(GetAutoTaggingTagSpecificationTags(mapper))
.Distinct() .Distinct()
.ToArray(); .ToArray();
@ -45,10 +54,31 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
private int[] GetUsedTags(string table, IDbConnection mapper) private int[] GetUsedTags(string table, IDbConnection mapper)
{ {
return mapper.Query<List<int>>($"SELECT DISTINCT \"Tags\" FROM \"{table}\" WHERE NOT \"Tags\" = '[]' AND NOT \"Tags\" IS NULL") return mapper
.Query<List<int>>(
$"SELECT DISTINCT \"Tags\" FROM \"{table}\" WHERE NOT \"Tags\" = '[]' AND NOT \"Tags\" IS NULL")
.SelectMany(x => x) .SelectMany(x => x)
.Distinct() .Distinct()
.ToArray(); .ToArray();
} }
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;
}
} }
} }

View File

@ -146,6 +146,7 @@
"AutoTaggingLoadError": "Unable to load auto tagging", "AutoTaggingLoadError": "Unable to load auto tagging",
"AutoTaggingNegateHelpText": "If checked, the auto tagging rule will not apply if this {implementationName} condition matches.", "AutoTaggingNegateHelpText": "If checked, the auto tagging rule will not apply if this {implementationName} condition matches.",
"AutoTaggingRequiredHelpText": "This {implementationName} condition must match for the auto tagging rule to apply. Otherwise a single {implementationName} match is sufficient.", "AutoTaggingRequiredHelpText": "This {implementationName} condition must match for the auto tagging rule to apply. Otherwise a single {implementationName} match is sufficient.",
"AutoTaggingSpecificationTag": "Tag",
"Automatic": "Automatic", "Automatic": "Automatic",
"AutomaticAdd": "Automatic Add", "AutomaticAdd": "Automatic Add",
"AutomaticSearch": "Automatic Search", "AutomaticSearch": "Automatic Search",

View File

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using NzbDrone.Core.AutoTagging; using NzbDrone.Core.AutoTagging;
using NzbDrone.Core.AutoTagging.Specifications;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
using NzbDrone.Core.Download; using NzbDrone.Core.Download;
using NzbDrone.Core.ImportLists; using NzbDrone.Core.ImportLists;
@ -121,7 +122,7 @@ namespace NzbDrone.Core.Tags
var artists = _artistService.GetAllArtistsTags(); var artists = _artistService.GetAllArtistsTags();
var rootFolders = _rootFolderService.All(); var rootFolders = _rootFolderService.All();
var indexers = _indexerService.All(); var indexers = _indexerService.All();
var autotags = _autoTaggingService.All(); var autoTags = _autoTaggingService.All();
var downloadClients = _downloadClientFactory.All(); var downloadClients = _downloadClientFactory.All();
var details = new List<TagDetails>(); var details = new List<TagDetails>();
@ -139,7 +140,7 @@ namespace NzbDrone.Core.Tags
ArtistIds = artists.Where(c => c.Value.Contains(tag.Id)).Select(c => c.Key).ToList(), ArtistIds = artists.Where(c => c.Value.Contains(tag.Id)).Select(c => c.Key).ToList(),
RootFolderIds = rootFolders.Where(c => c.DefaultTags.Contains(tag.Id)).Select(c => c.Id).ToList(), RootFolderIds = rootFolders.Where(c => c.DefaultTags.Contains(tag.Id)).Select(c => c.Id).ToList(),
IndexerIds = indexers.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList(), IndexerIds = indexers.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList(),
AutoTagIds = autotags.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList(), AutoTagIds = GetAutoTagIds(tag, autoTags),
DownloadClientIds = downloadClients.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList(), DownloadClientIds = downloadClients.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList(),
}); });
} }
@ -190,5 +191,23 @@ namespace NzbDrone.Core.Tags
_repo.Delete(tagId); _repo.Delete(tagId);
_eventAggregator.PublishEvent(new TagsUpdatedEvent()); _eventAggregator.PublishEvent(new TagsUpdatedEvent());
} }
private List<int> GetAutoTagIds(Tag tag, List<AutoTag> autoTags)
{
var autoTagIds = autoTags.Where(c => c.Tags.Contains(tag.Id)).Select(c => c.Id).ToList();
foreach (var autoTag in autoTags)
{
foreach (var specification in autoTag.Specifications)
{
if (specification is TagSpecification)
{
autoTagIds.Add(autoTag.Id);
}
}
}
return autoTagIds.Distinct().ToList();
}
} }
} }