feat: use existing items to get recent tags instead of database query
Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
@@ -101,21 +101,36 @@ fn build_item_list(conn: &Connection, params: &ListQueryParams, columns: &[Colum
|
||||
html.push_str("<h1>Items</h1>");
|
||||
html.push_str("<p><a href=\"/swagger\">API Documentation</a></p>");
|
||||
|
||||
// Add recent tags section
|
||||
// Add recent tags section using the items we already have
|
||||
html.push_str("<h2>Recent Tags</h2>");
|
||||
let recent_tags_query = "
|
||||
SELECT DISTINCT t.name
|
||||
FROM tags t
|
||||
JOIN item_tags it ON t.id = it.tag_id
|
||||
ORDER BY it.id DESC
|
||||
LIMIT 20
|
||||
";
|
||||
let mut stmt = conn.prepare(recent_tags_query)?;
|
||||
let tag_iter = stmt.query_map([], |row| {
|
||||
Ok(row.get::<_, String>(0)?)
|
||||
})?;
|
||||
|
||||
let recent_tags: Vec<String> = tag_iter.collect::<Result<Vec<_>, _>>()?;
|
||||
// Collect all tags from the items, keeping track of their timestamps
|
||||
let mut all_tags_with_time: Vec<(String, chrono::DateTime<chrono::Utc>)> = Vec::new();
|
||||
for item in &sorted_items {
|
||||
if let Some(item_id) = item.id {
|
||||
if let Some(tags) = tags_map.get(&item_id) {
|
||||
for tag in tags {
|
||||
all_tags_with_time.push((tag.name.clone(), item.ts));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by timestamp descending and deduplicate
|
||||
all_tags_with_time.sort_by(|a, b| b.1.cmp(&a.1));
|
||||
|
||||
// Get unique tags in order of most recent appearance
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
let mut recent_tags = Vec::new();
|
||||
for (tag, _) in all_tags_with_time {
|
||||
if !seen.contains(&tag) {
|
||||
seen.insert(tag.clone());
|
||||
recent_tags.push(tag);
|
||||
if recent_tags.len() >= 20 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if recent_tags.is_empty() {
|
||||
html.push_str("<p>No tags found</p>");
|
||||
|
||||
Reference in New Issue
Block a user