summaryrefslogtreecommitdiffstats
path: root/ranking/src/authorrank.rs
blob: ea775e6a4b53190490e7b1efe6d9a8a953a45ecf (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
use rich_crate::RichCrate;
use std::collections::HashMap;
use kitchen_sink::{KitchenSink, CratesIndexCrate, Origin,, stopped};
use fxhash::FxHashMap;
use fxhash::FxHashSet;
use rayon::prelude::*;
use std::sync::Mutex;
use chrono::prelude::*;

#[derive(Default, Copy, Clone, Debug)]
pub struct CrateScore {
    /// Estimate of how much crate's author cares about the crate.
    /// This is just from author's perspective - doesn't need external validation.
    pub authors_perspective: f64,
    /// Crate's importance from users' perspective.
    /// Mainly based on external factors, which ideally
    /// should be somewhat harder to game by the author.
    pub users_perspective: f64,
}


pub fn do_author_pr(crates: &KitchenSink) -> Option<FxHashMap<String, f64>> {
    let crates_io_crates = crates.all_crates_io_crates();

    let (crate_pr, crate_ownership) = rayon::join(
        || get_crate_pr(&crates, crates_io_crates),
        || get_crate_ownership(&crates, crates_io_crates));

    let mut all_authors = FxHashMap::default();
    for (crate_name, crate_importance) in &crate_pr {
        if stopped() {return None;}

        if let Some(authors) = crate_ownership.get(crate_name) {
            for (author_name, &UserContrib {is_owner, contrib}) in authors {
                let author_info = all_authors.entry(author_name).or_insert_with(AuthorInfo::default);
                author_info.crates.insert(crate_name);

                // This is supposed to be author's total external validation
                // (author is important if users think their crates are important).
                // Weighed by ownership, so that minor contributors won't get all the glory for
                // someone else's crate.
                //
                // This can be computed for non-owning contributors too, because owners control who's in
                // (owner -> contributor relationship).
                // Spammy crates will have low scores, so even fake contributors won't add much noise.
                author_info.total_importance += crate_importance.users_perspective * contrib;

                // Passing trust to co-authors.
                // Non-owners can't pass trust to others (contributor -> owner),
                // because they could have been faked (e.g. added to git history against their will)
                if !is_owner {
                    continue;
                }

                // This is how much this author cares about this crate. The more they care, the more
                // trust it requires to collaborate with someone.
                let authors_perspective_relevance = crate_importance.authors_perspective * contrib;

                // Including self in coauthors to see how often author collaborates.
                // If they mostly own code, they don't trust anyone else!
                for (coauthor, UserContrib {contrib, ..}) in authors.iter() {
                    // contrib is already artificially decreased for non-owners.
                    // All values here will be normalized later.
                    let corelevance = authors_perspective_relevance * contrib;
                    *author_info.coauthors.entry(coauthor).or_insert(0.) += corelevance;
                }
            }
        }
    }

    all_authors.par_iter_mut().for_each(|(login, author_info)| {
        if stopped() {return;}

        // this magic number is here only to reduce useless traffic to github
        // (assuming minor authors aren't part of major orgs anyway)
        if author_info.total_importance > 4. {
            // We trust some github orgs, so we trust their members
            if let Ok(Some(orgs)) = crates.user_github_orgs(login) {
                for org in orgs {
                    author_info.total_importance += org_trust(&org.login);
                }
            }
        }

        // normalize weights, since its using authors_perspective scores
        // to divide trust among co-authors
        let coauthors = &mut author_info.coauthors;
        let to_give_sum = coauthors.values().cloned().sum::<f64>().max(0.001); // div/0
        for (_, w) in coauthors {
            *w /= to_give_sum;
        }
    });

    // and now spread users_perspective score based on authors_perspective trust
    let mut final_score: FxHashMap<String, f64> = FxHashMap::with_capacity_and_hasher(all_authors.len(), Default::default());
    for (author_name, author_info) in all_authors {
        *final_score.entry(author_name.to_string()).or_insert(0.) += author_info.total_importance;
        for (coauthor, trust_weight) in author_info.coauthors {
            *final_score.entry(coauthor.to_string()).or_insert(0.) += trust_weight * author_info.total_importance;
        }
    }
    Some(final_score)
}

struct UserContrib {
    contrib: f64,
    is_owner: bool,
}

/// For each crate list of all users (by their github login) who contributed to that crate
/// along with percentage of how much each user "owns" the crate, based on crates.io ownership,
/// weighed by approximate code contribution (derived from number of commits)
fn get_crate_ownership<'a>(crates: &KitchenSink, crates_io_crates: &'a FxHashMap<Origin, CratesIndexCrate>) -> FxHashMap<&'a str, FxHashMap<String, UserContrib>> {
    let crate_ownership = Mutex::new(FxHashMap::<&str, FxHashMap<String, UserContrib>>::default());
    crates_io_crates.par_iter().for_each(|(origin, k1)| {
        if stopped() {return;}

        let name = k1.name();
        let k = match crates.rich_crate_version(origin, CrateData::Minimal) {
            Ok(k) => k,
            Err(e) => {
                for c in e.iter_chain() {
                    eprintln!("• error: -- {}", c);
                }
                return;
            },
        };

        if let Ok((authors, owners, ..)) = crates.all_contributors(&k) {
            let mut user_contributions: FxHashMap<_,_> = owners.into_iter().chain(authors)
                .filter_map(|a| {
                    let is_owner = a.owner;
                    let contrib = a.contribution;
                    a.github.map(|gh_user| (gh_user.login.to_ascii_lowercase(), UserContrib {is_owner, contrib}))
                })
                .filter(|(login, _)| {
                    // ignore rust-bus, nursery, and other orgs that are ownership backups, not contributors
                    is_a_real_author(login)
                })
                .collect();
            // contribution value based on commits is rather arbitrary, so normalize its range,
            // so that we can reliably mix it with values based on ownership.
            normalize_contribution(&mut user_contributions);

            for (_, c) in &mut user_contributions {
                c.contrib =
                        // non-owner contributors get very little trust,
                        // since accepting someone's PR is not as serious as giving access
                        c.contrib * if c.is_owner {1.} else {0.05}
                        // even non-contributing owners should get some trust,
                        // since they still have write access
                        + if c.is_owner {0.2} else {0.};
            }
            normalize_contribution(&mut user_contributions);
            crate_ownership.lock().unwrap().insert(name, user_contributions);
        }
    });

    crate_ownership.into_inner().unwrap()
}

fn normalize_contribution(contrib: &mut FxHashMap<String, UserContrib>) {
    let total_contrib = contrib.values().map(|o| o.contrib).sum::<f64>();
    if total_contrib > 0. {
        for c in contrib.values_mut() {
            c.contrib /= total_contrib;
        }
    }
}

#[derive(Default, Debug)]
struct AuthorInfo<'a> {
    total_importance: f64,
    coauthors: HashMap<&'a str, f64>, // github login name => degree of cooperation
    crates: FxHashSet<&'a str>,
}

// there are orgs which are collections of crates, but aren't actual contributors
fn is_a_real_author(login: &str) -> bool {
    match login {
        "rust-bus" | "rust-bus-owner" | "rust-lang-nursery" | "rust-lang-deprecated" => false,
        _ => true,
    }
}

// We trust members in some github orgs (there's some vetting required to get in these orgs)
fn org_trust(login: &str) -> f64 {
    match login {
        "maintainers" => 5.0, // general github
        "rust-lang-deprecated" | "rust-lang-nursery" |
        "rust-community" | "rust-embedded" | "mozilla-standards" => 10.0,
        "google" | "rustwasm" | "integer32llc" | "rustbridge" => 50.0,
        "mozilla" | "servo" => 200.0,
        "rust-lang" => 1000.0,
        _ => 0.,
    }
}

/// Score how many owners is right (on 0..=1 scale)
fn bus_factor_score(k: &RichCrate) -> f64 {
    let num_owners = k.owners().len();
    match num_owners {
        1 => 0.1, // meh
        n @ 2..=5 => n as f64 * 0.2, // good
        n => (1. - (n-5) as f64 * 0.05).max(0.) // suspicious
    }
}

fn time_between_first_and_last_version(k: &RichCrate) -> chrono::Duration {
    let mut max = parse_date("1970-01-01");
    let mut min = parse_date("2222-01-01");
    for v in k.versions() {
        let created = parse_date(&v.created_at);
        if created < min {
            min = created;
        }
        </