Skip to content

Commit 1914a61

Browse files
authored
Feature/improve card processing (#285)
* Use relevancy to sort cards and remove filter * Reduce metadata processing cache time to 1 hour
1 parent f3e70ac commit 1914a61

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

apps/src/metabase/helpers/metabaseAPIHelpers.ts

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -209,36 +209,56 @@ export async function getAllCardsAndModels(forceRefresh = false) {
209209
// Get selected database ID
210210
const selectedDbId = await getSelectedDbId();
211211

212-
// Calculate 3 months ago date
213-
const threeMonthsAgo = new Date();
214-
threeMonthsAgo.setMonth(threeMonthsAgo.getMonth() - 3);
215-
const cutoffDate = threeMonthsAgo.toISOString();
212+
console.log('[minusx] getAllCards - Total cards:', cards.length);
216213

217-
console.log('[minusx] getAllCards - Total cards:', cards);
218-
219-
// Filter cards by database_id and last_used_at (last 3 months only)
214+
// Filter cards by database_id only
220215
const filteredCardsAndModels = filter(cards, (card) => {
221-
const lastUsedAt = get(card, 'last_used_at');
222216
const databaseId = get(card, 'database_id');
223217

224-
// Filter by selected database and last 3 months
225-
const matchesDatabase = selectedDbId ? databaseId === selectedDbId : true;
226-
const isRecent = lastUsedAt && lastUsedAt > cutoffDate;
218+
// Filter by selected database only
219+
const matchesDatabase = selectedDbId && databaseId? databaseId === selectedDbId : true;
227220

228-
return matchesDatabase && isRecent;
221+
return matchesDatabase;
229222
});
223+
console.log('[minusx] getAllCards - Filtered:', filteredCardsAndModels.length);
224+
230225
// split into cards and models
231226
const [filteredCards, filteredModels] = partition(filteredCardsAndModels, (card) => get(card, 'type') !== 'model');
232-
// Sort by view_count descending
233-
// if length > 1000, limit to 1000
234-
const sortedCards = reverse(sortBy(filteredCards, 'view_count'));
227+
228+
console.log('[minusx] Non model cards:', filteredCards.length);
229+
console.log('[minusx] Model cards:', filteredModels.length);
230+
// Calculate relevancy score and sort by it
231+
const cardsWithRelevancy = filteredCards.map((card) => {
232+
const viewCount = get(card, 'view_count') || 0;
233+
const lastUsedAt = get(card, 'last_used_at');
234+
235+
let daysAgo = 0;
236+
if (lastUsedAt) {
237+
const lastUsedDate = new Date(lastUsedAt);
238+
const now = new Date();
239+
const diffTime = Math.abs(now.getTime() - lastUsedDate.getTime());
240+
daysAgo = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
241+
}
242+
243+
const relevancy = viewCount - daysAgo;
244+
245+
return {
246+
...card,
247+
relevancy
248+
};
249+
});
250+
251+
// Sort by relevancy descending
252+
const sortedCards = reverse(sortBy(cardsWithRelevancy, 'relevancy'));
235253

236-
// Limit to 1000 cards
237-
if (sortedCards.length > 1000) {
238-
sortedCards.length = 1000;
254+
// Limit to 2000 cards
255+
if (sortedCards.length > 2000) {
256+
sortedCards.length = 2000;
239257
}
240258

241-
const processedCards = map(sortedCards, processCard);
259+
// Remove the relevancy property before processing since it's not part of the card schema
260+
const cardsForProcessing = sortedCards.map(card => omit(card, ['relevancy']));
261+
const processedCards = map(cardsForProcessing, processCard);
242262

243263
const processedModelFields = filteredModels.flatMap((model) => {
244264
const result_metadata = get(model, 'result_metadata', [])

web/src/helpers/metadataProcessor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ export async function processAllMetadata(forceRefresh = false) : Promise<Metadat
191191
const cacheEntry = currentState.settings.metadataProcessingCache[selectedDbId]
192192

193193
if (!forceRefresh && cacheEntry) {
194-
const SEVEN_DAYS_MS = 7 * 24 * 60 * 60 * 1000
195-
const isStale = Date.now() - cacheEntry.timestamp > SEVEN_DAYS_MS
194+
const ONE_HOUR_MS = 1 * 60 * 60 * 1000
195+
const isStale = Date.now() - cacheEntry.timestamp > ONE_HOUR_MS
196196

197197
if (!isStale) {
198198
console.log(`[minusx] Using cached metadata for database ${selectedDbId}`)

0 commit comments

Comments
 (0)