Skip to content

Commit

Permalink
Change parameter of average from the absolute(periods[].day) to relat…
Browse files Browse the repository at this point in the history
…ive(column.length)
  • Loading branch information
59naga committed Jul 5, 2015
1 parent 885ccc4 commit f9e1a4d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
23 changes: 13 additions & 10 deletions src/calculator.coffee
Expand Up @@ -17,9 +17,9 @@ backSlice= (stats,i,volume,label=[])->
# Public
class Calculator extends Utility
periods: [
{name:'weekly',days:7}
{name:'monthly',days:30}
{name:'yearly',days:365}
{name:'weekly',day:7}
{name:'monthly',day:30}
{name:'yearly',day:365}
]

# eg.
Expand Down Expand Up @@ -132,14 +132,14 @@ class Calculator extends Utility
for pkg,pkgI in normalized.packages
periods=
for period in @periods
length= Math.ceil(pkg.stats.length / period.days)
length= Math.ceil(pkg.stats.length / period.day)

i= 1
while i <= length
{start,end,column}= backSlice pkg.stats,i++,period.days,normalized.days
{start,end,column}= backSlice pkg.stats,i++,period.day,normalized.days

total= _.sum column,(stat)-> stat
average= total/period.days
average= total/column.length

{start,end,total,average,column}

Expand All @@ -164,12 +164,15 @@ class Calculator extends Utility

column=
for cell,j in page.column
packages.reduce (left,right)->
left= left[period.name]?[i].column[j] ? left
left + right[period.name]?[i].column[j]
if packages.length <= 1
packages[0]?[period.name]?[i].column[j] ? 0
else
packages.reduce (left,right)->
left= left[period.name]?[i].column[j] ? left
left + right[period.name]?[i].column[j] ? 0

total= _.sum column,(stat)-> stat
average= total/(period.days)
average= total/page.column.length

{start,end,total,average,column}

Expand Down
27 changes: 15 additions & 12 deletions test/calculator.spec.coffee
Expand Up @@ -6,15 +6,6 @@ fixture= require './fixtures/pkgs'
util= require './util'

# Specs
describe 'issues:calculator',->
it '#3 calculated.weekly[0].total is 0',->
fixture= require './issues/003'
normalized= calculator.normalize fixture
calculated= calculator.calculate normalized

expect(util.commas calculated.total).toBe util.commas 8
expect(util.commas calculated.average).toBe util.commas 8/7

describe 'calculator',->
normalized= calculator.normalize fixture
calculated= calculator.calculate normalized
Expand Down Expand Up @@ -66,7 +57,7 @@ describe 'calculator',->

should= {}
should.total= util.grandTotalNormalized normalized,start,end
should.average= should.total / day
should.average= should.total / (pkg.stats.slice start,end).length

expect(should.total).toBe period.total
expect(should.average).toBe period.average
Expand Down Expand Up @@ -96,6 +87,8 @@ describe 'calculator',->
console.log '%s: sum:%s avg:%s',
period.start+':'+period.end,period.total,period.average

pkg= null

periodTotals=
for pkg in normalized.packages
if process.env.DEBUG
Expand All @@ -107,7 +100,7 @@ describe 'calculator',->
should= {}
should.column= pkg.stats.slice start,end
should.total= util.totalNormalized normalized,pkg.name,start,end
should.average= should.total / day
should.average= should.total / should.column.length

expect(should.column).toEqual column
expect(util.commas should.total).toBe util.commas total
Expand All @@ -117,7 +110,17 @@ describe 'calculator',->

# sum of the package's total of period equal to the grand total of period
should= {}
should.column= pkg.stats.slice start,end
should.total= periodTotals.reduce (a,b)-> a+b
should.average= should.total / day
should.average= should.total / should.column.length
expect(util.commas should.total).toBe util.commas calculated[periodName][periodIndex].total
expect(util.commas should.average).toBe util.commas calculated[periodName][periodIndex].average

describe 'issues:calculator',->
it '#3 calculated.weekly[0].total is 0',->
fixture= require './issues/003'
normalized= calculator.normalize fixture
calculated= calculator.calculate normalized

expect(util.commas calculated.total).toBe util.commas 8
expect(util.commas calculated.average).toBe util.commas 8/7
13 changes: 13 additions & 0 deletions test/util.coffee
Expand Up @@ -10,6 +10,17 @@ module.exports=
else
number

# https://github.com/59naga/npm-count/issues/3
toPackages: (pkg)->
if pkg.package
{downloads,start,end}= pkg

packages= {}
packages[pkg.package]= {start,end,downloads}
packages
else
pkg

# eg:
# util.total ({
# foo: {
Expand All @@ -23,6 +34,8 @@ module.exports=
#
# -> 1
total: (packages)->
packages= @toPackages packages

_.chain packages
.pluck 'downloads'
.flatten(true)
Expand Down

0 comments on commit f9e1a4d

Please sign in to comment.