From f1f7660ea2c2f3ea9541644a579fe5aaa53d9e9e Mon Sep 17 00:00:00 2001 From: William Oldham Date: Sat, 11 Nov 2023 16:02:37 +0000 Subject: [PATCH 1/2] Set error fields to have type `text` and add hostname field --- package.json | 2 +- src/db/migrations/.snapshot-movie_web.json | 15 ++++++++++++--- src/db/migrations/Migration20231111160045.ts | 17 +++++++++++++++++ src/db/models/ProviderMetrics.ts | 7 +++++-- src/modules/metrics/index.ts | 2 ++ src/routes/metrics.ts | 11 +++++++++-- 6 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 src/db/migrations/Migration20231111160045.ts diff --git a/package.json b/package.json index f822165..369c912 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "backend", - "version": "1.0.3", + "version": "1.0.4", "private": true, "homepage": "https://github.com/movie-web/backend", "engines": { diff --git a/src/db/migrations/.snapshot-movie_web.json b/src/db/migrations/.snapshot-movie_web.json index 7cbd87f..4b40879 100644 --- a/src/db/migrations/.snapshot-movie_web.json +++ b/src/db/migrations/.snapshot-movie_web.json @@ -363,20 +363,29 @@ }, "error_message": { "name": "error_message", - "type": "varchar(255)", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, "nullable": true, - "mappedType": "string" + "mappedType": "text" }, "full_error": { "name": "full_error", - "type": "varchar(255)", + "type": "text", "unsigned": false, "autoincrement": false, "primary": false, "nullable": true, + "mappedType": "text" + }, + "hostname": { + "name": "hostname", + "type": "varchar(255)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, "mappedType": "string" } }, diff --git a/src/db/migrations/Migration20231111160045.ts b/src/db/migrations/Migration20231111160045.ts new file mode 100644 index 0000000..83c42c2 --- /dev/null +++ b/src/db/migrations/Migration20231111160045.ts @@ -0,0 +1,17 @@ +import { Migration } from '@mikro-orm/migrations'; + +export class Migration20231111160045 extends Migration { + + async up(): Promise { + this.addSql('alter table "provider_metrics" add column "hostname" varchar(255) not null;'); + this.addSql('alter table "provider_metrics" alter column "error_message" type text using ("error_message"::text);'); + this.addSql('alter table "provider_metrics" alter column "full_error" type text using ("full_error"::text);'); + } + + async down(): Promise { + this.addSql('alter table "provider_metrics" alter column "error_message" type varchar(255) using ("error_message"::varchar(255));'); + this.addSql('alter table "provider_metrics" alter column "full_error" type varchar(255) using ("full_error"::varchar(255));'); + this.addSql('alter table "provider_metrics" drop column "hostname";'); + } + +} diff --git a/src/db/models/ProviderMetrics.ts b/src/db/models/ProviderMetrics.ts index 4f7e3b8..4a84ce0 100644 --- a/src/db/models/ProviderMetrics.ts +++ b/src/db/models/ProviderMetrics.ts @@ -40,9 +40,12 @@ export class ProviderMetric { @Property({ name: 'embed_id', nullable: true }) embedId?: string; - @Property({ name: 'error_message', nullable: true }) + @Property({ name: 'error_message', nullable: true, type: 'text' }) errorMessage?: string; - @Property({ name: 'full_error', nullable: true }) + @Property({ name: 'full_error', nullable: true, type: 'text' }) fullError?: string; + + @Property({ name: 'hostname' }) + hostname!: string; } diff --git a/src/modules/metrics/index.ts b/src/modules/metrics/index.ts index 45a25fc..9d23d4b 100644 --- a/src/modules/metrics/index.ts +++ b/src/modules/metrics/index.ts @@ -18,6 +18,7 @@ export type Metrics = { | 'type' | 'provider_id' | 'embed_id' + | 'hostname' >; }; @@ -57,6 +58,7 @@ export async function setupMetrics(app: FastifyInstance) { 'tmdb_id', 'type', 'embed_id', + 'hostname', ], }), }; diff --git a/src/routes/metrics.ts b/src/routes/metrics.ts index b53863d..899ce5e 100644 --- a/src/routes/metrics.ts +++ b/src/routes/metrics.ts @@ -37,19 +37,25 @@ export const metricsRouter = makeRouter((app) => { window: '30m', }); + const hostname = req.headers.origin?.slice(0, 255) ?? 'unknown origin'; + const entities = body.items.map((v) => { + const errorMessage = v.errorMessage?.slice(0, 200); + const truncatedFullError = v.fullError?.slice(0, 2000); + const metric = new ProviderMetric(); em.assign(metric, { providerId: v.providerId, embedId: v.embedId, - fullError: v.fullError, - errorMessage: v.errorMessage, + fullError: truncatedFullError, + errorMessage: errorMessage, episodeId: v.episodeId, seasonId: v.seasonId, status: v.status, title: v.title, tmdbId: v.tmdbId, type: v.type, + hostname, }); return metric; }); @@ -63,6 +69,7 @@ export const metricsRouter = makeRouter((app) => { title: entity.title, tmdb_id: entity.tmdbId, type: entity.type, + hostname, }); }); From f54a45256bc4adead4ea193a7f967011dd2c17d0 Mon Sep 17 00:00:00 2001 From: William Oldham Date: Sat, 11 Nov 2023 16:05:06 +0000 Subject: [PATCH 2/2] =?UTF-8?q?Format=20migrations=20to=20be=20=E2=9C=A8?= =?UTF-8?q?=20pretty=20=E2=9C=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/migrations/Migration20231111160045.ts | 22 +++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/db/migrations/Migration20231111160045.ts b/src/db/migrations/Migration20231111160045.ts index 83c42c2..866fc12 100644 --- a/src/db/migrations/Migration20231111160045.ts +++ b/src/db/migrations/Migration20231111160045.ts @@ -1,17 +1,25 @@ import { Migration } from '@mikro-orm/migrations'; export class Migration20231111160045 extends Migration { - async up(): Promise { - this.addSql('alter table "provider_metrics" add column "hostname" varchar(255) not null;'); - this.addSql('alter table "provider_metrics" alter column "error_message" type text using ("error_message"::text);'); - this.addSql('alter table "provider_metrics" alter column "full_error" type text using ("full_error"::text);'); + this.addSql( + 'alter table "provider_metrics" add column "hostname" varchar(255) not null;', + ); + this.addSql( + 'alter table "provider_metrics" alter column "error_message" type text using ("error_message"::text);', + ); + this.addSql( + 'alter table "provider_metrics" alter column "full_error" type text using ("full_error"::text);', + ); } async down(): Promise { - this.addSql('alter table "provider_metrics" alter column "error_message" type varchar(255) using ("error_message"::varchar(255));'); - this.addSql('alter table "provider_metrics" alter column "full_error" type varchar(255) using ("full_error"::varchar(255));'); + this.addSql( + 'alter table "provider_metrics" alter column "error_message" type varchar(255) using ("error_message"::varchar(255));', + ); + this.addSql( + 'alter table "provider_metrics" alter column "full_error" type varchar(255) using ("full_error"::varchar(255));', + ); this.addSql('alter table "provider_metrics" drop column "hostname";'); } - }