From 44c83d3b33b651bfdd42b863eeafcfd338acb796 Mon Sep 17 00:00:00 2001 From: ntr Date: Tue, 30 Aug 2022 18:39:22 +1000 Subject: [PATCH] moar logs --- api/prisma/schema.prisma | 2 - api/src/discogs.ts | 5 ++- api/src/events.ts | 26 +++++++------ api/src/utils.ts | 13 +++++++ client/src/app.tsx | 3 +- client/src/components/header.tsx | 9 ++++- client/src/routes/home.tsx | 2 + client/src/routes/login.tsx | 11 +++--- client/tsconfig.json | 64 ++++++++++++++++++++++++++++++++ 9 files changed, 113 insertions(+), 22 deletions(-) create mode 100644 client/tsconfig.json diff --git a/api/prisma/schema.prisma b/api/prisma/schema.prisma index 5afbe05..4fe6b0d 100644 --- a/api/prisma/schema.prisma +++ b/api/prisma/schema.prisma @@ -69,8 +69,6 @@ model Offer { total Decimal @db.Money comparison Decimal @db.Money - sellerId Int - listingId Int listing Listing @relation(fields: [listingId], references: [id]) diff --git a/api/src/discogs.ts b/api/src/discogs.ts index 0ed41e2..c8a29f9 100644 --- a/api/src/discogs.ts +++ b/api/src/discogs.ts @@ -7,7 +7,7 @@ import Bluebird from 'bluebird'; import type Log from 'bunyan'; import type { PrismaClient, Want, User, Listing } from '@prisma/client'; -import { logAxiosError } from './utils'; +import { logAxiosError, wantDetails } from './utils'; import { Pg } from './pg'; // margh @@ -39,6 +39,8 @@ export default function createDiscogs(log: Log, pg: Pg) { const { data: { wants, pagination }} = await api.get(`${apiBase}/users/${username}/wants`); const releases = await Promise.all(wants.map(want => release(want.id))); + log.info({ wants: wants.length, user: username, userId: user.id }, 'fetched discogs wantlist'); + // TODO filter releases to be vinyl format only // convert want object to actual release and ignore duplicate errors @@ -83,6 +85,7 @@ export default function createDiscogs(log: Log, pg: Pg) { // preferentially take listings from sellers if we hit req limits + log.info({ listings: ids.length, ...wantDetails(want)}, 'found listings'); return Bluebird.mapSeries(ids, async id => listing(id, want)); } diff --git a/api/src/events.ts b/api/src/events.ts index 5bf2ab8..160c634 100644 --- a/api/src/events.ts +++ b/api/src/events.ts @@ -1,9 +1,11 @@ import { EventEmitter } from 'node:events'; +import { setTimeout } from 'timers/promises'; import type Log from 'bunyan'; import type { Want } from '@prisma/client'; import { Pg } from './pg'; +import { wantDetails } from './utils'; import routerAuth from './routers/router.auth'; export enum Events { @@ -19,9 +21,10 @@ export default function createEvents(log: Log, pg: Pg, discogs) { }); events.on(Events.WantsAdded, async function wantsAdded(wants: Want[]) { + log.info({ event: Events.WantsAdded, wants: wants.length }); for (const want of wants) { const listings = await discogs.listings(want); - log.info({ count: listings.length, title: want.id }, 'listings added') + log.info({ count: listings.length, ...wantDetails(want) }, 'listings added') } log.info('finished fetching listings', { wants: wants.length }); @@ -29,20 +32,21 @@ export default function createEvents(log: Log, pg: Pg, discogs) { events.on(Events.StartupSync, async function wantsAdded() { const wants = await pg.wantsToSync(); + log.info({ event: Events.StartupSync, wants: wants.length }); - try { - for (const want of wants) { + for (const want of wants) { + try { const listings = await discogs.listings(want); - log.info({ count: listings.length, title: want.id }, 'listings added') - } - - log.info('finished syncing', { wants: wants.length }); - } catch (e) { - if (e.message === 'rate limited') { - log.warn('rate limited, trying again in 60s'); - return setTimeout(() => events.emit(Events.StartupSync), 60 * 1000); + log.info({ count: listings.length, ...wantDetails(want) }, 'listings added') + } catch (e) { + if (e.message === 'rate limited') { + log.warn('rate limited, trying again in 60s'); + await setTimeout(60 * 1000); + const listings = await discogs.listings(want); + } } } + log.info('finished syncing', { wants: wants.length }); }); diff --git a/api/src/utils.ts b/api/src/utils.ts index 4ef9082..7254e45 100644 --- a/api/src/utils.ts +++ b/api/src/utils.ts @@ -1,3 +1,5 @@ +import type { Want } from '@prisma/client'; + export function logAxiosError(e) { console.warn({ status: e.response.status, @@ -11,3 +13,14 @@ export function logAxiosError(e) { }, }); } + +export function wantDetails(want: Want) { + const data = want.data as any; + + return { + title: data.title, + release: data.id, + artist: data.artists_sort, + id: want.id + } +} \ No newline at end of file diff --git a/client/src/app.tsx b/client/src/app.tsx index 368510d..7843f36 100644 --- a/client/src/app.tsx +++ b/client/src/app.tsx @@ -37,7 +37,8 @@ function App() {
- + + diff --git a/client/src/components/header.tsx b/client/src/components/header.tsx index 96955c2..a910463 100644 --- a/client/src/components/header.tsx +++ b/client/src/components/header.tsx @@ -10,6 +10,13 @@ const Header = ({ user }) => { ); + const anonNav = () => ( + + ); + const adminNav = () => (