Skip to content

Commit e9c143a

Browse files
committed
Update filtering logic to include new & top cards
1 parent 9516989 commit e9c143a

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

apps/src/metabase/helpers/metabaseAPIHelpers.ts

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* from metabaseAPI.ts and state functions from metabaseStateAPI.ts.
66
*/
77

8-
import _, { map, get, isEmpty, flatMap, filter, sortBy, reverse, pick, omit, partition } from 'lodash';
8+
import _, { map, get, isEmpty, flatMap, filter, sortBy, reverse, pick } from 'lodash';
99
import { getTablesFromSqlRegex, TableAndSchema } from './parseSql';
1010
import { handlePromise, deterministicSample } from '../../common/utils';
1111
import { getCurrentUserInfo, getSelectedDbId } from './metabaseStateAPI';
@@ -225,37 +225,39 @@ export async function getAllCardsAndModels(forceRefresh = false, currentDBId: nu
225225

226226

227227
console.log('[minusx] Non model cards:', filteredCards.length);
228-
// Calculate relevancy score and sort by it
229-
const cardsWithRelevancy = filteredCards.map((card) => {
230-
const viewCount = get(card, 'view_count') || 0;
231-
const lastUsedAt = get(card, 'last_used_at');
232-
233-
let daysAgo = 0;
234-
if (lastUsedAt) {
235-
const lastUsedDate = new Date(lastUsedAt);
236-
const now = new Date();
237-
const diffTime = Math.abs(now.getTime() - lastUsedDate.getTime());
238-
daysAgo = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
239-
}
240-
241-
const relevancy = viewCount - daysAgo;
242-
243-
return {
244-
...card,
245-
relevancy
246-
};
247-
});
248-
249-
// Sort by relevancy descending
250-
const sortedCards = reverse(sortBy(cardsWithRelevancy, 'relevancy'));
251228

252-
// Limit to 2000 cards
253-
// if (sortedCards.length > 2000) {
254-
// sortedCards.length = 2000;
255-
// }
229+
// Sort all cards by view_count descending
230+
const cardsSortedByViews = reverse(sortBy(filteredCards, (card) => get(card, 'view_count') || 0));
231+
232+
let finalCards: any[];
233+
234+
if (cardsSortedByViews.length > 7000) {
235+
console.log('[minusx] More than 7000 cards, applying selective filtering');
236+
237+
// Take top 5000 cards by view count
238+
const top5000Cards = cardsSortedByViews.slice(0, 5000);
239+
240+
// Get remaining cards sorted by creation date (newest first)
241+
const remainingCards = cardsSortedByViews.slice(5000);
242+
const recentlyCreatedCards = remainingCards
243+
.sort((a, b) => {
244+
const dateA = new Date(get(a, 'created_at') || 0);
245+
const dateB = new Date(get(b, 'created_at') || 0);
246+
return dateB.getTime() - dateA.getTime(); // Newest first
247+
});
248+
249+
// Take up to 2000 most recently created cards
250+
const additionalCards = recentlyCreatedCards.slice(0, 2000);
251+
252+
finalCards = [...top5000Cards, ...additionalCards];
253+
console.log('[minusx] Selected cards: top 5000 by views + ' + additionalCards.length + ' recently created cards');
254+
} else {
255+
// If <= 7000 cards, use all cards sorted by view count
256+
finalCards = cardsSortedByViews;
257+
console.log('[minusx] Using all cards (≤ 7000), sorted by view count');
258+
}
256259

257-
// Remove the relevancy property before processing since it's not part of the card schema
258-
const cardsForProcessing = sortedCards.map(card => omit(card, ['relevancy']));
260+
const cardsForProcessing = finalCards;
259261
const processedCards = map(cardsForProcessing, processCard);
260262

261263
console.log('Processed cards:', processedCards);

0 commit comments

Comments
 (0)