Skip to content

Commit 51141a9

Browse files
authored
Merge branch 'p-stream:master' into master
2 parents e0a70f5 + 8637487 commit 51141a9

File tree

5 files changed

+38
-23
lines changed

5 files changed

+38
-23
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ dist
77
.env
88
.vscode
99
.metrics.json
10-
.metrics.json
10+
pnpm-lock.yaml

.metrics.json

Whitespace-only changes.

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/routes/letterboxd/index.get.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ export default defineCachedEventHandler(async (event) => {
2929
for (let i = 0; i < listItems.length; i++) {
3030
const listItem = listItems[i];
3131

32-
if (!listItem.href) continue;
32+
if (!listItem.href) {
33+
continue;
34+
}
3335

3436
try {
3537
const listUrl = `https://letterboxd.com${listItem.href}`;
@@ -63,6 +65,7 @@ export default defineCachedEventHandler(async (event) => {
6365

6466
for (const selector of possibleFilmSelectors) {
6567
const elements = list$(selector);
68+
6669
if (elements.length > 0) {
6770
workingSelector = selector;
6871
films = elements.map((i, el) => {
@@ -84,19 +87,23 @@ export default defineCachedEventHandler(async (event) => {
8487
};
8588
}).get().filter(film => film.name);
8689

87-
if (films.length > 0) break;
90+
if (films.length > 0) {
91+
break;
92+
}
8893
}
8994
}
9095

91-
const tmdbIds = [];
96+
const tmdbMovies = [];
9297

93-
for (const film of films) {
98+
for (let j = 0; j < films.length; j++) {
99+
const film = films[j];
100+
94101
try {
95102
const searchResult = await tmdb.search.movies({ query: film.name });
96103

97104
if (searchResult.results && searchResult.results.length > 0) {
98-
const tmdbId = searchResult.results[0].id;
99-
tmdbIds.push(tmdbId);
105+
const tmdbMovie = searchResult.results[0];
106+
tmdbMovies.push(tmdbMovie);
100107
}
101108
} catch (error) {
102109
continue;
@@ -106,10 +113,10 @@ export default defineCachedEventHandler(async (event) => {
106113
allLists.push({
107114
listName: listName,
108115
listUrl: listUrl,
109-
tmdbIds,
116+
tmdbMovies,
110117
metadata: {
111118
originalFilmCount: films.length,
112-
foundTmdbIds: tmdbIds.length,
119+
foundTmdbMovies: tmdbMovies.length,
113120
expectedItemCount: expectedItemCount,
114121
workingSelector
115122
}
@@ -119,10 +126,10 @@ export default defineCachedEventHandler(async (event) => {
119126
allLists.push({
120127
listName: listItem.title,
121128
listUrl: `https://letterboxd.com${listItem.href}`,
122-
tmdbIds: [],
129+
tmdbMovies: [],
123130
metadata: {
124131
originalFilmCount: 0,
125-
foundTmdbIds: 0,
132+
foundTmdbMovies: 0,
126133
expectedItemCount: null,
127134
error: 'Failed to process list'
128135
}
@@ -134,8 +141,8 @@ export default defineCachedEventHandler(async (event) => {
134141
lists: allLists,
135142
totalLists: allLists.length,
136143
summary: {
137-
totalTmdbIds: allLists.reduce((sum, list) => sum + list.tmdbIds.length, 0),
138-
totalExpectedItems: allLists.reduce((sum, list) => sum + (list.metadata.expectedItemCount || 0), 0)
144+
totalTmdbMovies: allLists.reduce((sum, list) => sum + list.tmdbMovies.length, 0),
145+
totalExpectedItems: allLists.reduce((sum, list) => sum + (list.metadata?.expectedItemCount || 0), 0)
139146
}
140147
};
141148
} catch (error) {

server/utils/metrics.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,22 +281,30 @@ export function recordProviderMetrics(items: any[], hostname: string, tool?: str
281281
// Record hostname once per request
282282
metrics.providerHostnames.inc({ hostname });
283283

284-
// Record status and watch metrics for each item
284+
// Record status metrics for each item
285285
items.forEach(item => {
286286
// Record provider status
287287
metrics.providerStatuses.inc({
288288
provider_id: item.embedId ?? item.providerId,
289289
status: item.status,
290290
});
291+
});
292+
293+
// Reverse items to get the last one, and find the last successful item
294+
const itemList = [...items];
295+
itemList.reverse();
296+
const lastSuccessfulItem = items.find(v => v.status === 'success');
297+
const lastItem = itemList[0];
291298

292-
// Record watch metrics for each item
299+
// Record watch metrics only for the last item
300+
if (lastItem) {
293301
metrics.watchMetrics.inc({
294-
tmdb_full_id: item.type + '-' + item.tmdbId,
295-
provider_id: item.providerId,
296-
title: item.title,
297-
success: (item.status === 'success').toString(),
302+
tmdb_full_id: lastItem.type + '-' + lastItem.tmdbId,
303+
provider_id: lastSuccessfulItem?.providerId ?? lastItem.providerId,
304+
title: lastItem.title,
305+
success: (!!lastSuccessfulItem).toString(),
298306
});
299-
});
307+
}
300308

301309
// Record tool metrics
302310
if (tool) {

0 commit comments

Comments
 (0)