-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
86 lines (75 loc) · 2.46 KB
/
index.js
File metadata and controls
86 lines (75 loc) · 2.46 KB
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
const _ = require('lodash')
const git = require('nodegit')
const pronunciation = require('natural').SoundEx
const game = './tmp'
const branch = 'master'
const authors = new Set()
// Open the repository directory.
git.Repository.open(game)
// Open the master branch.
// Open branch, default to master.
.then(repo => repo.getBranchCommit(branch))
// .then(repo => repo.getMasterCommit())
// Display information about commits on master.
.then(firstCommit => {
// Create a new history event emitter.
const history = firstCommit.history()
// Listen for commit events from the history.
history.on('commit', commit => {
const author = commit.author()
// Can also use author.email()
authors.add({ hacker: author.name(), time: commit.date(), unix: commit.timeMs() })
})
history.on('end', () => printResults([...authors]))
// Start emitting events.
history.start()
})
.catch(console.error)
function printResults(commits) {
// Get times between commits.
commits.reduce((acc, curr) => {
if (curr.hacker === acc[acc.length - 1].hacker) {
return acc
}
acc.push({
hacker: curr.hacker,
unix: curr.unix - acc[acc.length - 1].unix
})
return acc
}, [{ hacker: '', unix: 0 }])
// Calc derivative.
commits.reduce((a, b) => {
b.delta = a.unix - b.unix
return b
})
const aggregated = commits.reduce((acc, curr) => {
acc[curr.hacker] ?
acc[curr.hacker] += curr.delta :
acc[curr.hacker] = curr.delta || 0
return acc
}, {})
const grupped = _.toPairs(aggregated)
const total = grupped.reduce((acc, curr) => acc + curr[1], 0)
const possession = grupped.map(e => ({ name: e[0], possession: Number((e[1] / total).toFixed(2)) * 100 }))
.filter(e => e.possession)
const merged = possession
// Sort hackers by name
.sort((a, b) => b.name - a.name)
// Merge hackers that have similar names ex: ć, ń => c, n
.reduce((acc, curr) => {
if (acc[acc.length - 1] && pronunciation.compare(acc[acc.length - 1].name, curr.name)) {
acc[acc.length - 1].possession += curr.possession
return acc
}
acc.push(curr)
return acc
}, [])
// Order hackers by a head possession
.sort((a, b) => b.possession - a.possession)
.map(e => {
e.possession = e.possession.toFixed(2)
return e
})
const results = _.extend({ players: merged.slice(0, 2) }, { name: `${game}:${branch}` })
console.log(JSON.stringify(results))
}