|
|
@@ -4,6 +4,7 @@ use std::sync::{Arc};
|
|
|
use std::sync::atomic::AtomicBool;
|
|
|
use std::time::Duration;
|
|
|
use lazy_static::lazy_static;
|
|
|
+use rust_decimal::prelude::ToPrimitive;
|
|
|
use tokio::sync::Mutex;
|
|
|
use tokio::time::Instant;
|
|
|
use tracing::info;
|
|
|
@@ -12,6 +13,7 @@ use exchanges::response_base::ResponseData;
|
|
|
use standard::{Depth, OrderBook};
|
|
|
use standard::exchange::ExchangeEnum;
|
|
|
use standard::exchange_struct_handler::ExchangeStructHandler;
|
|
|
+use crate::json_db_utils::{generate_file_path, write_to_file};
|
|
|
|
|
|
type DepthMap = HashMap<String, Vec<Depth>>;
|
|
|
|
|
|
@@ -42,8 +44,6 @@ pub async fn run_listener(is_shutdown_arc: Arc<AtomicBool>) {
|
|
|
|
|
|
// 读取数据
|
|
|
pub async fn data_listener(response: ResponseData) {
|
|
|
- let mut depth_map = DEPTH_MAP.lock().await;
|
|
|
-
|
|
|
if response.code != 200 {
|
|
|
return;
|
|
|
}
|
|
|
@@ -53,9 +53,7 @@ pub async fn data_listener(response: ResponseData) {
|
|
|
"futures.order_book" => {
|
|
|
let depth = ExchangeStructHandler::order_book_handle(ExchangeEnum::GateSwap, &response);
|
|
|
|
|
|
- depth_map.entry(depth.symbol.clone())
|
|
|
- .or_insert_with(Vec::new)
|
|
|
- .push(depth);
|
|
|
+ update_depth(&depth).await;
|
|
|
},
|
|
|
// 订单流数据
|
|
|
"futures.trades" => {
|
|
|
@@ -69,12 +67,43 @@ pub async fn data_listener(response: ResponseData) {
|
|
|
info!("48 未知的数据类型: {:?}", response)
|
|
|
}
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
+// 更新深度数据
|
|
|
+pub async fn update_depth(new_depth: &Depth) {
|
|
|
+ let mut depth_map = DEPTH_MAP.lock().await;
|
|
|
+
|
|
|
+ // 估算当前depth的内存占用率
|
|
|
let mut last_print_time = LAST_PRINT_TIME.lock().await;
|
|
|
- if last_print_time.elapsed() >= Duration::from_secs(60) {
|
|
|
+ if last_print_time.elapsed() >= Duration::from_secs(10) {
|
|
|
estimate_depth_map_memory_usage(&depth_map);
|
|
|
*last_print_time = Instant::now();
|
|
|
}
|
|
|
+
|
|
|
+ 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.time.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!(?depths_json);
|
|
|
+ info!(?path);
|
|
|
+
|
|
|
+ write_to_file(depths_json, path);
|
|
|
+
|
|
|
+ depths.clear();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ depths.push(new_depth.clone());
|
|
|
+ } else {
|
|
|
+ // 如果该symbol不存在,则创建新的Vec并添加depth
|
|
|
+ depth_map.insert(new_depth.symbol.clone(), vec![new_depth.clone()]);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
pub fn estimate_depth_map_memory_usage(depth_map: &DepthMap) {
|