This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #[actix_rt::test] | |
| async fn test_to_sql_query_ranges_must() { | |
| let query = r#"select date, departure, depart_scheduled, destination, arrival_scheduled, delay, created_at, count(*) OVER() AS total_count from delays WHERE ( date >= '"2017-01-01"' AND date <= '"2017-06-30"' )"#; | |
| let query_str = r#" | |
| { | |
| "query": { | |
| "must": { | |
| "ranges": [ | |
| {"date": {"gte": "2017-01-01", "lte": "2017-06-30"}} | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Application Container | |
| pub struct Application { | |
| pub pipeline: Box<dyn MlPipeline> | |
| } | |
| // Application Interface | |
| #[async_trait(?Send)] | |
| pub trait MlPipeline{ | |
| async fn extract(&self) -> Result<String, AppError>; | |
| async fn read_csv(&self, path: &Path) -> Result<PolarResult<DataFrame>, AppError>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async fn extract(&self) -> Result<String, AppError> { | |
| let stmt = &self | |
| .client | |
| .prepare("COPY (SELECT * From delays) TO STDOUT (FORMAT CSV, HEADER TRUE, quote ' ')") | |
| .await?; | |
| let data = self | |
| .client | |
| .copy_out(stmt) | |
| .await? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async fn read_csv(&self, p: &Path) -> Result<PolarResult<DataFrame>, AppError> { | |
| let file = File::open(p)?; | |
| Ok(CsvReader::new(file).has_header(true).finish()) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async fn feature_and_target( | |
| &self, | |
| df: &DataFrame, | |
| ) -> (PolarResult<DataFrame>, PolarResult<DataFrame>) { | |
| /* Read a dataframe, select the columns we need for feature training and target and return | |
| the two new dataframes*/ | |
| //id,date,departure,depart_scheduled,destination,arrival_scheduled | |
| let features = df.select(vec![ | |
| "date", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async fn feature_to_matrix(&self, df: &DataFrame) -> Result<DenseMatrix<f64>, AppError> { | |
| /* function to convert feature dataframe to a DenseMatrix, readable by smartcore*/ | |
| let nrows = df.height(); | |
| let ncols = df.width(); | |
| // convert to array | |
| let features_res = df.to_ndarray::<Float64Type>()?; | |
| // create a zero matrix and populate with features | |
| let mut xmatrix: DenseMatrix<f64> = BaseMatrix::zeros(nrows, ncols); | |
| // populate the matrix |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| async fn linear_regression_model_build( | |
| &self, | |
| x: &DenseMatrix<f64>, | |
| y: &Vec<f64>, | |
| ) -> Result<String, AppError> { | |
| // train split | |
| let (x_train, _, y_train, _) = train_test_split(x, y, 0.3, true); | |
| // model | |
| let linear_regression_model = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // convert to feature and target dataset | |
| let (features, target) = pg_source_app.pipeline.feature_and_target(&df).await; | |
| //convert feature to smartcore data matrix | |
| let x_matrix = pg_source_app | |
| .pipeline | |
| .feature_to_matrix(&features.unwrap()) | |
| .await; | |
| // convert target to class data vec<f64> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #[post("/predict")] | |
| pub async fn predict( | |
| // _state: web::Data<AppState>, | |
| predict: web::Json<PredictData>, | |
| ) -> Result<HttpResponse, AppError> { | |
| // Load the model | |
| let lr_model: LinearRegression<f64, DenseMatrix<f64>> = { | |
| let mut buf: Vec<u8> = Vec::new(); | |
| File::open(&"./models/go_train_delay_lr_2021-12-22UTC.model") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #[derive(Debug, Clone)] | |
| pub struct Shortener { | |
| id: u64, | |
| generator: Harsh, | |
| } | |
| impl Default for Shortener { | |
| fn default() -> Self { | |
| Shortener{ | |
| id: 0, |