Renaming all co-occurring to overlapping, easier to spell. Filtering by only overlapping tags when searching by a tag, this is because any non-overlapping will not produce results anyways.

This commit is contained in:
Roland 2026-03-07 15:43:11 -07:00
parent 3d74b4bcfc
commit f59a4198d9
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
6 changed files with 203 additions and 83 deletions

View file

@ -1,5 +1,5 @@
import React, {useEffect} from "react";
import {ApiDownError, deleteTag, getCooccurringTags, getRecentTags, getTags, saveTag} from "./api";
import {ApiDownError, deleteTag, getRecentTags, getTags, saveTag} from "./api";
import {Dimmer, Divider, Grid, GridColumn, GridRow, Input, Label, TableCell, TableRow,} from "semantic-ui-react";
import {
APIButton,
@ -49,7 +49,8 @@ export function useTags() {
return;
}
try {
const t = await getTags();
const body = await getTags();
const t = body['tags'];
setTags(t);
setTagNames(t.map(i => i['name']));
} catch (e) {
@ -403,6 +404,7 @@ export function AddTagsButton({
closeAfterLimit = true,
limit = null,
disabled = false,
filterByOverlap = false,
}) {
// A button which displays a modal in which the user can add or remove tags.
@ -414,6 +416,7 @@ export function AddTagsButton({
const [frequentTags, setFrequentTags] = React.useState([]);
const [frequentTagsLabel, setFrequentTagsLabel] = React.useState('Recent Tags');
const [hasSelectedTag, setHasSelectedTag] = React.useState(false);
const [overlappingTags, setOverlappingTags] = React.useState(null);
const filterInputRef = React.useRef(null);
// Sync localTags with selectedTagNames when it changes from parent
@ -421,26 +424,40 @@ export function AddTagsButton({
setLocalTags(selectedTagNames);
}, [selectedTagNames]);
const fetchOverlappingFilter = React.useCallback((currentTags) => {
if (filterByOverlap && currentTags && currentTags.length > 0) {
getTags(currentTags).then(body => {
setOverlappingTags(body['overlapping_tag_names'] || []);
});
} else {
setOverlappingTags(null);
}
}, [filterByOverlap]);
React.useEffect(() => {
if (open) {
setFilterText('');
setHasSelectedTag(false);
if (selectedTagNames && selectedTagNames.length === 1) {
setHasSelectedTag(true);
getCooccurringTags(selectedTagNames[0]).then(coTags => {
if (coTags && coTags.length > 0) {
setFrequentTags(coTags);
setFrequentTagsLabel('Frequent Tags');
} else {
setFrequentTags([]);
setFrequentTagsLabel('Recent Tags');
}
});
} else if (!selectedTagNames || selectedTagNames.length === 0) {
setFrequentTagsLabel('Recent Tags');
getRecentTags().then(names => setFrequentTags(names));
} else {
setFrequentTags([]);
fetchOverlappingFilter(selectedTagNames);
if (!filterByOverlap) {
if (selectedTagNames && selectedTagNames.length === 1) {
setHasSelectedTag(true);
getTags(selectedTagNames[0]).then(body => {
const coTags = body['overlapping_tag_names'] || [];
if (coTags.length > 0) {
setFrequentTags(coTags);
setFrequentTagsLabel('Frequent Tags');
} else {
setFrequentTags([]);
setFrequentTagsLabel('Recent Tags');
}
});
} else if (!selectedTagNames || selectedTagNames.length === 0) {
setFrequentTagsLabel('Recent Tags');
getRecentTags().then(names => setFrequentTags(names));
} else {
setFrequentTags([]);
}
}
const timeout = setTimeout(() => {
if (filterInputRef.current && window.innerWidth >= 700) {
@ -471,10 +488,12 @@ export function AddTagsButton({
setFilterText('');
onAdd(name);
onChange(newTags, null);
if (!hasSelectedTag) {
fetchOverlappingFilter(newTags);
if (!filterByOverlap && !hasSelectedTag) {
setHasSelectedTag(true);
getCooccurringTags(name).then(coTags => {
if (coTags && coTags.length > 0) {
getTags(name).then(body => {
const coTags = body['overlapping_tag_names'] || [];
if (coTags.length > 0) {
setFrequentTags(coTags);
setFrequentTagsLabel('Frequent Tags');
} else {
@ -497,6 +516,7 @@ export function AddTagsButton({
setLocalTags(newTags)
onRemove(name);
onChange(newTags, null);
fetchOverlappingFilter(newTags);
} finally {
setLoading(false);
}
@ -521,13 +541,17 @@ export function AddTagsButton({
}
const selectedTagsGroup = <TagsGroup tagNames={localTags} onClick={removeTag}/>;
const unusedTags = _.difference(tagNames, localTags);
let unusedTags = _.difference(tagNames, localTags);
if (overlappingTags !== null) {
unusedTags = unusedTags.filter(name => overlappingTags.includes(name));
}
const filteredUnusedTags = filterText
? unusedTags.filter(name => name.toLowerCase().includes(filterText.toLowerCase()))
: unusedTags;
const unusedTagsGroup = <TagsGroup tagNames={filteredUnusedTags} onClick={addTag}/>;
const emptySelectedTags = limit === 1 ? 'Add only one tag below' : 'Add one or more tags below';
const visibleFrequentTags = frequentTags.filter(name => !(localTags || []).includes(name));
const hideFrequentTags = filterByOverlap && localTags && localTags.length > 0;
return <>
<Button
@ -547,7 +571,7 @@ export function AddTagsButton({
{localTags && localTags.length > 0 ? selectedTagsGroup : emptySelectedTags}
{visibleFrequentTags.length > 0 && <>
{!hideFrequentTags && visibleFrequentTags.length > 0 && <>
<Divider/>
<Header as='h5'>{frequentTagsLabel}</Header>
<TagsGroup tagNames={visibleFrequentTags} onClick={addTag}/>
@ -565,7 +589,11 @@ export function AddTagsButton({
style={{marginBottom: '0.5em'}}
/>
{unusedTags && unusedTags.length > 0 ? unusedTagsGroup : 'You have no tags'}
{unusedTags && unusedTags.length > 0
? unusedTagsGroup
: (overlappingTags !== null && tagNames.length > 0
? 'No overlapping tags'
: 'You have no tags')}
</Modal.Content>
<Modal.Actions>
<Grid textAlign='left'>
@ -599,6 +627,7 @@ export const TagsSelector = ({
closeAfterLimit = true,
limit = null,
disabled = false,
filterByOverlap = false,
}) => {
// Provides a button to add tags to a list. Displays the tags of that list.
const {TagsLinkGroup} = React.useContext(TagsContext);
@ -619,6 +648,7 @@ export const TagsSelector = ({
anyTag={anyTag}
limit={limit}
disabled={disabled}
filterByOverlap={filterByOverlap}
/>;
if (hideGroup) {

View file

@ -1487,12 +1487,17 @@ export async function getEvents(after) {
return response
}
export async function getTags() {
const uri = `${API_URI}/tag`;
export async function getTags(tagNames) {
let uri = `${API_URI}/tag`;
if (tagNames && tagNames.length > 0) {
const params = new URLSearchParams();
(typeof tagNames === 'string' ? [tagNames] : tagNames).forEach(name => params.append('tag_names', name));
uri = `${uri}?${params.toString()}`;
}
const response = await apiGet(uri);
if (response.ok) {
const body = await response.json();
return body['tags'];
return body;
}
// Not toasting because this happens often.
return response
@ -1508,15 +1513,6 @@ export async function getRecentTags() {
return [];
}
export async function getCooccurringTags(tagName) {
const uri = `${API_URI}/tag/cooccurring/${encodeURIComponent(tagName)}`;
const response = await apiGet(uri);
if (response.ok) {
const body = await response.json();
return body['tag_names'] || [];
}
return [];
}
export async function tagFileGroup(fileGroup, name) {
const body = {tag_name: name};

View file

@ -468,6 +468,7 @@ export function TagsQuerySelector({onChange, showAny = true}) {
selectedTagNames={activeTags}
anyTag={anyTag}
onChange={localOnChange}
filterByOverlap={true}
style={{marginLeft: '0.3em', marginTop: '0.3em'}}
/>
}

View file

@ -1,7 +1,6 @@
import asyncio
import pathlib
import re
import urllib.parse
from http import HTTPStatus
import vininfo.exceptions
@ -524,9 +523,15 @@ async def feed(request: Request, query: schema.EventsRequest):
@openapi.definition(
description='Get a list of all Tags',
)
async def get_tags_request(_: Request):
async def get_tags_request(request: Request):
tags_ = tags.get_tags()
return json_response(dict(tags=tags_))
tag_names = request.args.getlist('tag_names')
result = dict(tags=tags_)
if tag_names:
limit = request.args.get('limit')
limit = int(limit) if limit is not None else None
result['overlapping_tag_names'] = tags.get_overlapping_tags(tag_names, limit=limit)
return json_response(result)
@api_bp.get('/tag/recent')
@ -538,16 +543,6 @@ async def get_recent_tags_request(_: Request):
return json_response(dict(tag_names=tag_names))
@api_bp.get('/tag/cooccurring/<tag_name:str>')
@openapi.definition(
description='Get tags that frequently co-occur with the given tag',
)
async def get_cooccurring_tags_request(_: Request, tag_name: str):
tag_name = urllib.parse.unquote(tag_name)
tag_names = tags.get_cooccurring_tags(tag_name)
return json_response(dict(tag_names=tag_names))
@api_bp.post('/tag', name='tag_crate')
@api_bp.post('/tag/<tag_id:int>', name='tag_update')
@openapi.definition(

View file

@ -3,7 +3,7 @@ import contextlib
import pathlib
from dataclasses import dataclass, field
from datetime import datetime
from typing import List, Dict, Tuple, Optional
from typing import List, Dict, Tuple, Optional, Union
import cachetools
from cachetools.keys import hashkey
@ -714,27 +714,44 @@ def get_recent_tags(limit: int = 5) -> List[str]:
return [row['name'] for row in curs.fetchall()]
def get_cooccurring_tags(tag_name: str, limit: int = 5) -> List[str]:
def get_overlapping_tags(tag_names: Union[str, List[str]], limit: int = 5) -> List[str]:
if isinstance(tag_names, str):
tag_names = [tag_names]
limit_clause = 'LIMIT %(limit)s' if limit is not None else ''
with get_db_curs() as curs:
curs.execute('''
WITH target_tag AS (SELECT id FROM tag WHERE name = %(tag_name)s),
file_cooccur AS (
SELECT tf.tag_id, COUNT(*) AS cnt FROM tag_file tf
WHERE tf.file_group_id IN (SELECT file_group_id FROM tag_file WHERE tag_id = (SELECT id FROM target_tag))
AND tf.tag_id != (SELECT id FROM target_tag) GROUP BY tf.tag_id
curs.execute(f'''
WITH target_tags AS (SELECT id FROM tag WHERE name = ANY(%(tag_names)s::TEXT[])),
file_matching AS (
SELECT tf.file_group_id FROM tag_file tf
WHERE tf.tag_id IN (SELECT id FROM target_tags)
GROUP BY tf.file_group_id
HAVING COUNT(DISTINCT tf.tag_id) = %(tag_count)s
),
zim_cooccur AS (
zim_matching AS (
SELECT tz.zim_id, tz.zim_entry FROM tag_zim tz
WHERE tz.tag_id IN (SELECT id FROM target_tags)
GROUP BY tz.zim_id, tz.zim_entry
HAVING COUNT(DISTINCT tz.tag_id) = %(tag_count)s
),
file_overlap AS (
SELECT tf.tag_id, COUNT(*) AS cnt FROM tag_file tf
WHERE tf.file_group_id IN (SELECT file_group_id FROM file_matching)
AND tf.tag_id NOT IN (SELECT id FROM target_tags) GROUP BY tf.tag_id
),
zim_overlap AS (
SELECT tz.tag_id, COUNT(*) AS cnt FROM tag_zim tz
WHERE (tz.zim_id, tz.zim_entry) IN (SELECT zim_id, zim_entry FROM tag_zim WHERE tag_id = (SELECT id FROM target_tag))
AND tz.tag_id != (SELECT id FROM target_tag) GROUP BY tz.tag_id
WHERE (tz.zim_id, tz.zim_entry) IN (SELECT zim_id, zim_entry FROM zim_matching)
AND tz.tag_id NOT IN (SELECT id FROM target_tags) GROUP BY tz.tag_id
),
combined AS (
SELECT tag_id, SUM(cnt) AS total FROM (
SELECT tag_id, cnt FROM file_cooccur UNION ALL SELECT tag_id, cnt FROM zim_cooccur
) AS all_cooccur GROUP BY tag_id
SELECT tag_id, cnt FROM file_overlap UNION ALL SELECT tag_id, cnt FROM zim_overlap
) AS all_overlap GROUP BY tag_id
)
SELECT t.name FROM combined c JOIN tag t ON t.id = c.tag_id ORDER BY c.total DESC LIMIT %(limit)s
''', dict(tag_name=tag_name, limit=limit))
SELECT t.name FROM combined c JOIN tag t ON t.id = c.tag_id ORDER BY c.total DESC {limit_clause}
''', dict(tag_names=tag_names, tag_count=len(tag_names), limit=limit))
return [row['name'] for row in curs.fetchall()]

View file

@ -680,8 +680,8 @@ async def test_get_recent_tags(test_session, make_files_structure, tag_factory,
@pytest.mark.asyncio
async def test_get_cooccurring_tags(test_session, make_files_structure, tag_factory, video_bytes, refresh_files):
"""Tags that frequently co-occur with a given tag are returned."""
async def test_get_overlapping_tags(test_session, make_files_structure, tag_factory, video_bytes, refresh_files):
"""Tags that frequently overlap with a given tag are returned."""
make_files_structure({'a.mp4': video_bytes, 'b.mp4': video_bytes})
await refresh_files()
file_groups = test_session.query(FileGroup).order_by(FileGroup.primary_path).all()
@ -698,16 +698,16 @@ async def test_get_cooccurring_tags(test_session, make_files_structure, tag_fact
file_groups[1].add_tag(test_session, tag2.id)
test_session.commit()
result = tags.get_cooccurring_tags('alpha')
# beta co-occurs with alpha twice, gamma once.
result = tags.get_overlapping_tags('alpha')
# beta overlaps with alpha twice, gamma once.
assert result == ['beta', 'gamma']
@pytest.mark.asyncio
async def test_get_cooccurring_tags_empty(test_session, tag_factory):
"""No co-occurrences returns an empty list."""
async def test_get_overlapping_tags_empty(test_session, tag_factory):
"""No overlaps returns an empty list."""
await tag_factory('lonely')
result = tags.get_cooccurring_tags('lonely')
result = tags.get_overlapping_tags('lonely')
assert result == []
@ -728,25 +728,106 @@ async def test_recent_tags_api(async_client, test_session, make_files_structure,
assert 'alpha' in body['tag_names']
@pytest.mark.asyncio
async def test_cooccurring_tags_api(async_client, test_session, make_files_structure, tag_factory, video_bytes,
refresh_files):
"""The co-occurring tags API endpoint returns tag names."""
async def test_get_overlapping_tags_multiple(test_session, make_files_structure, tag_factory, video_bytes,
refresh_files):
"""When multiple tag names are provided, only tags overlapping with ALL given tags are returned."""
make_files_structure({'a.mp4': video_bytes, 'b.mp4': video_bytes, 'c.mp4': video_bytes})
await refresh_files()
file_groups = test_session.query(FileGroup).order_by(FileGroup.primary_path).all()
tag1 = await tag_factory('alpha')
tag2 = await tag_factory('beta')
tag3 = await tag_factory('gamma')
tag4 = await tag_factory('delta')
# File a: alpha, beta, gamma
file_groups[0].add_tag(test_session, tag1.id)
file_groups[0].add_tag(test_session, tag2.id)
file_groups[0].add_tag(test_session, tag3.id)
# File b: alpha, beta, delta
file_groups[1].add_tag(test_session, tag1.id)
file_groups[1].add_tag(test_session, tag2.id)
file_groups[1].add_tag(test_session, tag4.id)
# File c: alpha, gamma
file_groups[2].add_tag(test_session, tag1.id)
file_groups[2].add_tag(test_session, tag3.id)
test_session.commit()
# Overlapping with both alpha AND beta: gamma (file a), delta (file b).
result = tags.get_overlapping_tags(['alpha', 'beta'])
assert set(result) == {'gamma', 'delta'}
# Overlapping with both alpha AND gamma: beta (file a only, since file c has no beta).
result = tags.get_overlapping_tags(['alpha', 'gamma'])
assert 'beta' in result
@pytest.mark.asyncio
async def test_get_overlapping_tags_no_limit(test_session, make_files_structure, tag_factory, video_bytes,
refresh_files):
"""When limit is None, all overlapping tags are returned."""
make_files_structure({'a.mp4': video_bytes})
await refresh_files()
fg = test_session.query(FileGroup).one()
tag1 = await tag_factory('alpha')
tag2 = await tag_factory('beta')
tags_list = []
for i in range(10):
t = await tag_factory(f'tag{i}')
tags_list.append(t)
fg.add_tag(test_session, tag1.id)
fg.add_tag(test_session, tag2.id)
for t in tags_list:
fg.add_tag(test_session, t.id)
test_session.commit()
_, response = await async_client.get('/api/tag/cooccurring/alpha')
assert response.status == 200
body = response.json
assert body['tag_names'] == ['beta']
# Default limit of 5 returns only 5.
result = tags.get_overlapping_tags('alpha')
assert len(result) == 5
# Unknown tag returns empty.
_, response = await async_client.get('/api/tag/cooccurring/nonexistent')
# No limit returns all 10.
result = tags.get_overlapping_tags('alpha', limit=None)
assert len(result) == 10
@pytest.mark.asyncio
async def test_overlapping_tags_multi_api(async_client, test_session, make_files_structure, tag_factory, video_bytes,
refresh_files):
"""The /api/tag endpoint supports optional tag_names query params to return overlapping tags."""
make_files_structure({'a.mp4': video_bytes, 'b.mp4': video_bytes})
await refresh_files()
file_groups = test_session.query(FileGroup).order_by(FileGroup.primary_path).all()
tag1 = await tag_factory('alpha')
tag2 = await tag_factory('beta')
tag3 = await tag_factory('gamma')
# File a: alpha, beta, gamma
file_groups[0].add_tag(test_session, tag1.id)
file_groups[0].add_tag(test_session, tag2.id)
file_groups[0].add_tag(test_session, tag3.id)
# File b: alpha, beta
file_groups[1].add_tag(test_session, tag1.id)
file_groups[1].add_tag(test_session, tag2.id)
test_session.commit()
# Multiple tag_names returns overlapping tags alongside full tags list.
_, response = await async_client.get('/api/tag?tag_names=alpha&tag_names=beta')
assert response.status == 200
assert response.json['tag_names'] == []
assert response.json['overlapping_tag_names'] == ['gamma']
assert 'tags' in response.json
# Single tag_name still works.
_, response = await async_client.get('/api/tag?tag_names=alpha')
assert response.status == 200
assert 'beta' in response.json['overlapping_tag_names']
assert 'tags' in response.json
# No tag_names returns tags without overlapping_tag_names.
_, response = await async_client.get('/api/tag')
assert response.status == 200
assert 'tags' in response.json
assert 'overlapping_tag_names' not in response.json