mirror of
https://github.com/Dasemu/Synapse.git
synced 2026-11-26 23:36:20 +00:00
feat(core): tag catalog counts explicit, type and hub sources per tag
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2b079a8d74
commit
4fb3b39c03
3 files changed
+45
-12
No files matched your search
@@ -17,7 +17,7 @@ use std::collections::{HashMap, HashSet};
|
|||||||
pub const MAX_TAG_CHARS: usize = 48;
|
pub const MAX_TAG_CHARS: usize = 48;
|
||||||
|
|
||||||
/// A tag with the number of pages filed under it (explicitly or through a type).
|
/// A tag with the number of pages filed under it (explicitly or through a type).
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||||
pub struct TagInfo {
|
pub struct TagInfo {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub count: u32,
|
pub count: u32,
|
||||||
@@ -25,6 +25,12 @@ pub struct TagInfo {
|
|||||||
pub suggested: bool,
|
pub suggested: bool,
|
||||||
/// Emoji of the type ([`crate::TypeDef::icon`]; types only).
|
/// Emoji of the type ([`crate::TypeDef::icon`]; types only).
|
||||||
pub icon: Option<String>,
|
pub icon: Option<String>,
|
||||||
|
/// Pages listing it among their explicit tags (`tag_catalog` only).
|
||||||
|
pub explicit: u32,
|
||||||
|
/// Pages having it as their type (`tag_catalog` only).
|
||||||
|
pub typed: u32,
|
||||||
|
/// Pages carrying it because their hub has this title (`tag_catalog` only).
|
||||||
|
pub from_hub: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Cleans a tag name: leading `#` dropped, whitespace collapsed. `None` when nothing is left.
|
/// Cleans a tag name: leading `#` dropped, whitespace collapsed. `None` when nothing is left.
|
||||||
@@ -137,7 +143,7 @@ impl Graph {
|
|||||||
/// Every tag in use (listed, implied by a type or by the page's hub): spelling of its first
|
/// Every tag in use (listed, implied by a type or by the page's hub): spelling of its first
|
||||||
/// page, with its page count. Most used first, then by name.
|
/// page, with its page count. Most used first, then by name.
|
||||||
pub fn tag_catalog(&self) -> Result<Vec<TagInfo>> {
|
pub fn tag_catalog(&self) -> Result<Vec<TagInfo>> {
|
||||||
let mut used: HashMap<String, (String, u32)> = HashMap::new();
|
let mut used: HashMap<String, TagInfo> = HashMap::new();
|
||||||
let mut sources = schema::tag_sources(self.conn(), None)?;
|
let mut sources = schema::tag_sources(self.conn(), None)?;
|
||||||
sources.sort_by(|a, b| a.id.cmp(&b.id));
|
sources.sort_by(|a, b| a.id.cmp(&b.id));
|
||||||
for src in sources {
|
for src in sources {
|
||||||
@@ -145,19 +151,19 @@ impl Graph {
|
|||||||
for t in src.names() {
|
for t in src.names() {
|
||||||
let key = title_key(t);
|
let key = title_key(t);
|
||||||
if seen.insert(key.clone()) {
|
if seen.insert(key.clone()) {
|
||||||
used.entry(key).or_insert((t.clone(), 0)).1 += 1;
|
let info = used.entry(key.clone()).or_insert_with(|| TagInfo {
|
||||||
|
name: t.clone(),
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
|
info.count += 1;
|
||||||
|
let is = |n: &String| title_key(n) == key;
|
||||||
|
info.explicit += u32::from(src.tags.iter().any(is));
|
||||||
|
info.typed += u32::from(src.types.iter().take(1).any(is));
|
||||||
|
info.from_hub += u32::from(src.hub.iter().any(is));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut out: Vec<TagInfo> = used
|
let mut out: Vec<TagInfo> = used.into_values().collect();
|
||||||
.into_values()
|
|
||||||
.map(|(name, count)| TagInfo {
|
|
||||||
name,
|
|
||||||
count,
|
|
||||||
suggested: false,
|
|
||||||
icon: None,
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
out.sort_by(|a, b| {
|
out.sort_by(|a, b| {
|
||||||
b.count
|
b.count
|
||||||
.cmp(&a.count)
|
.cmp(&a.count)
|
||||||
|
|||||||
@@ -116,6 +116,7 @@ impl Graph {
|
|||||||
count: *count,
|
count: *count,
|
||||||
suggested: suggested(k),
|
suggested: suggested(k),
|
||||||
icon: live_icon(k),
|
icon: live_icon(k),
|
||||||
|
..Default::default()
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
out.sort_by(|a, b| {
|
out.sort_by(|a, b| {
|
||||||
@@ -135,6 +136,7 @@ impl Graph {
|
|||||||
count: 0,
|
count: 0,
|
||||||
suggested: false,
|
suggested: false,
|
||||||
icon: d.icon.clone(),
|
icon: d.icon.clone(),
|
||||||
|
..Default::default()
|
||||||
}));
|
}));
|
||||||
for s in SUGGESTED_TYPES {
|
for s in SUGGESTED_TYPES {
|
||||||
let k = title_key(s);
|
let k = title_key(s);
|
||||||
@@ -145,6 +147,7 @@ impl Graph {
|
|||||||
count: 0,
|
count: 0,
|
||||||
suggested: true,
|
suggested: true,
|
||||||
icon: def.and_then(|d| d.icon.clone()),
|
icon: def.and_then(|d| d.icon.clone()),
|
||||||
|
..Default::default()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1089,6 +1089,30 @@ fn renaming_a_type_keeps_its_icon_and_removing_drops_it() {
|
|||||||
assert_eq!(type_row(&g, "lugar"), Some(("lugar".into(), 1, None)));
|
assert_eq!(type_row(&g, "lugar"), Some(("lugar".into(), 1, None)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tag_catalog_says_where_each_tag_comes_from() {
|
||||||
|
let (_d, g) = graph();
|
||||||
|
let h = hub(&g, "Anime");
|
||||||
|
let a = page(&g, "A", &["x"]);
|
||||||
|
let b = page(&g, "B", &["x"]);
|
||||||
|
g.add_to_hub(a, h).unwrap();
|
||||||
|
g.add_page_tag(a, "idea").unwrap();
|
||||||
|
g.set_page_type(b, Some("Idea")).unwrap();
|
||||||
|
g.add_page_tag(b, "idea").unwrap();
|
||||||
|
let cat = g.tag_catalog().unwrap();
|
||||||
|
let get = |n: &str| cat.iter().find(|t| t.name.eq_ignore_ascii_case(n)).unwrap();
|
||||||
|
let idea = get("idea");
|
||||||
|
assert_eq!(
|
||||||
|
(idea.count, idea.explicit, idea.typed, idea.from_hub),
|
||||||
|
(2, 2, 1, 0)
|
||||||
|
);
|
||||||
|
let anime = get("anime");
|
||||||
|
assert_eq!(
|
||||||
|
(anime.count, anime.explicit, anime.typed, anime.from_hub),
|
||||||
|
(1, 0, 0, 1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn type_definitions_sync_last_writer_wins() {
|
fn type_definitions_sync_last_writer_wins() {
|
||||||
let (_a, a) = graph();
|
let (_a, a) = graph();
|
||||||
|
|||||||
Reference in new issue
Block a user