Skip to content

Commit

Permalink
Performance improvement for csv-report (#136)
Browse files Browse the repository at this point in the history
* Remove multiple clones

* Use xlsxwriter instead of simple-excel-writer

* Use slice instead of vec

* Update CHANGELOG.md
  • Loading branch information
fxwiegand authored Mar 9, 2021
1 parent ca53616 commit f702008
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [0.19.7] - 2021-03-09
### Changed
- Performance improvement for `rbt csv-report`.

## [0.19.6] - 2021-03-09
### Changed
- Fixed a JSON syntax error in `rbt vcf-report`
- Fixed a JSON syntax error in `rbt vcf-report`.

## [0.19.5] - 2021-03-05
### Changed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ regex = "1.3"
tera = "1"
jsonm = "0.1.4"
chrono = "0.4"
simple_excel_writer="0.1.4"
xlsxwriter = "0.3.2"
lazy_static = "1.4"
anyhow = "1"
thiserror = "1"
Expand Down
43 changes: 19 additions & 24 deletions src/csv/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ use itertools::Itertools;
use lz_str::compress_to_utf16;
use serde_derive::Serialize;
use serde_json::json;
use simple_excel_writer::*;
use std::collections::{HashMap, HashSet};
use std::convert::TryInto;
use std::fs;
use std::io::Write;
use std::path::Path;
use std::str::FromStr;
use tera::{Context, Tera};
use xlsxwriter::*;

type LookupTable = HashMap<String, HashMap<String, Vec<(String, usize, usize)>>>;

Expand Down Expand Up @@ -85,11 +86,11 @@ pub(crate) fn csv_report(
for title in &titles {
match is_numeric.get(title) {
Some(true) => {
let plot = num_plot(table.clone(), title.to_string());
let plot = num_plot(&table, title.to_string());
num_plot_data.insert(title, plot);
}
Some(false) => {
let plot = nominal_plot(table.clone(), title.to_string());
let plot = nominal_plot(&table, title.to_string());
plot_data.insert(title, plot);
}
_ => unreachable!(),
Expand Down Expand Up @@ -118,28 +119,22 @@ pub(crate) fn csv_report(
(_, _) => {}
}

let mut wb = Workbook::create(&(output_path.to_owned() + "/report.xlsx"));
let mut sheet = wb.create_sheet("Report");
for _ in 1..titles.len() {
sheet.add_column(Column { width: 50.0 });
let wb = Workbook::new(&(output_path.to_owned() + "/report.xlsx"));
let mut sheet = wb.add_worksheet(Some("Report"))?;
for (i, title) in titles.iter().enumerate() {
sheet.write_string(0, i.try_into()?, title, None)?;
}

wb.write_sheet(&mut sheet, |sheet_writer| {
let sw = sheet_writer;
let mut title_row = Row::new();
for title in titles.clone() {
title_row.add_cell(title);
}
sw.append_row(title_row)?;
for row in table.clone() {
let mut excel_row = Row::new();
for title in titles.clone() {
excel_row.add_cell(row.get(title).unwrap().as_str());
}
sw.append_row(excel_row)?;
for (i, row) in table.iter().enumerate() {
for (c, title) in titles.iter().enumerate() {
sheet.write_string(
(i + 1).try_into()?,
c.try_into()?,
row.get(*title).unwrap(),
None,
)?;
}
Ok(())
})?;
}

wb.close()?;

Expand Down Expand Up @@ -341,7 +336,7 @@ pub(crate) fn csv_report(
Ok(())
}

fn num_plot(table: Vec<HashMap<String, String>>, column: String) -> Vec<BinnedPlotRecord> {
fn num_plot(table: &[HashMap<String, String>], column: String) -> Vec<BinnedPlotRecord> {
let mut values = Vec::new();
let mut nan = 0;
for row in table {
Expand Down Expand Up @@ -390,7 +385,7 @@ fn num_plot(table: Vec<HashMap<String, String>>, column: String) -> Vec<BinnedPl
plot_data
}

fn nominal_plot(table: Vec<HashMap<String, String>>, column: String) -> Vec<PlotRecord> {
fn nominal_plot(table: &[HashMap<String, String>], column: String) -> Vec<PlotRecord> {
let mut values = Vec::new();
for row in table {
let val = row.get(&column).unwrap();
Expand Down

0 comments on commit f702008

Please sign in to comment.