@@ -18,14 +18,27 @@ struct _DatabaseError {
1818 error : String ,
1919}
2020
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 > > ,
2525}
2626
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 > >
2942 where
3043 Q : InfluxDbQuery ,
3144 T : DeserializeOwned ,
@@ -43,7 +56,7 @@ impl InfluxDbSerdeORM for InfluxDbClient {
4356 let error = InfluxDbError :: UnspecifiedError {
4457 error : format ! ( "{}" , err) ,
4558 } ;
46- return Box :: new ( future:: err :: < T , InfluxDbError > ( error) ) ;
59+ return Box :: new ( future:: err :: < Option < Vec < T > > , InfluxDbError > ( error) ) ;
4760 }
4861 Ok ( query) => query,
4962 } ;
@@ -89,18 +102,28 @@ impl InfluxDbSerdeORM for InfluxDbClient {
89102 error : format ! ( "{}" , err)
90103 } )
91104 . and_then ( |body| {
105+ println ! ( "{:?}" , & body) ;
92106 // Try parsing InfluxDBs { "error": "error message here" }
93107 if let Ok ( error) = serde_json:: from_slice :: < _DatabaseError > ( & body) {
94108 return futures:: future:: err ( InfluxDbError :: DatabaseError {
95109 error : error. error . to_string ( )
96110 } )
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) ) ;
100111 } 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) ;
104127 }
105128 } )
106129 )
0 commit comments