Skip to content

Commit

Permalink
Account for no current owner, but historical owners
Browse files Browse the repository at this point in the history
Also add cache hit header, and ignore some caches locally.
  • Loading branch information
mcongrove committed Jan 11, 2025
1 parent 6cbf592 commit 8b6e8a1
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 17 deletions.
21 changes: 19 additions & 2 deletions src/pages/Car.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,24 @@ export const CarProfile = () => {
setCar(carData);

if (carData?.owner_history?.length) {
setTimelineOwners(carData.owner_history);
const ownerHistory = carData.owner_history;
const lastOwner = ownerHistory[0];

if (lastOwner?.date_end && !carData.destroyed) {
ownerHistory.unshift({
id: 'unknown-current',
name: 'Unknown',
date_end: undefined,
date_start: lastOwner.date_end,
city: undefined,
country: undefined,
state: undefined,
car_id: carData.id,
owner_id: 'unknown-current',
});
}

setTimelineOwners(ownerHistory);
}

if (carData?.vin) {
Expand Down Expand Up @@ -298,7 +315,7 @@ export const CarProfile = () => {
!startYear && !endYear
? ''
: index === 0
? car.destroyed
? car.destroyed || endYear
? `${startYear || 'Unknown'}${endYear || 'Destruction'}`
: `${startYear || 'Unknown'} – Present`
: `${startYear || 'Unknown'}${endYear || 'Unknown'}`,
Expand Down
1 change: 1 addition & 0 deletions src/utils/car.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const colorMap: Record<string, string> = {
'sapphire blue mica': '#002B7F',
silver: '#C6C9CA',
'silver stone metallic': '#C7C9C7',
'snowflake white pearl mica': '#F0F0F0',
'starlight mica blue': '#2E2787',
'strato blue mica': '#1B365D',
'sunburst yellow': '#FFD700',
Expand Down
20 changes: 16 additions & 4 deletions src/worker/routes/cars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ carsRouter.get('/', async (c) => {
const cached = await c.env.CACHE.get(cacheKey);

if (cached) {
return c.json(JSON.parse(cached));
const response = c.json(JSON.parse(cached));

response.headers.set('X-Cache', 'HIT');

return response;
}
}

Expand Down Expand Up @@ -254,8 +258,12 @@ carsRouter.get('/:id', async (c) => {
const cacheKey = `cars:details:${id}`;
const cached = await c.env.CACHE.get(cacheKey);

if (cached) {
return c.json(JSON.parse(cached));
if (cached && c.env.NODE_ENV !== 'development') {
const response = c.json(JSON.parse(cached));

response.headers.set('X-Cache', 'HIT');

return response;
}

const db = createDb(c.env.DB);
Expand Down Expand Up @@ -374,7 +382,11 @@ carsRouter.get('/:id/summary', async (c) => {
const cached = await c.env.CACHE.get(cacheKey);

if (cached) {
return c.json(JSON.parse(cached));
const response = c.json(JSON.parse(cached));

response.headers.set('X-Cache', 'HIT');

return response;
}

const db = createDb(c.env.DB);
Expand Down
16 changes: 12 additions & 4 deletions src/worker/routes/editions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ editionsRouter.get('/', async (c) => {
try {
const cached = await c.env.CACHE.get('editions:all');

if (cached) {
return c.json(JSON.parse(cached));
if (cached && c.env.NODE_ENV !== 'development') {
const response = c.json(JSON.parse(cached));

response.headers.set('X-Cache', 'HIT');

return response;
}

const db = createDb(c.env.DB);
Expand Down Expand Up @@ -97,8 +101,12 @@ editionsRouter.get('/names', async (c) => {
try {
const cached = await c.env.CACHE.get('editions:names');

if (cached) {
return c.json(JSON.parse(cached));
if (cached && c.env.NODE_ENV !== 'development') {
const response = c.json(JSON.parse(cached));

response.headers.set('X-Cache', 'HIT');

return response;
}

const db = createDb(c.env.DB);
Expand Down
12 changes: 10 additions & 2 deletions src/worker/routes/heartbeat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ archiveRouter.get('/pulse', async (c) => {
const cached = await c.env.CACHE.get('heartbeat:pulse');

if (cached) {
return c.json(JSON.parse(cached));
const response = c.json(JSON.parse(cached));

response.headers.set('X-Cache', 'HIT');

return response;
}

const clerk = createClerkClient({
Expand Down Expand Up @@ -104,7 +108,11 @@ archiveRouter.get('/archive', async (c) => {
return c.json({ error: 'No archive information available' }, 404);
}

return c.json(JSON.parse(cached));
const response = c.json(JSON.parse(cached));

response.headers.set('X-Cache', 'HIT');

return response;
} catch (error) {
console.error('Error fetching archive info:', error);

Expand Down
18 changes: 15 additions & 3 deletions src/worker/routes/news.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ newsRouter.get('/', async (c) => {
const cached = await c.env.CACHE.get(cacheKey);

if (cached) {
return c.json(JSON.parse(cached));
const response = c.json(JSON.parse(cached));

response.headers.set('X-Cache', 'HIT');

return response;
}
}

Expand Down Expand Up @@ -93,7 +97,11 @@ newsRouter.get('/featured', async (c) => {
const cached = await c.env.CACHE.get(cacheKey);

if (cached) {
return c.json(JSON.parse(cached));
const response = c.json(JSON.parse(cached));

response.headers.set('X-Cache', 'HIT');

return response;
}

const db = createDb(c.env.DB);
Expand Down Expand Up @@ -142,7 +150,11 @@ newsRouter.get('/:id', async (c) => {
const cached = await c.env.CACHE.get(cacheKey);

if (cached) {
return c.json(JSON.parse(cached));
const response = c.json(JSON.parse(cached));

response.headers.set('X-Cache', 'HIT');

return response;
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/worker/routes/owners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ ownersRouter.get('/countries', async (c) => {
const cached = await c.env.CACHE.get('owners:countries');

if (cached) {
return c.json(JSON.parse(cached));
const response = c.json(JSON.parse(cached));

response.headers.set('X-Cache', 'HIT');

return response;
}

const db = createDb(c.env.DB);
Expand Down
6 changes: 5 additions & 1 deletion src/worker/routes/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ statsRouter.get('/', async (c) => {
const cached = await c.env.CACHE.get('stats:all');

if (cached) {
return c.json(JSON.parse(cached));
const response = c.json(JSON.parse(cached));

response.headers.set('X-Cache', 'HIT');

return response;
}

const db = createDb(c.env.DB);
Expand Down

0 comments on commit 8b6e8a1

Please sign in to comment.