Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
[C-1678] Add metrics for offline mode in the workers (#2844)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Shanks authored Feb 14, 2023
1 parent 43adc79 commit bf71c13
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ import RNFetchBlob from 'rn-fetch-blob'
import { select, call, put, take, race, all } from 'typed-redux-saga'

import { createAllImageSources } from 'app/hooks/useContentNodeImage'
import { make, track } from 'app/services/analytics'
import {
getCollectionCoverArtPath,
getLocalCollectionDir,
getLocalCollectionJsonPath,
mkdirSafe
} from 'app/services/offline-downloader'
import { DOWNLOAD_REASON_FAVORITES } from 'app/store/offline-downloads/constants'
import { EventNames } from 'app/types/analytics'

import { getCollectionOfflineDownloadStatus } from '../../../selectors'
import type { CollectionId } from '../../../slice'
import type { CollectionId, DownloadQueueItem } from '../../../slice'
import {
abandonDownload,
errorDownload,
Expand Down Expand Up @@ -48,7 +50,14 @@ function* shouldAbortDownload(collectionId: CollectionId) {
}

export function* downloadCollectionWorker(collectionId: CollectionId) {
yield* put(startDownload({ type: 'collection', id: collectionId }))
const queueItem = {
type: 'collection',
id: collectionId
} as DownloadQueueItem
track(
make({ eventName: EventNames.OFFLINE_MODE_DOWNLOAD_START, ...queueItem })
)
yield* put(startDownload(queueItem))

const { jobResult, cancel, abort } = yield* race({
jobResult: call(downloadCollectionAsync, collectionId),
Expand All @@ -63,14 +72,32 @@ export function* downloadCollectionWorker(collectionId: CollectionId) {
yield* put(cancelDownload({ type: 'collection', id: collectionId }))
yield* call(removeDownloadedCollection, collectionId)
} else if (jobResult === OfflineDownloadStatus.ERROR) {
track(
make({
eventName: EventNames.OFFLINE_MODE_DOWNLOAD_FAILURE,
...queueItem
})
)
yield* put(errorDownload({ type: 'collection', id: collectionId }))
yield* call(removeDownloadedCollection, collectionId)
yield* put(requestDownloadQueuedItem())
} else if (jobResult === OfflineDownloadStatus.ABANDONED) {
track(
make({
eventName: EventNames.OFFLINE_MODE_DOWNLOAD_FAILURE,
...queueItem
})
)
yield* put(abandonDownload({ type: 'collection', id: collectionId }))
yield* call(removeDownloadedCollection, collectionId)
yield* put(requestDownloadQueuedItem())
} else if (jobResult === OfflineDownloadStatus.SUCCESS) {
track(
make({
eventName: EventNames.OFFLINE_MODE_DOWNLOAD_SUCCESS,
...queueItem
})
)
yield* put(completeDownload({ type: 'collection', id: collectionId }))
yield* put(requestDownloadQueuedItem())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ import RNFetchBlob from 'rn-fetch-blob'
import { select, call, put, all, take, race } from 'typed-redux-saga'

import { createAllImageSources } from 'app/hooks/useContentNodeImage'
import { make, track } from 'app/services/analytics'
import {
getLocalAudioPath,
getLocalTrackCoverArtDestination,
getLocalTrackDir,
getLocalTrackJsonPath
} from 'app/services/offline-downloader'
import { EventNames } from 'app/types/analytics'

import { getTrackOfflineDownloadStatus } from '../../../selectors'
import type { DownloadQueueItem } from '../../../slice'
import {
cancelDownload,
completeDownload,
Expand All @@ -45,7 +48,11 @@ function* shouldAbortDownload(trackId: ID) {
}

export function* downloadTrackWorker(trackId: ID) {
yield* put(startDownload({ type: 'track', id: trackId }))
const queueItem = { type: 'track', id: trackId } as DownloadQueueItem
track(
make({ eventName: EventNames.OFFLINE_MODE_DOWNLOAD_START, ...queueItem })
)
yield* put(startDownload(queueItem))

const { jobResult, cancel, abort } = yield* race({
jobResult: call(downloadTrackAsync, trackId),
Expand All @@ -60,14 +67,32 @@ export function* downloadTrackWorker(trackId: ID) {
yield* put(cancelDownload({ type: 'track', id: trackId }))
yield* call(removeDownloadedTrack, trackId)
} else if (jobResult === OfflineDownloadStatus.ERROR) {
track(
make({
eventName: EventNames.OFFLINE_MODE_DOWNLOAD_FAILURE,
...queueItem
})
)
yield* put(errorDownload({ type: 'track', id: trackId }))
yield* call(removeDownloadedTrack, trackId)
yield* put(requestDownloadQueuedItem())
} else if (jobResult === OfflineDownloadStatus.ABANDONED) {
track(
make({
eventName: EventNames.OFFLINE_MODE_DOWNLOAD_FAILURE,
...queueItem
})
)
yield* put(abandonDownload({ type: 'track', id: trackId }))
yield* call(removeDownloadedTrack, trackId)
yield* put(requestDownloadQueuedItem())
} else if (jobResult === OfflineDownloadStatus.SUCCESS) {
track(
make({
eventName: EventNames.OFFLINE_MODE_DOWNLOAD_SUCCESS,
...queueItem
})
)
yield* put(
completeDownload({ type: 'track', id: trackId, completedAt: Date.now() })
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import type { ID } from '@audius/common'
import { tracksSocialActions } from '@audius/common'
import { put } from 'typed-redux-saga'

import { make, track } from 'app/services/analytics'
import { EventNames } from 'app/types/analytics'

import { completePlayCount, requestDownloadQueuedItem } from '../../../slice'

const { recordListen } = tracksSocialActions

export function* playCounterWorker(trackId: ID) {
track(make({ eventName: EventNames.OFFLINE_MODE_PLAY, trackId }))
yield* put(recordListen(trackId))
yield* put(completePlayCount())
yield* put(requestDownloadQueuedItem())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import moment from 'moment'
import { takeEvery, select, call, put } from 'typed-redux-saga'

import { getAccountCollections } from 'app/screens/favorites-screen/selectors'
import { make, track } from 'app/services/analytics'
import { DOWNLOAD_REASON_FAVORITES } from 'app/store/offline-downloads/constants'
import { EventNames } from 'app/types/analytics'

import type { OfflineItem } from '../slice'
import { addOfflineItems, requestDownloadAllFavorites } from '../slice'
Expand All @@ -21,6 +23,11 @@ export function* requestDownloadAllFavoritesSaga() {
}

function* downloadAllFavorites() {
track(
make({
eventName: EventNames.OFFLINE_MODE_DOWNLOAD_ALL_TOGGLE_ON
})
)
const currentUserId = yield* select(getUserId)
if (!currentUserId) return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
} from '@audius/common'
import { takeEvery, select, put, call } from 'typed-redux-saga'

import { make, track } from 'app/services/analytics'
import { EventNames } from 'app/types/analytics'

import type { CollectionAction, OfflineItem } from '../slice'
import { addOfflineItems, requestDownloadCollection } from '../slice'

Expand All @@ -19,6 +22,12 @@ export function* requestDownloadCollectionSaga() {

function* downloadCollection(action: CollectionAction) {
const { collectionId } = action.payload
track(
make({
eventName: EventNames.OFFLINE_MODE_DOWNLOAD_COLLECTION_TOGGLE_ON,
collectionId
})
)

const currentUserId = yield* select(getUserId)
if (!currentUserId) return
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { takeEvery, select, put } from 'typed-redux-saga'

import { make, track } from 'app/services/analytics'
import { DOWNLOAD_REASON_FAVORITES } from 'app/store/offline-downloads/constants'
import { EventNames } from 'app/types/analytics'

import {
getOfflineCollectionMetadata,
Expand All @@ -25,6 +27,11 @@ export function* requestRemoveAllDownloadedFavoritesSaga() {
}

function* removeAllDownloadedFavoritesWorker() {
track(
make({
eventName: EventNames.OFFLINE_MODE_DOWNLOAD_ALL_TOGGLE_OFF
})
)
const offlineItemsToRemove: OfflineItem[] = []
const offlineCollectionMetadata = yield* select(getOfflineCollectionMetadata)
const offlineCollectionIds = Object.keys(offlineCollectionMetadata).map(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { takeEvery, select, put } from 'typed-redux-saga'

import { make, track } from 'app/services/analytics'
import { EventNames } from 'app/types/analytics'

import { getOfflineTrackMetadata } from '../selectors'
import type { CollectionAction, OfflineItem } from '../slice'
import { removeOfflineItems, requestRemoveDownloadedCollection } from '../slice'
Expand All @@ -13,6 +16,12 @@ export function* requestRemoveDownloadedCollectionSaga() {

function* removeDownloadedCollectionWorker(action: CollectionAction) {
const { collectionId } = action.payload
track(
make({
eventName: EventNames.OFFLINE_MODE_DOWNLOAD_COLLECTION_TOGGLE_OFF,
collectionId
})
)

const offlineItemsToRemove: OfflineItem[] = []

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import RNFetchBlob from 'rn-fetch-blob'
import { takeEvery, select, call } from 'typed-redux-saga'

import { make, track } from 'app/services/analytics'
import {
getLocalCollectionDir,
getLocalTrackDir
} from 'app/services/offline-downloader'
import { EventNames } from 'app/types/analytics'

import {
getOfflineCollectionsStatus,
Expand All @@ -23,6 +25,12 @@ function* deleteItemsFromDisk(action: RemoveOfflineItemsAction) {
const collectionStatus = yield* select(getOfflineCollectionsStatus)

for (const item of items) {
track(
make({
eventName: EventNames.OFFLINE_MODE_REMOVE_ITEM,
...item
})
)
if (item.type === 'collection' && !collectionStatus[item.id]) {
const collectionDirectory = yield* call(
getLocalCollectionDir,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { collectionsSocialActions, FavoriteSource } from '@audius/common'
import { takeEvery, select, put } from 'typed-redux-saga'

import { make, track } from 'app/services/analytics'
import { EventNames } from 'app/types/analytics'

import { getIsFavoritesDownloadsEnabled } from '../selectors'
import { requestDownloadFavoritedCollection } from '../slice'

Expand All @@ -19,6 +22,13 @@ function* checkIfShouldDownload(action: ReturnType<typeof saveCollection>) {
isFavoritesDownloadEnabled &&
source !== FavoriteSource.OFFLINE_DOWNLOAD
) {
track(
make({
eventName: EventNames.OFFLINE_MODE_DOWNLOAD_REQUEST,
type: 'collection',
id: collectionId
})
)
yield* put(requestDownloadFavoritedCollection({ collectionId }))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { tracksSocialActions } from '@audius/common'
import moment from 'moment'
import { put, takeEvery, select } from 'typed-redux-saga'

import { make, track } from 'app/services/analytics'
import { DOWNLOAD_REASON_FAVORITES } from 'app/store/offline-downloads/constants'
import { EventNames } from 'app/types/analytics'

import { getIsFavoritesDownloadsEnabled } from '../selectors'
import { addOfflineItems } from '../slice'
Expand All @@ -20,6 +22,13 @@ function* downloadSavedTrack(
)

if (isFavoritesDownloadEnabled) {
track(
make({
eventName: EventNames.OFFLINE_MODE_DOWNLOAD_REQUEST,
type: 'track',
id: trackId
})
)
yield* put(
addOfflineItems({
items: [
Expand Down
14 changes: 7 additions & 7 deletions packages/mobile/src/store/offline-downloads/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,19 +277,19 @@ const slice = createSlice({
}
},
completeDownload: (state, action: CompleteDownloadAction) => {
const item = action.payload
if (item.type === 'collection') {
state.collectionStatus[item.id] = OfflineDownloadStatus.SUCCESS
} else if (item.type === 'track') {
const { id, completedAt } = item
const { type, id } = action.payload
if (type === 'collection') {
state.collectionStatus[id] = OfflineDownloadStatus.SUCCESS
} else if (type === 'track') {
const { completedAt } = action.payload
state.trackStatus[id] = OfflineDownloadStatus.SUCCESS
const trackMetadata = state.offlineTrackMetadata[id]
if (trackMetadata) {
trackMetadata.last_verified_time = completedAt
trackMetadata.download_completed_time = completedAt
}
} else if (item.type === 'stale-track') {
const { id, verifiedAt } = item
} else if (type === 'stale-track') {
const { verifiedAt } = action.payload
const trackMetadata = state.offlineTrackMetadata[id]
if (trackMetadata) {
trackMetadata.last_verified_time = verifiedAt
Expand Down
Loading

0 comments on commit bf71c13

Please sign in to comment.