From 1d1ed7f97bb0142c00e3a1874dfb368903b1b506 Mon Sep 17 00:00:00 2001 From: andresgutgon Date: Wed, 11 Dec 2024 18:10:33 +0100 Subject: [PATCH] feat: wip --- .../DocumentLogs/DocumentLogsTable.tsx | 2 +- .../logs/[documentLogUuid]/position/route.ts | 18 ++---- .../[documentUuid]/logs/aggregations/route.ts | 16 ++--- .../[documentUuid]/logs/daily-count/route.ts | 15 ++--- .../[documentUuid]/logs/pagination/route.ts | 23 ++----- .../hooks/logFilters/useProcessLogFilters.ts | 3 +- apps/web/src/services/helpers.ts | 25 -------- apps/web/src/services/routes/api.ts | 60 +++++++++++-------- packages/core/src/constants.ts | 1 + .../generateDocumentLogsApiRouteWithParams.ts | 23 +++++++ .../documentLogs/logsFilterUtils/index.ts | 1 + 11 files changed, 81 insertions(+), 106 deletions(-) delete mode 100644 apps/web/src/services/helpers.ts create mode 100644 packages/core/src/services/documentLogs/logsFilterUtils/generateDocumentLogsApiRouteWithParams.ts diff --git a/apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/_components/DocumentLogs/DocumentLogsTable.tsx b/apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/_components/DocumentLogs/DocumentLogsTable.tsx index a2f8add35..c34ed68a2 100644 --- a/apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/_components/DocumentLogs/DocumentLogsTable.tsx +++ b/apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/_components/DocumentLogs/DocumentLogsTable.tsx @@ -6,6 +6,7 @@ import { capitalize } from 'lodash-es' import { DocumentLogFilterOptions, EvaluationResultableType, + LOG_FILTERS_ENCODED_PARAMS, } from '@latitude-data/core/browser' import { buildPagination } from '@latitude-data/core/lib/pagination/buildPagination' import { @@ -40,7 +41,6 @@ import useDocumentLogsPagination from '$/stores/useDocumentLogsPagination' import { useSearchParams } from 'next/navigation' import { ResultCellContent } from '../../../evaluations/[evaluationId]/_components/EvaluationResults/EvaluationResultsTable' -import { LOG_FILTERS_ENCODED_PARAMS } from '$/hooks/logFilters/useProcessLogFilters' const countLabel = (selected: number) => (count: number) => { return selected ? `${selected} of ${count} logs selected` : `${count} logs` diff --git a/apps/web/src/app/api/projects/[projectId]/documents/[documentUuid]/logs/[documentLogUuid]/position/route.ts b/apps/web/src/app/api/projects/[projectId]/documents/[documentUuid]/logs/[documentLogUuid]/position/route.ts index af54e6524..180cea610 100644 --- a/apps/web/src/app/api/projects/[projectId]/documents/[documentUuid]/logs/[documentLogUuid]/position/route.ts +++ b/apps/web/src/app/api/projects/[projectId]/documents/[documentUuid]/logs/[documentLogUuid]/position/route.ts @@ -1,9 +1,9 @@ -import { LogSources, Workspace } from '@latitude-data/core/browser' +import { Workspace } from '@latitude-data/core/browser' import { fetchDocumentLogWithPosition } from '@latitude-data/core/services/documentLogs/fetchDocumentLogWithPosition' import { authHandler } from '$/middlewares/authHandler' import { errorHandler } from '$/middlewares/errorHandler' import { NextRequest, NextResponse } from 'next/server' -import { decodeParameters } from '$/services/helpers' +import { parseApiDocumentLogParams } from '@latitude-data/core/services/documentLogs/index' export const GET = errorHandler( authHandler( @@ -23,20 +23,12 @@ export const GET = errorHandler( }, ) => { const searchParams = req.nextUrl.searchParams - const excludeErrors = searchParams.get('excludeErrors') === 'true' - const { commitIds, logSources } = decodeParameters(req.nextUrl.search) - const filterOptions = { - commitIds: Array.isArray(commitIds) ? commitIds.map(Number) : [], - logSources: (Array.isArray(logSources) - ? logSources - : []) as LogSources[], - } - + const queryParams = parseApiDocumentLogParams({ searchParams }) const result = await fetchDocumentLogWithPosition({ workspace, - filterOptions, documentLogUuid, - excludeErrors, + filterOptions: queryParams.filterOptions, + excludeErrors: queryParams.excludeErrors, }) if (result.error) { diff --git a/apps/web/src/app/api/projects/[projectId]/documents/[documentUuid]/logs/aggregations/route.ts b/apps/web/src/app/api/projects/[projectId]/documents/[documentUuid]/logs/aggregations/route.ts index 0b4a92a9d..1bc6ef040 100644 --- a/apps/web/src/app/api/projects/[projectId]/documents/[documentUuid]/logs/aggregations/route.ts +++ b/apps/web/src/app/api/projects/[projectId]/documents/[documentUuid]/logs/aggregations/route.ts @@ -1,9 +1,9 @@ -import { LogSources, Workspace } from '@latitude-data/core/browser' +import { Workspace } from '@latitude-data/core/browser' import { computeDocumentLogsAggregations } from '@latitude-data/core/services/documentLogs/computeDocumentLogsAggregations' import { authHandler } from '$/middlewares/authHandler' import { errorHandler } from '$/middlewares/errorHandler' import { NextRequest, NextResponse } from 'next/server' -import { decodeParameters } from '$/services/helpers' +import { parseApiDocumentLogParams } from '@latitude-data/core/services/documentLogs/index' export const GET = errorHandler( authHandler( @@ -22,18 +22,12 @@ export const GET = errorHandler( }, ) => { const { documentUuid } = params - const { commitIds, logSources } = decodeParameters(req.nextUrl.search) - const filterOptions = { - commitIds: Array.isArray(commitIds) ? commitIds.map(Number) : [], - logSources: (Array.isArray(logSources) - ? logSources - : []) as LogSources[], - } - + const searchParams = req.nextUrl.searchParams + const queryParams = parseApiDocumentLogParams({ searchParams }) const result = await computeDocumentLogsAggregations({ workspace, documentUuid, - filterOptions, + filterOptions: queryParams.filterOptions, }) return NextResponse.json(result, { status: 200 }) diff --git a/apps/web/src/app/api/projects/[projectId]/documents/[documentUuid]/logs/daily-count/route.ts b/apps/web/src/app/api/projects/[projectId]/documents/[documentUuid]/logs/daily-count/route.ts index d8661bd70..84eaf8edb 100644 --- a/apps/web/src/app/api/projects/[projectId]/documents/[documentUuid]/logs/daily-count/route.ts +++ b/apps/web/src/app/api/projects/[projectId]/documents/[documentUuid]/logs/daily-count/route.ts @@ -1,9 +1,9 @@ -import { LogSources, Workspace } from '@latitude-data/core/browser' +import { Workspace } from '@latitude-data/core/browser' import { computeDocumentLogsDailyCount } from '@latitude-data/core/services/documentLogs/computeDocumentLogsDailyCount' import { authHandler } from '$/middlewares/authHandler' import { errorHandler } from '$/middlewares/errorHandler' import { NextRequest, NextResponse } from 'next/server' -import { decodeParameters } from '$/services/helpers' +import { parseApiDocumentLogParams } from '@latitude-data/core/services/documentLogs/index' export const GET = errorHandler( authHandler( @@ -23,21 +23,14 @@ export const GET = errorHandler( ) => { const { documentUuid } = params const searchParams = req.nextUrl.searchParams + const queryParams = parseApiDocumentLogParams({ searchParams }) const days = searchParams.get('days') ? parseInt(searchParams.get('days')!, 10) : undefined - const { commitIds, logSources } = decodeParameters(req.nextUrl.search) - const filterOptions = { - commitIds: Array.isArray(commitIds) ? commitIds.map(Number) : [], - logSources: (Array.isArray(logSources) - ? logSources - : []) as LogSources[], - } - const result = await computeDocumentLogsDailyCount({ workspace, documentUuid, - filterOptions, + filterOptions: queryParams.filterOptions, days, }) diff --git a/apps/web/src/app/api/projects/[projectId]/documents/[documentUuid]/logs/pagination/route.ts b/apps/web/src/app/api/projects/[projectId]/documents/[documentUuid]/logs/pagination/route.ts index ab1a6de31..027690caa 100644 --- a/apps/web/src/app/api/projects/[projectId]/documents/[documentUuid]/logs/pagination/route.ts +++ b/apps/web/src/app/api/projects/[projectId]/documents/[documentUuid]/logs/pagination/route.ts @@ -1,4 +1,4 @@ -import { LogSources, Workspace } from '@latitude-data/core/browser' +import { Workspace } from '@latitude-data/core/browser' import { buildPagination } from '@latitude-data/core/lib/pagination/buildPagination' import { computeDocumentLogsCount } from '@latitude-data/core/services/documentLogs/computeDocumentLogs' import { computeDocumentLogsWithMetadataCount } from '@latitude-data/core/services/documentLogs/computeDocumentLogsWithMetadata' @@ -6,7 +6,7 @@ import { authHandler } from '$/middlewares/authHandler' import { errorHandler } from '$/middlewares/errorHandler' import { ROUTES } from '$/services/routes' import { NextRequest, NextResponse } from 'next/server' -import { decodeParameters } from '$/services/helpers' +import { parseApiDocumentLogParams } from '@latitude-data/core/services/documentLogs/index' function pageUrl(params: { projectId: string @@ -37,15 +37,8 @@ export const GET = errorHandler( }, ) => { const searchParams = req.nextUrl.searchParams + const queryParams = parseApiDocumentLogParams({ searchParams }) const excludeErrors = searchParams.get('excludeErrors') === 'true' - const { commitIds, logSources } = decodeParameters(req.nextUrl.search) - const filterOptions = { - commitIds: Array.isArray(commitIds) ? commitIds.map(Number) : [], - logSources: (Array.isArray(logSources) - ? logSources - : []) as LogSources[], - } - const queryFn = excludeErrors ? computeDocumentLogsCount : computeDocumentLogsWithMetadataCount @@ -53,7 +46,7 @@ export const GET = errorHandler( const count = await queryFn({ workspaceId: workspace.id, documentUuid: params.documentUuid, - filterOptions, + filterOptions: queryParams.filterOptions, }) const pagination = buildPagination({ @@ -63,12 +56,8 @@ export const GET = errorHandler( documentUuid: params.documentUuid, }), count, - page: searchParams.get('page') - ? parseInt(searchParams.get('page') as string) - : 1, - pageSize: searchParams.get('pageSize') - ? parseInt(searchParams.get('pageSize') as string) - : 25, + page: +queryParams.page, + pageSize: +queryParams.pageSize, }) return NextResponse.json(pagination, { status: 200 }) diff --git a/apps/web/src/hooks/logFilters/useProcessLogFilters.ts b/apps/web/src/hooks/logFilters/useProcessLogFilters.ts index a36726547..9363db264 100644 --- a/apps/web/src/hooks/logFilters/useProcessLogFilters.ts +++ b/apps/web/src/hooks/logFilters/useProcessLogFilters.ts @@ -2,14 +2,13 @@ import { useCallback, useMemo } from 'react' import { usePathname, useRouter } from 'next/navigation' import { DocumentLogFilterOptions, + LOG_FILTERS_ENCODED_PARAMS, LOG_SOURCES, LogSources, } from '@latitude-data/core/browser' import { ReactStateDispatch } from '@latitude-data/web-ui' import { paramsToString } from '@latitude-data/core/lib/pagination/buildPaginatedUrl' -export const LOG_FILTERS_ENCODED_PARAMS = ['customIdentifier'] - function useEditableSearchParams() { const router = useRouter() const pathname = usePathname() diff --git a/apps/web/src/services/helpers.ts b/apps/web/src/services/helpers.ts deleted file mode 100644 index 2ae380cc6..000000000 --- a/apps/web/src/services/helpers.ts +++ /dev/null @@ -1,25 +0,0 @@ -type SimpleParam = string | number | boolean -type UriParam = SimpleParam | SimpleParam[] - -function encodeUriParam(value: UriParam) { - if (Array.isArray(value)) return value.map(encodeURIComponent).join(',') - - return encodeURIComponent(value) -} - -export function addParameters( - route: string, - parameters: Record, -): string { - const [path, query] = route.split('?') - const params = [ - ...(query?.split('&') ?? []), - ...Object.entries(parameters) - .filter(([_, v]) => v !== undefined) - .map(([key, value]) => `${key}=${encodeUriParam(value!)}`), - ] - - if (!params.length) return path! - - return `${path}?${params.join('&')}` -} diff --git a/apps/web/src/services/routes/api.ts b/apps/web/src/services/routes/api.ts index 8652f0f2e..dd4eda04a 100644 --- a/apps/web/src/services/routes/api.ts +++ b/apps/web/src/services/routes/api.ts @@ -1,6 +1,5 @@ import { DocumentLogFilterOptions } from '@latitude-data/core/browser' - -import { addParameters } from '../helpers' +import { generateDocumentLogsApiRouteWithParams } from '@latitude-data/core/services/documentLogs/index' type PaginationParameters = { page: number; pageSize: number } @@ -91,13 +90,15 @@ export const _API_ROUTES = { excludeErrors?: boolean filterOptions: DocumentLogFilterOptions }) => - addParameters(`${documentRoot}/logs`, { - page, - pageSize, - excludeErrors, - ...filterOptions, + generateDocumentLogsApiRouteWithParams({ + path: `${documentRoot}/logs`, + params: { + page, + pageSize, + excludeErrors, + filterOptions, + }, }), - pagination: ({ page, pageSize, @@ -107,18 +108,22 @@ export const _API_ROUTES = { excludeErrors?: boolean filterOptions: DocumentLogFilterOptions }) => - addParameters(`${documentRoot}/logs/pagination`, { - page, - pageSize, - excludeErrors, - ...filterOptions, + generateDocumentLogsApiRouteWithParams({ + path: `${documentRoot}/logs/pagination`, + params: { + page, + pageSize, + excludeErrors, + filterOptions, + }, }), - aggregations: (filterOptions: DocumentLogFilterOptions) => - addParameters( - `${documentRoot}/logs/aggregations`, - filterOptions, - ), + generateDocumentLogsApiRouteWithParams({ + path: `${documentRoot}/logs/aggregations`, + params: { + filterOptions, + }, + }), dailyCount: ({ filterOptions, days, @@ -126,9 +131,12 @@ export const _API_ROUTES = { filterOptions: DocumentLogFilterOptions days?: number }) => - addParameters(`${documentRoot}/logs/daily-count`, { - days, - ...filterOptions, + generateDocumentLogsApiRouteWithParams({ + path: `${documentRoot}/logs/daily-count`, + params: { + days, + filterOptions, + }, }), detail: (documentLogUuid: string) => { return { @@ -139,13 +147,13 @@ export const _API_ROUTES = { excludeErrors?: boolean filterOptions: DocumentLogFilterOptions }) => - addParameters( - `${documentRoot}/logs/${documentLogUuid}/position`, - { + generateDocumentLogsApiRouteWithParams({ + path: `${documentRoot}/logs/${documentLogUuid}/position`, + params: { excludeErrors, - ...filterOptions, + filterOptions, }, - ), + }), } }, }, diff --git a/packages/core/src/constants.ts b/packages/core/src/constants.ts index ac38f602b..d9ff06b04 100644 --- a/packages/core/src/constants.ts +++ b/packages/core/src/constants.ts @@ -470,3 +470,4 @@ export const RELATIVE_DATES = { } as const export type RelativeDate = keyof typeof RELATIVE_DATES +export const LOG_FILTERS_ENCODED_PARAMS = ['customIdentifier'] diff --git a/packages/core/src/services/documentLogs/logsFilterUtils/generateDocumentLogsApiRouteWithParams.ts b/packages/core/src/services/documentLogs/logsFilterUtils/generateDocumentLogsApiRouteWithParams.ts new file mode 100644 index 000000000..2acb40bf7 --- /dev/null +++ b/packages/core/src/services/documentLogs/logsFilterUtils/generateDocumentLogsApiRouteWithParams.ts @@ -0,0 +1,23 @@ +import { + DocumentLogFilterOptions, + LOG_FILTERS_ENCODED_PARAMS, +} from '../../../constants' + +export function generateDocumentLogsApiRouteWithParams({ + path: _path, + params: _params, + paramsToEncode: _pe = LOG_FILTERS_ENCODED_PARAMS, +}: { + path: string + paramsToEncode?: string[] + params: { + filterOptions?: DocumentLogFilterOptions + page?: number + pageSize?: number + excludeErrors?: boolean + days?: number | undefined + } +}) { + // TODO: Implement this function + return 'caca' +} diff --git a/packages/core/src/services/documentLogs/logsFilterUtils/index.ts b/packages/core/src/services/documentLogs/logsFilterUtils/index.ts index 255f5947c..82b1a2c40 100644 --- a/packages/core/src/services/documentLogs/logsFilterUtils/index.ts +++ b/packages/core/src/services/documentLogs/logsFilterUtils/index.ts @@ -1,2 +1,3 @@ export * from './parseLogFilterParams' export * from './parseApiLogFilterParams' +export * from './generateDocumentLogsApiRouteWithParams'