Skip to content

Commit

Permalink
feat: support save summary for json file
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Jan 25, 2024
1 parent ae40f9e commit 5b47bdd
Show file tree
Hide file tree
Showing 6 changed files with 660 additions and 506 deletions.
40 changes: 20 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ license = "Apache-2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
axum = "0.7.3"
axum = "0.7.4"
axum-client-ip = "0.5.0"
bytes = "1.5.0"
bytesize = {version = "1.3.0", features = ["serde"]}
chrono = "0.4.31"
clap = { version = "4.4.14", features = ["derive"] }
chrono = "0.4.33"
clap = { version = "4.4.18", features = ["derive"] }
colored = "2.1.0"
config = "0.13.4"
crossterm = "0.27.0"
Expand All @@ -31,7 +31,7 @@ nanoid = "0.4.0"
once_cell = "1.19.0"
pad = "0.1.6"
ratatui = "0.25.0"
regex = "1.10.2"
regex = "1.10.3"
reqwest = { version = "0.11.23", default-features = false, features = ["rustls-tls", "json"] }
rust-embed = { version = "8.2.0", features = ["compression", "mime-guess"] }
serde = { version = "1.0.195", features = ["derive"] }
Expand All @@ -42,7 +42,7 @@ signal-hook-registry = "1.4.1"
snafu = "0.8.0"
substring = "1.4.5"
tar = "0.4.40"
tempfile = "3.8.0"
tempfile = "3.9.0"
time = "0.3.31"
tokio = { version = "1.35.1", features = ["macros", "rt", "rt-multi-thread", "net", "signal", "fs"] }
tokio-cron-scheduler = "0.9.4"
Expand Down
4 changes: 2 additions & 2 deletions src/image/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,14 @@ pub struct DockerAnalyzeResult {
pub big_modified_file_list: Vec<BigModifiedFileInfo>,
}

#[derive(Default, Debug, Clone, PartialEq)]
#[derive(Default, Debug, Clone, PartialEq, Serialize)]
pub struct ImageFileWastedSummary {
pub path: String,
pub total_size: u64,
pub count: u32,
}

#[derive(Default, Debug, Clone)]
#[derive(Default, Debug, Clone, Serialize)]
pub struct DockerAnalyzeSummary {
pub wasted_list: Vec<ImageFileWastedSummary>,
pub wasted_size: u64,
Expand Down
18 changes: 14 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use axum::{error_handling::HandleErrorLayer, middleware::from_fn, Router};
use bytesize::ByteSize;
use clap::Parser;
use colored::*;
use std::fs;
use std::net::SocketAddr;
use std::time::Duration;
use std::{env, str::FromStr};
Expand Down Expand Up @@ -41,6 +42,9 @@ struct Args {
/// The listen addr of web mode
#[arg(short, long, default_value = "127.0.0.1:7001")]
listen: String,
/// The result output file
#[arg(short, long)]
output_file: Option<String>,
}

impl Args {
Expand Down Expand Up @@ -98,14 +102,14 @@ fn is_ci() -> bool {
}

// 分析镜像(错误直接以字符串返回)
async fn analyze(image: String) -> Result<(), String> {
async fn analyze(image: String, output_file: String) -> Result<(), String> {
// 命令行模式下清除过期数据
clear_blob_files().await.map_err(|item| item.to_string())?;
let image_info = parse_image_info(&image);
let result = analyze_docker_image(image_info)
.await
.map_err(|item| item.to_string())?;
if is_ci() {
if is_ci() || !output_file.is_empty() {
let summary = result.summary();
let lowest_efficiency = (config::get_lowest_efficiency() * 100.0) as u64;
let highest_wasted_bytes = config::get_highest_wasted_bytes();
Expand Down Expand Up @@ -143,7 +147,13 @@ async fn analyze(image: String) -> Result<(), String> {
);
passed = false;
}
if !passed {
if !output_file.is_empty() {
fs::write(
output_file,
serde_json::to_string(&summary).map_err(|err| err.to_string())?,
)
.map_err(|err| err.to_string())?;
} else if !passed {
return Err("CI check fail".to_string());
}
} else {
Expand All @@ -161,7 +171,7 @@ async fn run() {
if let Some(value) = args.image {
TRACE_ID
.scope(generate_trace_id(), async {
if let Err(err) = analyze(value).await {
if let Err(err) = analyze(value, args.output_file.unwrap_or_default()).await {
error!(err, "analyze image fail");
std::process::exit(1)
}
Expand Down
10 changes: 5 additions & 5 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
"preview": "vite preview"
},
"dependencies": {
"antd": "^5.12.8",
"axios": "^1.6.5",
"antd": "^5.13.2",
"axios": "^1.6.6",
"pretty-bytes": "^6.1.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.2.47",
"@types/react": "^18.2.48",
"@types/react-dom": "^18.2.18",
"@vitejs/plugin-react-swc": "^3.5.0",
"prettier": "^3.1.1",
"prettier": "^3.2.4",
"typescript": "^5.3.3",
"vite": "^5.0.11"
"vite": "^5.0.12"
}
}
Loading

0 comments on commit 5b47bdd

Please sign in to comment.