Skip to content

Commit

Permalink
fix: process order of sorting and picking
Browse files Browse the repository at this point in the history
  • Loading branch information
mirumirumi committed May 8, 2023
1 parent 937af9b commit be4906d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ pub trait Retrieve: Debug {
result.extend(klines);
}

let data = Pick::up(result, &args.pick);
let data = Order::sort(result, &args.order);
let data = Pick::up(data, &args.pick);

Ok(data)
}

Expand Down
7 changes: 2 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod pick;
mod types;
mod unit;

use crate::{args::*, guide::*, order::Order};
use crate::{args::*, guide::*};

fn main() -> Result<(), anyhow::Error> {
let _timer = time::Instant::now();
Expand Down Expand Up @@ -72,15 +72,12 @@ fn main() -> Result<(), anyhow::Error> {
args.valdate()?;

let mut args: ParsedArgs = args.try_into()?;

let data = args.exchange.retrieve(&mut args.clone())?;
let data = Order::sort(data, &args.order);
let data = args.output.format(&data);

if data.is_empty() {
println!("No data");
} else {
println!("{}", data);
println!("{}", args.output.format(&data));
}

// if cfg!(debug_assertions) {
Expand Down
35 changes: 33 additions & 2 deletions src/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::cmp::Ordering;

use clap::ValueEnum;

use crate::{pick::*, types::*};
use crate::{exchange::*, pick::*, types::*};

#[derive(
Debug, Clone, ValueEnum, strum::Display, strum::IntoStaticStr, strum::EnumIter, strum::AsRefStr,
Expand All @@ -14,7 +14,38 @@ pub enum Order {

impl Order {
/// Use to print finally result
pub fn sort(mut data: Vec<Row>, order: &Self) -> Vec<Row> {
pub fn sort(mut data: Vec<Kline>, order: &Self) -> Vec<Kline> {
if data.len() < 2 {
return data;
}

let compare = |a: &Kline, b: &Kline| {
let unixtime_a = a.unixtime_msec;
let unixtime_b = b.unixtime_msec;
unixtime_a
.partial_cmp(&unixtime_b)
.unwrap_or(Ordering::Equal)
};

match order {
Self::Asc => {
data.sort_unstable_by(compare);
}
Self::Desc => {
data.sort_unstable_by(compare);
data.reverse();
}
}

data
}

#[allow(dead_code)]
fn sort_old(mut data: Vec<Row>, order: &Self) -> Vec<Row> {
if data.len() < 2 {
return data;
}

let compare = |a: &Row, b: &Row| {
let unixtime_a = a.iter().flat_map(|map| map.get(&Pick::T)).next().unwrap();
let unixtime_b = b.iter().flat_map(|map| map.get(&Pick::T)).next().unwrap();
Expand Down

0 comments on commit be4906d

Please sign in to comment.