Ver código fonte

1. 加入交易次数
2. 优化查询速度

skyffire 1 ano atrás
pai
commit
314440b466
3 arquivos alterados com 122 adições e 26 exclusões
  1. 10 1
      README.MD
  2. 62 0
      src/db_connector.rs
  3. 50 25
      src/symbol_filter.rs

+ 10 - 1
README.MD

@@ -83,7 +83,11 @@
         },
         {
             "target": "ROA",                // ROA是涨跌幅百分比绝对值,Rise Of Abs
-            "value": "3"                    // 10表示3%
+            "value": "3"                    // 3表示3%
+        },
+        {
+            "target": "TC",                 // TC是交易次数计数,Trades Count
+            "value": "3"                    // 3表示3次
         }
     ]
 }
@@ -106,6 +110,11 @@
                 "bitget_usdt_swap": "0.5",
                 "total": "0.6",             // 所有交易量之和
             },
+            "tc": {                         // 交易次数
+                "gate_usdt_swap": "1",
+                "bitget_usdt_swap": "5",
+                "total": "6",               // 所有交易次数之和
+            },
         },
         ...
     ],

+ 62 - 0
src/db_connector.rs

@@ -76,6 +76,68 @@ pub async fn get_records_json(exchange: &str, symbol: &str, start_at: i64, end_a
     }
 }
 
+pub async fn get_records_map(exchange: &str, start_at: i64, end_at: i64) -> Response {
+    let url = "http://dc.skyfffire.com:8888/get_records_map";
+    let params = json!({
+        "exchange": exchange,
+        "start_time": start_at,
+        "end_time": end_at
+    });
+
+    // 创建 HTTP 客户端
+    let client = Client::new();
+
+    // 发送 GET 请求
+    let response = client.get(url)
+        .query(&params)
+        .send()
+        .await.unwrap();
+
+    // 错误处理
+    if response.status().is_success() {
+        let response_text = response.text().await.unwrap();
+        serde_json::from_str(response_text.as_str()).unwrap()
+    } else {
+        Response {
+            msg: Some("get_records_map 请求失败,预计是指标层的网络请求错误。".to_string()),
+            query: params,
+            data: Default::default(),
+            code: 500,
+        }
+    }
+}
+
+pub async fn get_trades_count(exchange: &str, start_at: i64, end_at: i64) -> Response {
+    let url = "http://dc.skyfffire.com:8888/trades_count";
+    let params = json!({
+        "exchange": exchange,
+        "start_time": start_at,
+        "end_time": end_at
+    });
+
+    // 创建 HTTP 客户端
+    let client = Client::new();
+
+    // 发送 GET 请求
+    let response = client.get(url)
+        .query(&params)
+        .send()
+        .await.unwrap();
+
+    // 错误处理
+    if response.status().is_success() {
+        let response_text = response.text().await.unwrap();
+        serde_json::from_str(response_text.as_str()).unwrap()
+    } else {
+        Response {
+            msg: Some("get_trades_count 请求失败,预计是指标层的网络请求错误。".to_string()),
+            query: params,
+            data: Default::default(),
+            code: 500,
+        }
+    }
+}
+
 pub async fn get_symbols_json(exchange: &str) -> Response {
     let url = "http://dc.skyfffire.com:8888/symbols";
     let params = json!({

+ 50 - 25
src/symbol_filter.rs

@@ -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);