diff --git a/modules/videos/channel/test/test_models.py b/modules/videos/channel/test/test_models.py index e3a21afd..4383d318 100644 --- a/modules/videos/channel/test/test_models.py +++ b/modules/videos/channel/test/test_models.py @@ -144,6 +144,37 @@ async def test_channel_statistics_with_tagged_videos(async_client, test_session, assert statistics['video_tags'] == 2 +def test_channel_directory_video_counts(test_session, channel_factory, video_factory): + """A Channel can count the video FileGroups in its directory, and which Channel they are assigned to. + + These counts are logged after a Channel refresh to help debug Videos missing from their Channel.""" + channel1 = channel_factory() + channel2 = channel_factory() + video_factory(channel_id=channel1.id) + video_factory(channel_id=channel1.id) + # These Videos' files are in channel1's directory, but they are not assigned to channel1. + other_channel_video = video_factory(with_video_file=channel1.directory / 'other channel.mp4') + unassigned_video = video_factory(with_video_file=channel1.directory / 'unassigned.mp4') + other_channel_video.channel_id = channel2.id + unassigned_video.channel_id = None + test_session.commit() + + assert channel1.get_directory_video_counts() == dict( + video_file_groups=4, + assigned_to_channel=2, + assigned_to_other_channels=1, + unassigned=1, + ) + + # channel2's directory contains no files. + assert channel2.get_directory_video_counts() == dict( + video_file_groups=0, + assigned_to_channel=0, + assigned_to_other_channels=0, + unassigned=0, + ) + + def test_channel_info_json(test_session, test_directory): from wrolpi.collections import Collection diff --git a/modules/videos/models.py b/modules/videos/models.py index 02379696..9ac88208 100644 --- a/modules/videos/models.py +++ b/modules/videos/models.py @@ -969,6 +969,26 @@ class Channel(ModelHelper, Base): curs.execute(stmt, dict(id=self.id)) return dict(curs.fetchone()) + def get_directory_video_counts(self) -> dict: + """Count the video/audio FileGroups within my directory, and which Channel they are assigned to. + + Logged after a refresh to help debug Videos missing from their Channel.""" + with get_db_curs() as curs: + stmt = ''' + SELECT COUNT(fg.id) AS "video_file_groups", + COUNT(fg.id) FILTER ( WHERE v.channel_id = %(id)s ) AS "assigned_to_channel", + COUNT(fg.id) FILTER ( WHERE v.channel_id IS NOT NULL + AND v.channel_id != %(id)s ) AS "assigned_to_other_channels", + COUNT(fg.id) FILTER ( WHERE v.id IS NULL + OR v.channel_id IS NULL ) AS "unassigned" + FROM file_group fg + LEFT JOIN video v ON v.file_group_id = fg.id + WHERE (fg.directory = %(directory)s OR fg.directory LIKE %(directory)s || '/%%') + AND (fg.mimetype LIKE 'video/%%' OR fg.mimetype LIKE 'audio/%%') \ + ''' + curs.execute(stmt, dict(id=self.id, directory=str(self.directory))) + return dict(curs.fetchone()) + def set_tag(self, tag_id_or_name: int | str | None) -> Tag | None: """Change the Tag relationship of this Channel. Will clear the Tag if provided with None.""" session = Session.object_session(self) @@ -1018,6 +1038,12 @@ class Channel(ModelHelper, Base): with get_db_session(commit=True) as session_: channel_ = cls.find_by_id(session_, id_) channel_.refreshed = True + counts = channel_.get_directory_video_counts() + logger.info(f'Refreshed {channel_}:' + f' {counts["video_file_groups"]} video FileGroups in directory,' + f' {counts["assigned_to_channel"]} assigned to this Channel,' + f' {counts["assigned_to_other_channels"]} assigned to other Channels,' + f' {counts["unassigned"]} unassigned') background_task(_()) return job_id