@@ -18,14 +18,27 @@ struct _DatabaseError {
18
18
error : String ,
19
19
}
20
20
21
- pub trait InfluxDbSerdeORM {
22
- fn json_query < T : ' static , Q > ( self , q : Q ) -> Box < dyn Future < Item = T , Error = InfluxDbError > > where
23
- Q : InfluxDbQuery ,
24
- T : DeserializeOwned ;
21
+ # [ derive ( Deserialize , Debug ) ]
22
+ # [ doc ( hidden ) ]
23
+ pub struct DatabaseQueryResult < T > {
24
+ pub results : Vec < InfluxDbReturn < T > > ,
25
25
}
26
26
27
- impl InfluxDbSerdeORM for InfluxDbClient {
28
- fn json_query < T : ' static , Q > ( self , q : Q ) -> Box < dyn Future < Item = T , Error = InfluxDbError > >
27
+ #[ derive( Deserialize , Debug ) ]
28
+ #[ doc( hidden) ]
29
+ pub struct InfluxDbReturn < T > {
30
+ pub series : Option < Vec < InfluxDbSeries < T > > > ,
31
+ }
32
+
33
+ #[ derive( Deserialize , Debug ) ]
34
+ #[ doc( hidden) ]
35
+ pub struct InfluxDbSeries < T > {
36
+ pub name : String ,
37
+ pub values : Vec < T > ,
38
+ }
39
+
40
+ impl InfluxDbClient {
41
+ pub fn json_query < T : ' static , Q > ( self , q : Q ) -> Box < dyn Future < Item = Option < Vec < T > > , Error = InfluxDbError > >
29
42
where
30
43
Q : InfluxDbQuery ,
31
44
T : DeserializeOwned ,
@@ -43,7 +56,7 @@ impl InfluxDbSerdeORM for InfluxDbClient {
43
56
let error = InfluxDbError :: UnspecifiedError {
44
57
error : format ! ( "{}" , err) ,
45
58
} ;
46
- return Box :: new ( future:: err :: < T , InfluxDbError > ( error) ) ;
59
+ return Box :: new ( future:: err :: < Option < Vec < T > > , InfluxDbError > ( error) ) ;
47
60
}
48
61
Ok ( query) => query,
49
62
} ;
@@ -89,18 +102,28 @@ impl InfluxDbSerdeORM for InfluxDbClient {
89
102
error : format ! ( "{}" , err)
90
103
} )
91
104
. and_then ( |body| {
105
+ println ! ( "{:?}" , & body) ;
92
106
// Try parsing InfluxDBs { "error": "error message here" }
93
107
if let Ok ( error) = serde_json:: from_slice :: < _DatabaseError > ( & body) {
94
108
return futures:: future:: err ( InfluxDbError :: DatabaseError {
95
109
error : error. error . to_string ( )
96
110
} )
97
- } else if let Ok ( t_result) = serde_json:: from_slice :: < T > ( & body) {
98
- // Json has another structure, let's try actually parsing it to the type we're deserializing to
99
- return futures:: future:: result ( Ok ( t_result) ) ;
100
111
} else {
101
- return futures:: future:: err ( InfluxDbError :: UnspecifiedError {
102
- error : "something wen't wrong during deserializsation of the database response. this might be a bug!" . to_string ( )
103
- } )
112
+ let from_slice = serde_json:: from_slice :: < DatabaseQueryResult < T > > ( & body) ;
113
+
114
+ let mut deserialized = match from_slice {
115
+ Ok ( deserialized) => deserialized,
116
+ Err ( err) => return futures:: future:: err ( InfluxDbError :: UnspecifiedError {
117
+ error : format ! ( "serde error: {}" , err)
118
+ } )
119
+ } ;
120
+
121
+ // Json has another structure, let's try actually parsing it to the type we're deserializing to
122
+ let t_result = match deserialized. results . remove ( 0 ) . series {
123
+ Some ( series) => Ok ( Some ( series. into_iter ( ) . flat_map ( |x| { x. values } ) . collect :: < Vec < T > > ( ) ) ) ,
124
+ None => Ok ( None )
125
+ } ;
126
+ return futures:: future:: result ( t_result) ;
104
127
}
105
128
} )
106
129
)
0 commit comments