qadata_rs/
qafetch.rs

1use std::path::Prefix::Verbatim;
2
3
4pub use mongodb::{
5    bson::{doc, Bson, Document},
6    error::Result,
7    options::{
8        DeleteOptions, FindOneOptions, FindOptions, InsertOneOptions, UpdateModifications,
9        UpdateOptions,ClientOptions
10    },
11    results::{DeleteResult, InsertManyResult, InsertOneResult, UpdateResult},
12    sync::{Client, Collection, Cursor, Database},
13};
14use chrono::{DateTime, FixedOffset, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};
15
16
17use mifi_rs::kline::{future_day, future_min, stock_day, stock_min};
18
19fn to_timestamp(date: String) -> i64{
20    Utc.datetime_from_str(format!("{} 00:00:00", date).as_str(), "%Y-%m-%d %H:%M:%S")
21    .unwrap()
22    .timestamp() -28800
23}
24
25
26pub struct QAMongoClient {
27    pub uri: String,
28    pub database: Database,
29}
30
31impl QAMongoClient {
32    pub fn new(uri: &str, database: &str) -> Self {
33        // let mut client_options = ClientOptions::(uri).unwrap();
34        // client_options.app_name = Some("QUANTAXIS".to_string());
35        // let client = Client::with_options(client_options).unwrap();
36
37        let client = Client::with_uri_str(uri).unwrap();
38        let db = client.database(database);
39        Self {
40            uri: uri.to_string(),
41            database: db,
42        }
43    }
44    pub fn get_stocks_day(&mut self, code: Vec<String>, start: &str, end: &str) -> Vec<stock_day> {
45        let collection = self.database.collection("stock_day");
46        //println!("start {} end {}", start, end);
47        let filter = doc! {"code": {"$in": code},
48                                            "date": {"$gte": start, "$lte": end}};
49        let find_options = FindOptions::builder().sort(doc!{"date_stamp":1}).batch_size(Option::from(10000000)).build();
50        let cursor = collection.find(filter, find_options).unwrap();
51        let mut res = Vec::new();
52        for result in cursor {
53            match result {
54                Ok(document) => {
55                    let u: stock_day = bson::from_document(document).unwrap();
56                    res.push(u);
57                }
58                Err(e) => { println!("ERROR"); } //return Err(e.into()),
59            }
60        }
61        res
62    }
63
64    pub fn get_stock_day(&mut self, code: &str, start: &str, end: &str) -> Vec<stock_day> {
65        let collection = self.database.collection("stock_day");
66
67        let filter = doc! {"code": code,
68                                            "date_stamp": {"$gte": to_timestamp(start.to_string()), "$lte":  to_timestamp(end.to_string())}};
69        let find_options = FindOptions::builder().sort(doc!{"date_stamp":1}).build();
70        let cursor = collection.find(filter, find_options).unwrap();
71        let mut res = Vec::new();
72        for result in cursor {
73            match result {
74                Ok(document) => {
75                    let u: stock_day = bson::from_document(document).unwrap();
76                    res.push(u);
77                }
78                Err(e) => { println!("ERROR"); } //return Err(e.into()),
79            }
80        }
81        res
82    }
83
84    pub fn get_stock_min(&mut self, code: &str, start: &str, end: &str, frequence: &str) -> Vec<stock_min> {
85        let collection = self.database.collection("stock_min");
86
87        let filter = doc! {"code":code, "type": frequence, "datetime": {"$gte": start, "$lte": end}};
88        let find_options = FindOptions::builder().sort(doc!{"datetime":1}).build();
89        let cursor = collection.find(filter, find_options).unwrap();
90
91        let mut res = Vec::new();
92        for result in cursor {
93            match result {
94                Ok(document) => {
95                    let u: stock_min = bson::from_document(document).unwrap();
96                    res.push(u);
97                }
98                Err(e) => { println!("ERROR"); } //return Err(e.into()),
99            }
100        }
101        res
102    }
103    pub fn get_future_day(&mut self, code: &str, start: &str, end: &str) -> Vec<future_day> {
104        let collection = self.database.collection("future_day");
105
106        let filter = doc! {"code": code,
107                                            "date": {"$gte": start, "$lte": end}};
108        let find_options = FindOptions::builder().sort(doc!{"date":1}).build();
109        let cursor = collection.find(filter, find_options).unwrap();
110        let mut res = Vec::new();
111        for result in cursor {
112            match result {
113                Ok(document) => {
114                    let u: future_day = bson::from_document(document).unwrap();
115                    res.push(u);
116                }
117                Err(e) => { println!("ERROR"); } //return Err(e.into()),
118            }
119        }
120        res
121    }
122    pub fn get_future_min(&mut self, code: &str, start: &str, end: &str, frequence: &str) -> Vec<future_min> {
123        let collection = self.database.collection("future_min");
124
125        let filter = doc! {"code":code, "type": frequence, "datetime": {"$gte": start, "$lte": end}};
126        let find_options = FindOptions::builder().sort(doc!{"datetime":1}).build();
127        let cursor = collection.find(filter, find_options).unwrap();
128
129        let mut res = Vec::new();
130        for result in cursor {
131            match result {
132                Ok(document) => {
133                    let u: future_min = bson::from_document(document).unwrap();
134                    res.push(u);
135                }
136                Err(e) => { println!("ERROR"); } //return Err(e.into()),
137            }
138        }
139        res
140    }
141
142    pub fn get_collection(&mut self, collection: &str) -> Collection{
143        self.database.collection(collection)
144    }
145
146    // pub fn get_stock_realtime(&mut self, code: Vec<&str>, start: &str, end: &str, frequence: &str) -> Vec<future_min> {
147    //     let collection = self.database.collection("stock_realtime_min");
148    //
149    //     let filter = doc! {"code": {"$in": Array(code)}, "type": frequence, "datetime": {"$gte": start, "$lte": end}};
150    //     let find_options = FindOptions::builder().build();
151    //     let cursor = collection.find(filter, find_options).unwrap();
152    //
153    //     let mut res = Vec::new();
154    //     for result in cursor {
155    //         match result {
156    //             Ok(document) => {
157    //                 let u: future_min = bson::from_bson(bson::Bson::Document(document)).unwrap();
158    //                 res.push(u);
159    //             }
160    //             Err(e) => { println!("ERROR"); } //return Err(e.into()),
161    //         }
162    //     }
163    //     res
164    // }
165}
166
167#[cfg(test)]
168mod tests {
169
170    use super::*;
171
172    #[test]
173    fn test_totimestamp(){
174        println!("{:#?}",to_timestamp("2020-01-01".to_string()));
175        //assert_eq!(1577836800)
176    }
177}