Skip to content

Commit

Permalink
ci(l1, l2, levm): add crates loc summary to daily loc report (#1669)
Browse files Browse the repository at this point in the history
- Counts the loc of the crates
- Adds a new section to both the GitHub step message and the Slack
message listing the loc count of each crate (descendent).

## Slack message example

<img width="288" alt="image"
src="https://github.com/user-attachments/assets/fed1f4be-181f-429b-a2ce-a398b8a31902"
/>
  • Loading branch information
ilitteri authored Jan 9, 2025
1 parent 18ae6b9 commit c47b96a
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 11 deletions.
38 changes: 29 additions & 9 deletions cmd/loc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ use tokei::{Config, Language, LanguageType, Languages};

mod report;

fn count_crates_loc(crates_path: &PathBuf, config: &Config) -> Vec<(String, usize)> {
let mut ethrex_crates_loc: Vec<(String, usize)> = std::fs::read_dir(crates_path)
.into_iter()
.flatten()
.map(|crate_dir_entry| {
let crate_path = crate_dir_entry.unwrap().path();
let crate_loc = count_loc(crate_path.clone(), config);
(
crate_path.file_name().unwrap().to_str().unwrap().to_owned(),
crate_loc.code,
)
})
.collect::<Vec<(String, usize)>>();
ethrex_crates_loc.sort_by_key(|(_crate_name, loc)| *loc);
ethrex_crates_loc.reverse();
ethrex_crates_loc
}

fn count_loc(path: PathBuf, config: &Config) -> Language {
let mut languages = Languages::new();
languages.get_statistics(&[path], &["tests"], config);
Expand All @@ -17,16 +35,17 @@ fn main() {

let mut spinner = Spinner::new(Dots, "Counting lines of code...", Color::Cyan);

let ethrex = current_dir().unwrap();
let ethrex_crates = ethrex.join("crates");
let levm = ethrex_crates.join("vm");
let ethrex_l2 = ethrex_crates.join("l2");
let ethrex_path = current_dir().unwrap();
let ethrex_crates_path = ethrex_path.join("crates");
let levm_path = ethrex_crates_path.join("vm");
let ethrex_l2_path = ethrex_crates_path.join("l2");

let config = Config::default();

let ethrex_loc = count_loc(ethrex, &config);
let levm_loc = count_loc(levm, &config);
let ethrex_l2_loc = count_loc(ethrex_l2, &config);
let ethrex_loc = count_loc(ethrex_path, &config);
let levm_loc = count_loc(levm_path, &config);
let ethrex_l2_loc = count_loc(ethrex_l2_path, &config);
let ethrex_crates_loc = count_crates_loc(&ethrex_crates_path, &config);

spinner.success("Lines of code calculated!");

Expand All @@ -37,6 +56,7 @@ fn main() {
ethrex_l1: ethrex_loc.code - ethrex_l2_loc.code - levm_loc.code,
ethrex_l2: ethrex_l2_loc.code,
levm: levm_loc.code,
ethrex_crates: ethrex_crates_loc,
};

if opts.detailed {
Expand Down Expand Up @@ -85,11 +105,11 @@ fn main() {

let old_report: LinesOfCodeReport = std::fs::read_to_string("loc_report.json.old")
.map(|s| serde_json::from_str(&s).unwrap())
.unwrap_or(new_report);
.unwrap_or(new_report.clone());

std::fs::write(
"loc_report_slack.txt",
report::slack_message(old_report, new_report),
report::slack_message(old_report.clone(), new_report.clone()),
)
.unwrap();
std::fs::write(
Expand Down
84 changes: 82 additions & 2 deletions cmd/loc/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ pub struct LinesOfCodeReporterOptions {
pub compare_detailed: bool,
}

#[derive(Default, Serialize, Deserialize, Clone, Copy)]
#[derive(Default, Serialize, Deserialize, Clone)]
pub struct LinesOfCodeReport {
pub ethrex: usize,
pub ethrex_l1: usize,
pub ethrex_l2: usize,
pub levm: usize,
pub ethrex_crates: Vec<(String, usize)>,
}

pub fn pr_message(
Expand Down Expand Up @@ -84,6 +85,32 @@ pub fn slack_message(old_report: LinesOfCodeReport, new_report: LinesOfCodeRepor
let levm_diff = new_report.levm.abs_diff(old_report.levm);
let ethrex_diff_total = ethrex_l1_diff + ethrex_l2_diff + levm_diff;

let ethrex_crates_mrkdwn =
new_report
.ethrex_crates
.iter()
.fold(String::new(), |acc, (crate_name, loc)| {
let old_loc = old_report
.ethrex_crates
.iter()
.find(|(old_crate_name, _)| old_crate_name == crate_name)
.map(|(_, old_loc)| old_loc)
.unwrap_or(&0);

let loc_diff = loc.abs_diff(*old_loc);
format!(
"{}*{}*: {} {}\\n",
acc,
crate_name,
loc,
match loc.cmp(old_loc) {
std::cmp::Ordering::Greater => format!("(+{loc_diff})"),
std::cmp::Ordering::Less => format!("(-{loc_diff})"),
std::cmp::Ordering::Equal => "".to_string(),
}
)
});

format!(
r#"{{
"blocks": [
Expand All @@ -97,12 +124,33 @@ pub fn slack_message(old_report: LinesOfCodeReport, new_report: LinesOfCodeRepor
{{
"type": "divider"
}},
{{
"type": "header",
"text": {{
"type": "plain_text",
"text": "Summary"
}}
}},
{{
"type": "section",
"text": {{
"type": "mrkdwn",
"text": "*ethrex L1:* {} {}\n*ethrex L2:* {} {}\n*levm:* {} {}\n*ethrex (total):* {} {}"
}}
}},
{{
"type": "header",
"text": {{
"type": "plain_text",
"text": "Crates"
}}
}},
{{
"type": "section",
"text": {{
"type": "mrkdwn",
"text": "{}"
}}
}}
]
}}"#,
Expand Down Expand Up @@ -130,6 +178,7 @@ pub fn slack_message(old_report: LinesOfCodeReport, new_report: LinesOfCodeRepor
std::cmp::Ordering::Less => format!("(-{ethrex_diff_total})"),
std::cmp::Ordering::Equal => "".to_string(),
},
ethrex_crates_mrkdwn
)
}

Expand All @@ -139,14 +188,44 @@ pub fn github_step_summary(old_report: LinesOfCodeReport, new_report: LinesOfCod
let levm_diff = new_report.levm.abs_diff(old_report.levm);
let ethrex_diff_total = ethrex_l1_diff + ethrex_l2_diff + levm_diff;

let ethrex_crates_github =
new_report
.ethrex_crates
.iter()
.fold(String::new(), |acc, (crate_name, loc)| {
let old_loc = old_report
.ethrex_crates
.iter()
.find(|(old_crate_name, _)| old_crate_name == crate_name)
.map(|(_, old_loc)| old_loc)
.unwrap_or(&0);

let loc_diff = loc.abs_diff(*old_loc);
format!(
"{}{}: {} {}\n",
acc,
crate_name,
loc,
match loc.cmp(old_loc) {
std::cmp::Ordering::Greater => format!("(+{loc_diff})"),
std::cmp::Ordering::Less => format!("(-{loc_diff})"),
std::cmp::Ordering::Equal => "".to_string(),
}
)
});

format!(
r#"```
ethrex loc summary
====================
ethrex L1: {} {}
ethrex L2: {} {}
levm: {} ({})
levm: {} {}
ethrex (total): {} {}
ethrex crates loc
=================
{}
```"#,
new_report.ethrex_l1,
if new_report.ethrex > old_report.ethrex {
Expand All @@ -172,6 +251,7 @@ ethrex (total): {} {}
} else {
format!("(-{ethrex_diff_total})")
},
ethrex_crates_github
)
}

Expand Down

0 comments on commit c47b96a

Please sign in to comment.