11use crate :: flow:: DateTime ;
22use crate :: flow:: FlowOutput ;
33use crate :: flow:: Message ;
4+ use crate :: flow:: MessageSource ;
45use crate :: runtime:: MessageProcessor ;
56use crate :: InputMessage ;
67use crate :: OutputMessage ;
@@ -22,6 +23,7 @@ use tedge_mqtt_ext::TopicFilter;
2223use tokio:: time:: sleep_until;
2324use tokio:: time:: Instant ;
2425use tracing:: error;
26+ use tracing:: info;
2527
2628pub const STATS_DUMP_INTERVAL : Duration = Duration :: from_secs ( 300 ) ;
2729
@@ -42,13 +44,17 @@ impl Actor for FlowsMapper {
4244 while let Some ( message) = self . next_message ( ) . await {
4345 match message {
4446 InputMessage :: Tick ( _) => {
45- self . on_interval ( ) . await ?;
46-
4747 let drained_messages = self . drain_db ( ) . await ?;
48- self . filter_all ( drained_messages) . await ?;
48+ self . on_messages ( MessageSource :: MeaDB , drained_messages)
49+ . await ?;
50+
51+ self . on_interval ( ) . await ?;
4952 }
5053 InputMessage :: MqttMessage ( message) => match Message :: try_from ( message) {
51- Ok ( message) => self . on_message ( message) . await ?,
54+ Ok ( message) => {
55+ self . on_message ( MessageSource :: MQTT , DateTime :: now ( ) , message)
56+ . await ?
57+ }
5258 Err ( err) => {
5359 error ! ( target: "flows" , "Cannot process message: {err}" ) ;
5460 }
@@ -125,24 +131,16 @@ impl FlowsMapper {
125131 diff
126132 }
127133
128- async fn on_message ( & mut self , message : Message ) -> Result < ( ) , RuntimeError > {
129- let timestamp = DateTime :: now ( ) ;
130- for ( flow_id, flow_messages) in self . processor . on_message ( timestamp, & message) . await {
134+ async fn on_message (
135+ & mut self ,
136+ source : MessageSource ,
137+ timestamp : DateTime ,
138+ message : Message ,
139+ ) -> Result < ( ) , RuntimeError > {
140+ for ( flow_id, flow_messages) in self . processor . on_message ( source, timestamp, & message) . await
141+ {
131142 match flow_messages {
132- Ok ( messages) => {
133- for message in messages {
134- match MqttMessage :: try_from ( message) {
135- Ok ( message) => {
136- self . messages
137- . send ( OutputMessage :: MqttMessage ( message) )
138- . await ?
139- }
140- Err ( err) => {
141- error ! ( target: "flows" , "{flow_id}: cannot send transformed message: {err}" )
142- }
143- }
144- }
145- }
143+ Ok ( messages) => self . publish_messages ( flow_id, timestamp, messages) . await ?,
146144 Err ( err) => {
147145 error ! ( target: "flows" , "{flow_id}: {err}" ) ;
148146 }
@@ -152,26 +150,14 @@ impl FlowsMapper {
152150 Ok ( ( ) )
153151 }
154152
155- async fn filter_all ( & mut self , messages : Vec < ( DateTime , Message ) > ) -> Result < ( ) , RuntimeError > {
153+ async fn on_messages (
154+ & mut self ,
155+ source : MessageSource ,
156+ messages : Vec < ( DateTime , Message ) > ,
157+ ) -> Result < ( ) , RuntimeError > {
156158 for ( timestamp, message) in messages {
157- self . filter ( timestamp, message) . await ?
158- }
159- Ok ( ( ) )
160- }
161-
162- async fn filter ( & mut self , timestamp : DateTime , message : Message ) -> Result < ( ) , RuntimeError > {
163- for ( flow_id, flow_messages) in self . processor . process ( timestamp, & message) . await {
164- match flow_messages {
165- Ok ( messages) => {
166- self . publish_messages ( flow_id. clone ( ) , timestamp, messages)
167- . await ?;
168- }
169- Err ( err) => {
170- error ! ( target: "flows" , "{flow_id}: {err}" ) ;
171- }
172- }
159+ self . on_message ( source, timestamp, message) . await ?
173160 }
174-
175161 Ok ( ( ) )
176162 }
177163
@@ -225,6 +211,7 @@ impl FlowsMapper {
225211 }
226212 FlowOutput :: MeaDB { output_series } => {
227213 for message in messages {
214+ info ! ( target: "flows" , "store {output_series} @{}.{} [{}]" , timestamp. seconds, timestamp. nanoseconds, message. topic) ;
228215 if let Err ( err) = self
229216 . processor
230217 . database
@@ -246,6 +233,9 @@ impl FlowsMapper {
246233 for ( flow_id, flow_messages) in self . processor . drain_db ( timestamp) . await {
247234 match flow_messages {
248235 Ok ( flow_messages) => {
236+ for ( t, m) in flow_messages. iter ( ) {
237+ info ! ( target: "flows" , "drained: @{}.{} [{}]" , t. seconds, t. nanoseconds, m. topic) ;
238+ }
249239 messages. extend ( flow_messages) ;
250240 }
251241 Err ( err) => {
0 commit comments