Skip to content

Commit

Permalink
Split maker and taker coin in stats DB to ticker and platform #909 (#917
Browse files Browse the repository at this point in the history
)

* WIP.

* WIP.

* Split maker and taker coin in stats DB to ticker and platform.
  • Loading branch information
artemii235 authored Apr 23, 2021
1 parent 2b3db4f commit 4fb8d62
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 5 deletions.
3 changes: 3 additions & 0 deletions mm2src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ fn migration_2(ctx: &MmArc) -> Vec<(&'static str, Vec<String>)> {

fn migration_3() -> Vec<(&'static str, Vec<String>)> { vec![(stats_swaps::ADD_STARTED_AT_INDEX, vec![])] }

fn migration_4() -> Vec<(&'static str, Vec<String>)> { stats_swaps::add_and_split_tickers() }

fn statements_for_migration(ctx: &MmArc, current_migration: i64) -> Option<Vec<(&'static str, Vec<String>)>> {
match current_migration {
1 => Some(migration_1(ctx)),
2 => Some(migration_2(ctx)),
3 => Some(migration_3()),
4 => Some(migration_4()),
_ => None,
}
}
Expand Down
87 changes: 82 additions & 5 deletions mm2src/database/stats_swaps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,41 @@ const CREATE_STATS_SWAPS_TABLE: &str = "CREATE TABLE IF NOT EXISTS stats_swaps (

const INSERT_STATS_SWAP: &str = "INSERT INTO stats_swaps (
maker_coin,
maker_coin_ticker,
maker_coin_platform,
taker_coin,
uuid,
started_at,
finished_at,
maker_amount,
taker_coin_ticker,
taker_coin_platform,
uuid,
started_at,
finished_at,
maker_amount,
taker_amount,
is_success
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)";
) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)";

const ADD_SPLIT_TICKERS: &[&str] = &[
"ALTER TABLE stats_swaps ADD COLUMN maker_coin_ticker VARCHAR(255) NOT NULL DEFAULT '';",
"ALTER TABLE stats_swaps ADD COLUMN maker_coin_platform VARCHAR(255) NOT NULL DEFAULT '';",
"ALTER TABLE stats_swaps ADD COLUMN taker_coin_ticker VARCHAR(255) NOT NULL DEFAULT '';",
"ALTER TABLE stats_swaps ADD COLUMN taker_coin_platform VARCHAR(255) NOT NULL DEFAULT '';",
"UPDATE stats_swaps SET maker_coin_ticker = CASE instr(maker_coin, '-') \
WHEN 0 THEN maker_coin \
ELSE substr(maker_coin, 0, instr(maker_coin, '-')) \
END;",
"UPDATE stats_swaps SET maker_coin_platform = CASE instr(maker_coin, '-') \
WHEN 0 THEN '' \
ELSE substr(maker_coin, instr(maker_coin, '-') + 1) \
END;",
"UPDATE stats_swaps SET taker_coin_ticker = CASE instr(taker_coin, '-') \
WHEN 0 THEN taker_coin \
ELSE substr(taker_coin, 0, instr(taker_coin, '-')) \
END;",
"UPDATE stats_swaps SET taker_coin_platform = CASE instr(taker_coin, '-') \
WHEN 0 THEN '' \
ELSE substr(taker_coin, instr(taker_coin, '-') + 1) \
END;",
];

pub const ADD_STARTED_AT_INDEX: &str = "CREATE INDEX timestamp_index ON stats_swaps (started_at);";

Expand Down Expand Up @@ -106,6 +133,13 @@ pub fn create_and_fill_stats_swaps_from_json_statements(ctx: &MmArc) -> Vec<(&'s
result
}

fn split_coin(coin: &str) -> (String, String) {
let mut split = coin.split('-');
let ticker = split.next().expect("split returns empty string at least").into();
let platform = split.next().map_or("".into(), |platform| platform.into());
(ticker, platform)
}

fn insert_stats_maker_swap_sql(swap: &MakerSavedSwap) -> Option<(&'static str, Vec<String>)> {
let swap_data = match swap.swap_data() {
Ok(d) => d,
Expand All @@ -124,9 +158,17 @@ fn insert_stats_maker_swap_sql(swap: &MakerSavedSwap) -> Option<(&'static str, V
let is_success = swap
.is_success()
.expect("is_success can return error only when swap is not finished");

let (maker_coin_ticker, maker_coin_platform) = split_coin(&swap_data.maker_coin);
let (taker_coin_ticker, taker_coin_platform) = split_coin(&swap_data.taker_coin);

let params = vec![
swap_data.maker_coin.clone(),
maker_coin_ticker,
maker_coin_platform,
swap_data.taker_coin.clone(),
taker_coin_ticker,
taker_coin_platform,
swap.uuid.to_string(),
swap_data.started_at.to_string(),
finished_at,
Expand Down Expand Up @@ -155,9 +197,17 @@ fn insert_stats_taker_swap_sql(swap: &TakerSavedSwap) -> Option<(&'static str, V
let is_success = swap
.is_success()
.expect("is_success can return error only when swap is not finished");

let (maker_coin_ticker, maker_coin_platform) = split_coin(&swap_data.maker_coin);
let (taker_coin_ticker, taker_coin_platform) = split_coin(&swap_data.taker_coin);

let params = vec![
swap_data.maker_coin.clone(),
maker_coin_ticker,
maker_coin_platform,
swap_data.taker_coin.clone(),
taker_coin_ticker,
taker_coin_platform,
swap.uuid.to_string(),
swap_data.started_at.to_string(),
finished_at,
Expand Down Expand Up @@ -197,3 +247,30 @@ pub fn add_swap_to_index(conn: &Connection, swap: &SavedSwap) {
error!("Error {} on query {} with params {:?}", e, sql, params);
};
}

pub fn add_and_split_tickers() -> Vec<(&'static str, Vec<String>)> {
ADD_SPLIT_TICKERS.iter().map(|sql| (*sql, vec![])).collect()
}

#[test]
fn test_split_coin() {
let input = "";
let expected = ("".into(), "".into());
let actual = split_coin(input);
assert_eq!(expected, actual);

let input = "RICK";
let expected = ("RICK".into(), "".into());
let actual = split_coin(input);
assert_eq!(expected, actual);

let input = "RICK-BEP20";
let expected = ("RICK".into(), "BEP20".into());
let actual = split_coin(input);
assert_eq!(expected, actual);

let input = "RICK-";
let expected = ("RICK".into(), "".into());
let actual = split_coin(input);
assert_eq!(expected, actual);
}

0 comments on commit 4fb8d62

Please sign in to comment.