Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send some info about specific user's collection item resolving to sentry #3050

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 140 additions & 61 deletions ui/redux/actions/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import { getThumbnailFromClaim } from 'util/claim';
import { creditsToString } from 'util/format-credits';
import { doToast } from 'redux/actions/notifications';

import { Lbryio } from 'lbryinc';
import { selectUser } from 'redux/selectors/user';

const FETCH_BATCH_SIZE = 50;

export const doFetchCollectionListMine =
Expand Down Expand Up @@ -243,87 +246,136 @@ export const doToggleCollectionSavedForId = (collectionId: string) => (dispatch:
dispatch({ type: ACTIONS.COLLECTION_TOGGLE_SAVE, data: collectionId });
};

const doFetchCollectionItems = (items: Array<any>, pageSize?: number) => async (dispatch: Dispatch) => {
const sortResults = (resultItems: Array<Claim>) => {
const newItems: Array<Claim> = [];

items.forEach((item) => {
const index = resultItems.findIndex((i) => [i.canonical_url, i.permanent_url, i.claim_id].includes(item));
const doFetchCollectionItems =
(items: Array<any>, pageSize?: number, isFromEdit?: boolean) => async (dispatch: Dispatch, getState: GetState) => {
const sortResults = (resultItems: Array<Claim>) => {
const newItems: Array<Claim> = [];

if (index >= 0) newItems.push(resultItems[index]);
});
items.forEach((item) => {
const index = resultItems.findIndex((i) => [i.canonical_url, i.permanent_url, i.claim_id].includes(item));

return newItems;
};
if (index >= 0) newItems.push(resultItems[index]);
});

const mergeBatches = (arrayOfResults: Array<any>) => {
let resultItems = [];
return newItems;
};

arrayOfResults.forEach((result: any) => {
// $FlowFixMe
const claims = result.items || Object.values(result).map((item) => item.stream || item);
resultItems = resultItems.concat(claims);
});
const mergeBatches = (arrayOfResults: Array<any>) => {
let resultItems = [];

return resultItems;
};
arrayOfResults.forEach((result: any) => {
// $FlowFixMe
const claims = result.items || Object.values(result).map((item) => item.stream || item);
resultItems = resultItems.concat(claims);
});

try {
const batchSize = pageSize || FETCH_BATCH_SIZE;
const uriBatches: Array<Promise<any>> = [];
const idBatches: Array<Promise<any>> = [];
return resultItems;
};

const totalItems = items.length;
try {
const batchSize = pageSize || FETCH_BATCH_SIZE;
const uriBatches: Array<Promise<any>> = [];
const idBatches: Array<Promise<any>> = [];

for (let i = 0; i < Math.ceil(totalItems / batchSize); i++) {
const batchInitialIndex = i * batchSize;
const batchLength = (i + 1) * batchSize;
const totalItems = items.length;

// --> Filter in case null/undefined are collection items
const batchItems = items.slice(batchInitialIndex, batchLength).filter(Boolean);
/// Temporary debug
const state = getState();
const user = selectUser(state);
if (isFromEdit && user.id === 645368535) {
await Lbryio.call('event', 'desktop_error', {
error_message: `Playlist debug: ----------------------------- `,
});
await Lbryio.call('event', 'desktop_error', {
error_message: `Playlist debug:items: ${JSON.stringify(
items.map((item) => {
const match = item.match(/[a-f0-9]{40}/);
const claimId = match && match[0];
return claimId || item;
})
)}`,
});
}
/// ^^^
for (let i = 0; i < Math.ceil(totalItems / batchSize); i++) {
const batchInitialIndex = i * batchSize;
const batchLength = (i + 1) * batchSize;

// --> Filter in case null/undefined are collection items
const batchItems = items.slice(batchInitialIndex, batchLength).filter(Boolean);

const uris = new Set([]);
const ids = new Set([]);
batchItems.forEach((item) => {
if (item.startsWith('lbry://')) {
uris.add(item);
} else {
ids.add(item);
}
});

const uris = new Set([]);
const ids = new Set([]);
batchItems.forEach((item) => {
if (item.startsWith('lbry://')) {
uris.add(item);
} else {
ids.add(item);
if (uris.size > 0) {
uriBatches[i] = dispatch(doResolveUris(Array.from(uris), true));
}
if (ids.size > 0) {
idBatches[i] = dispatch(doResolveClaimIds(Array.from(ids)));
}
});

if (uris.size > 0) {
uriBatches[i] = dispatch(doResolveUris(Array.from(uris), true));
}
if (ids.size > 0) {
idBatches[i] = dispatch(doResolveClaimIds(Array.from(ids)));
const itemsInBatches = await Promise.all([...uriBatches, ...idBatches]);
const resultItems = sortResults(mergeBatches(itemsInBatches.filter(Boolean)));

// The resolve calls will NOT return items when they still are in a previous call's 'Processing' state.
const itemsWereFetching = resultItems.length !== items.length;

// Temporary debug
if (isFromEdit && user.id === 645368535) {
await Lbryio.call('event', 'desktop_error', {
error_message: `Playlist debug:resultItems: ${JSON.stringify(
resultItems.map((item) => {
if (!item.claim_id) {
return item;
}
return item.claim_id;
})
)}`,
});
}
}
const itemsInBatches = await Promise.all([...uriBatches, ...idBatches]);
const resultItems = sortResults(mergeBatches(itemsInBatches.filter(Boolean)));

// The resolve calls will NOT return items when they still are in a previous call's 'Processing' state.
const itemsWereFetching = resultItems.length !== items.length;

if (resultItems && !itemsWereFetching) {
return resultItems;
} else {
/// ^^^
if (resultItems && !itemsWereFetching) {
return resultItems;
} else {
return null;
}
} catch (e) {
await Lbryio.call('event', 'desktop_error', { error_message: `Playlist debug:error: ${e.message || e}` });
return null;
}
} catch (e) {
return null;
}
};
};

export const doFetchItemsInCollection =
(params: { collectionId: string, pageSize?: number }) => async (dispatch: Dispatch, getState: GetState) => {
(params: { collectionId: string, pageSize?: number, isFromEdit?: boolean }) =>
async (dispatch: Dispatch, getState: GetState) => {
let state = getState();
const { collectionId, pageSize } = params;
const { collectionId, pageSize, isFromEdit } = params;

const isAlreadyFetching = selectAreCollectionItemsFetchingForId(state, collectionId);

const user = selectUser(state);
const shouldLog = user.id === 645368535;

if (isAlreadyFetching && shouldLog) {
await Lbryio.call('event', 'desktop_error', {
error_message: `Playlist debug: Already fetching, collectionId: ${collectionId}`,
});
}

if (isAlreadyFetching) return Promise.resolve();

if (shouldLog) {
await Lbryio.call('event', 'desktop_error', {
error_message: `Playlist debug: ITEM_RESOLVE_START, collectionId: ${collectionId}`,
});
}
dispatch({ type: ACTIONS.COLLECTION_ITEMS_RESOLVE_START, data: collectionId });

const isPrivate = selectHasPrivateCollectionForId(state, collectionId);
Expand All @@ -338,6 +390,11 @@ export const doFetchItemsInCollection =
}

if (!isPrivate && hasClaim === null) {
if (shouldLog) {
await Lbryio.call('event', 'desktop_error', {
error_message: `Playlist debug: ITEM_RESOLVE_FAIL (public), collectionId: ${collectionId}`,
});
}
return dispatch({ type: ACTIONS.COLLECTION_ITEMS_RESOLVE_FAIL, data: collectionId });
}

Expand All @@ -347,15 +404,21 @@ export const doFetchItemsInCollection =
const collection = selectCollectionForId(state, collectionId);

if (collection.items.length > 0) {
promisedCollectionItemsFetch = collection.items && dispatch(doFetchCollectionItems(collection.items, pageSize));
promisedCollectionItemsFetch =
collection.items && dispatch(doFetchCollectionItems(collection.items, pageSize, isFromEdit));
} else {
if (shouldLog) {
await Lbryio.call('event', 'desktop_error', {
error_message: `Playlist debug: ITEM_RESOLVE_FAIL (private), collectionId: ${collectionId}`,
});
}
return dispatch({ type: ACTIONS.COLLECTION_ITEMS_RESOLVE_FAIL, data: collectionId });
}
} else {
const claim = selectClaimForClaimId(state, collectionId);

const claimIds = getClaimIdsInCollectionClaim(claim);
promisedCollectionItemsFetch = claimIds && dispatch(doFetchCollectionItems(claimIds, pageSize));
promisedCollectionItemsFetch = claimIds && dispatch(doFetchCollectionItems(claimIds, pageSize, isFromEdit));
}

// -- Await results:
Expand All @@ -364,6 +427,11 @@ export const doFetchItemsInCollection =
}

if (!collectionItems || collectionItems.length === 0) {
if (shouldLog) {
await Lbryio.call('event', 'desktop_error', {
error_message: `Playlist debug: ITEM_RESOLVE_FAIL (general), collectionId: ${collectionId}`,
});
}
return dispatch({ type: ACTIONS.COLLECTION_ITEMS_RESOLVE_FAIL, data: collectionId });
}

Expand All @@ -375,6 +443,12 @@ export const doFetchItemsInCollection =

const newPrivateCollection = { ...collection, items: newItems, key: collectionKey };

if (shouldLog) {
await Lbryio.call('event', 'desktop_error', {
error_message: `Playlist debug: ITEM_RESOLVE_SUCCESS (private), collectionId: ${collectionId}`,
});
}

return dispatch({
type: ACTIONS.COLLECTION_ITEMS_RESOLVE_SUCCESS,
data: { resolvedCollection: newPrivateCollection },
Expand Down Expand Up @@ -415,6 +489,11 @@ export const doFetchItemsInCollection =
...resolveAuxParams(collectionType, claim),
};

if (shouldLog) {
await Lbryio.call('event', 'desktop_error', {
error_message: `Playlist debug: ITEM_RESOLVE_SUCCESS (public), collectionId: ${collectionId}`,
});
}
return dispatch(
batchActions(
{ type: ACTIONS.COLLECTION_ITEMS_RESOLVE_SUCCESS, data: { resolvedCollection: newStoreCollectionClaim } },
Expand Down Expand Up @@ -524,7 +603,7 @@ export const doCollectionEdit =

let collectionUrls = selectUrlsForCollectionId(state, collectionId);
if (collectionUrls === undefined) {
await dispatch(doFetchItemsInCollection({ collectionId }));
await dispatch(doFetchItemsInCollection({ collectionId, isFromEdit: true }));
state = getState();
collectionUrls = selectUrlsForCollectionId(state, collectionId);
}
Expand Down
Loading