forked from dtolnay/db-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user-downloads.rs
75 lines (65 loc) · 2.16 KB
/
user-downloads.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! $ cargo run --release --example user-downloads
//!
//! Computes time series of the fraction of crates.io downloads attributed to a
//! single given user's crates.
use chrono::{Date, Utc};
use std::collections::{BTreeMap as Map, BTreeSet as Set};
const USER: &str = "dtolnay";
#[derive(Default)]
struct Downloads {
theirs: u64,
all: u64,
}
fn main() -> db_dump::Result<()> {
let mut user_id = None;
let mut crate_owners = Vec::new();
let mut versions = Vec::new();
let mut version_downloads = Vec::new();
db_dump::Loader::new()
.users(|row| {
if row.gh_login == USER {
user_id = Some(row.id);
}
})
.crate_owners(|row| crate_owners.push(row))
.versions(|row| versions.push(row))
.version_downloads(|row| version_downloads.push(row))
.load("./db-dump.tar.gz")?;
// User id of the crate author we care about.
let user_id = user_id.expect("no such user");
// Set of crate ids currently owned by that user.
let mut their_crates = Set::new();
for crate_owner in crate_owners {
if crate_owner.owner_id == user_id {
their_crates.insert(crate_owner.crate_id);
}
}
// Set of version ids of all of those crates.
let mut their_versions = Set::new();
for version in versions {
if their_crates.contains(&version.crate_id) {
their_versions.insert(version.id);
}
}
// Add up downloads across that user's crates, as well as total downloads of
// all crates.
let mut downloads = Map::<Date<Utc>, Downloads>::new();
for stat in version_downloads {
let entry = downloads.entry(stat.date).or_default();
entry.all += stat.downloads;
if their_versions.contains(&stat.version_id) {
entry.theirs += stat.downloads;
}
}
// Print user's downloads as a fraction of total crates.io downloads by day.
for (date, downloads) in downloads {
if downloads.theirs > 0 {
println!(
"{},{}",
date,
downloads.theirs as f64 / downloads.all as f64,
);
}
}
Ok(())
}