Skip to content

Commit e30f4d6

Browse files
author
Robb Shecter
committed
convert metrics.js to cf
1 parent 56b989f commit e30f4d6

File tree

9 files changed

+262
-168
lines changed

9 files changed

+262
-168
lines changed

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ <h4>Break-down</h4>
224224
<script src="bower_components/octokat/dist/octokat.js"></script>
225225
<script src="lib/core.js"></script>
226226
<script src="src/octokat-ext.js"></script>
227-
<script src="src/metrics.js"></script>
227+
<script src="lib/metrics.js"></script>
228228
<script src="lib/github.js"></script>
229229
<script src="lib/repo.js"></script>
230230
<script src="lib/ui.js"></script>

lib/metrics.js

+113
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/spec/test.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ui.js

+9-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/metrics.coffee

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
class App.Metrics
2+
3+
@repoEffectivenessIcon = (repo) ->
4+
rating = @repoEffectiveness(repo)
5+
return "fa-frown-o" if rating >= 0 and rating < 3
6+
return "fa-meh-o" if rating >= 3 and rating < 4.5
7+
return "fa-smile-o" if rating >= 4.5 and rating < 7
8+
return "fa-smile-o green-glow" if rating >= 7 and rating <= 10
9+
throw new RangeError "Rating must be between 0 and 10"
10+
11+
12+
@repoEffectivenessDesc = (repo) ->
13+
@effectivenessDesc @repoEffectiveness(repo)
14+
15+
16+
@repoEffectiveness = (repo) ->
17+
@effectiveness(
18+
repo.closedPullRequestCount(),
19+
repo.openPullRequestCount(),
20+
repo.closedIssueCount(),
21+
repo.openIssueCount())
22+
23+
24+
@effectiveness = (merged_prs, proposed_prs, closed_issues, new_issues) ->
25+
inputs = [ merged_prs, proposed_prs, closed_issues, new_issues ].join(", ")
26+
prs = @pr_effectiveness(merged_prs, proposed_prs)
27+
issues = @issue_effectiveness(closed_issues, new_issues)
28+
(0.66 * prs) + (0.34 * issues)
29+
30+
31+
@prEffectiveness = (repo) ->
32+
@pr_effectiveness(
33+
repo.closedPullRequestCount(),
34+
repo.openPullRequestCount())
35+
36+
37+
@issueEffectiveness = (repo) ->
38+
@issue_effectiveness repo.closedIssueCount(), repo.openIssueCount()
39+
40+
41+
@pr_effectiveness = (merged_prs, proposed_prs) ->
42+
@scaled @ratio(merged_prs, proposed_prs)
43+
44+
45+
@issue_effectiveness = (closed_issues, new_issues) ->
46+
@scaled @ratio(closed_issues, new_issues)
47+
48+
49+
@effectivenessDesc = (rating) ->
50+
return "In the weeds" if rating >= 0 and rating <= 3
51+
return "Doing fine" if rating >= 4 and rating <= 6
52+
return "Super effective!" if rating >= 7 and rating <= 10
53+
throw new RangeError("Rating must be between 0 and 10")
54+
55+
56+
# Convert a ratio of two Reals into a floating point.
57+
@ratio = (x, y) ->
58+
if x is 0 and y is 0
59+
1
60+
else
61+
x / y
62+
63+
64+
# Scale a ratio (a real number between 0 and infinity) to the range 0–10.
65+
# This will be used heavily by the algorithms for normalizing data such as "a
66+
# ratio of 6 merged PR's in the past month to 4 new ones" to a scale of 0–10.
67+
# This is a component of the app's first metric: Effectiveness.
68+
#
69+
# The function below is a curve which pretty much passes through these
70+
# points:
71+
#
72+
# f(0) -> 0
73+
# f(0.1) -> 1.0 # a 1:10 ratio
74+
# f(1) -> 5.0 # a 1:1 ratio
75+
# f(10) -> 9.0 # a 10:1 ratio
76+
# f(inf) -> 10.0
77+
#
78+
# So in the example above, the 6:4 ratio would become 6 on the scale of 1-10.
79+
# And then this 6 would be translated to a textual description like, "doing
80+
# fine".
81+
#
82+
# See http://math.stackexchange.com/questions/...
83+
# 1582722/how-to-scale-a-ratio-to-a-limited-range
84+
@scaled = (ratio) ->
85+
return 10 if ratio is Infinity
86+
10 * (ratio / (1 + ratio))
87+
88+
89+
@groupByWeek = (anArray, attribute) ->
90+
if _.isUndefined(attribute)
91+
throw new RangeError("required param \"attribute\" not supplied")
92+
return [] if _.isEmpty(anArray)
93+
94+
items = _.sortBy(anArray, attribute)
95+
weekEndingDate = _.last(items)[attribute]
96+
weekStartingDate = sixDaysBefore(weekEndingDate)
97+
partitionResult = _.partition(items, (i) ->
98+
i[attribute] >= weekStartingDate)
99+
thisWeek = _.first(partitionResult)
100+
previousDays = _.last(partitionResult)
101+
@groupByWeek(previousDays, attribute).concat [ thisWeek ]
102+
103+
104+
sixDaysBefore = (aDate) ->
105+
new Date(aDate.getFullYear(), aDate.getMonth(), aDate.getDate() - 6)

0 commit comments

Comments
 (0)