Skip to content

Commit

Permalink
fix(explorer): throw errors in getCachedTopHostsFunc
Browse files Browse the repository at this point in the history
  • Loading branch information
telestrial committed Feb 24, 2025
1 parent fc11c57 commit 5f6eb45
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-cougars-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'explorer': minor
---

Improved top hosts list caching.
22 changes: 5 additions & 17 deletions apps/explorer/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { buildMetadata } from '../lib/utils'
import { getLatestBlocks } from '../lib/blocks'
import { to } from '@siafoundation/request'
import { explored } from '../config/explored'
import { rankHosts } from '../lib/hosts'
import { getCachedTopHostsFunc } from '../lib/hosts'
import { unstable_cache } from 'next/cache'

export function generateMetadata(): Metadata {
Expand All @@ -24,22 +24,10 @@ export function generateMetadata(): Metadata {
export const revalidate = 0

// Cache our top hosts fetch and ranking. Revalidate every 24 hours.
const getCachedTopHosts = unstable_cache(
async () => {
const [hosts, hostsError] = await to(
explored.hostsList({
params: { sortBy: 'date_created', dir: 'asc', limit: 500 },
data: { online: true },
})
)
if (hostsError) return []
return rankHosts(hosts)
.slice(0, 5) // Select the top 5.
.map((result) => result.host) // Strip score key.
},
['top-hosts'],
{ revalidate: 86400 }
)
const getCachedTopHosts = unstable_cache(getCachedTopHostsFunc, ['top-hosts'], {
tags: ['top-hosts'],
revalidate: 86400,
})

export default async function HomePage() {
const [[hostMetrics, hostMetricsError], [blockMetrics, blockMetricsError]] =
Expand Down
28 changes: 27 additions & 1 deletion apps/explorer/lib/hosts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ExplorerHost } from '@siafoundation/explored-types'
import { to } from '@siafoundation/request'
import { explored } from '../config/explored'

const weights = {
age: 0.3,
Expand All @@ -15,7 +17,7 @@ function normalize(value, min, max, invert = false) {
return invert ? 1 - normalized : normalized
}

export function rankHosts(hosts: ExplorerHost[] | undefined) {
function rankHosts(hosts: ExplorerHost[] | undefined) {
if (!hosts) return []
const minMax = (key) => ({
min: Math.min(...hosts.map((h) => h[key])),
Expand Down Expand Up @@ -75,3 +77,27 @@ export function rankHosts(hosts: ExplorerHost[] | undefined) {
}))
.sort((a, b) => b.score - a.score) // Sort descending
}

export async function getCachedTopHostsFunc() {
const [hosts, hostsError] = await to(
explored.hostsList({
params: { sortBy: 'date_created', dir: 'asc', limit: 500 },
data: { online: true, acceptContracts: true },
})
)
if (hostsError)
throw new Error(
'Error from getcachedTopHostsFunc /hosts request:' + hostsError.message
)
if (!hosts)
throw new Error('No hosts found from getcachedTopHostsFunc /hosts request')

const rankedHosts = rankHosts(hosts)
.slice(0, 5) // Select the top 5.
.map((result) => result.host) // Strip score key.

if (!rankedHosts || rankedHosts.length === 0)
throw new Error('No hosts from getcachedTopHostsFunc rankHosts call')

return rankedHosts
}

0 comments on commit 5f6eb45

Please sign in to comment.