Skip to content

Commit

Permalink
fix: stats requires data attr (#1627)
Browse files Browse the repository at this point in the history
* fix: stats requires data attr

* fix: handle infinity, return zero
  • Loading branch information
dashcraft authored Mar 12, 2022
1 parent 4f02475 commit 5684bb2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
17 changes: 17 additions & 0 deletions packages/website/lib/__tests__/statsUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,21 @@ describe('Stats Utils unit tests', () => {
expect(stats.growthRate).toBe('4.87')
expect(stats.totalUploads).toBe(43352125)
})

it('Should return 0 if totalBefore is 0 (dividing by zero)', () => {
const infinityData = {
data: {
uploads_past_7_total: 0,
uploads_nft_total: 0,
uploads_remote_total: 0,
uploads_car_total: 0,
uploads_multipart_total: 0,
uploads_blob_total: 0,
},
}
const stats = decorateAdditionalCalculatedValues(infinityData.data)

// passing in 0/0 should return 0, not infinity
expect(stats.growthRate).toBe(0)
})
})
4 changes: 2 additions & 2 deletions packages/website/lib/statsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ function uploadsTotal(statsObject) {
}, 0)

const totalBefore = total - statsObject.uploads_past_7_total
const uploadsGrowthRate = calcuateGrowthRate(total, totalBefore)

const uploadsGrowthRate =
totalBefore > 0 ? calcuateGrowthRate(total, totalBefore) : 0
return { ...statsObject, totalUploads: total, growthRate: uploadsGrowthRate }
}

Expand Down
2 changes: 1 addition & 1 deletion packages/website/pages/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default function Stats({ logos }) {
'Content-Type': 'application/json',
},
}).then((res) => res.json())
setStats(decorateAdditionalCalculatedValues(stats))
setStats(decorateAdditionalCalculatedValues(stats.data))
} catch (e) {
const fakeData = {
ok: true,
Expand Down

0 comments on commit 5684bb2

Please sign in to comment.