Skip to content

Commit

Permalink
Fix bug in dp selection (#100)
Browse files Browse the repository at this point in the history
* Fix bug in dp selection

* Fix lint

* Rm line
  • Loading branch information
raymondjacobson authored and michellebrier committed Oct 9, 2023
1 parent 44610f6 commit aeade5a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 34 deletions.
13 changes: 7 additions & 6 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, fetchWithTimeout } from '../../../utils/fetch'
import { fetchUntilSuccess } from '../../../utils/fetch'
dayjs.extend(duration)

const MONTH_IN_MS = dayjs.duration({ months: 1 }).asMilliseconds()
Expand Down Expand Up @@ -357,17 +357,18 @@ export function fetchTrailingTopGenres(
nodes: DiscoveryProvider[]
): ThunkAction<void, AppState, Audius, Action<string>> {
return async dispatch => {
const node = nodes[0]
if (!node) return
try {
const startTime = getStartTime(bucket)
const url = `${node.endpoint}/v1/metrics/genres?start_time=${startTime}`
const res = await fetchWithTimeout(url)
const json = await fetchUntilSuccess(
nodes.map(
node => `${node.endpoint}/v1/metrics/genres?start_time=${startTime}`
)
)

const agg: CountRecord = {
Electronic: 0
}
res.data.forEach((genre: { name: string; count: number }) => {
json.data.forEach((genre: { name: string; count: number }) => {
const name = genre.name
if (ELECTRONIC_SUB_GENRES.has(name)) {
agg['Electronic'] += genre.count
Expand Down
39 changes: 23 additions & 16 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 { fetchWithTimeout } from '../../../utils/fetch'
import { fetchUntilSuccess } from '../../../utils/fetch'

const AUDIUS_URL = process.env.REACT_APP_AUDIUS_URL

Expand All @@ -27,13 +27,14 @@ export const getTopAlbums = (state: AppState) => state.cache.music.topAlbums
// -------------------------------- Thunk Actions ---------------------------------

export function fetchTopTracks(
node: DiscoveryProvider | any
nodes: DiscoveryProvider[]
): ThunkAction<void, AppState, Audius, Action<string>> {
return async (dispatch, getState, aud) => {
try {
const url = `${node.endpoint}/v1/tracks/trending?limit=4`
const res = await fetchWithTimeout(url)
const tracks: Track[] = res.data.slice(0, 4).map((d: any) => ({
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) => ({
title: d.title,
handle: d.user.handle,
artwork: d.artwork?.['480x480'] ?? imageBlank,
Expand All @@ -49,13 +50,16 @@ export function fetchTopTracks(
}

export function fetchTopPlaylists(
node: DiscoveryProvider | any
nodes: DiscoveryProvider[]
): ThunkAction<void, AppState, Audius, Action<string>> {
return async (dispatch, getState, aud) => {
try {
const url = `${node.endpoint}/v1/playlists/top?type=playlist&limit=5`
const res = await fetchWithTimeout(url)
const playlists: Playlist[] = res.data.map((d: any) => ({
const json = await fetchUntilSuccess(
nodes.map(
node => `${node.endpoint}/v1/playlists/top?type=playlist&limit=5`
)
)
const playlists: Playlist[] = json.data.map((d: any) => ({
title: d.playlist_name,
handle: d.user.handle,
artwork: d.artwork?.['480x480'] ?? imageBlank,
Expand All @@ -71,13 +75,16 @@ export function fetchTopPlaylists(
}

export function fetchTopAlbums(
node: DiscoveryProvider | any
nodes: DiscoveryProvider[]
): ThunkAction<void, AppState, Audius, Action<string>> {
return async (dispatch, getState, aud) => {
try {
const url = `${node.endpoint}/v1/playlists/top?type=album&limit=5`
const res = await fetchWithTimeout(url)
const albums: Playlist[] = res.data.map((d: any) => ({
const json = await fetchUntilSuccess(
nodes.map(
node => `${node.endpoint}/v1/playlists/top?type=album&limit=5`
)
)
const albums: Playlist[] = json.data.map((d: any) => ({
title: d.playlist_name,
handle: d.user.handle,
artwork: d.artwork?.['480x480'] ?? imageBlank,
Expand All @@ -103,7 +110,7 @@ export const useTopTracks = () => {
useEffect(() => {
if (!doOnce && nodes[0] && !topTracks) {
setDoOnce(true)
dispatch(fetchTopTracks(nodes[0]))
dispatch(fetchTopTracks(nodes))
}
}, [doOnce, topTracks, dispatch, nodes])

Expand All @@ -125,7 +132,7 @@ export const useTopPlaylists = () => {
useEffect(() => {
if (!doOnce && nodes[0] && !topPlaylists) {
setDoOnce(true)
dispatch(fetchTopPlaylists(nodes[0]))
dispatch(fetchTopPlaylists(nodes))
}
}, [topPlaylists, dispatch, nodes, doOnce])

Expand All @@ -147,7 +154,7 @@ export const useTopAlbums = () => {
useEffect(() => {
if (!doOnce && nodes[0] && !topAlbums) {
setDoOnce(true)
dispatch(fetchTopAlbums(nodes[0]))
dispatch(fetchTopAlbums(nodes))
}
}, [topAlbums, dispatch, nodes, doOnce])

Expand Down
13 changes: 1 addition & 12 deletions packages/protocol-dashboard/src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,11 @@ export const withTimeout = async (

// TODO: put in env vars for staging
export const fetchUntilSuccess = async (endpoints: string[]): Promise<any> => {
const allowList = [
'https://discoveryprovider.audius.co',
'https://discoveryprovider2.audius.co',
'https://discoveryprovider3.audius.co',
'https://dn2.monophonic.digital',
'https://dn1.monophonic.digital'
]

try {
const response = await fetchWithTimeout('https://api.audius.co')
const allHealthyDps = response.data as string[]
const allowedHealthyDPs = allHealthyDps.filter(url =>
allowList.includes(url)
)
const allowedEndpoints = endpoints.filter(endpoint =>
allowedHealthyDPs.some(url => endpoint.startsWith(url))
allHealthyDps.some(url => endpoint.startsWith(url))
)

// Pick a random endpoint from the allowed endpoints
Expand Down

0 comments on commit aeade5a

Please sign in to comment.