summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md7
-rw-r--r--src/birka-cli.rs5
-rw-r--r--src/birka-web.rs2
-rw-r--r--src/database.rs19
4 files changed, 25 insertions, 8 deletions
diff --git a/README.md b/README.md
index 3a0c7f2..9d24c59 100644
--- a/README.md
+++ b/README.md
@@ -89,6 +89,13 @@ Removing an image can be done with 'remove'.
$ birka-cli remove Cat.jpeg
```
+Getting the tags in the database:
+
+```sh
+$ birka-cli tags
+...
+```
+
### Web-Interface (API)
The web interface for бирка-тян exposes a RESTful API for remotely interacting
diff --git a/src/birka-cli.rs b/src/birka-cli.rs
index abbd0d0..4a8681f 100644
--- a/src/birka-cli.rs
+++ b/src/birka-cli.rs
@@ -108,6 +108,11 @@ fn main() {
println!("{}", file.path);
}
}
+ "tags" => {
+ for (count, tag) in tb.top_tags(None).unwrap() {
+ println!("{}: {}", count, tag);
+ }
+ }
_ => {
eprintln!("Unknown action '{}'", action);
std::process::exit(1);
diff --git a/src/birka-web.rs b/src/birka-web.rs
index a3bed1d..cd68923 100644
--- a/src/birka-web.rs
+++ b/src/birka-web.rs
@@ -277,7 +277,7 @@ fn display_posts(conn: SiteState, query: String, last: Option<i64>) -> Result<Te
.lock()
.map_err(|_| anyhow!("Could not lock database."))?
.tb;
- tb.top_tags(20)?
+ tb.top_tags(Some(20))?
.iter()
.map(|tup| TagResult {
name: tup.1.clone(),
diff --git a/src/database.rs b/src/database.rs
index 5591d6a..c7c84a0 100644
--- a/src/database.rs
+++ b/src/database.rs
@@ -357,18 +357,23 @@ impl TagDatabase {
.collect())
}
- pub fn top_tags(&self, n: i64) -> Result<Vec<(i64, String)>> {
+ pub fn top_tags(&self, n: Option<i64>) -> Result<Vec<(i64, String)>> {
Ok(self
.0
- .prepare(
+ .prepare(&format!(
"SELECT tags.name, COUNT(mapping.tag) AS tag_count
FROM mapping
INNER JOIN tags ON tags.id = mapping.tag
GROUP BY tag
ORDER BY tag_count DESC
- LIMIT ?",
- )?
- .query_map(params![n], |row| -> rusqlite::Result<(i64, String)> {
+ {limit}",
+ limit = if let Some(count) = n {
+ format!("LIMIT {}", count)
+ } else {
+ "".into()
+ }
+ ))?
+ .query_map(params![], |row| -> rusqlite::Result<(i64, String)> {
Ok((row.get(1)?, row.get(0)?))
})?
.filter_map(|s| s.ok())
@@ -599,7 +604,7 @@ mod tests {
tb.tag_file(ids[1].id, &vec!["2"]).unwrap();
tb.tag_file(ids[2].id, &vec!["1"]).unwrap();
tb.tag_file(ids[2].id, &vec!["2"]).unwrap();
- assert_eq!(tb.top_tags(2).unwrap().len(), 2);
- assert_eq!(tb.top_tags(2).unwrap()[0].0, 2);
+ assert_eq!(tb.top_tags(Some(2)).unwrap().len(), 2);
+ assert_eq!(tb.top_tags(Some(2)).unwrap()[0].0, 2);
}
}