|
|
@@ -1,11 +1,15 @@
|
|
|
+use std::collections::HashMap;
|
|
|
use std::str::FromStr;
|
|
|
use rust_decimal::Decimal;
|
|
|
use rust_decimal::prelude::ToPrimitive;
|
|
|
use tokio::sync::MutexGuard;
|
|
|
-use standard::{Record, SpecialTrade, Trade};
|
|
|
-use crate::gate_usdt_swap_data_listener::{RecordMap, TradeMap};
|
|
|
+use standard::{Depth, Record, SpecialDepth, SpecialTrade, Trade};
|
|
|
use crate::json_db_utils::{generate_file_path, minute_to_date, write_to_file};
|
|
|
|
|
|
+pub type DepthMap = HashMap<String, Vec<SpecialDepth>>;
|
|
|
+pub type TradeMap = HashMap<String, Vec<SpecialTrade>>;
|
|
|
+pub type RecordMap = HashMap<String, Record>;
|
|
|
+
|
|
|
// 更新订单流数据
|
|
|
pub async fn update_trade(new_trade: &Trade, mut trades_map: MutexGuard<'_, TradeMap>, exchange: &str) {
|
|
|
if let Some(trades) = trades_map.get_mut(new_trade.symbol.as_str()) {
|
|
|
@@ -58,31 +62,27 @@ pub async fn update_record(new_record: &Record, mut records_map: MutexGuard<'_,
|
|
|
records_map.insert(new_record.symbol.clone(), new_record.clone());
|
|
|
}
|
|
|
|
|
|
-// // 更新深度数据
|
|
|
-// pub async fn update_depth(new_depth: &Depth) {
|
|
|
-// let mut depth_map = DEPTH_MAP.lock().await;
|
|
|
-//
|
|
|
-// if let Some(depths) = depth_map.get_mut(new_depth.symbol.as_str()) {
|
|
|
-// if let Some(last_depth) = depths.last() {
|
|
|
-// let last_depth_minutes = last_depth.t.to_i64().unwrap() / 60000; // 将毫秒转换成分钟数
|
|
|
-// let new_depth_minutes = new_depth.time.to_i64().unwrap() / 60000; // 同上
|
|
|
-//
|
|
|
-// // 如果分钟数不同,则清空列表并添加新的depth
|
|
|
-// if last_depth_minutes != new_depth_minutes {
|
|
|
-// let depths_json = serde_json::to_string(depths).unwrap();
|
|
|
-// let path = generate_file_path("gate_usdt_swap",
|
|
|
-// new_depth.symbol.as_str(), "order_book", last_depth_minutes);
|
|
|
-//
|
|
|
-// info!(?path);
|
|
|
-//
|
|
|
-// write_to_file(depths_json, path);
|
|
|
-//
|
|
|
-// depths.clear();
|
|
|
-// }
|
|
|
-// }
|
|
|
-// depths.push(SpecialDepth::new(&new_depth));
|
|
|
-// } else {
|
|
|
-// // 如果该symbol不存在,则创建新的Vec并添加depth
|
|
|
-// depth_map.insert(new_depth.symbol.clone(), vec![SpecialDepth::new(&new_depth)]);
|
|
|
-// }
|
|
|
-// }
|
|
|
+// 更新深度数据
|
|
|
+pub async fn update_depth(new_depth: &Depth, mut depth_map: MutexGuard<'_, DepthMap>, exchange: &str) {
|
|
|
+ if let Some(depths) = depth_map.get_mut(new_depth.symbol.as_str()) {
|
|
|
+ if let Some(last_depth) = depths.last() {
|
|
|
+ let last_depth_minutes = last_depth.t.to_i64().unwrap() / 60000; // 将毫秒转换成分钟数
|
|
|
+ let new_depth_minutes = new_depth.time.to_i64().unwrap() / 60000; // 同上
|
|
|
+
|
|
|
+ // 如果分钟数不同,则清空列表并添加新的depth
|
|
|
+ if last_depth_minutes != new_depth_minutes {
|
|
|
+ let depths_json = serde_json::to_string(depths).unwrap();
|
|
|
+ let date_str = minute_to_date(last_depth_minutes);
|
|
|
+ let path = generate_file_path(exchange, date_str.as_str(), new_depth.symbol.as_str(), "order_book", last_depth_minutes);
|
|
|
+
|
|
|
+ write_to_file(depths_json, path).await;
|
|
|
+
|
|
|
+ depths.clear();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ depths.push(SpecialDepth::new(&new_depth));
|
|
|
+ } else {
|
|
|
+ // 如果该symbol不存在,则创建新的Vec并添加depth
|
|
|
+ depth_map.insert(new_depth.symbol.clone(), vec![SpecialDepth::new(&new_depth)]);
|
|
|
+ }
|
|
|
+}
|