1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use crate::{
    client::ScrapeClient,
    stage::{FlowControl, ScrapeStage},
    value::JsonValue,
};
use fantoccini::{
    elements::Element,
    error::{CmdError, NewSessionError},
};
use futures::future::{BoxFuture, FutureExt};
use log::{info, warn};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::{
    error::Error,
    fmt::{Display, Formatter, Result as FormatResult},
};
use tokio::time::{sleep, Duration};

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct ScrapePipeline {
    stages: Vec<ScrapeStage>,
}

pub type ScrapePipelineResult = Result<(), ScrapeError>;

impl ScrapePipeline {
    pub fn push<T: Into<ScrapeStage>>(mut self, stage: T) -> Self {
        self.stages.push(stage.into());
        self
    }

    pub fn execute<'a>(&'a self, context: &'a mut ScrapeContext) -> BoxFuture<'a, ScrapePipelineResult> {
        async move {
            let mut idx = 0;
            loop {
                match self.stages.get(idx) {
                    Some(stage) => {
                        match stage.name {
                            Some(ref name) => info!("Executing {}: {}", name, stage.action),
                            None => info!("Executing: {}", stage.action),
                        }

                        // Stage action execution, flow control evaluation based on the result
                        let flow: &FlowControl;
                        match stage.action.execute(context).await {
                            // "On complete" branch is executed
                            Ok(_) => flow = &stage.on_complete,

                            // Internal client error - pipeline execution is stopped, the error is propagated
                            Err(e @ ScrapeError::WebdriverCommandError(_)) => return Err(e),

                            // Stage action execution failure - "on error" branch is executed
                            Err(error) => {
                                warn!("Action execution failure: {}", error);
                                flow = &stage.on_error;
                            }
                        }

                        match flow {
                            // Following pipeline stage is executed
                            FlowControl::Continue => idx += 1,

                            // Pipeline execution is stopped
                            FlowControl::Quit => {
                                info!("Flow control quit, stopping the pipeline execution");
                                break;
                            }

                            // Pipeline execution is redirected to a named stage
                            FlowControl::Goto(next_stage) => {
                                info!("Flow control redirection to stage \"{}\"", next_stage);
                                match self.stages.iter().position(|stage| match &stage.name {
                                    Some(name) => next_stage.eq(name),
                                    _ => false,
                                }) {
                                    Some(pos) => idx = pos,
                                    None => return Err(ScrapeError::MissingPipelineStage),
                                }
                            }

                            // Current pipeline stage execution is repeated after an optional delay
                            FlowControl::Repeat { delay } => {
                                match delay {
                                    Some(x) => info!("Repeating stage after {} seconds", x),
                                    None => info!("Repeating stage immediately"),
                                };

                                if let Some(delay) = *delay {
                                    sleep(Duration::from_secs_f64(delay)).await;
                                }
                            }
                        };
                    }

                    None => break,
                }
            }

            Ok(())
        }
        .boxed()
    }
}

pub struct ScrapeContext {
    pub client: Box<dyn ScrapeClient>,
    pub model: JsonValue,
    pub values: JsonValue,
    pub models: Vec<JsonValue>,
    pub scoped_element: Option<Element>,
    pub current_element: Option<Element>,
}

impl ScrapeContext {
    pub fn new<C: ScrapeClient + 'static, V: Into<Option<JsonValue>>>(client: C, values: V) -> Self {
        ScrapeContext {
            client: Box::new(client),
            model: json!({}),
            values: values.into().unwrap_or(json!({})),
            models: Vec::new(),
            current_element: None,
            scoped_element: None,
        }
    }
}

#[derive(Debug)]
pub enum ScrapeError {
    ValueResolveError,
    ElementQueryEmptyResult,
    MissingElement,
    MissingUrl,
    MissingQuery,
    MissingPipelineStage,
    SetModelAttributeError,
    TestError,
    WebdriverConnectionError(NewSessionError),
    WebdriverCommandError(CmdError),
}

impl Display for ScrapeError {
    fn fmt(&self, fmt: &mut Formatter<'_>) -> FormatResult {
        match self {
            ScrapeError::ValueResolveError => {
                write!(fmt, "failed to resolve value")
            }

            ScrapeError::ElementQueryEmptyResult => {
                write!(fmt, "element query empty result")
            }

            ScrapeError::MissingElement => {
                write!(fmt, "required element is missing in the pipeline execution context")
            }

            ScrapeError::MissingUrl => {
                write!(fmt, "missing URL to open")
            }

            ScrapeError::MissingQuery => {
                write!(fmt, "missing element query")
            }

            ScrapeError::MissingPipelineStage => {
                write!(fmt, "missing specified pipeline stage")
            }

            ScrapeError::SetModelAttributeError => {
                write!(fmt, "failed to populate model attribute")
            }

            ScrapeError::TestError => {
                write!(fmt, "test error")
            }

            ScrapeError::WebdriverConnectionError(error) => {
                write!(fmt, "webdriver connection error: {}", error)
            }

            ScrapeError::WebdriverCommandError(error) => {
                write!(fmt, "webdriver command error: {}", error)
            }
        }
    }
}

impl Error for ScrapeError {}

#[cfg(test)]
mod test {
    use crate::{
        action::{OpenUrl, ScrapeAction, TestError, TestSuccess},
        client::MockScrapeClient,
        pipeline::{ScrapeContext, ScrapeError, ScrapePipeline},
        scrapman::Scrapman,
        stage::{FlowControl, ScrapeStage},
        value::Value,
        StoreModel,
    };
    use futures::future;
    use mockall::predicate;

    #[tokio::test]
    async fn test_plain_pipeline() {
        let url = "http://localhost";
        let pipeline = ScrapePipeline::default().push(OpenUrl::new(Value::Constant(url.into())));

        let mut client = MockScrapeClient::new();
        client
            .expect_goto()
            .with(predicate::eq(url))
            .times(1)
            .returning(|_| Box::pin(future::ok(())));

        client
            .expect_disconnect()
            .times(1)
            .returning(|| Box::pin(future::ok(())));

        let scrapman = Scrapman::new(url);

        let result = scrapman.launch_with_client(pipeline, None, client).await;
        assert_eq!(true, result.is_ok());
    }

    #[tokio::test]
    async fn test_quit_on_error() {
        let pipeline = ScrapePipeline::default()
            .push(ScrapeStage::from(TestError).on_any_error(FlowControl::Quit))
            .push(StoreModel);

        let mut client = MockScrapeClient::new();
        client
            .expect_disconnect()
            .times(1)
            .returning(|| Box::pin(future::ok(())));

        let scrapman = Scrapman::new("");
        let result = scrapman.launch_with_client(pipeline, None, client).await;
        assert_eq!(true, result.is_ok());

        let ctx = result.unwrap();
        assert_eq!(0, ctx.models.len());
    }

    #[tokio::test]
    async fn test_conditional_pipeline_1() {
        let result = test_conditional_pipeline(TestSuccess).await;
        assert_eq!(true, result.is_ok());

        let ctx = result.unwrap();
        assert_eq!(0, ctx.models.len());
    }

    #[tokio::test]
    async fn test_conditional_pipeline_2() {
        let result = test_conditional_pipeline(TestError).await;
        assert_eq!(true, result.is_ok());

        let ctx = result.unwrap();
        assert_eq!(1, ctx.models.len());
    }

    async fn test_conditional_pipeline<T: ScrapeAction + 'static>(f: T) -> Result<ScrapeContext, ScrapeError> {
        let pipeline = ScrapePipeline::default()
            .push(ScrapeStage::from(f).on_complete(FlowControl::Goto("Complete".into())))
            .push(StoreModel)
            .push(ScrapeStage::from(TestSuccess).with_name("Complete"));

        let mut client = MockScrapeClient::new();
        client
            .expect_disconnect()
            .times(1)
            .returning(|| Box::pin(future::ok(())));

        let scrapman = Scrapman::new("");
        scrapman.launch_with_client(pipeline, None, client).await
    }
}