Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order Series Items by ordering type #460

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 99 additions & 53 deletions critiquebrainz/frontend/external/bookbrainz_db/author.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from brainzutils import cache
from typing import List
import sqlalchemy
import critiquebrainz.frontend.external.bookbrainz_db as db
import critiquebrainz.frontend.external.bookbrainz_db as db
from critiquebrainz.frontend.external.bookbrainz_db import DEFAULT_CACHE_EXPIRATION
from critiquebrainz.frontend.external.bookbrainz_db.identifiers import fetch_bb_external_identifiers
from critiquebrainz.frontend.external.bookbrainz_db.relationships import fetch_relationships, AUTHOR_WORK_AUTHOR_REL_ID
from critiquebrainz.frontend.external.bookbrainz_db.identifiers import process_bb_identifiers


def get_author_by_bbid(bbid: str) -> dict:
"""
Expand Down Expand Up @@ -50,65 +50,111 @@ def fetch_multiple_authors(bbids: List[str]) -> dict:
if not results:
with db.bb_engine.connect() as connection:
result = connection.execute(sqlalchemy.text("""
SELECT
bbid::text,
author.name,
sort_name,
author_type,
disambiguation,
identifier_set_id,
relationship_set_id,
area_id,
begin_year,
begin_month,
begin_day,
begin_area_id,
end_year,
end_month,
end_day,
end_area_id,
author.ended,
gender.name as gender,
COALESCE (json_agg(area)
FILTER (WHERE area IS NOT NULL),
SELECT
bbid::text,
author.name,
sort_name,
author_type,
disambiguation,
identifier_set_id,
relationship_set_id,
area_id,
begin_year,
begin_month,
begin_day,
begin_area_id,
end_year,
end_month,
end_day,
end_area_id,
author.ended,
gender.name as gender,
COALESCE (json_agg(area)
FILTER (WHERE area.name IS NOT NULL),
'[]'
) as area_info,
COALESCE (json_agg(DISTINCT relationships)
FILTER (WHERE relationships IS NOT NULL),
'[]'
) as rels,
COALESCE (json_agg(DISTINCT identifiers)
FILTER (WHERE identifiers IS NOT NULL),
'[]'
) as area_info
FROM author
LEFT JOIN musicbrainz.area area
ON begin_area_id = area.id
OR end_area_id = area.id
OR area_id = area.id
) as identifiers
FROM author
LEFT JOIN musicbrainz.area area
ON begin_area_id = area.id
OR end_area_id = area.id
OR area_id = area.id
LEFT JOIN musicbrainz.gender
ON gender_id = gender.id
WHERE bbid IN :bbids
AND master = 't'
GROUP BY bbid,
author.name,
sort_name,
author_type,
disambiguation,
identifier_set_id,
relationship_set_id,
area_id,
begin_year,
begin_month,
begin_day,
begin_area_id,
end_year,
end_month,
end_day,
end_area_id,
author.ended,
gender.name
"""), {'bbids': tuple(bbids)})
LEFT JOIN LATERAL (
SELECT rel.id as id,
reltype.id as relationship_type_id,
reltype.label as label,
rel.source_bbid::text as source_bbid,
rel.target_bbid::text as target_bbid,
reltype.target_entity_type as target_entity_type,
reltype.source_entity_type as source_entity_type,
COALESCE(
jsonb_object_agg(relatttype.name, relatttext.text_value)
FILTER (WHERE relatts IS NOT NULL),
'[]'
) as attributes
FROM relationship_set__relationship rels
LEFT JOIN relationship rel ON rels.relationship_id = rel.id
LEFT JOIN relationship_type reltype ON rel.type_id = reltype.id
LEFT JOIN relationship_attribute_set__relationship_attribute relatts ON rel.attribute_set_id = relatts.set_id
LEFT JOIN relationship_attribute relatt ON relatts.attribute_id = relatt.id
LEFT JOIN relationship_attribute_type relatttype ON relatt.attribute_type = relatttype.id
LEFT JOIN relationship_attribute_text_value relatttext ON relatts.attribute_id = relatttext.attribute_id
WHERE rels.set_id = author.relationship_set_id
GROUP BY rel.id,
reltype.id,
reltype.label,
rel.source_bbid,
rel.target_bbid,
reltype.target_entity_type,
reltype.source_entity_type
) AS relationships ON TRUE
LEFT JOIN LATERAL (
SELECT iden.type_id as type_id,
idtype.label as label,
idtype.display_template as url_template,
iden.value as value
FROM identifier_set__identifier idens
LEFT JOIN identifier iden on idens.identifier_id = iden.id
LEFT JOIN identifier_type idtype on iden.type_id = idtype.id
WHERE idens.set_id = author.identifier_set_id
) AS identifiers ON TRUE
WHERE bbid IN :bbids
AND master = 't'
GROUP BY bbid,
author.name,
sort_name,
author_type,
disambiguation,
identifier_set_id,
relationship_set_id,
area_id,
begin_year,
begin_month,
begin_day,
begin_area_id,
end_year,
end_month,
end_day,
end_area_id,
author.ended,
gender.name
"""), {'bbids': tuple(bbids)})

authors = result.mappings()
results = {}
for author in authors:
author = dict(author)
author['bbid'] = str(author['bbid'])
author['identifiers'] = fetch_bb_external_identifiers(author['identifier_set_id'])
author['rels'] = fetch_relationships( author['relationship_set_id'], [AUTHOR_WORK_AUTHOR_REL_ID])
author['identifiers'] = process_bb_identifiers(author['identifiers'])
results[author['bbid']] = author

cache.set(bb_author_key, results, DEFAULT_CACHE_EXPIRATION)
Expand Down
56 changes: 51 additions & 5 deletions critiquebrainz/frontend/external/bookbrainz_db/edition_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import sqlalchemy
import critiquebrainz.frontend.external.bookbrainz_db as db
from critiquebrainz.frontend.external.bookbrainz_db import DEFAULT_CACHE_EXPIRATION
from critiquebrainz.frontend.external.bookbrainz_db.identifiers import fetch_bb_external_identifiers
from critiquebrainz.frontend.external.bookbrainz_db.relationships import fetch_relationships, EDITION_EDITION_GROUP_EDITION_REL_ID, EDITION_WORK_CONTAINS_REL_ID
from critiquebrainz.frontend.external.bookbrainz_db.identifiers import process_bb_identifiers
from critiquebrainz.frontend.external.bookbrainz_db.relationships import EDITION_WORK_CONTAINS_REL_ID


def get_edition_group_by_bbid(bbid: str) -> dict:
Expand Down Expand Up @@ -63,9 +63,56 @@ def fetch_multiple_edition_groups(bbids: List[str]) -> dict:
COALESCE( json_agg( acn ORDER BY "position" ASC )
FILTER (WHERE acn IS NOT NULL),
'[]'
) as author_credits
) as author_credits,
COALESCE (json_agg(DISTINCT relationships)
FILTER (WHERE relationships IS NOT NULL),
'[]'
) as rels,
COALESCE (json_agg(DISTINCT identifiers)
FILTER (WHERE identifiers IS NOT NULL),
'[]'
) as identifiers
FROM edition_group
LEFT JOIN author_credit_name acn ON acn.author_credit_id = edition_group.author_credit_id
LEFT JOIN LATERAL (
SELECT rel.id as id,
reltype.id as relationship_type_id,
reltype.label as label,
rel.source_bbid::text as source_bbid,
rel.target_bbid::text as target_bbid,
reltype.target_entity_type as target_entity_type,
reltype.source_entity_type as source_entity_type,
COALESCE(
jsonb_object_agg(relatttype.name, relatttext.text_value)
FILTER (WHERE relatts IS NOT NULL),
'[]'
) as attributes
FROM relationship_set__relationship rels
LEFT JOIN relationship rel ON rels.relationship_id = rel.id
LEFT JOIN relationship_type reltype ON rel.type_id = reltype.id
LEFT JOIN relationship_attribute_set__relationship_attribute relatts ON rel.attribute_set_id = relatts.set_id
LEFT JOIN relationship_attribute relatt ON relatts.attribute_id = relatt.id
LEFT JOIN relationship_attribute_type relatttype ON relatt.attribute_type = relatttype.id
LEFT JOIN relationship_attribute_text_value relatttext ON relatts.attribute_id = relatttext.attribute_id
WHERE rels.set_id = edition_group.relationship_set_id
GROUP BY rel.id,
reltype.id,
reltype.label,
rel.source_bbid,
rel.target_bbid,
reltype.target_entity_type,
reltype.source_entity_type
) AS relationships ON TRUE
LEFT JOIN LATERAL (
SELECT iden.type_id as type_id,
idtype.label as label,
idtype.display_template as url_template,
iden.value as value
FROM identifier_set__identifier idens
LEFT JOIN identifier iden on idens.identifier_id = iden.id
LEFT JOIN identifier_type idtype on iden.type_id = idtype.id
WHERE idens.set_id = edition_group.identifier_set_id
) AS identifiers ON TRUE
WHERE bbid in :bbids
AND master = 't'
AND data_id IS NOT NULL
Expand All @@ -82,8 +129,7 @@ def fetch_multiple_edition_groups(bbids: List[str]) -> dict:
results = {}
for edition_group in edition_groups:
edition_group = dict(edition_group)
edition_group['identifiers'] = fetch_bb_external_identifiers(edition_group['identifier_set_id'])
edition_group['rels'] = fetch_relationships( edition_group['relationship_set_id'], [EDITION_EDITION_GROUP_EDITION_REL_ID])
edition_group['identifiers'] = process_bb_identifiers(edition_group['identifiers'])
results[edition_group['bbid']] = edition_group

edition_groups = results
Expand Down
42 changes: 0 additions & 42 deletions critiquebrainz/frontend/external/bookbrainz_db/identifiers.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,4 @@
from typing import List
from brainzutils import cache
import sqlalchemy
import critiquebrainz.frontend.external.bookbrainz_db as db
from critiquebrainz.frontend.external.bookbrainz_db import DEFAULT_CACHE_EXPIRATION

def fetch_bb_external_identifiers(identifier_set_id: int) -> List:
"""
Fetch identifiers from the database.
Args:
identifier_set_id (int): Identifier set ID.
Returns:
List of identifiers containing the following fields:
- name (str): Identifier name.
- url (str): Identifier URL.
- value (str): Identifier value.
- icon (str): Identifier icon.
"""
if not identifier_set_id:
return []

bb_identifiers_key = cache.gen_key('identifier', identifier_set_id)
identifiers = cache.get(bb_identifiers_key)
if not identifiers:
with db.bb_engine.connect() as connection:
result = connection.execute(sqlalchemy.text("""
SELECT iden.type_id as type_id,
idtype.label as label,
idtype.display_template as url_template,
iden.value as value
FROM identifier_set__identifier idens
LEFT JOIN identifier iden on idens.identifier_id = iden.id
LEFT JOIN identifier_type idtype on iden.type_id = idtype.id
WHERE idens.set_id = :identifier_set_id
"""), {'identifier_set_id': identifier_set_id})
identifiers = result.mappings()
identifiers = [dict(identifier) for identifier in identifiers]
identifiers = process_bb_identifiers(identifiers)
cache.set(bb_identifiers_key, identifiers, DEFAULT_CACHE_EXPIRATION)

if not identifiers:
return []
return identifiers


def process_bb_identifiers(identifiers: List) -> List:
Expand Down
58 changes: 52 additions & 6 deletions critiquebrainz/frontend/external/bookbrainz_db/literary_work.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import sqlalchemy
import critiquebrainz.frontend.external.bookbrainz_db as db
from critiquebrainz.frontend.external.bookbrainz_db import DEFAULT_CACHE_EXPIRATION
from critiquebrainz.frontend.external.bookbrainz_db.identifiers import fetch_bb_external_identifiers
from critiquebrainz.frontend.external.bookbrainz_db.relationships import fetch_relationships, WORK_WORK_TRANSLATION_REL_ID, EDITION_WORK_CONTAINS_REL_ID
from critiquebrainz.frontend.external.bookbrainz_db.identifiers import process_bb_identifiers
from critiquebrainz.frontend.external.bookbrainz_db.relationships import EDITION_WORK_CONTAINS_REL_ID

WORK_TYPE_FILTER_OPTIONS = ('Novel', 'Short Story', 'Poem')

Expand Down Expand Up @@ -80,13 +80,60 @@ def fetch_multiple_literary_works(bbids: List[str], work_type=None, limit=None,
disambiguation,
identifier_set_id,
relationship_set_id,
COALESCE (json_agg(mbl.name)
COALESCE (json_agg(DISTINCT mbl.name)
FILTER (WHERE mbl IS NOT NULL),
'[]'
) as languages
) as languages,
COALESCE (json_agg(DISTINCT relationships)
FILTER (WHERE relationships IS NOT NULL),
'[]'
) as rels,
COALESCE (json_agg(DISTINCT identifiers)
FILTER (WHERE identifiers IS NOT NULL),
'[]'
) as identifiers
FROM work
LEFT JOIN bookbrainz.language_set__language lsl ON lsl.set_id = work.language_set_id
LEFT JOIN musicbrainz.language mbl on mbl.id = lsl.language_id
LEFT JOIN LATERAL (
SELECT rel.id as id,
reltype.id as relationship_type_id,
reltype.label as label,
rel.source_bbid::text as source_bbid,
rel.target_bbid::text as target_bbid,
reltype.target_entity_type as target_entity_type,
reltype.source_entity_type as source_entity_type,
COALESCE(
jsonb_object_agg(relatttype.name, relatttext.text_value)
FILTER (WHERE relatts IS NOT NULL),
'[]'
) as attributes
FROM relationship_set__relationship rels
LEFT JOIN relationship rel ON rels.relationship_id = rel.id
LEFT JOIN relationship_type reltype ON rel.type_id = reltype.id
LEFT JOIN relationship_attribute_set__relationship_attribute relatts ON rel.attribute_set_id = relatts.set_id
LEFT JOIN relationship_attribute relatt ON relatts.attribute_id = relatt.id
LEFT JOIN relationship_attribute_type relatttype ON relatt.attribute_type = relatttype.id
LEFT JOIN relationship_attribute_text_value relatttext ON relatts.attribute_id = relatttext.attribute_id
WHERE rels.set_id = work.relationship_set_id
GROUP BY rel.id,
reltype.id,
reltype.label,
rel.source_bbid,
rel.target_bbid,
reltype.target_entity_type,
reltype.source_entity_type
) AS relationships ON TRUE
LEFT JOIN LATERAL (
SELECT iden.type_id as type_id,
idtype.label as label,
idtype.display_template as url_template,
iden.value as value
FROM identifier_set__identifier idens
LEFT JOIN identifier iden on idens.identifier_id = iden.id
LEFT JOIN identifier_type idtype on iden.type_id = idtype.id
WHERE idens.set_id = work.identifier_set_id
) AS identifiers ON TRUE
WHERE bbid IN :bbids
AND master = 't'
AND data_id IS NOT NULL
Expand All @@ -107,8 +154,7 @@ def fetch_multiple_literary_works(bbids: List[str], work_type=None, limit=None,
results = {}
for literary_work in literary_works:
literary_work = dict(literary_work)
literary_work['identifiers'] = fetch_bb_external_identifiers(literary_work['identifier_set_id'])
literary_work['rels'] = fetch_relationships(literary_work['relationship_set_id'], [WORK_WORK_TRANSLATION_REL_ID])
literary_work['identifiers'] = process_bb_identifiers(literary_work['identifiers'])
results[literary_work['bbid']] = literary_work

cache.set(bb_literary_work_key, results, DEFAULT_CACHE_EXPIRATION)
Expand Down
Loading