From d704cc91fa713e838781a80867439da7f1d40001 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 30 Oct 2021 10:37:03 +0200 Subject: [PATCH] Add sorting of namespaces by numeric value if it can be parsed Signed-off-by: trivernis --- .../mediarepo-database/src/lib.rs | 1 + .../mediarepo-database/src/queries/mod.rs | 1 + .../mediarepo-database/src/queries/tags.rs | 43 +++++++++++++++++++ .../mediarepo-socket/src/namespaces/files.rs | 28 ++++++++++-- 4 files changed, 69 insertions(+), 4 deletions(-) diff --git a/mediarepo-daemon/mediarepo-database/src/lib.rs b/mediarepo-daemon/mediarepo-database/src/lib.rs index 2361700..e716e45 100644 --- a/mediarepo-daemon/mediarepo-database/src/lib.rs +++ b/mediarepo-daemon/mediarepo-database/src/lib.rs @@ -3,6 +3,7 @@ use sea_orm::{Database, DatabaseConnection}; use sqlx::migrate::MigrateDatabase; pub mod entities; +pub mod queries; /// Connects to the database, runs migrations and returns the RepoDatabase wrapper type pub async fn get_database>(uri: S) -> RepoDatabaseResult { diff --git a/mediarepo-daemon/mediarepo-database/src/queries/mod.rs b/mediarepo-daemon/mediarepo-database/src/queries/mod.rs index e69de29..cb4849d 100644 --- a/mediarepo-daemon/mediarepo-database/src/queries/mod.rs +++ b/mediarepo-daemon/mediarepo-database/src/queries/mod.rs @@ -0,0 +1 @@ +pub mod tags; diff --git a/mediarepo-daemon/mediarepo-database/src/queries/tags.rs b/mediarepo-daemon/mediarepo-database/src/queries/tags.rs index e69de29..e96755d 100644 --- a/mediarepo-daemon/mediarepo-database/src/queries/tags.rs +++ b/mediarepo-daemon/mediarepo-database/src/queries/tags.rs @@ -0,0 +1,43 @@ +use crate::entities::hash; +use crate::entities::hash_tag; +use crate::entities::namespace; +use crate::entities::tag; +use sea_orm::prelude::*; +use sea_orm::sea_query::Query; +use sea_orm::{DatabaseConnection, JoinType}; +use std::collections::HashMap; +/* +pub async fn get_hashes_with_namespaced_tags( + db: DatabaseConnection, + hash_ids: Vec, +) -> HashMap> { + Query::select() + .expr(hash_tag::Column::HashId) + .expr(tag::Column::Name) + .expr(namespace::Column::Name) + .from(tag::Entity) + .join( + JoinType::LeftJoin, + hash_tag::Entity, + hash_tag::Column::TagId.eq(tag::Column::Id), + ) + .join( + JoinType::InnerJoin, + namespace::Entity, + tag::Column::NamespaceId.eq(namespace::Column::Id), + ) + .build(&db) + .await?; + let tags: Vec<(tag::Model, Option)> = tag::Entity::find() + .find_also_related(namespace::Entity) + .join(JoinType::LeftJoin, hash_tag::Relation::Tag.def().rev()) + .join(JoinType::InnerJoin, hash_tag::Relation::Hash.def()) + .filter(hash::Column::Id.eq(self.hash.id)) + .all(&self.db) + .await?; + let tags = tags + .into_iter() + .map(|(tag, namespace)| Tag::new(self.db.clone(), tag, namespace)) + .collect(); +} +*/ diff --git a/mediarepo-daemon/mediarepo-socket/src/namespaces/files.rs b/mediarepo-daemon/mediarepo-socket/src/namespaces/files.rs index e03ad82..0721fed 100644 --- a/mediarepo-daemon/mediarepo-socket/src/namespaces/files.rs +++ b/mediarepo-daemon/mediarepo-socket/src/namespaces/files.rs @@ -201,12 +201,22 @@ fn compare_files( expression: &Vec, ) -> Ordering { let cmp_date = compare::natural(); + for sort_key in expression { let ordering = match sort_key { - SortKey::Namespace(namespace) => adjust_for_dir( - compare_opts(nsp_a.get(&namespace.tag), nsp_b.get(&namespace.tag)), - &namespace.direction, - ), + SortKey::Namespace(namespace) => { + let tag_a = nsp_a.get(&namespace.tag); + let tag_b = nsp_b.get(&namespace.tag); + + if let (Some(a), Some(b)) = ( + tag_a.and_then(|a| a.parse::().ok()), + tag_b.and_then(|b| b.parse::().ok()), + ) { + adjust_for_dir(compare_f32(a, b), &namespace.direction) + } else { + adjust_for_dir(compare_opts(tag_a, tag_b), &namespace.direction) + } + } SortKey::FileName(direction) => adjust_for_dir( compare_opts(file_a.name().clone(), file_b.name().clone()), direction, @@ -266,6 +276,16 @@ fn compare_opts(opt_a: Option, opt_b: Option) -> Ordering } } +fn compare_f32(a: f32, b: f32) -> Ordering { + if a > b { + Ordering::Greater + } else if b > a { + Ordering::Less + } else { + Ordering::Equal + } +} + fn adjust_for_dir(ordering: Ordering, direction: &SortDirection) -> Ordering { if *direction == SortDirection::Descending { ordering.reverse()