mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-21 15:36:18 -05:00
Adding tags filter input on tags selector, adding frequently used tags to suggest tags that are used together.
This commit is contained in:
parent
d2d09da296
commit
1913755db5
5 changed files with 236 additions and 3 deletions
|
|
@ -1,6 +1,6 @@
|
|||
import React, {useEffect} from "react";
|
||||
import {ApiDownError, deleteTag, getTags, saveTag} from "./api";
|
||||
import {Dimmer, Divider, Grid, GridColumn, GridRow, Label, TableCell, TableRow,} from "semantic-ui-react";
|
||||
import {ApiDownError, deleteTag, getCooccurringTags, getRecentTags, getTags, saveTag} from "./api";
|
||||
import {Dimmer, Divider, Grid, GridColumn, GridRow, Input, Label, TableCell, TableRow,} from "semantic-ui-react";
|
||||
import {
|
||||
APIButton,
|
||||
contrastingColor,
|
||||
|
|
@ -410,12 +410,32 @@ export function AddTagsButton({
|
|||
const [open, setOpen] = React.useState(false);
|
||||
const [loading, setLoading] = React.useState(false);
|
||||
const [localTags, setLocalTags] = React.useState(selectedTagNames);
|
||||
const [filterText, setFilterText] = React.useState('');
|
||||
const [frequentTags, setFrequentTags] = React.useState([]);
|
||||
const [frequentTagsLabel, setFrequentTagsLabel] = React.useState('Recent Tags');
|
||||
const [hasSelectedTag, setHasSelectedTag] = React.useState(false);
|
||||
const filterInputRef = React.useRef(null);
|
||||
|
||||
// Sync localTags with selectedTagNames when it changes from parent
|
||||
React.useEffect(() => {
|
||||
setLocalTags(selectedTagNames);
|
||||
}, [selectedTagNames]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (open) {
|
||||
setFilterText('');
|
||||
setHasSelectedTag(false);
|
||||
setFrequentTagsLabel('Recent Tags');
|
||||
getRecentTags().then(names => setFrequentTags(names));
|
||||
const timeout = setTimeout(() => {
|
||||
if (filterInputRef.current && window.innerWidth >= 700) {
|
||||
filterInputRef.current.focus();
|
||||
}
|
||||
}, 100);
|
||||
return () => clearTimeout(timeout);
|
||||
}
|
||||
}, [open]);
|
||||
|
||||
const active = anyTag || (selectedTagNames && selectedTagNames.length > 0);
|
||||
|
||||
const handleOpen = (e) => {
|
||||
|
|
@ -433,8 +453,18 @@ export function AddTagsButton({
|
|||
return;
|
||||
}
|
||||
setLocalTags(newTags);
|
||||
setFilterText('');
|
||||
onAdd(name);
|
||||
onChange(newTags, null);
|
||||
if (!hasSelectedTag && newTags.length === 1) {
|
||||
setHasSelectedTag(true);
|
||||
getCooccurringTags(name).then(coTags => {
|
||||
if (coTags && coTags.length > 0) {
|
||||
setFrequentTags(coTags);
|
||||
setFrequentTagsLabel('Frequent Tags');
|
||||
}
|
||||
});
|
||||
}
|
||||
if (closeAfterLimit && newTags && limit && newTags.length >= limit) {
|
||||
setOpen(false);
|
||||
}
|
||||
|
|
@ -475,8 +505,12 @@ export function AddTagsButton({
|
|||
|
||||
const selectedTagsGroup = <TagsGroup tagNames={localTags} onClick={removeTag}/>;
|
||||
const unusedTags = _.difference(tagNames, localTags);
|
||||
const unusedTagsGroup = <TagsGroup tagNames={unusedTags} onClick={addTag}/>;
|
||||
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));
|
||||
|
||||
return <>
|
||||
<Button
|
||||
|
|
@ -496,8 +530,24 @@ export function AddTagsButton({
|
|||
|
||||
{localTags && localTags.length > 0 ? selectedTagsGroup : emptySelectedTags}
|
||||
|
||||
{visibleFrequentTags.length > 0 && <>
|
||||
<Divider/>
|
||||
<Header as='h5'>{frequentTagsLabel}</Header>
|
||||
<TagsGroup tagNames={visibleFrequentTags} onClick={addTag}/>
|
||||
</>}
|
||||
|
||||
<Divider/>
|
||||
|
||||
<Input
|
||||
ref={filterInputRef}
|
||||
placeholder='Filter tags...'
|
||||
value={filterText}
|
||||
onChange={(e) => setFilterText(e.target.value)}
|
||||
icon='search'
|
||||
fluid
|
||||
style={{marginBottom: '0.5em'}}
|
||||
/>
|
||||
|
||||
{unusedTags && unusedTags.length > 0 ? unusedTagsGroup : 'You have no tags'}
|
||||
</Modal.Content>
|
||||
<Modal.Actions>
|
||||
|
|
|
|||
|
|
@ -1498,6 +1498,26 @@ export async function getTags() {
|
|||
return response
|
||||
}
|
||||
|
||||
export async function getRecentTags() {
|
||||
const uri = `${API_URI}/tag/recent`;
|
||||
const response = await apiGet(uri);
|
||||
if (response.ok) {
|
||||
const body = await response.json();
|
||||
return body['tag_names'] || [];
|
||||
}
|
||||
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};
|
||||
|
||||
|
|
|
|||
|
|
@ -528,6 +528,24 @@ async def get_tags_request(_: Request):
|
|||
return json_response(dict(tags=tags_))
|
||||
|
||||
|
||||
@api_bp.get('/tag/recent')
|
||||
@openapi.definition(
|
||||
description='Get the most recently used tags',
|
||||
)
|
||||
async def get_recent_tags_request(_: Request):
|
||||
tag_names = tags.get_recent_tags()
|
||||
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_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(
|
||||
|
|
|
|||
|
|
@ -698,6 +698,46 @@ def get_tags() -> List[dict]:
|
|||
return tags
|
||||
|
||||
|
||||
def get_recent_tags(limit: int = 5) -> List[str]:
|
||||
with get_db_curs() as curs:
|
||||
curs.execute('''
|
||||
SELECT t.name FROM (
|
||||
SELECT tag_id, MAX(created_at) AS latest FROM (
|
||||
SELECT tag_id, created_at FROM tag_file
|
||||
UNION ALL
|
||||
SELECT tag_id, created_at FROM tag_zim
|
||||
) AS combined
|
||||
GROUP BY tag_id ORDER BY latest DESC LIMIT %(limit)s
|
||||
) AS recent
|
||||
JOIN tag t ON t.id = recent.tag_id ORDER BY recent.latest DESC
|
||||
''', dict(limit=limit))
|
||||
return [row['name'] for row in curs.fetchall()]
|
||||
|
||||
|
||||
def get_cooccurring_tags(tag_name: str, limit: int = 5) -> List[str]:
|
||||
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
|
||||
),
|
||||
zim_cooccur 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
|
||||
),
|
||||
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 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))
|
||||
return [row['name'] for row in curs.fetchall()]
|
||||
|
||||
|
||||
async def upsert_tag(session: Session, name: str, color: str, tag_id: int = None) -> Tag:
|
||||
if tag_id:
|
||||
tag = Tag.find_by_id(session, tag_id)
|
||||
|
|
|
|||
|
|
@ -645,3 +645,108 @@ async def test_tags_directory_disabled(test_session, test_directory, tag_factory
|
|||
# Now the tags directory should exist with the linked file.
|
||||
assert tags_dir.is_dir(), 'Tags directory should exist after enabling setting'
|
||||
assert (tags_dir / tag.name / 'video.mp4').is_file(), 'Video should be linked in tags directory'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_recent_tags(test_session, make_files_structure, tag_factory, video_bytes, image_bytes_factory,
|
||||
refresh_files):
|
||||
"""Recent tags are returned in order of most recently used."""
|
||||
from datetime import timedelta
|
||||
from wrolpi.dates import now as dates_now
|
||||
|
||||
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')
|
||||
|
||||
base = dates_now()
|
||||
tf1 = file_groups[0].add_tag(test_session, tag1.id)
|
||||
tf1.created_at = base - timedelta(hours=3)
|
||||
tf2 = file_groups[1].add_tag(test_session, tag2.id)
|
||||
tf2.created_at = base - timedelta(hours=1)
|
||||
tf3 = file_groups[2].add_tag(test_session, tag3.id)
|
||||
tf3.created_at = base
|
||||
test_session.commit()
|
||||
|
||||
result = tags.get_recent_tags(limit=5)
|
||||
assert result == ['gamma', 'beta', 'alpha']
|
||||
|
||||
# Limit is respected.
|
||||
result = tags.get_recent_tags(limit=2)
|
||||
assert result == ['gamma', 'beta']
|
||||
|
||||
|
||||
@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."""
|
||||
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')
|
||||
|
||||
# Both files tagged with alpha and beta.
|
||||
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_groups[1].add_tag(test_session, tag1.id)
|
||||
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.
|
||||
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."""
|
||||
await tag_factory('lonely')
|
||||
result = tags.get_cooccurring_tags('lonely')
|
||||
assert result == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_recent_tags_api(async_client, test_session, make_files_structure, tag_factory, video_bytes,
|
||||
refresh_files):
|
||||
"""The recent tags API endpoint returns tag names."""
|
||||
make_files_structure({'a.mp4': video_bytes})
|
||||
await refresh_files()
|
||||
fg = test_session.query(FileGroup).one()
|
||||
tag1 = await tag_factory('alpha')
|
||||
fg.add_tag(test_session, tag1.id)
|
||||
test_session.commit()
|
||||
|
||||
_, response = await async_client.get('/api/tag/recent')
|
||||
assert response.status == 200
|
||||
body = response.json
|
||||
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."""
|
||||
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')
|
||||
fg.add_tag(test_session, tag1.id)
|
||||
fg.add_tag(test_session, tag2.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']
|
||||
|
||||
# Unknown tag returns empty.
|
||||
_, response = await async_client.get('/api/tag/cooccurring/nonexistent')
|
||||
assert response.status == 200
|
||||
assert response.json['tag_names'] == []
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue