From 9ee3d9e8acc142e89e97c4629736cb295b133805 Mon Sep 17 00:00:00 2001 From: chenjiandongx Date: Sat, 26 Mar 2022 19:55:31 +0800 Subject: [PATCH] =?UTF-8?q?Add:=20=E6=96=B0=E5=A2=9E=20active=20table?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config.rs | 3 +++ src/executor.rs | 1 + src/fetcher.rs | 6 ++++++ src/record.rs | 45 +++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/config.rs b/src/config.rs index 968d581..8ab1294 100644 --- a/src/config.rs +++ b/src/config.rs @@ -22,6 +22,9 @@ pub struct Repository { pub branch: Option, pub remote: Option, pub path: String, + pub forks_count: Option, + pub stargazers_count: Option, + pub watchers_count: Option, } #[derive(Debug, Clone, Default, Deserialize, PartialEq, Eq, Hash)] diff --git a/src/executor.rs b/src/executor.rs index da1fca4..930deac 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -62,6 +62,7 @@ impl Executor { Self::register(&mut ctx, &c.dir, &c.db_name, record::RecordChange::name()).await?; Self::register(&mut ctx, &c.dir, &c.db_name, record::RecordTag::name()).await?; Self::register(&mut ctx, &c.dir, &c.db_name, record::RecordSnapshot::name()).await?; + Self::register(&mut ctx, &c.dir, &c.db_name, record::RecordActive::name()).await?; } Ok(ctx) } diff --git a/src/fetcher.rs b/src/fetcher.rs index b833e29..a7098e6 100644 --- a/src/fetcher.rs +++ b/src/fetcher.rs @@ -80,6 +80,9 @@ struct GithubRepoResponse { full_name: String, clone_url: String, default_branch: String, + forks_count: usize, + stargazers_count: usize, + watchers_count: usize, } impl GithubRepoFetcher { @@ -145,6 +148,9 @@ impl GithubRepoFetcher { .to_str() .unwrap() .to_string(), + forks_count: Some(repo.forks_count), + stargazers_count: Some(repo.stargazers_count), + watchers_count: Some(repo.watchers_count), }); } } diff --git a/src/record.rs b/src/record.rs index a594c22..d15d6d8 100644 --- a/src/record.rs +++ b/src/record.rs @@ -20,6 +20,7 @@ pub enum RecordType { Change(RecordChange), Tag(RecordTag), Snapshot(RecordSnapshot), + Active(RecordActive), } #[derive(Debug, Default, Serialize, Clone)] @@ -88,6 +89,20 @@ impl RecordSnapshot { } } +#[derive(Debug, Default, Serialize, Clone)] +pub struct RecordActive { + pub repo_name: String, + pub forks: usize, + pub watch: usize, + pub stars: usize, +} + +impl RecordActive { + pub fn name() -> String { + String::from("active") + } +} + fn datetime_rfc339(datetime: &str) -> String { match DateTime::parse_from_rfc2822(datetime) { Ok(t) => t.to_rfc3339().to_string(), @@ -184,6 +199,19 @@ impl CsvSerializer { Ok(()) } + async fn serialize_active(tx: Sender, repo: &Repository) -> Result<()> { + let record = RecordActive { + repo_name: repo.name.clone(), + forks: repo.forks_count.unwrap_or_default(), + watch: repo.watchers_count.unwrap_or_default(), + stars: repo.stargazers_count.unwrap_or_default(), + }; + if tx.send(RecordType::Active(record)).await.is_err() { + return Ok(()); + } + Ok(()) + } + async fn serialize_records( database: Database, author_mappings: Vec, @@ -229,6 +257,11 @@ impl CsvSerializer { exit(1) } + if let Err(e) = Self::serialize_active(tx.clone(), &repo).await { + println!("Failed to analyzer repo active, error: {}", e); + exit(1) + } + let mut lock = mutex.lock().unwrap(); *lock += 1; let n = lock; @@ -253,13 +286,16 @@ impl CsvSerializer { let mut tag_wtr = CsvWriter::try_new(&database.dir, RecordTag::name(), FLUSH_SIZE); let mut snapshot_wtr = CsvWriter::try_new(&database.dir, RecordSnapshot::name(), FLUSH_SIZE); + let mut active_wtr = + CsvWriter::try_new(&database.dir, RecordActive::name(), FLUSH_SIZE); while let Some(record) = rx.recv().await { match record { - RecordType::Commit(_) => commit_wtr.write(record), - RecordType::Change(_) => change_wtr.write(record), - RecordType::Tag(_) => tag_wtr.write(record), - RecordType::Snapshot(_) => snapshot_wtr.write(record), + RecordType::Commit(commit) => commit_wtr.write(commit), + RecordType::Change(change) => change_wtr.write(change), + RecordType::Tag(tag) => tag_wtr.write(tag), + RecordType::Snapshot(snapshot) => snapshot_wtr.write(snapshot), + RecordType::Active(active) => active_wtr.write(active), } } @@ -267,6 +303,7 @@ impl CsvSerializer { change_wtr.flush(); tag_wtr.flush(); snapshot_wtr.flush(); + active_wtr.flush(); }); for handle in handles {