From 72605984c771368ce575e6691f99918d60ff6772 Mon Sep 17 00:00:00 2001 From: Ken Powers Date: Tue, 31 Dec 2024 16:07:16 -0500 Subject: [PATCH] Add search logging --- .../src/__generated__/graphql-types.ts | 4 ++-- apps/web/src/__generated__/graphql-types.ts | 4 ++-- .../migration.sql | 9 ++++++++ .../prisma/migrations/migration_lock.toml | 2 +- services/gateway/prisma/schema.prisma | 9 ++++++++ services/gateway/src/schema/types/search.ts | 22 ++++++++++--------- 6 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 services/gateway/prisma/migrations/20241231210020_add_search_log/migration.sql diff --git a/apps/web-next/src/__generated__/graphql-types.ts b/apps/web-next/src/__generated__/graphql-types.ts index cae99e57..b5ea0c09 100644 --- a/apps/web-next/src/__generated__/graphql-types.ts +++ b/apps/web-next/src/__generated__/graphql-types.ts @@ -1298,7 +1298,7 @@ export type UploadRecord = { updatedAt: Scalars['DateTime']['output']; uploadFinalized: Scalars['Boolean']['output']; uploadFinalizedAt?: Maybe; - uploadFinalizedBy: AppUser; + uploadFinalizedBy?: Maybe; uploadListById?: Maybe; uploadSizeBytes?: Maybe; userComments: UploadRecordUserCommentsConnection; @@ -1398,7 +1398,7 @@ export type UploadUserComment = { id: Scalars['ShortUuid']['output']; myRating?: Maybe; replies: UploadUserCommentRepliesConnection; - replyingTo: UploadUserComment; + replyingTo?: Maybe; text: Scalars['String']['output']; totalDislikes: Scalars['Int']['output']; totalLikes: Scalars['Int']['output']; diff --git a/apps/web/src/__generated__/graphql-types.ts b/apps/web/src/__generated__/graphql-types.ts index cae99e57..b5ea0c09 100644 --- a/apps/web/src/__generated__/graphql-types.ts +++ b/apps/web/src/__generated__/graphql-types.ts @@ -1298,7 +1298,7 @@ export type UploadRecord = { updatedAt: Scalars['DateTime']['output']; uploadFinalized: Scalars['Boolean']['output']; uploadFinalizedAt?: Maybe; - uploadFinalizedBy: AppUser; + uploadFinalizedBy?: Maybe; uploadListById?: Maybe; uploadSizeBytes?: Maybe; userComments: UploadRecordUserCommentsConnection; @@ -1398,7 +1398,7 @@ export type UploadUserComment = { id: Scalars['ShortUuid']['output']; myRating?: Maybe; replies: UploadUserCommentRepliesConnection; - replyingTo: UploadUserComment; + replyingTo?: Maybe; text: Scalars['String']['output']; totalDislikes: Scalars['Int']['output']; totalLikes: Scalars['Int']['output']; diff --git a/services/gateway/prisma/migrations/20241231210020_add_search_log/migration.sql b/services/gateway/prisma/migrations/20241231210020_add_search_log/migration.sql new file mode 100644 index 00000000..e038815d --- /dev/null +++ b/services/gateway/prisma/migrations/20241231210020_add_search_log/migration.sql @@ -0,0 +1,9 @@ +-- CreateTable +CREATE TABLE "search_log_entry" ( + "id" UUID NOT NULL DEFAULT gen_random_uuid(), + "query" TEXT NOT NULL, + "params" JSONB NOT NULL DEFAULT '{}', + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "search_log_entry_pkey" PRIMARY KEY ("id") +); diff --git a/services/gateway/prisma/migrations/migration_lock.toml b/services/gateway/prisma/migrations/migration_lock.toml index fbffa92c..648c57fd 100644 --- a/services/gateway/prisma/migrations/migration_lock.toml +++ b/services/gateway/prisma/migrations/migration_lock.toml @@ -1,3 +1,3 @@ # Please do not edit this file manually -# It should be added in your version-control system (i.e. Git) +# It should be added in your version-control system (e.g., Git) provider = "postgresql" \ No newline at end of file diff --git a/services/gateway/prisma/schema.prisma b/services/gateway/prisma/schema.prisma index 4ebd0450..353ae4cd 100644 --- a/services/gateway/prisma/schema.prisma +++ b/services/gateway/prisma/schema.prisma @@ -534,3 +534,12 @@ model UploadList { @@unique([createdAt, id]) @@map("upload_list") } + +model SearchLogEntry { + id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid + query String + params Json @default("{}") + createdAt DateTime @default(now()) @map("created_at") + + @@map("search_log_entry") +} diff --git a/services/gateway/src/schema/types/search.ts b/services/gateway/src/schema/types/search.ts index 3806c9db..d01dabde 100644 --- a/services/gateway/src/schema/types/search.ts +++ b/services/gateway/src/schema/types/search.ts @@ -274,9 +274,8 @@ builder.queryFields((t) => ({ }, ), }, - resolve: async ( - _root, - { + resolve: async (_root, args, context, _info) => { + const { query, focus, channels, @@ -287,11 +286,9 @@ builder.queryFields((t) => ({ orgType, organization, tags, - ...args - }, - context, - _info, - ) => { + ...restArgs + } = args; + let totalCount = 0; let uploadHitCount = 0; let transcriptHitCount = 0; @@ -301,7 +298,7 @@ builder.queryFields((t) => ({ let dateAggData: { min: Date; max: Date } | null = null; const publishedAt: { lte?: string; gte?: string } = {}; - const orderBy = OrderByEnum.parse(args.orderBy ?? 'avg'); + const orderBy = OrderByEnum.parse(restArgs.orderBy ?? 'avg'); if (minPublishedAt) { publishedAt.gte = @@ -345,7 +342,7 @@ builder.queryFields((t) => ({ : null; const res = await resolveOffsetConnection( - { args }, + { args: restArgs }, async ({ offset, limit }) => { const esRes = await esClient.msearch({ searches: [ @@ -543,6 +540,11 @@ builder.queryFields((t) => ({ }, ); + const { query: logQuery, ...logParams } = args; + await prisma.searchLogEntry.create({ + data: { query: logQuery, params: logParams }, + }); + return { ...res, totalCount,