-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: wrote first draft of statistics logging
- Loading branch information
1 parent
37a3e72
commit b563994
Showing
10 changed files
with
221 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use std::time::{SystemTime, SystemTimeError, UNIX_EPOCH}; | ||
|
||
use harper_core::linting::LintKind; | ||
use serde::{Deserialize, Serialize}; | ||
use uuid::Uuid; | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
pub struct LintRecord { | ||
pub kind: LintKind, | ||
/// Recorded as seconds from the Unix Epoch | ||
pub when: u64, | ||
pub uuid: Uuid, | ||
} | ||
|
||
impl LintRecord { | ||
/// Record a new instance at the current system time. | ||
pub fn record_now(kind: LintKind) -> Result<Self, SystemTimeError> { | ||
Ok(Self { | ||
kind, | ||
when: SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs(), | ||
uuid: Uuid::new_v4(), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use std::collections::HashMap; | ||
|
||
use harper_core::linting::LintKind; | ||
|
||
pub struct LintSummary { | ||
counts: HashMap<LintKind, usize>, | ||
} | ||
|
||
impl LintSummary { | ||
pub fn new() -> Self { | ||
Self { | ||
counts: HashMap::new(), | ||
} | ||
} | ||
|
||
/// Increment the count for a particular lint kind. | ||
pub fn inc(&mut self, kind: LintKind) { | ||
self.counts | ||
.entry(kind) | ||
.and_modify(|counter| *counter += 1) | ||
.or_insert(1); | ||
} | ||
|
||
/// Get the count for a particular lint kind. | ||
pub fn get(&self, kind: LintKind) -> usize { | ||
self.counts.get(&kind).copied().unwrap_or(0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
mod lint_record; | ||
mod lint_summary; | ||
|
||
use std::io::Write; | ||
|
||
pub use lint_record::LintRecord; | ||
pub use lint_summary::LintSummary; | ||
use tokio::io; | ||
|
||
pub struct Stats { | ||
/// A record of the lints the user has applied. | ||
lints_applied: Vec<LintRecord>, | ||
} | ||
|
||
impl Stats { | ||
pub fn new() -> Self { | ||
Self { | ||
lints_applied: Vec::new(), | ||
} | ||
} | ||
|
||
/// Count the number of each kind of lint applied. | ||
pub fn summarize_lints_applied(&self) -> LintSummary { | ||
let mut summary = LintSummary::new(); | ||
|
||
for lint in &self.lints_applied { | ||
summary.inc(lint.kind); | ||
} | ||
|
||
summary | ||
} | ||
|
||
pub fn lint_applied(&mut self, record: LintRecord) { | ||
self.lints_applied.push(record); | ||
} | ||
|
||
pub fn write_csv(&self, w: &mut impl Write) -> io::Result<()> { | ||
let mut writer = csv::WriterBuilder::new().has_headers(false).from_writer(w); | ||
|
||
for record in &self.lints_applied { | ||
writer.serialize(record)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl Default for Stats { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} |