Skip to content

Commit

Permalink
Add: 新增 active table
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiandongx committed Mar 26, 2022
1 parent 30f8ffa commit 9ee3d9e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub struct Repository {
pub branch: Option<String>,
pub remote: Option<String>,
pub path: String,
pub forks_count: Option<usize>,
pub stargazers_count: Option<usize>,
pub watchers_count: Option<usize>,
}

#[derive(Debug, Clone, Default, Deserialize, PartialEq, Eq, Hash)]
Expand Down
1 change: 1 addition & 0 deletions src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
6 changes: 6 additions & 0 deletions src/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
});
}
}
Expand Down
45 changes: 41 additions & 4 deletions src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub enum RecordType {
Change(RecordChange),
Tag(RecordTag),
Snapshot(RecordSnapshot),
Active(RecordActive),
}

#[derive(Debug, Default, Serialize, Clone)]
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -184,6 +199,19 @@ impl CsvSerializer {
Ok(())
}

async fn serialize_active(tx: Sender<RecordType>, 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<AuthorMapping>,
Expand Down Expand Up @@ -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;
Expand All @@ -253,20 +286,24 @@ 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),
}
}

commit_wtr.flush();
change_wtr.flush();
tag_wtr.flush();
snapshot_wtr.flush();
active_wtr.flush();
});

for handle in handles {
Expand Down

0 comments on commit 9ee3d9e

Please sign in to comment.