Skip to content

Commit

Permalink
internal: use gz to compress json data
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash committed Nov 29, 2024
1 parent a4355a1 commit ab2c863
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
35 changes: 35 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ serde_json = "1.0.127"
tempfile = "3.12.0"
thiserror = "1.0.63"
walkdir = "2.5.0"
flate2 = "1.0.35"
13 changes: 13 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use chrono::{FixedOffset, Local};

use clap::Parser;
use colored::Colorize;
use flate2::{write::GzEncoder, Compression};
use moon_dashboard::{
cli,
dashboard::{
Expand Down Expand Up @@ -443,6 +444,18 @@ fn main0() -> anyhow::Result<()> {
let mut writer = std::io::BufWriter::new(fp);
writeln!(writer, "{}", serde_json::to_string(&dashboard)?)?;
writer.flush()?;

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
let encoder = GzEncoder::new(fp, Compression::default());
let mut writer = std::io::BufWriter::new(encoder);
writeln!(writer, "{}", serde_json::to_string(&dashboard)?)?;
writer.flush()?;
Ok(())
}
Err(e) => Err(anyhow::anyhow!(e)),
Expand Down
10 changes: 6 additions & 4 deletions webapp/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ interface BuildState {
}

async function get_data(): Promise<MoonBuildDashboard> {
const response = await fetch('/data.jsonl');
const response = await fetch('/latest_data.jsonl.gz', {
headers: {
'Accept-Encoding': 'gzip'
}
});
const text = await response.text();
const lines = text.split('\n').filter((line) => line.trim() !== '');
const parsedData = lines.map((line) => JSON.parse(line));
return parsedData[parsedData.length - 1];
return JSON.parse(text);
}

interface ModalProps {
Expand Down

0 comments on commit ab2c863

Please sign in to comment.