Skip to content

Commit d8b68e2

Browse files
authored
Merge pull request #40 from atomantic/better/code-quality
fix: improve code quality - log errors, remove non-null assertions, extract constants
2 parents 37ae9fa + 00c61b2 commit d8b68e2

5 files changed

Lines changed: 31 additions & 14 deletions

File tree

server/src/services/cache.service.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,30 @@ class LRUCache<T> {
130130
}
131131
}
132132

133+
// Cache size and TTL constants per cache type
134+
const QUERY_CACHE_MAX_SIZE = 2000;
135+
const QUERY_CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes
136+
const PERSON_CACHE_MAX_SIZE = 5000;
137+
const PERSON_CACHE_TTL_MS = 10 * 60 * 1000; // 10 minutes
138+
const LIST_CACHE_MAX_SIZE = 100;
139+
const LIST_CACHE_TTL_MS = 60 * 1000; // 1 minute - lists change more frequently
140+
133141
// Create named caches for different query types
134142
const queryCache = new LRUCache<unknown>({
135-
maxSize: 2000,
136-
ttlMs: 5 * 60 * 1000, // 5 minutes
143+
maxSize: QUERY_CACHE_MAX_SIZE,
144+
ttlMs: QUERY_CACHE_TTL_MS,
137145
name: 'query'
138146
});
139147

140148
const personCache = new LRUCache<unknown>({
141-
maxSize: 5000,
142-
ttlMs: 10 * 60 * 1000, // 10 minutes
149+
maxSize: PERSON_CACHE_MAX_SIZE,
150+
ttlMs: PERSON_CACHE_TTL_MS,
143151
name: 'person'
144152
});
145153

146154
const listCache = new LRUCache<unknown>({
147-
maxSize: 100,
148-
ttlMs: 60 * 1000, // 1 minute - lists change more frequently
155+
maxSize: LIST_CACHE_MAX_SIZE,
156+
ttlMs: LIST_CACHE_TTL_MS,
149157
name: 'list'
150158
});
151159

server/src/services/id-mapping.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const canonicalToExternalCache = new Map<string, Map<string, string>>();
1717

1818
// Cache size limit (LRU eviction)
1919
const MAX_CACHE_SIZE = 100000;
20+
const EVICTION_RATIO = 0.1; // Evict 10% of entries when cache is full
2021

2122
/**
2223
* Generate a cache key for external ID lookups
@@ -30,8 +31,7 @@ function cacheKey(source: string, externalId: string): string {
3031
*/
3132
function evictIfNeeded(): void {
3233
if (externalToCanonicalCache.size > MAX_CACHE_SIZE) {
33-
// Simple eviction: remove first 10% of entries
34-
const toRemove = Math.floor(MAX_CACHE_SIZE * 0.1);
34+
const toRemove = Math.floor(MAX_CACHE_SIZE * EVICTION_RATIO);
3535
const keys = externalToCanonicalCache.keys();
3636
for (let i = 0; i < toRemove; i++) {
3737
const key = keys.next().value;

server/src/services/multi-platform-comparison.service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ async function downloadProviderPhoto(
9494

9595
await downloadImage(normalizedUrl, photoPath).catch(err => {
9696
logger.error('compare', `Failed to download ${provider} photo: ${err.message}`);
97-
return null;
9897
});
9998

10099
if (fs.existsSync(photoPath)) {

server/src/services/path.service.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ function buildAncestryMap(
1717
const queue: Array<{ id: string; depth: number }> = [{ id: startId, depth: 0 }];
1818

1919
while (queue.length > 0) {
20-
const current = queue.shift()!;
20+
const current = queue.shift();
21+
if (!current) break;
2122
if (current.depth >= maxDepth) continue;
2223

2324
const parents = sqliteService.queryAll<{ parent_id: string }>(
@@ -246,7 +247,8 @@ export const pathService = {
246247
const queue: Array<{ id: string; depth: number }> = [{ id: canonicalId, depth: 0 }];
247248

248249
while (queue.length > 0) {
249-
const current = queue.shift()!;
250+
const current = queue.shift();
251+
if (!current) break;
250252
if (current.depth >= maxDepth) continue;
251253

252254
const parents = sqliteService.queryAll<{ parent_id: string }>(
@@ -282,7 +284,8 @@ export const pathService = {
282284
const queue: Array<{ id: string; depth: number }> = [{ id: canonicalId, depth: 0 }];
283285

284286
while (queue.length > 0) {
285-
const current = queue.shift()!;
287+
const current = queue.shift();
288+
if (!current) break;
286289
if (current.depth >= maxDepth) continue;
287290

288291
const children = sqliteService.queryAll<{ child_id: string }>(

server/src/services/sync.service.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
import { browserService } from './browser.service';
1212
import { providerService } from './provider.service';
1313
import { DATA_DIR } from '../utils/paths.js';
14+
import { logger } from '../lib/logger.js';
1415

1516
/**
1617
* Sync service for comparing and syncing data across providers
@@ -60,7 +61,10 @@ export const syncService = {
6061
if (!config.enabled) continue;
6162

6263
const scraped = await this.scrapeFromProvider(provider, personId)
63-
.catch(() => null);
64+
.catch(err => {
65+
logger.error('sync', `Failed to scrape ${provider}/${personId}: ${err.message}`);
66+
return null;
67+
});
6468

6569
comparison.providerData[provider] = scraped;
6670
}
@@ -139,7 +143,10 @@ export const syncService = {
139143
const resultLink = await page.$eval(
140144
'a[href*="/person/"], a[href*="/tree/person/"], a[href*="/wiki/"]',
141145
el => el.getAttribute('href')
142-
).catch(() => null);
146+
).catch(err => {
147+
logger.error('sync', `Failed to find result link for ${targetProvider}: ${err.message}`);
148+
return null;
149+
});
143150

144151
if (!resultLink) {
145152
await page.close().catch(() => {});

0 commit comments

Comments
 (0)