Skip to content

Commit

Permalink
MBS-13075: Indicate the number of items in a series on search results
Browse files Browse the repository at this point in the history
It seems useful to be able to see at a glance the amount
of entities in each series in a search result.

This requires loading the item_entity_type for the series type
when doing schema_fixup_type, and that in return needs
the type gid, which we have in the search response.
For some reason, we were creating a type with just the name;
this adds the type gid to the fixed up types as well.
  • Loading branch information
reosarevok committed Dec 14, 2023
1 parent 487d247 commit 1187791
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/MusicBrainz/Server/Controller/Search.pm
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ sub direct : Private
elsif ($type eq 'series') {
$c->model('SeriesType')->load(@entities);
$c->model('SeriesOrderingType')->load(@entities);
$c->model('Series')->load_entity_count(@entities);
}
elsif ($type eq 'event') {
$c->model('Event')->load_related_info(@entities);
Expand Down
25 changes: 24 additions & 1 deletion lib/MusicBrainz/Server/Data/Search.pm
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,31 @@ my %mapping = (
'title' => 'name',
'artist-credit' => 'artist_credit',
'label-code' => 'label_code',
'type-id' => 'type_id',
);

Readonly our @ENTITIES_WITH_SIMPLE_TYPES => entities_with(['type', 'simple']);

sub schema_fixup_type {
my ($self, $data, $type) = @_;

my $type_gid = delete $data->{type_id};

if (defined $data->{type} && contains_string(\@ENTITIES_WITH_SIMPLE_TYPES, $type)) {
my $item_entity_type;
if ($type eq 'series') {
# This is needed to query the number of entities later
$item_entity_type = $self->c->sql->select_single_value(
'SELECT entity_type FROM series_type WHERE gid = ?',
$type_gid,
);
}
my $model = 'MusicBrainz::Server::Entity::' . type_to_model($type) . 'Type';
$data->{type} = $model->new( name => $data->{type} );
$data->{type} = $model->new(
name => $data->{type},
gid => $type_gid,
defined $item_entity_type ? (item_entity_type => $item_entity_type) : (),
);
}
return $data;
}
Expand Down Expand Up @@ -962,6 +978,13 @@ sub external_search
$self->c->model('Artwork')->load_for_release_groups(@entities);
}

if ($type eq 'series')
{
my @entities = map { $_->entity } @results;
$self->c->model('Series')->load_ids(@entities);
$self->c->model('Series')->load_entity_count(@entities);
}

my $pager = Data::Page->new;
$pager->current_page($page);
$pager->entries_per_page($limit);
Expand Down
26 changes: 26 additions & 0 deletions lib/MusicBrainz/Server/Data/Series.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use MusicBrainz::Server::Data::Utils qw(
merge_table_attributes
load_subobjects
order_by
placeholders
);
use MusicBrainz::Server::Data::Utils::Cleanup qw( used_in_relationship );
use MusicBrainz::Server::Data::Utils::Uniqueness qw( assert_uniqueness_conserved );
Expand Down Expand Up @@ -121,6 +122,31 @@ sub _merge_impl {
return 1;
}

=method load_ids
Load internal IDs for event objects that only have GIDs.
=cut

sub load_ids
{
my ($self, @series) = @_;

my @gids = map { $_->gid } @series;
return () unless @gids;

my $query = '
SELECT gid, id FROM series
WHERE gid IN (' . placeholders(@gids) . ')
';
my %map = map { $_->[0] => $_->[1] }
@{ $self->sql->select_list_of_lists($query, @gids) };

for my $series (@series) {
$series->id($map{$series->gid}) if exists $map{$series->gid};
}
}

sub load
{
my ($self, @objs) = @_;
Expand Down
4 changes: 4 additions & 0 deletions root/search/components/SeriesResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ function buildResult(
? lp_attributes(series.typeName, 'series_type')
: null}
</td>
<td>
{series.entity_count}
</td>
</tr>
);
}
Expand All @@ -56,6 +59,7 @@ const SeriesResults = ({
<>
<th>{l('Name')}</th>
<th>{l('Type')}</th>
<th>{l('Number of entities')}</th>
</>
}
pager={pager}
Expand Down
2 changes: 2 additions & 0 deletions t/lib/t/MusicBrainz/Server/Controller/Search/Indexed.pm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test all => sub {
{
"id": "e414a176-b978-492f-b6bc-9fd4c89df221",
"type": "Group",
"type-id": "e431f5f6-b5d2-343d-8b36-72607fffb74b",
"score": "97",
"name": "L.O.V.E.",
"sort-name": "L.O.V.E.",
Expand Down Expand Up @@ -61,6 +62,7 @@ test all => sub {
{
"id": "53299fed-ec27-464f-94eb-5fda2db2902a",
"type": "Person",
"type-id": "b6e035f4-3ce9-331c-97df-83397230b0df",
"score": "65",
"name": "Laura Love",
"sort-name": "Love, Laura",
Expand Down
4 changes: 4 additions & 0 deletions t/lib/t/MusicBrainz/Server/Data/Search.pm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ our $artist_json = <<~'EOF';
{
"id": "34ec9a8d-c65b-48fd-bcdd-aad2f72fdb47",
"type": "Group",
"type-id": "e431f5f6-b5d2-343d-8b36-72607fffb74b",
"score": "98",
"name": "Love",
"sort-name": "Love",
Expand Down Expand Up @@ -93,6 +94,7 @@ test all => sub {
is($artist->comment, 'folk-rock/psychedelic band');
is($artist->gid, '34ec9a8d-c65b-48fd-bcdd-aad2f72fdb47');
is($artist->type->name, 'Group');
is($artist->type->gid, 'e431f5f6-b5d2-343d-8b36-72607fffb74b');

# release_group search
$data = load_data('release_group', $test->c, <<~'EOF');
Expand Down Expand Up @@ -478,6 +480,7 @@ test all => sub {
{
"id": "e24ca2f9-416e-42bd-a223-bed20fa409d0",
"type": "Production",
"type-id": "a2426aab-2dd4-339c-b47d-b4923a241678",
"score": "100",
"name": "Love Records",
"sort-name": "Love Records",
Expand Down Expand Up @@ -516,6 +519,7 @@ test all => sub {
is($label->comment, 'Finnish label');
is($label->gid, 'e24ca2f9-416e-42bd-a223-bed20fa409d0');
is($label->type->name, 'Production');
is($label->type->gid, 'a2426aab-2dd4-339c-b47d-b4923a241678');

# annotation search
$data = load_data('annotation', $test->c, <<~"EOF");
Expand Down

0 comments on commit 1187791

Please sign in to comment.