From be094ef8e55a93a0530325727a164f920fcf8d54 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Tue, 14 Jun 2022 10:27:50 -0500 Subject: [PATCH] Tweaks to stats page --- common/util/math.ts | 12 ++++++++---- web/pages/stats.tsx | 14 ++++++-------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/common/util/math.ts b/common/util/math.ts index a89b31e1..66bcff1b 100644 --- a/common/util/math.ts +++ b/common/util/math.ts @@ -1,4 +1,4 @@ -import { sortBy } from 'lodash' +import { sortBy, sum } from 'lodash' export const logInterpolation = (min: number, max: number, value: number) => { if (value <= min) return 0 @@ -20,13 +20,17 @@ export function normpdf(x: number, mean = 0, variance = 1) { export const TAU = Math.PI * 2 -export function median(values: number[]) { - if (values.length === 0) return NaN +export function median(xs: number[]) { + if (xs.length === 0) return NaN - const sorted = sortBy(values, (x) => x) + const sorted = sortBy(xs, (x) => x) const mid = Math.floor(sorted.length / 2) if (sorted.length % 2 === 0) { return (sorted[mid - 1] + sorted[mid]) / 2 } return sorted[mid] } + +export function average(xs: number[]) { + return sum(xs) / xs.length +} diff --git a/web/pages/stats.tsx b/web/pages/stats.tsx index e7590e98..401749b6 100644 --- a/web/pages/stats.tsx +++ b/web/pages/stats.tsx @@ -16,6 +16,7 @@ import { getDailyContracts } from 'web/lib/firebase/contracts' import { getDailyNewUsers } from 'web/lib/firebase/users' import { SiteLink } from 'web/components/site-link' import { Linkify } from 'web/components/linkify' +import { average } from 'common/util/math' export const getStaticProps = fromPropz(getStaticPropz) export async function getStaticPropz() { @@ -61,7 +62,7 @@ export async function getStaticPropz() { }) const monthlyActiveUsers = dailyUserIds.map((_, i) => { - const start = Math.max(0, i - 30) + const start = Math.max(0, i - 29) const end = i const uniques = new Set() for (let j = start; j <= end; j++) @@ -166,16 +167,12 @@ export async function getStaticPropz() { const weeklyTopTenthActions = dailyTopTenthActions.map((_, i) => { const start = Math.max(0, i - 6) const end = i - const total = sum(dailyTopTenthActions.slice(start, end)) - if (end - start < 7) return (total * 7) / (end - start) - return total + return average(dailyTopTenthActions.slice(start, end)) }) const monthlyTopTenthActions = dailyTopTenthActions.map((_, i) => { const start = Math.max(0, i - 29) const end = i - const total = sum(dailyTopTenthActions.slice(start, end)) - if (end - start < 30) return (total * 30) / (end - start) - return total + return average(dailyTopTenthActions.slice(start, end)) }) // Total mana divided by 100. @@ -193,7 +190,8 @@ export async function getStaticPropz() { const start = Math.max(0, i - 29) const end = i const total = sum(dailyManaBet.slice(start, end)) - if (end - start < 30) return (total * 30) / (end - start) + const range = end - start + 1 + if (range < 30) return (total * 30) / range return total })