|
|
@@ -1,40 +1,17 @@
|
|
|
use std::sync::Arc;
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
use actix_cors::Cors;
|
|
|
-use actix_web::{web, App, HttpResponse, HttpServer, Responder, get};
|
|
|
-use chrono::Utc;
|
|
|
+use actix_web::{web, App, HttpResponse, HttpServer, Responder, post};
|
|
|
use rust_decimal::Decimal;
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
use serde_json::Value;
|
|
|
use tracing::{info};
|
|
|
-use crate::db_connector::get_trades_json;
|
|
|
-use crate::msv_generate::generate_msv;
|
|
|
-
|
|
|
-// 定义用于反序列化查询参数的结构体
|
|
|
-#[derive(Serialize, Deserialize, Clone)]
|
|
|
-pub struct IndicatorQuery {
|
|
|
- symbol: Option<String>,
|
|
|
- exchange: Option<String>
|
|
|
-}
|
|
|
-
|
|
|
-impl IndicatorQuery {
|
|
|
- pub fn validate(&self) -> bool {
|
|
|
- if self.symbol.is_none() {
|
|
|
- return false
|
|
|
- }
|
|
|
-
|
|
|
- if self.exchange.is_none() {
|
|
|
- return false
|
|
|
- }
|
|
|
-
|
|
|
- true
|
|
|
- }
|
|
|
-}
|
|
|
+use crate::msv::generate_msv;
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
pub struct Response {
|
|
|
- pub message: Option<String>,
|
|
|
- pub query_string: Value,
|
|
|
+ pub msg: Option<String>,
|
|
|
+ pub query: Value,
|
|
|
pub data: Value,
|
|
|
pub code: i32,
|
|
|
}
|
|
|
@@ -48,11 +25,11 @@ pub struct Trade {
|
|
|
}
|
|
|
|
|
|
// 句柄 GET 请求
|
|
|
-#[get("/ia/get_symbols")]
|
|
|
-async fn get_symbols_by_filter() -> impl Responder {
|
|
|
+#[post("/ia/get_symbols_by_filter")]
|
|
|
+async fn get_symbols_by_filter(query: web::Json<Value>) -> impl Responder {
|
|
|
let response = Response {
|
|
|
- query_string: Value::Null,
|
|
|
- message: Some("get_symbols_by_filter 这个接口还没做".to_string()),
|
|
|
+ query: query.into_inner(),
|
|
|
+ msg: Some("get_symbols_by_filter 这个接口还没做".to_string()),
|
|
|
code: 400,
|
|
|
data: Value::Null,
|
|
|
};
|
|
|
@@ -62,49 +39,42 @@ async fn get_symbols_by_filter() -> impl Responder {
|
|
|
}
|
|
|
|
|
|
// ia: intelligence agency, 情报部门
|
|
|
-#[get("/ia/get_indicator")]
|
|
|
-async fn get_indicator(query: web::Query<IndicatorQuery>) -> impl Responder {
|
|
|
- // 客户端传过来的数据校验
|
|
|
- if query.validate() {
|
|
|
- // 链接数据服务器查询数据
|
|
|
- let end_time = Utc::now().timestamp_millis();
|
|
|
- let start_time = end_time - 4 * 60 * 60 * 1000;
|
|
|
- let db_response = get_trades_json(
|
|
|
- query.exchange.clone().unwrap().as_str(),
|
|
|
- query.symbol.clone().unwrap().as_str(),
|
|
|
- start_time,
|
|
|
- end_time,
|
|
|
- ).await;
|
|
|
+#[post("/ia/get_indicator")]
|
|
|
+async fn get_indicator(query: web::Json<Value>) -> impl Responder {
|
|
|
+ let indicator = match query["indicator"].as_str() {
|
|
|
+ None => {
|
|
|
+ let query_json = query.into_inner().clone();
|
|
|
+ let response = Response {
|
|
|
+ query: Value::Null,
|
|
|
+ msg: Some(format!("indicator没有传给后端, {}", query_json.to_string())),
|
|
|
+ code: 500,
|
|
|
+ data: Value::Null,
|
|
|
+ };
|
|
|
|
|
|
- // 对数据库返回的数据进行容错处理
|
|
|
- if db_response.code == 200 {
|
|
|
- // 指标生成
|
|
|
- let indicator = generate_msv(db_response.data);
|
|
|
+ let json_string = serde_json::to_string(&response).unwrap();
|
|
|
+ return HttpResponse::BadRequest().content_type("application/json").body(json_string)
|
|
|
+ }
|
|
|
+ Some(indicator) => {
|
|
|
+ indicator
|
|
|
+ }
|
|
|
+ };
|
|
|
|
|
|
- // 返回数据
|
|
|
+ match indicator {
|
|
|
+ "msv" => {
|
|
|
+ generate_msv(query.into_inner().get("query").unwrap().clone()).await
|
|
|
+ },
|
|
|
+ _ => {
|
|
|
+ let query_json = query.into_inner().clone();
|
|
|
let response = Response {
|
|
|
- query_string: serde_json::to_value(&query.into_inner()).unwrap(),
|
|
|
- message: Some("指标生成完毕".to_string()),
|
|
|
- code: 200,
|
|
|
- data: indicator,
|
|
|
+ query: Value::Null,
|
|
|
+ msg: Some(format!("不存在的指标, {}", query_json.to_string())),
|
|
|
+ code: 500,
|
|
|
+ data: Value::Null,
|
|
|
};
|
|
|
|
|
|
let json_string = serde_json::to_string(&response).unwrap();
|
|
|
- HttpResponse::Ok().content_type("application/json").body(json_string)
|
|
|
- } else {
|
|
|
- let json_string = serde_json::to_string(&db_response).unwrap();
|
|
|
- HttpResponse::Ok().content_type("application/json").body(json_string)
|
|
|
+ HttpResponse::BadRequest().content_type("application/json").body(json_string)
|
|
|
}
|
|
|
- } else {
|
|
|
- let response = Response {
|
|
|
- query_string: serde_json::to_value(&query.into_inner()).unwrap(),
|
|
|
- message: Some("[symbol以及exchange必传]".to_string()),
|
|
|
- code: 400,
|
|
|
- data: Value::Null,
|
|
|
- };
|
|
|
-
|
|
|
- let json_string = serde_json::to_string(&response).unwrap();
|
|
|
- HttpResponse::BadRequest().content_type("application/json").body(json_string)
|
|
|
}
|
|
|
}
|
|
|
|