Skip to content

Commit

Permalink
Merge pull request #103 from AudiusProject/sd-select-libs-dn
Browse files Browse the repository at this point in the history
Use discovery node selection from libs
  • Loading branch information
sddioulde authored and michellebrier committed Oct 9, 2023
1 parent 56162bd commit 6250728
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 66 deletions.
2 changes: 2 additions & 0 deletions packages/protocol-dashboard/src/services/Audius/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ const configureReadOnlyLibs = async () => {
ethProviderUrl,
ethOwnerWallet
)
const discoveryProviderConfig = audius.configDiscoveryProvider()

let audiusLibsConfig = {
ethWeb3Config,
discoveryProviderConfig,
isServer: false
}
const libs = new audius(audiusLibsConfig)
Expand Down
66 changes: 28 additions & 38 deletions packages/protocol-dashboard/src/store/cache/analytics/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { useDiscoveryProviders } from '../discoveryProvider/hooks'
import { useAverageBlockTime, useEthBlockNumber } from '../protocol/hooks'
import { weiAudToAud } from 'utils/numeric'
import { ELECTRONIC_SUB_GENRES } from './genres'
import { fetchUntilSuccess } from '../../../utils/fetch'
import { fetchWithLibs } from '../../../utils/fetch'
dayjs.extend(duration)

const MONTH_IN_MS = dayjs.duration({ months: 1 }).asMilliseconds()
Expand Down Expand Up @@ -129,14 +129,11 @@ async function fetchRoutesTimeSeries(
let error = false
let metric: TimeSeriesRecord[] = []
try {
const bucket_size = BUCKET_GRANULARITY_MAP[bucket]
const res = await fetchUntilSuccess(
nodes.map(
node =>
`${node.endpoint}/v1/metrics/aggregates/routes/${bucket}?bucket_size=${bucket_size}`
)
)
metric = res.data
const bucketSize = BUCKET_GRANULARITY_MAP[bucket]
metric = await fetchWithLibs({
endpoint: `v1/metrics/aggregates/routes/${bucket}`,
queryParams: { bucket_size: bucketSize }
})
} catch (e) {
console.error(e)
error = true
Expand Down Expand Up @@ -168,14 +165,12 @@ async function fetchTimeSeries(
let error = false
let metric: TimeSeriesRecord[] = []
try {
const bucket_size = BUCKET_GRANULARITY_MAP[bucket]
const res = await fetchUntilSuccess(
nodes.map(
node =>
`${node.endpoint}/v1/metrics/${route}?bucket_size=${bucket_size}&start_time=${startTime}`
)
)
metric = res.data.reverse()
const bucketSize = BUCKET_GRANULARITY_MAP[bucket]
const data = await fetchWithLibs({
endpoint: `v1/metrics/${route}`,
queryParams: { bucket_size: bucketSize, start_time: startTime }
})
metric = data.reverse()
} catch (e) {
console.error(e)
error = true
Expand Down Expand Up @@ -261,15 +256,13 @@ export function fetchTotalStaked(
}

const getTrailingAPI = async (nodes: DiscoveryProvider[]) => {
const json = await fetchUntilSuccess(
nodes.map(
node => `${node.endpoint}/v1/metrics/aggregates/routes/trailing/month`
)
)
const data = await fetchWithLibs({
endpoint: 'v1/metrics/aggregates/routes/trailing/month'
})
return {
total_count: json?.data?.total_count ?? 0,
unique_count: json?.data?.unique_count ?? 0,
summed_unique_count: json?.data?.summed_unique_count ?? 0
total_count: data?.total_count ?? 0,
unique_count: data?.unique_count ?? 0,
summed_unique_count: data?.summed_unique_count ?? 0
} as CountRecord
}

Expand Down Expand Up @@ -299,13 +292,11 @@ const getTrailingTopApps = async (
bucket: Bucket,
limit: number
) => {
const json = await fetchUntilSuccess(
nodes.map(
node =>
`${node.endpoint}/v1/metrics/aggregates/apps/${bucket}?limit=${limit}`
)
)
return json.data as { name: string; count: number }[]
const data = await fetchWithLibs({
endpoint: `v1/metrics/aggregates/apps/${bucket}`,
queryParams: { limit }
})
return data as { name: string; count: number }[]
}

export function fetchTopApps(
Expand Down Expand Up @@ -359,16 +350,15 @@ export function fetchTrailingTopGenres(
return async dispatch => {
try {
const startTime = getStartTime(bucket)
const json = await fetchUntilSuccess(
nodes.map(
node => `${node.endpoint}/v1/metrics/genres?start_time=${startTime}`
)
)
const data = await fetchWithLibs({
endpoint: 'v1/metrics/genres',
queryParams: { start_time: startTime }
})

const agg: CountRecord = {
Electronic: 0
}
json.data.forEach((genre: { name: string; count: number }) => {
data.forEach((genre: { name: string; count: number }) => {
const name = genre.name
if (ELECTRONIC_SUB_GENRES.has(name)) {
agg['Electronic'] += genre.count
Expand Down
33 changes: 16 additions & 17 deletions packages/protocol-dashboard/src/store/cache/music/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
setTopPlaylists,
setTopTracks
} from './slice'
import { fetchUntilSuccess } from '../../../utils/fetch'
import { fetchWithLibs } from '../../../utils/fetch'

const AUDIUS_URL = process.env.REACT_APP_AUDIUS_URL

Expand All @@ -31,10 +31,11 @@ export function fetchTopTracks(
): ThunkAction<void, AppState, Audius, Action<string>> {
return async (dispatch, getState, aud) => {
try {
const json = await fetchUntilSuccess(
nodes.map(node => `${node.endpoint}/v1/tracks/trending?limit=4`)
)
const tracks: Track[] = json.data.slice(0, 4).map((d: any) => ({
const data = await fetchWithLibs({
endpoint: '/v1/tracks/trending',
queryParams: { limit: 4 }
})
const tracks: Track[] = data.slice(0, 4).map((d: any) => ({
title: d.title,
handle: d.user.handle,
artwork: d.artwork?.['480x480'] ?? imageBlank,
Expand All @@ -54,12 +55,11 @@ export function fetchTopPlaylists(
): ThunkAction<void, AppState, Audius, Action<string>> {
return async (dispatch, getState, aud) => {
try {
const json = await fetchUntilSuccess(
nodes.map(
node => `${node.endpoint}/v1/playlists/top?type=playlist&limit=5`
)
)
const playlists: Playlist[] = json.data.map((d: any) => ({
const data = await fetchWithLibs({
endpoint: '/v1/playlists/top',
queryParams: { type: 'playlist', limit: 5 }
})
const playlists: Playlist[] = data.map((d: any) => ({
title: d.playlist_name,
handle: d.user.handle,
artwork: d.artwork?.['480x480'] ?? imageBlank,
Expand All @@ -79,12 +79,11 @@ export function fetchTopAlbums(
): ThunkAction<void, AppState, Audius, Action<string>> {
return async (dispatch, getState, aud) => {
try {
const json = await fetchUntilSuccess(
nodes.map(
node => `${node.endpoint}/v1/playlists/top?type=album&limit=5`
)
)
const albums: Playlist[] = json.data.map((d: any) => ({
const data = await fetchWithLibs({
endpoint: '/v1/playlists/top',
queryParams: { type: 'album', limit: 5 }
})
const albums: Playlist[] = data.map((d: any) => ({
title: d.playlist_name,
handle: d.user.handle,
artwork: d.artwork?.['480x480'] ?? imageBlank,
Expand Down
22 changes: 11 additions & 11 deletions packages/protocol-dashboard/src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ export const withTimeout = async (
return res
}

// TODO: put in env vars for staging
export const fetchUntilSuccess = async (endpoints: string[]): Promise<any> => {
try {
// Pick a random endpoint from the allowed endpoints
const endpoint = endpoints[Math.floor(Math.random() * endpoints.length)]
console.info('Attempting endpoint: ', endpoint)
return await fetchWithTimeout(endpoint)
} catch (e) {
console.error(e)
return await fetchUntilSuccess(endpoints)
}
export const fetchWithLibs = async (req: {
endpoint: string
queryParams?: object
}) => {
// TODO use audius client instead of window.audiusLibs
const data = await window.audiusLibs.discoveryProvider._makeRequest(req)

// if all nodes are unhealthy and unavailable
if (!data) return Promise.reject()

return data
}

0 comments on commit 6250728

Please sign in to comment.