From c93fc0ae59adfdbfe242e756a012c27944e258bf Mon Sep 17 00:00:00 2001 From: JesusPerez Date: Tue, 19 Oct 2021 21:03:37 +0100 Subject: [PATCH] chore: update handlers --- src/handlers/new_h_home.rs | 229 ------------------------------------- 1 file changed, 229 deletions(-) delete mode 100644 src/handlers/new_h_home.rs diff --git a/src/handlers/new_h_home.rs b/src/handlers/new_h_home.rs deleted file mode 100644 index daf52b8..0000000 --- a/src/handlers/new_h_home.rs +++ /dev/null @@ -1,229 +0,0 @@ -use std::collections::{HashMap,BTreeMap}; -use warp::{ - // http::{StatusCode}, - http::{method::Method, HeaderMap, HeaderValue}, - Reply, Rejection, -}; -use reqenv::ReqEnv; -// use app_env::profile::Profile; -use app_auth::{UserCtx}; - -// use kloud::{ -// defs::{ -// KloudQueryConfigFilters, -// }, -// }; -use clds::defs::{KloudCheckHome,CloudCheckItem}; -use clds::status::{get_statusinfo_fileslist,load_statusinfo}; -use clds::clouds::defs::{Cloud,SrvcsHostInfOut,AppOut,AppsrvcInfo,AppsrvcInfOut, TskSrvcOut, TsksrvcInfo, TsksrvcInfOut}; -use crate::defs::{KldCheck, MapCheckInfo, DataDBs}; // ,CollsData}; -use clds::clouds::on_clouds::{get_cloud_check,get_apps_check}; -use app_env::appenv::{Rol,Policy}; - -pub fn tsks_info_on_role(role: String, tsksrvcs: Vec) -> Vec { - tsksrvcs.into_iter().map(|tsk| { - let srvc = match role.as_str() { - "admin" => - TskSrvcOut { - name: tsk.srvc.name.to_owned(), - path: tsk.srvc.path.to_owned(), - req: tsk.srvc.req.to_owned(), - target: tsk.srvc.target.to_owned(), - liveness: tsk.srvc.liveness.to_owned(), - critical: tsk.srvc.critical.to_owned(), - }, - _ => - TskSrvcOut { - name: tsk.srvc.name.to_owned(), - path: String::from(""), - req: String::from(""), - target: tsk.srvc.target.to_owned(), - liveness: String::from(""), - critical: tsk.srvc.critical.to_owned(), - }, - }; - TsksrvcInfOut { - name: tsk.name.to_owned(), - info: tsk.info.to_owned(), - srvc - } - }).collect() -} -pub fn app_info_on_role(role: String, appsrvcs: Vec) -> Vec { - appsrvcs.into_iter().map(|app| { - let srvc = match role.as_str() { - "admin" => - AppOut { - name: app.srvc.name.to_owned(), - path: app.srvc.path.to_owned(), - req: app.srvc.req.to_owned(), - target: app.srvc.target.to_owned(), - liveness: app.srvc.liveness.to_owned(), - critical: app.srvc.critical.to_owned(), - }, - _ => - AppOut { - name: app.srvc.name.to_owned(), - path: String::from(""), - req: String::from(""), - target: app.srvc.target.to_owned(), - liveness: String::from(""), - critical: app.srvc.critical.to_owned(), - }, - }; - AppsrvcInfOut { - name: app.name.to_owned(), - info: app.info.to_owned(), - srvc - } - }).collect() -} -pub fn collect_clouds_check_items(target: String,role: &str, items: &Vec ) -> BTreeMap> { - let mut m_items = BTreeMap::new(); - items.iter().for_each(|itm| { - let liveness: Vec; - liveness = itm.liveness.to_owned().into_iter().map(|it| - match target.as_str() { - "apps" => - SrvcsHostInfOut { - hostname: format!("{}",&it.hostname), - tsksrvcs: Vec::new(), - appsrvcs: app_info_on_role(role.to_owned(), it.appsrvcs), - }, - _ => - SrvcsHostInfOut { - hostname: format!("{}",&it.hostname), - tsksrvcs: tsks_info_on_role(role.to_owned(), it.tsksrvcs), - appsrvcs: Vec::new(), - } - } - ).collect(); - m_items.insert(format!("{}",&itm.name),liveness); - }); - m_items -} -pub async fn get_tsks_apps_check(reqenv: &ReqEnv,_auth: UserCtx, role: &str) -> String { - let result_apps = get_apps_check(&reqenv).await; - // println!("Result apps: {}",&result_apps); - let res_apps: Vec = serde_json::from_str(&result_apps).unwrap_or_else(|e| { - println!("Error serde apps json: {}",e); - Vec::new() - }); - let result_cloud = get_cloud_check(&reqenv).await; - // println!("Result cloud: {}",&result_cloud); - let res_cloud: Vec = serde_json::from_str(&result_cloud).unwrap_or_else(|e| { - println!("Error serde apps json: {}",e); - Vec::new() - }); - let mut kld_check: Vec = Vec::new(); - res_apps.iter().for_each(|kld| { - // let mut list_groups = String::from(""); - let mut grp_check_hash: HashMap = HashMap::new(); - kld.groups.iter().for_each(|grp| { - // if !list_groups.is_empty() { - // list_groups.push(','); - // } - // list_groups.push_str(&grp.name); - grp_check_hash.insert(format!("{}",grp.name),collect_clouds_check_items(String::from("apps"),&role,&grp.items)); - }); - kld_check.push(KldCheck { - name: kld.name.to_owned(), - cloud: HashMap::new(), - apps: grp_check_hash, - infos: Vec::new(), - }); - }); - for kld in res_cloud { - let mut grp_check_hash: HashMap = HashMap::new(); - kld.groups.iter().for_each(|grp| { - grp_check_hash.insert(format!("{}",grp.name),collect_clouds_check_items(String::from("tsks"),&role,&grp.items)); - }); - let kld_statusinfo_files: Vec = get_statusinfo_fileslist(&reqenv.config().state_path,&kld.name).await.unwrap_or_else(|e|{ - println!("Error on infos {}: {}",&kld.name,e); - Vec::new() - }); - // dbg!(&kld_statusinfo_files); - let infos = load_statusinfo(&format!("{}/{}",&reqenv.config().state_path,&kld.name), kld_statusinfo_files).await; - //dbg!(&infos); - if kld_check.len() == 0 { - kld_check.push(KldCheck { - name: kld.name.to_owned(), - apps: HashMap::new(), - cloud: grp_check_hash, - infos, - }); - } else { - let mut kldix = 0; - kld_check.iter().enumerate().for_each(|(idx,k)| { - if k.name == kld.name { - kldix = idx; - } - }); - kld_check[kldix].cloud = grp_check_hash.to_owned(); - kld_check[kldix].infos = infos.to_owned(); - } - }; - // let result = format!("{}:{}{}:{}{}",r#"{"group_apps""#,&res_apps_json,r#","group_cloud""#,&res_cloud_json,r#"}"#); - let result=serde_json::to_string(&kld_check).unwrap_or_else(|e|{ - println!("Error serde from value: {}",e); - String::from("") - }); - result.to_owned() -} -pub async fn home( - header: HeaderMap, - method: Method, -// user: UserCtx, - db: DataDBs, - _cloud: Cloud, - prfx: String, -// headers: warp::http::HeaderMap -// path: warp::path::FullPath, headers: warp::http::HeaderMap -) -> Result { - let reqenv = ReqEnv::new(db.app, db.auth, header, method, "/home", "home", &prfx); - // let allow_origin = reqenv.websrvr().allow_origin; - // let opts = KloudQueryConfigFilters::default(); - // let source = format!("{}/{}","ma",""); - match reqenv.user_authentication().await { - Ok(auth) => { - // dbg!("auth: {}",&auth); - // println!("User: {} | {}",&user.user_id,&user.token); - // if let Some(lang) = reqtasks.params().get("lang") { - // res.render_json_text(&get_lang_items_str("langs",req_lang,"yaml")) - // } else { - // res.render_json_text(&get_lang_items_str("langs",&reqtasks.lang(),"yaml")) - // } - // log::debug!("LANG: {} - {}",language, lang); - // dbg!("LANG: {} - {}",language, lang); - let role = reqenv.req.user_role().await; - // reqenv.is_admin() - let result = get_tsks_apps_check(&reqenv,auth,&role).await; - // let mut path = format!("{}/profiles/{}/{}/defs.yaml",reqenv.websrvr().resources_path,&prfx,&auth.user_id); - // if ! std::path::Path::new(&path).exists() { - // path = format!("{}/profiles/{}/defs.yaml",reqenv.websrvr().resources_path,&prfx); - // } - // let content = Profile::load_fs_content(path.into()); - - // let lang = opts.lang.unwrap_or_else(|| String::from("es")); - // let section = opts.section.unwrap_or_else(|| String::from("")); - // let lang_items = LangItems::new("langs/ta",&lang,"yaml"); - // let result = lang_items.get_items_str(§ion); - - // let res = Profile::to_yaml(content); // String::from(""); - // let result = serde_json::to_string(&res).unwrap_or_else(|_| String::from("")); - - - Ok(warp::http::Response::builder() - .body(result.to_string()) - .into_response()) - }, - Err(_e) => { - // let result = format!("Error: no credentials found"); - // println!("{}",e); - let result = get_tsks_apps_check(&reqenv,UserCtx::default(),"").await; - Ok(warp::http::Response::builder() - .body(result.to_string()) - .into_response()) - } - } -}