Skip to content

Commit

Permalink
Merge pull request #1275 from YuliaProkopovych/unitore
Browse files Browse the repository at this point in the history
READY(unitore): Fix documentation
  • Loading branch information
Wandalen authored Apr 11, 2024
2 parents 43475cd + 530d27f commit 7f6eaab
Show file tree
Hide file tree
Showing 45 changed files with 2,087 additions and 1,504 deletions.
1 change: 1 addition & 0 deletions module/move/unitore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ enabled = []

[dependencies]
error_tools = { workspace = true, features = [ "default" ] }
proper_path_tools = { workspace = true, features = [ "default" ] }
tokio = { version = "1.36.0", features = [ "rt", "rt-multi-thread", "io-std", "macros" ] }
hyper = { version = "1.1.0", features = [ "client" ] }
hyper-tls = "0.6.0"
Expand Down
2 changes: 1 addition & 1 deletion module/move/unitore/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ cargo run .feeds.list
```
To get custom information about feeds or frames run SQL query to storage database using command `.query.execute` with query string:
```bash
cargo run .query.execute \'SELECT title, links, MIN\(published\) FROM frame\'
cargo run .query.execute 'SELECT title, links, MIN(published) FROM frame'
```
To remove config file from storage use command `.config.delete` with path to config file:
```bash
Expand Down
6 changes: 6 additions & 0 deletions module/move/unitore/src/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## File structure

- `command` - Contains commands for unitore cli.
- `action` - Contains functions that are executed when command are performed.
- `entity` - Contains entities that are used in commands execution.
- `tool` - Additional functions for convenient use of application.
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
//! Actions and report for commands for config files.
//! Actions and report for config files.
use std::path::PathBuf;

use crate::*;
use super::*;
use error_tools::{ err, for_app::Context, BasicError, Result };
use executor::FeedManager;
use storage::
use error_tools::{ for_app::Context, Result };
use sled_adapter::FeedStorage;
use entity::
{
FeedStorage,
feed::{ FeedStore, Feed },
config::{ ConfigStore, Config },
};
use action::Report;
use gluesql::{ prelude::Payload, sled_storage::SledStorage };

/// Add configuration file with subscriptions to storage.
pub async fn add_config( storage : FeedStorage< SledStorage >, args : &wca::Args ) -> Result< impl Report >
pub async fn config_add( mut storage : FeedStorage< SledStorage >, path : &PathBuf ) -> Result< impl Report >
{
let path : std::path::PathBuf = args
.get_owned::< wca::Value >( 0 )
.ok_or_else::< BasicError, _ >( || err!( "Cannot get path argument for command .config.add" ) )?
.into()
;
let path = proper_path_tools::path::normalize( path );

let mut err_str = format!( "Invalid path for config file {:?}", path );

Expand All @@ -37,54 +34,52 @@ pub async fn add_config( storage : FeedStorage< SledStorage >, args : &wca::Args
err_str = format!( "Invalid path for config file {:?}", abs_path );
}
}
let path = path.canonicalize().context( err_str )?;

let config = Config::new( path.to_string_lossy().to_string() );
let mut manager = FeedManager::new( storage );
if !path.exists()
{
return Err( error_tools::for_app::Error::msg( err_str ) );
}

//let abs_path = proper_path_tools::path::canonicalize( path )?;
let abs_path = path.canonicalize()?;
let config = Config::new( abs_path.to_string_lossy().to_string() );

let config_report = manager.storage
.add_config( &config )
let config_report = storage
.config_add( &config )
.await
.context( "Added 0 config files.\n Failed to add config file to storage." )?
;

let feeds = feed_config::read( config.path() )?
.into_iter()
.map( | feed | Feed::new( feed.link, feed.update_period ) )
.map( | feed | Feed::new( feed.link, feed.update_period, config.path() ) )
.collect::< Vec< _ > >()
;

let new_feeds = manager.storage.save_feeds( feeds ).await?;
let new_feeds = storage.feeds_save( feeds ).await?;

Ok( ConfigReport{ payload : config_report, new_feeds : Some( new_feeds ) } )
}

/// Remove configuration file from storage.
pub async fn delete_config( storage : FeedStorage< SledStorage >, args : &wca::Args ) -> Result< impl Report >
pub async fn config_delete( mut storage : FeedStorage< SledStorage >, path : &PathBuf ) -> Result< impl Report >
{
let path : std::path::PathBuf = args
.get_owned::< wca::Value >( 0 )
.ok_or_else::< BasicError, _ >( || err!( "Cannot get path argument for command .config.delete" ) )?
.into()
;

let path = proper_path_tools::path::normalize( path );
let path = path.canonicalize().context( format!( "Invalid path for config file {:?}", path ) )?;
let config = Config::new( path.to_string_lossy().to_string() );

let mut manager = FeedManager::new( storage );
Ok( ConfigReport::new(
manager.storage
.delete_config( &config )
storage
.config_delete( &config )
.await
.context( "Failed to remove config from storage." )?
) )
}

/// List all files with subscriptions that are currently in storage.
pub async fn list_configs( storage : FeedStorage< SledStorage >, _args : &wca::Args ) -> Result< impl Report >
pub async fn config_list( mut storage : FeedStorage< SledStorage >, _args : &wca::Args ) -> Result< impl Report >
{
let mut manager = FeedManager::new( storage );
Ok( ConfigReport::new( manager.storage.list_configs().await? ) )
Ok( ConfigReport::new( storage.config_list().await? ) )
}

/// Information about result of command for subscription config.
Expand Down Expand Up @@ -140,7 +135,7 @@ impl std::fmt::Display for ConfigReport
rows.push( vec![ EMPTY_CELL.to_owned(), String::from( row[ 0 ].clone() ) ] );
}

let table = table_display::plain_table( rows );
let table = tool::table_display::plain_table( rows );
if let Some( table ) = table
{
write!( f, "{}", table )?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
//! Endpoints and report for feed commands.
//! Feed actions and reports.
use crate::*;
use executor::
{
FeedManager,
actions::{ Report, frame::SelectedEntries },
};
use storage::{ FeedStorage, feed::FeedStore };
use action::{ Report, frame::SelectedEntries };
use sled_adapter::FeedStorage;
use entity::feed::FeedStore;
use error_tools::Result;

/// List all feeds.
pub async fn list_feeds(
storage : FeedStorage< gluesql::sled_storage::SledStorage >,
_args : &wca::Args,
) -> Result< impl Report >
/// List all feeds from storage.
pub async fn feeds_list( mut storage : FeedStorage< gluesql::sled_storage::SledStorage > ) -> Result< impl Report >
{
let mut manager = FeedManager::new( storage );
manager.storage.get_all_feeds().await
storage.feeds_list().await
}

const EMPTY_CELL : &'static str = "";
Expand Down Expand Up @@ -51,7 +44,7 @@ impl std::fmt::Display for FeedsReport
let mut headers = vec![ EMPTY_CELL.to_owned() ];
headers.extend( self.0.selected_columns.iter().map( | str | str.to_owned() ) );

let table = table_display::table_with_headers( headers, rows );
let table = tool::table_display::table_with_headers( headers, rows );
if let Some( table ) = table
{
write!( f, "{}", table )?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,33 @@
//! Frames commands actions.
//! Frames actions and reports.
use crate::*;
use super::*;
use executor::FeedManager;
use storage::
use sled_adapter::FeedStorage;
use entity::
{
FeedStorage,
feed::FeedStore,
config::ConfigStore,
frame::{ FrameStore, RowValue }
frame::{ FrameStore, CellValue }
};
use gluesql::prelude::{ Payload, Value, SledStorage };
use feed_config;
use error_tools::{ err, Result };
use action::Report;

// qqq : review the whole project and make sure all names are consitant: actions, commands, its tests

/// List all frames.
pub async fn list_frames
(
storage : FeedStorage< SledStorage >,
_args : &wca::Args,
) -> Result< impl Report >
pub async fn frames_list( mut storage : FeedStorage< SledStorage > ) -> Result< impl Report >
{
let mut manager = FeedManager::new( storage );
manager.storage.list_frames().await
storage.frames_list().await
}

/// Update all frames from config files saved in storage.
pub async fn download_frames
pub async fn frames_download
(
storage : FeedStorage< SledStorage >,
_args : &wca::Args,
mut storage : FeedStorage< SledStorage >
) -> Result< impl Report >
{
let mut manager = FeedManager::new( storage );
let payload = manager.storage.list_configs().await?;

let payload = storage.config_list().await?;
let configs = match &payload
{
Payload::Select { labels: _, rows: rows_vec } =>
Expand Down Expand Up @@ -71,12 +62,12 @@ pub async fn download_frames

let mut feeds = Vec::new();
let client = retriever::FeedClient;
for i in 0..subscriptions.len()
for subscription in subscriptions
{
let feed = retriever::FeedFetch::fetch(&client, subscriptions[ i ].link.clone()).await?;
feeds.push( ( feed, subscriptions[ i ].update_period.clone(), subscriptions[ i ].link.clone() ) );
let feed = client.fetch( subscription.link.clone() ).await?;
feeds.push( ( feed, subscription.update_period.clone(), subscription.link ) );
}
manager.storage.process_feeds( feeds ).await
storage.feeds_process( feeds ).await

}

Expand Down Expand Up @@ -123,7 +114,7 @@ impl std::fmt::Display for FramesReport
fn fmt( &self, f : &mut std::fmt::Formatter<'_> ) -> std::fmt::Result
{
let initial = vec![ vec![ format!( "Feed title: {}", self.feed_link ) ] ];
let table = table_display::table_with_headers( initial[ 0 ].clone(), Vec::new() );
let table = tool::table_display::table_with_headers( initial[ 0 ].clone(), Vec::new() );
if let Some( table ) = table
{
write!( f, "{}", table )?;
Expand All @@ -133,24 +124,30 @@ impl std::fmt::Display for FramesReport
[
vec![ EMPTY_CELL.to_owned(), format!( "Updated frames: {}", self.updated_frames ) ],
vec![ EMPTY_CELL.to_owned(), format!( "Inserted frames: {}", self.new_frames ) ],
vec![ EMPTY_CELL.to_owned(), format!( "Number of frames in storage: {}", self.existing_frames ) ],
vec![ EMPTY_CELL.to_owned(), format!( "Number of frames in storage: {}", self.existing_frames + self.new_frames ) ],
];

if !self.selected_frames.selected_columns.is_empty()
{
rows.push( vec![ EMPTY_CELL.to_owned(), format!( "Selected frames:" ) ] );
}

let table = table_display::plain_table( rows );
let table = tool::table_display::plain_table( rows );
if let Some( table ) = table
{
write!( f, "{}", table )?;
}

for frame in &self.selected_frames.selected_rows
{
let first_row = vec!
[
INDENT_CELL.to_owned(),
self.selected_frames.selected_columns[ 0 ].clone(),
textwrap::fill( &String::from( frame[ 0 ].clone() ), 120 ),
];
let mut rows = Vec::new();
for i in 0..self.selected_frames.selected_columns.len()
for i in 1..self.selected_frames.selected_columns.len()
{
let inner_row = vec!
[
Expand All @@ -161,7 +158,7 @@ impl std::fmt::Display for FramesReport
rows.push( inner_row );
}

let table = table_display::plain_table( rows );
let table = tool::table_display::table_with_headers( first_row, rows );
if let Some( table ) = table
{
writeln!( f, "{}", table )?;
Expand All @@ -174,7 +171,7 @@ impl std::fmt::Display for FramesReport

impl Report for FramesReport {}

/// Items get from select query from storage.
/// Items retrieved by select queries from storage.
#[ derive( Debug ) ]
pub struct SelectedEntries
{
Expand Down Expand Up @@ -203,7 +200,7 @@ impl std::fmt::Display for SelectedEntries
{
for i in 0..self.selected_columns.len()
{
write!( f, "{} : {}, ", self.selected_columns[ i ], RowValue( &row[ i ] ) )?;
write!( f, "{} : {}, ", self.selected_columns[ i ], CellValue( &row[ i ] ) )?;
}
writeln!( f, "" )?;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
// entity - with all entities
// tool - with something not directly related to the problem, but convenient to have as a separate function/structure

// aaa: added folders

pub mod frame;
pub mod feed;
pub mod config;
pub mod query;
pub mod table;

// qqq : what is it for? purpose?
/// General report.
// aaa : added explanation
/// General report trait for commands return type.
pub trait Report : std::fmt::Display + std::fmt::Debug
{
/// Print report of executed command.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
//! Query command endpoint and report.
//! Query actions and report.
// qqq : don't use both
// aaa : fixed
use crate::*;
use super::*;
use gluesql::core::executor::Payload;
use storage::{ FeedStorage, Store };
use executor::FeedManager;
use error_tools::{ err, BasicError, Result };
use sled_adapter::{ FeedStorage, Store };
use action::Report;
use error_tools::Result;

/// Execute query specified in query string.
pub async fn execute_query
pub async fn query_execute
(
storage : FeedStorage< gluesql::sled_storage::SledStorage >,
args : &wca::Args,
mut storage : FeedStorage< gluesql::sled_storage::SledStorage >,
query_str : String,
) -> Result< impl Report >
{
let query = args
.get_owned::< Vec::< String > >( 0 )
.ok_or_else::< BasicError, _ >( || err!( "Cannot get Query argument for command .query.execute" ) )?
.join( " " )
;

let mut manager = FeedManager::new( storage );
manager.storage.execute_query( query ).await
storage.execute_query( query_str ).await
}

const EMPTY_CELL : &'static str = "";
Expand Down Expand Up @@ -68,7 +61,7 @@ impl std::fmt::Display for QueryReport
];
rows.push( new_row );
}
let table = table_display::plain_table( rows );
let table = tool::table_display::plain_table( rows );
if let Some( table ) = table
{
writeln!( f, "{}", table )?;
Expand All @@ -91,3 +84,4 @@ impl Report for QueryReport {}

// qqq : good tests for query action
// all tables should be touched by these tests
// aaa : added in https://github.com/Wandalen/wTools/pull/1284
Loading

0 comments on commit 7f6eaab

Please sign in to comment.