Skip to content

Add metrics #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/node_modules
.DS_Store
25 changes: 25 additions & 0 deletions metrics/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
This directory contains some scripts and data for generating metrics about the work of Open Web Docs.

It's got three subdirectories: *query*, *data*, and *analyse*.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
It's got three subdirectories: *query*, *data*, and *analyse*.
It's got three subdirectories: *query*, *data*, and *analyze*.


## query

This contains a script that will fetch all PRs from mdn/content and just log them as a huge JSON array. It takes a few minutes to run. You will need a [personal access token from GitHub](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) to use this script.

Then you pass the token in like this:

```
node query-prs.js MY-TOKEN-HERE
```

It's (obviously) advisable not to run this again and again, but to run it once, then analyse the results at your leisure.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
It's (obviously) advisable not to run this again and again, but to run it once, then analyse the results at your leisure.
It's (obviously) advisable not to run this again and again, but to run it once, then analyze the results at your leisure.


## data

This contains a data file called "organizations.json".

The "organizations.json" file contains an object each of whose keys represents an organization. The key is the organization name. Each organization object lists the GitHub usernames for people in that organization for each month we are interested in.

## analyse
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## analyse
## analyze


This contains a script that figures out, month by month, which PRs were files by members of a particular organization listed in "organizations.json". Note that it expects to see "prs.json" in "data", but that file is not checked into GitHub because it's too big. You'll need to get your own copy of that.
60 changes: 60 additions & 0 deletions metrics/analyse/analyse-prs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const fs = require('fs');

function initializeBuckets(json) {

const buckets = {
'2021-01': [],
'2021-02': [],
'2021-03': [],
'2021-04': [],
'2021-05': [],
'2021-06': [],
'2021-07': [],
'2021-08': [],
'2021-09': [],
'2021-10': [],
'2021-11': [],
'2021-12': [],
'2022-01': [],
'2022-02': [],
'2022-03': []
}

for (const pr of json) {
const prefix = pr.created_at.slice(0, 7);
const bucket = buckets[prefix];
if (!bucket) continue;
const creatorIndex = bucket.findIndex(contributor => contributor.name === pr.user.login);
if (creatorIndex === -1) {
bucket.push({
name: pr.user.login,
count: 1
});
} else {
bucket[creatorIndex].count++;
}
}

return buckets;
}

function analyse(buckets, organization) {

for (const bucketName of Object.keys(buckets)) {
const team = organization[bucketName];
const users = buckets[bucketName].filter(user => team.includes(user.name));
const contributions = users.reduce((previousValue, currentValue) => previousValue + currentValue.count, 0)
console.log(`${bucketName}: ${contributions}`);
}

}

const prJSON = fs.readFileSync('../data/prs.json', 'utf8');
const prs = JSON.parse(prJSON);

const buckets = initializeBuckets(prs);

const orgJSON = fs.readFileSync('../data/organizations.json', 'utf8');
const organizations = JSON.parse(orgJSON);

analyse(buckets, organizations['owd']);
19 changes: 19 additions & 0 deletions metrics/data/organizations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"owd": {
"2021-01": ["Elchi3"],
"2021-02": ["Elchi3", "wbamberg"],
"2021-03": ["Elchi3", "wbamberg"],
"2021-04": ["Elchi3", "wbamberg"],
"2021-05": ["Elchi3", "wbamberg"],
"2021-06": ["Elchi3", "wbamberg", "teoli2003"],
"2021-07": ["Elchi3", "wbamberg", "teoli2003"],
"2021-08": ["Elchi3", "wbamberg", "teoli2003", "estelle"],
"2021-09": ["Elchi3", "wbamberg", "teoli2003", "estelle"],
"2021-10": ["Elchi3", "wbamberg", "teoli2003", "estelle"],
"2021-11": ["Elchi3", "wbamberg", "teoli2003", "estelle"],
"2021-12": ["Elchi3", "wbamberg", "teoli2003", "estelle"],
"2022-01": ["Elchi3", "wbamberg", "teoli2003", "estelle", "queengooborg"],
"2022-02": ["Elchi3", "wbamberg", "teoli2003", "estelle", "queengooborg"],
"2022-03": ["Elchi3", "wbamberg", "teoli2003", "estelle", "queengooborg"]
}
}
10 changes: 10 additions & 0 deletions metrics/query/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "openwebdocs/metrics/query",
"version": "0.0.1",
"repository": "https://github.com/openwebdocs/scripts/",
"license": "MPL-2.0",
"author": "Open Web Docs",
"dependencies": {
"node-fetch": "^2.6.7"
}
}
38 changes: 38 additions & 0 deletions metrics/query/query-prs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const fetch = require('node-fetch');

const startURL = 'https://api.github.com/repos/mdn/content/pulls?state=all&per_page=100';

function getNextURL(link) {
const bits = link.split(",").map(bit => bit.trim());
const next = bits.find( bit => bit.split(';')[1] === ' rel=\"next\"');
if (next) {
return next.split(';')[0].slice(1, -1);
}
return null;
}

let all = [];

async function get(url, token) {

const requestHeaders = {
Authorization: `token ${token}`
};

const r = await fetch(url, {method:'GET', headers: requestHeaders});
const json = await r.json();
all = all.concat(json);

const link = r.headers.get("Link");
const next = getNextURL(link);
if (next) {
get(next, token);
} else {
console.log(JSON.stringify(all, null, '\t'));
}

}

const token = process.argv[2];

get(startURL, token);