Skip to content

Commit

Permalink
calculate and emit rank
Browse files Browse the repository at this point in the history
  • Loading branch information
nonrational committed Oct 19, 2024
1 parent c0fe248 commit 58d3d51
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type AgentRelease = {
userAgent?: UserAgent | null
releaseDate: Date
usage: UsageFloat
rank: number
currentVersion: MajorVersionStr
}

Expand All @@ -45,6 +46,7 @@ export const getAgentReleaseInfo = (ua: string): AgentRelease => {
version: '0',
releaseDate: new Date(0),
usage: 0,
rank: 0,
currentVersion: '0',
}
}
Expand All @@ -53,14 +55,19 @@ export const getAgentReleaseInfo = (ua: string): AgentRelease => {

const [lastReleaseVersion] = Object.entries(agentStats.release_date)
.filter(([_, date]) => date !== null)
.toSorted(([versionA], [versionB]) => parseFloat(versionB) - parseFloat(versionA))[0]
.sort(([versionA], [versionB]) => parseFloat(versionB) - parseFloat(versionA))[0]

const versionRank = Object.entries(agentStats.usage_global)
.sort(([, usageA], [, usageB]) => usageB - usageA)
.findIndex(([v]) => v === version) + 1

return {
name,
version,
userAgent,
releaseDate: epochToDate(agentStats.release_date[version]),
usage: agentStats.usage_global[version],
rank: versionRank,
currentVersion: lastReleaseVersion,
}
}
6 changes: 5 additions & 1 deletion routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const ReleaseDescription = ({ userAgent, releaseDate, name, version, usage }: Ag
}

const Home: FunctionalComponent<HomeProps> = ({ data }: PageProps) => {
const { source, releaseDate, userAgent } = data
const { source, userAgent } = data

return (
<div class='container mx-auto flex flex-col items-center justify-center max-w-sm xl:max-w-xl'>
Expand All @@ -147,6 +147,10 @@ const Home: FunctionalComponent<HomeProps> = ({ data }: PageProps) => {
<div class='p-lg'>
<MaybeKnownBrowserPreamble {...data} />
<ReleaseDescription {...data} />

<p>
This is the {data.rank}th most popular version of {data.name} in the world.
</p>
</div>
</div>
)
Expand Down

0 comments on commit 58d3d51

Please sign in to comment.