Skip to content

Commit

Permalink
Relay sorting by score
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedilger committed Dec 22, 2024
1 parent 647ed42 commit a894a6c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
23 changes: 18 additions & 5 deletions gossip-bin/src/ui/relays/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::{
use eframe::egui;
use egui::{Context, Ui};
use egui_winit::egui::{vec2, Id, RichText};
use gossip_lib::{comms::ToOverlordMessage, Relay, GLOBALS};
use gossip_lib::{comms::ToOverlordMessage, Relay, ScoreFactors, GLOBALS};
use nostr_types::RelayUrl;

mod active;
Expand Down Expand Up @@ -76,6 +76,7 @@ impl RelayUi {
pub(super) enum RelaySorting {
#[default]
Default,
Score,
Rank,
Name,
WriteRelays,
Expand All @@ -89,6 +90,7 @@ impl RelaySorting {
pub fn get_name(&self) -> &str {
match self {
RelaySorting::Default => "Default",
RelaySorting::Score => "Score",
RelaySorting::Rank => "Rank",
RelaySorting::Name => "Name",
RelaySorting::WriteRelays => "Write Relays",
Expand Down Expand Up @@ -495,6 +497,11 @@ pub(super) fn relay_sort_combo(app: &mut GossipUi, ui: &mut Ui) {
RelaySorting::Default,
RelaySorting::Default.get_name(),
);
ui.selectable_value(
&mut app.relays.sort,
RelaySorting::Score,
RelaySorting::Score.get_name(),
);
ui.selectable_value(
&mut app.relays.sort,
RelaySorting::Rank,
Expand Down Expand Up @@ -611,10 +618,16 @@ pub(super) fn relay_filter_combo(app: &mut GossipUi, ui: &mut Ui) {
#[rustfmt::skip]
pub(super) fn sort_relay(rui: &RelayUi, a: &Relay, b: &Relay) -> Ordering {
match rui.sort {
RelaySorting::Default => b.get_usage_bits_for_sorting().cmp(&a.get_usage_bits_for_sorting())
.then(b.is_good_for_advertise().cmp(&a.is_good_for_advertise()))
.then(b.rank.cmp(&a.rank))
.then(a.url.cmp(&b.url)),
RelaySorting::Default => {
let sf = ScoreFactors { connected: false, success_count: true };
b.get_usage_bits_for_sorting().cmp(&a.get_usage_bits_for_sorting())
.then(b.adjusted_score(sf).partial_cmp(&a.adjusted_score(sf)).unwrap_or(Ordering::Equal))
.then(a.url.cmp(&b.url))
},
RelaySorting::Score => {
let sf = ScoreFactors { connected: false, success_count: true };
b.adjusted_score(sf).partial_cmp(&a.adjusted_score(sf)).unwrap_or(Ordering::Equal)
},
RelaySorting::Rank => b.rank.cmp(&a.rank)
.then(b.get_usage_bits_for_sorting().cmp(&a.get_usage_bits_for_sorting()))
.then(b.is_good_for_advertise().cmp(&a.is_good_for_advertise()))
Expand Down
2 changes: 1 addition & 1 deletion gossip-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub use profile::Profile;
mod relationship;

pub mod relay;
pub use relay::Relay;
pub use relay::{Relay, ScoreFactors};

pub mod relay_picker;
pub use relay_picker::RelayPicker;
Expand Down
2 changes: 1 addition & 1 deletion gossip-lib/src/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

/// Relay type, aliased to the latest version
pub type Relay = crate::storage::types::Relay3;
pub use crate::storage::types::ScoreFactors;

use crate::error::{Error, ErrorKind};
use crate::person_relay::PersonRelay;
use crate::storage::types::ScoreFactors;
use crate::GLOBALS;
use nostr_types::{Event, EventKind, Id, PublicKey, RelayUrl, RelayUsage, Unixtime};

Expand Down

0 comments on commit a894a6c

Please sign in to comment.