From d7ef40a72ac1abab209caef4fe0bc0959456c3c4 Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Fri, 29 Nov 2024 10:46:20 +0800 Subject: [PATCH] internal: use gz to compress historical json data --- .github/workflows/jobs.yml | 6 +++--- src/main.rs | 26 +++++++++++++++----------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/.github/workflows/jobs.yml b/.github/workflows/jobs.yml index 1981a03..9d04287 100644 --- a/.github/workflows/jobs.yml +++ b/.github/workflows/jobs.yml @@ -54,9 +54,9 @@ jobs: run: | git config --local user.name "github-actions[bot]" git config --local user.email "github-actions[bot]@users.noreply.github.com" - git add ./webapp/public/data.jsonl + git add ./webapp/public/*.gz git add ./repos.txt - git commit -m "Update data.jsonl" || echo "No changes to commit" + git commit -m "Update data" || echo "No changes to commit" - name: Push changes run: git push @@ -77,7 +77,7 @@ jobs: run: | pnpm run build sed -i '' 's|="/assets|="/moon-build-dashboard/assets|g' ./dist/index.html - sed -i '' 's|"/data.jsonl"|"/moon-build-dashboard/data.jsonl"|g' ./dist/assets/*.js + sed -i '' 's|"/latest_data.jsonl.gz"|"/moon-build-dashboard/latest_data.jsonl.gz"|g' ./dist/assets/*.js working-directory: ./webapp - name: Deploy to GitHub Pages diff --git a/src/main.rs b/src/main.rs index 1a04282..c2067fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -437,25 +437,22 @@ fn main0() -> anyhow::Result<()> { }; match res { Ok(dashboard) => { - let fp = std::fs::OpenOptions::new() - .create(true) - .append(true) - .open("webapp/public/data.jsonl")?; - let mut writer = std::io::BufWriter::new(fp); - writeln!(writer, "{}", serde_json::to_string(&dashboard)?)?; - writer.flush()?; + let date = Local::now().format("%Y-%m-%d"); + let filename = format!("webapp/public/{}_data.jsonl.gz", date); let fp = std::fs::OpenOptions::new() .create(true) .write(true) - .truncate(true) // make sure to truncate the file - .open("webapp/public/latest_data.jsonl.gz")?; - - // create a gzip encoder + .truncate(true) + .open(&filename)?; let encoder = GzEncoder::new(fp, Compression::default()); let mut writer = std::io::BufWriter::new(encoder); writeln!(writer, "{}", serde_json::to_string(&dashboard)?)?; writer.flush()?; + + let latest_filename = "webapp/public/latest_data.jsonl.gz"; + std::fs::copy(&filename, latest_filename)?; + Ok(()) } Err(e) => Err(anyhow::anyhow!(e)), @@ -465,3 +462,10 @@ fn main0() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> { main0() } + +#[test] +fn test_main() { + use chrono::Local; + let date = Local::now().format("%Y-%m-%d"); + println!("{}", date); +}