mifi_rs/
marketset.rs

1use serde_json::Value;
2use std::collections::HashMap;
3
4use crate::qafastkline;
5use crate::qafastkline::{QAAskBidBase, QAKlineBase, QASeries};
6
7pub struct marketcollection {
8    pub market: HashMap<String, QASeries>,
9}
10
11impl marketcollection {
12    pub fn new() -> marketcollection {
13        marketcollection {
14            market: HashMap::new(),
15        }
16    }
17
18    pub fn update(&mut self, data: &Value) -> (String, String) {
19        let code: &str = data["code"].as_str().unwrap();
20        let mut mk = QASeries::init();
21        if self.market.contains_key(code) {
22            //println!("insert!!");
23            mk = (self.market.get_mut(code).unwrap()).to_owned();
24        }
25        mk.update(data.clone());
26
27        let (data, fixdata) = mk.to_json();
28        self.market.insert(code.parse().unwrap(), mk);
29
30        (data, fixdata)
31    }
32
33    pub fn get_mut(&mut self, code: &str) -> QASeries {
34        let mut mk = QASeries::init();
35        if self.market.contains_key(code) {
36            println!("insert!!");
37            mk = (self.market.get_mut(code).unwrap()).to_owned();
38        }
39        mk
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use std::borrow::Borrow;
46    use std::collections::HashMap;
47    use std::ptr::hash;
48    use std::time::Instant;
49
50    use crate::qafastkline::{QAAskBidBase, QASeries};
51    use crate::qamarketset::marketcollection;
52
53    #[test]
54    fn test_hash() {
55        let mut u = HashMap::new();
56        let mut mk = QASeries::init();
57
58        //        if u.contains_key("S"){
59        //            mk = (u.get_mut("S").unwrap()).to_owned();
60        //        }
61        u.insert("S".to_string(), mk);
62        let l = u.get_mut("S").unwrap();
63
64        println!("{:#?}", l);
65    }
66
67    #[test]
68    fn test_update() {
69        let mut mc = marketcollection::new();
70
71        let new_data = QAAskBidBase {
72            BuyPrices: vec![],
73            BuyVols: vec![],
74            SellPrices: vec![],
75            SellVols: vec![],
76            code: "000001".to_string(),
77            open: 20.0,
78            high: 24.0,
79            low: 19.0,
80            close: 21.0,
81            amount: 10000,
82            productid: 110,
83            tickcount: 1120,
84            time: "2020-01-20 10:20:02".to_string(),
85            vol: 110,
86        };
87
88        let new_data1 = QAAskBidBase {
89            BuyPrices: vec![],
90            BuyVols: vec![],
91            SellPrices: vec![],
92            SellVols: vec![],
93            code: "000001".to_string(),
94            open: 21.0,
95            high: 28.0,
96            low: 25.0,
97            close: 26.0,
98            amount: 20000,
99            productid: 110,
100            tickcount: 1120,
101            time: "2020-01-20 10:20:43".to_string(),
102            vol: 210,
103        };
104        let new_data2 = QAAskBidBase {
105            BuyPrices: vec![],
106            BuyVols: vec![],
107            SellPrices: vec![],
108            SellVols: vec![],
109            code: "000001".to_string(),
110            open: 21.0,
111            high: 28.0,
112            low: 25.0,
113            close: 26.0,
114            amount: 20000,
115            productid: 110,
116            tickcount: 1120,
117            time: "2020-01-20 10:25:20".to_string(),
118            vol: 210,
119        };
120        let mut res: String;
121
122        let start = Instant::now();
123        let (res, fix) = mc.update(serde_json::to_value(new_data).unwrap().borrow());
124        println!("time cost: {:?} us", start.elapsed().as_micros()); // us
125        println!("fix~ {}", fix);
126
127        let start = Instant::now();
128        let (res, fix) = mc.update(serde_json::to_value(new_data1).unwrap().borrow());
129        println!("time cost: {:?} us", start.elapsed().as_micros()); // us
130        println!("fix~ {}", fix);
131
132        //test new bar
133        let start = Instant::now();
134        let (res, fix) = mc.update(serde_json::to_value(new_data2).unwrap().borrow());
135        println!("time cost: {:?} us", start.elapsed().as_micros()); // us
136        println!("fix~ {}", fix);
137
138        let mut mk = mc.get_mut("000001");
139        let (res1, res2) = mk.to_json();
140        println!("\nthis is json{:#?}\n", res1);
141
142        //assert_eq!(res, res1)
143    }
144}