|
|
@@ -3,9 +3,10 @@ use std::str::FromStr;
|
|
|
use actix_web::HttpResponse;
|
|
|
use chrono::Utc;
|
|
|
use rust_decimal::Decimal;
|
|
|
+use rust_decimal::prelude::FromPrimitive;
|
|
|
use rust_decimal_macros::dec;
|
|
|
use serde_json::{json, Value};
|
|
|
-use crate::db_connector::{get_records_json, get_symbols_json};
|
|
|
+use crate::db_connector::{get_records_map, get_symbols_json, get_trades_count};
|
|
|
use crate::server::Response;
|
|
|
|
|
|
fn get_public_symbols(symbols_map: &Value) -> Vec<String> {
|
|
|
@@ -85,32 +86,45 @@ pub async fn get_final_symbols(mode: &str, exchanges: &Vec<String>, minute_time_
|
|
|
|
|
|
// 3. 获取它们的k线数据,注意时间范围以及保存形式。
|
|
|
// {
|
|
|
- // "symbol1": {
|
|
|
- // "exchange1": [k1,k2,...]
|
|
|
- // "exchange2": [k1,k2,...]
|
|
|
- // },
|
|
|
- // "symbol2": {
|
|
|
- // "exchange1": [k1,k2,...]
|
|
|
- // "exchange2": [k1,k2,...]
|
|
|
- // },
|
|
|
+ // "exchange1": {
|
|
|
+ // "symbol1": [k1,k2,...]
|
|
|
+ // "symbol2": [k1,k2,...]
|
|
|
+ // },
|
|
|
+ // "exchange2": {
|
|
|
+ // "symbol1": [k1,k2,...]
|
|
|
+ // "symbol2": [k1,k2,...]
|
|
|
+ // },
|
|
|
// }
|
|
|
let end_time = Utc::now().timestamp_millis();
|
|
|
let start_time = end_time - minute_time_range * 60 * 1000;
|
|
|
let mut records_map = json!({});
|
|
|
- for symbol in &symbols {
|
|
|
- records_map[symbol.clone()] = json!({});
|
|
|
- for exchange in exchanges {
|
|
|
- let db_response = get_records_json(exchange.as_str(), symbol.as_str(), start_time, end_time).await;
|
|
|
+ for exchange in exchanges {
|
|
|
+ let db_response = get_records_map(exchange.as_str(), start_time, end_time).await;
|
|
|
|
|
|
- // 对数据库返回的数据进行容错处理
|
|
|
- if db_response.code == 200 {
|
|
|
- let records = db_response.data;
|
|
|
+ // 对数据库返回的数据进行容错处理
|
|
|
+ if db_response.code == 200 {
|
|
|
+ let records = db_response.data;
|
|
|
|
|
|
- records_map[symbol.clone()][exchange] = records.clone();
|
|
|
- } else {
|
|
|
- let json_string = serde_json::to_string(&db_response).unwrap();
|
|
|
- return HttpResponse::Ok().content_type("application/json").body(json_string);
|
|
|
- }
|
|
|
+ records_map[exchange] = records.clone();
|
|
|
+ } else {
|
|
|
+ let json_string = serde_json::to_string(&db_response).unwrap();
|
|
|
+ return HttpResponse::Ok().content_type("application/json").body(json_string);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3.5 获取交易次数
|
|
|
+ let mut trades_count_map = json!({});
|
|
|
+ for exchange in exchanges {
|
|
|
+ let db_response = get_trades_count(exchange.as_str(), start_time, end_time).await;
|
|
|
+
|
|
|
+ // 对数据库返回的数据进行容错处理
|
|
|
+ if db_response.code == 200 {
|
|
|
+ let records = db_response.data;
|
|
|
+
|
|
|
+ trades_count_map[exchange] = records.clone();
|
|
|
+ } else {
|
|
|
+ let json_string = serde_json::to_string(&db_response).unwrap();
|
|
|
+ return HttpResponse::Ok().content_type("application/json").body(json_string);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -145,20 +159,31 @@ pub async fn get_final_symbols(mode: &str, exchanges: &Vec<String>, minute_time_
|
|
|
for symbol in &symbols {
|
|
|
let mut rise = json!({});
|
|
|
let mut volume = json!({});
|
|
|
+ let mut tc = json!({});
|
|
|
let mut total_volume_by_symbol = Decimal::ZERO;
|
|
|
+ let mut total_count_by_symbol = Decimal::ZERO;
|
|
|
for exchange in exchanges {
|
|
|
- rise[exchange] = json!(calc_rise_percentage(records_map[symbol][exchange].clone()));
|
|
|
- let volume_by_exchange = calc_total_volume(records_map[symbol][exchange].clone());
|
|
|
- volume[exchange] = json!(volume_by_exchange);
|
|
|
+ // 交易次数处理
|
|
|
+ let count_by_exchange = Decimal::from_i64(trades_count_map[exchange][symbol].as_i64().unwrap()).unwrap();
|
|
|
+ tc[exchange] = json!(count_by_exchange);
|
|
|
+ total_count_by_symbol = total_count_by_symbol + count_by_exchange;
|
|
|
|
|
|
+ // 上涨幅度处理
|
|
|
+ rise[exchange] = json!(calc_rise_percentage(records_map[exchange][symbol].clone()));
|
|
|
+
|
|
|
+ // 交易量处理
|
|
|
+ let volume_by_exchange = calc_total_volume(records_map[exchange][symbol].clone());
|
|
|
+ volume[exchange] = json!(volume_by_exchange);
|
|
|
total_volume_by_symbol = total_volume_by_symbol + volume_by_exchange;
|
|
|
}
|
|
|
volume["total"] = json!(total_volume_by_symbol);
|
|
|
+ tc["total"] = json!(total_count_by_symbol);
|
|
|
|
|
|
let value = json!({
|
|
|
"symbol": symbol.clone(),
|
|
|
"rise": rise,
|
|
|
- "volume": volume
|
|
|
+ "volume": volume,
|
|
|
+ "tc": tc
|
|
|
});
|
|
|
|
|
|
temp_value.as_array_mut().unwrap().push(value);
|