Skip to content

Commit

Permalink
add progress to file info
Browse files Browse the repository at this point in the history
  • Loading branch information
rawdaGastan committed Aug 6, 2024
1 parent 04c021e commit d5b217d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 15 deletions.
1 change: 1 addition & 0 deletions fl-server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct Job {
#[derive(Debug, ToSchema)]
pub struct AppState {
pub jobs_state: Mutex<HashMap<String, handlers::FlistState>>,
pub flists_progress: Mutex<HashMap<String, f32>>,
}

#[derive(Debug, Default, Clone, Deserialize)]
Expand Down
28 changes: 22 additions & 6 deletions fl-server/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ pub async fn create_flist_handler(
job.id.clone(),
FlistState::Accepted(format!("flist '{}' is accepted", fl_name)),
);
state
.flists_progress
.lock()
.unwrap()
.insert(fl_path.clone(), 0.0);

tokio::spawn(async move {
state.jobs_state.lock().unwrap().insert(
Expand All @@ -186,6 +191,7 @@ pub async fn create_flist_handler(

let res = docker_to_fl.prepare().await;
if res.is_err() {
let _ = tokio::fs::remove_file(&fl_path).await;
state
.jobs_state
.lock()
Expand All @@ -197,19 +203,25 @@ pub async fn create_flist_handler(
let files_count = docker_to_fl.files_count();
let st = state.clone();
let job_id = job.id.clone();
let cloned_fl_path = fl_path.clone();
tokio::spawn(async move {
let mut progress: f32 = 0.0;

for _ in 0..files_count - 1 {
let step = rx.recv().unwrap() as f32;
progress += step;
let progress_percentage = progress / files_count as f32 * 100.0;
st.jobs_state.lock().unwrap().insert(
job_id.clone(),
FlistState::InProgress(FlistStateInfo {
msg: "flist is in progress".to_string(),
progress: progress / files_count as f32 * 100.0,
progress: progress_percentage,
}),
);
st.flists_progress
.lock()
.unwrap()
.insert(cloned_fl_path.clone(), progress_percentage);
}
});

Expand All @@ -230,10 +242,11 @@ pub async fn create_flist_handler(
state.jobs_state.lock().unwrap().insert(
job.id.clone(),
FlistState::Created(format!(
"flist {}:{}/{}/{}/{} is created successfully",
cfg.host, cfg.port, cfg.flist_dir, username, fl_name
"flist {}:{}/{} is created successfully",
cfg.host, cfg.port, fl_path
)),
);
state.flists_progress.lock().unwrap().insert(fl_path, 100.0);
});

Ok(ResponseResult::FlistCreated(current_job))
Expand Down Expand Up @@ -311,16 +324,19 @@ pub async fn get_flist_state_handler(
)
)]
#[debug_handler]
pub async fn list_flists_handler(Extension(cfg): Extension<config::Config>) -> impl IntoResponse {
pub async fn list_flists_handler(
Extension(cfg): Extension<config::Config>,
State(state): State<Arc<config::AppState>>,
) -> impl IntoResponse {
let mut flists: HashMap<String, Vec<FileInfo>> = HashMap::new();

let rs = visit_dir_one_level(std::path::Path::new(&cfg.flist_dir)).await;
let rs = visit_dir_one_level(std::path::Path::new(&cfg.flist_dir), &state).await;
match rs {
Ok(files) => {
for file in files {
if !file.is_file {
let flists_per_username =
visit_dir_one_level(std::path::Path::new(&file.path_uri)).await;
visit_dir_one_level(std::path::Path::new(&file.path_uri), &state).await;
match flists_per_username {
Ok(files) => flists.insert(file.name, files),
Err(e) => {
Expand Down
1 change: 1 addition & 0 deletions fl-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ async fn app() -> Result<()> {

let app_state = Arc::new(config::AppState {
jobs_state: Mutex::new(HashMap::new()),
flists_progress: Mutex::new(HashMap::new()),
});

let cors = CorsLayer::new()
Expand Down
50 changes: 41 additions & 9 deletions fl-server/src/serve_flists.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use askama::Template;
use axum::response::{Html, Response};
use axum::{
extract::State,
response::{Html, Response},
};
use serde::Serialize;
use std::path::PathBuf;
use std::{path::PathBuf, sync::Arc};
use tokio::io;
use tower::util::ServiceExt;
use tower_http::services::ServeDir;
Expand All @@ -15,8 +18,13 @@ use axum::{
use axum_macros::debug_handler;
use percent_encoding::percent_decode;

use crate::config;

#[debug_handler]
pub async fn serve_flists(req: Request<Body>) -> impl IntoResponse {
pub async fn serve_flists(
State(state): State<Arc<config::AppState>>,
req: Request<Body>,
) -> impl IntoResponse {
let path = req.uri().path().to_string();

return match ServeDir::new("").oneshot(req).await {
Expand Down Expand Up @@ -45,7 +53,7 @@ pub async fn serve_flists(req: Request<Body>) -> impl IntoResponse {

match cur_path.is_dir() {
true => {
let rs = visit_dir_one_level(&full_path).await;
let rs = visit_dir_one_level(&full_path, &state).await;
match rs {
Ok(files) => Ok(DirListTemplate {
lister: DirLister { files },
Expand Down Expand Up @@ -77,24 +85,47 @@ pub async fn serve_flists(req: Request<Body>) -> impl IntoResponse {
};
}

pub async fn visit_dir_one_level(path: &std::path::Path) -> io::Result<Vec<FileInfo>> {
pub async fn visit_dir_one_level(
path: &std::path::Path,
state: &Arc<config::AppState>,
) -> io::Result<Vec<FileInfo>> {
let mut dir = tokio::fs::read_dir(path).await?;
let mut files: Vec<FileInfo> = Vec::new();

while let Some(child) = dir.next_entry().await? {
let the_uri_path = child.path().to_string_lossy().to_string();
let path_uri = child.path().to_string_lossy().to_string();
let is_file = child.file_type().await?.is_file();
let name = child.file_name().to_string_lossy().to_string();

let mut progress = 0.0;
if is_file {
match state.flists_progress.lock().unwrap().get(&format!(
"{}/{}",
path.to_string_lossy().to_string(),
name
)) {
Some(p) => progress = p.to_owned(),
None => progress = 100.0,
}

let ext = child.path().extension().unwrap().to_string_lossy().to_string();
if ext != "fl" {
continue;
}
}

files.push(FileInfo {
name: child.file_name().to_string_lossy().to_string(),
path_uri: the_uri_path,
is_file: child.file_type().await?.is_file(),
name,
path_uri,
is_file,
last_modified: child
.metadata()
.await?
.modified()?
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs() as i64,
progress,
});
}

Expand Down Expand Up @@ -149,6 +180,7 @@ pub struct FileInfo {
pub path_uri: String,
pub is_file: bool,
pub last_modified: i64,
pub progress: f32,
}

#[derive(Template)]
Expand Down

0 comments on commit d5b217d

Please sign in to comment.