Skip to content

Commit 9448d61

Browse files
authored
Feature/document logs limited view part 2 (#1364)
* Document logs limited view * PR comments
1 parent 6a56fab commit 9448d61

File tree

73 files changed

+8096
-664
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+8096
-664
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,3 @@ TODO.md
6868
# Sentry
6969
.sentryclirc
7070
.env.sentry-build-plugin
71-
bin

apps/console/src/index.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import * as loadModules from './replReload'
1+
import { database, utils } from '@latitude-data/core/client'
22
import * as models from '@latitude-data/core/schema'
3+
import { randomUUID as uuid } from 'node:crypto'
34
import repl from 'node:repl'
4-
import { database, dbUtils } from '@latitude-data/core/client'
5+
import { inspect as utilInspect } from 'node:util'
56
import { setupReplHistory } from './replHistory'
7+
import * as loadModules from './replReload'
8+
9+
const inspect = (value: any) => console.log(utilInspect(value, { depth: null }))
610

711
const hasS3 = process.env.S3_BUCKET
812

@@ -21,15 +25,11 @@ const r = repl.start({
2125
// History in Repl enabled
2226
setupReplHistory(r)
2327

24-
r.context.database = {
25-
...dbUtils,
28+
Object.assign(r.context, {
29+
...utils,
2630
...models,
27-
db: database,
28-
}
29-
30-
// Use this to load code
31-
// Ex: mod = await loadModule('@latitude-data/core/data-migrations')
32-
r.context.loadModule = loadModules.loadModule
33-
34-
// Reload TS code changed in the modules you're importing
35-
r.context.reload = loadModules.reloadAllModules
31+
database,
32+
uuid,
33+
inspect,
34+
...loadModules,
35+
})

apps/web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"openid-client": "6.3.4",
5959
"oslo": "1.2.0",
6060
"pdfjs-dist": "4.9.155",
61-
"pg": "8.12.0",
61+
"pg": "catalog:",
6262
"posthog-js": "1.161.6",
6363
"promptl-ai": "catalog:",
6464
"rate-limiter-flexible": "5.0.3",

apps/web/src/app/(private)/_data-access/index.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ import { cache } from 'react'
22

33
import { getCurrentUser } from '$/services/auth/getCurrentUser'
44
import {
5+
DOCUMENT_STATS_CACHE_KEY,
6+
DocumentLogsLimitedView,
57
EvaluationMetric,
68
EvaluationType,
79
EvaluationV2,
10+
PROJECT_STATS_CACHE_KEY,
11+
ProjectLimitedView,
812
Workspace,
913
type Commit,
1014
} from '@latitude-data/core/browser'
15+
import { cache as redis } from '@latitude-data/core/cache'
1116
import { NotFoundError } from '@latitude-data/core/lib/errors'
1217
import { ApiKeysRepository } from '@latitude-data/core/repositories/apiKeysRepository'
1318
import {
@@ -213,6 +218,22 @@ export const getEvaluationByIdCached = cache(async (id: number) => {
213218
return evaluation
214219
})
215220

221+
export const getDocumentStatsCached = cache(async (documentUuid: string) => {
222+
const { workspace } = await getCurrentUser()
223+
const cache = await redis()
224+
const key = DOCUMENT_STATS_CACHE_KEY(workspace.id, documentUuid)
225+
const stats = await cache.get(key)
226+
return (stats ? JSON.parse(stats) : null) as DocumentLogsLimitedView | null
227+
})
228+
229+
export const getProjectStatsCached = cache(async (projectId: number) => {
230+
const { workspace } = await getCurrentUser()
231+
const cache = await redis()
232+
const key = PROJECT_STATS_CACHE_KEY(workspace.id, projectId)
233+
const stats = await cache.get(key)
234+
return (stats ? JSON.parse(stats) : null) as ProjectLimitedView | null
235+
})
236+
216237
export const getDocumentLogsApproximatedCountCached = cache(
217238
async (documentUuid: string) => {
218239
const { workspace } = await getCurrentUser()
@@ -223,6 +244,32 @@ export const getDocumentLogsApproximatedCountCached = cache(
223244
},
224245
)
225246

247+
export const getDocumentLogsApproximatedCountByProjectCached = cache(
248+
async (projectId: number) => {
249+
const { workspace } = await getCurrentUser()
250+
const repository = new DocumentLogsRepository(workspace.id)
251+
return await repository
252+
.approximatedCountByProject({ projectId })
253+
.then((r) => r.unwrap())
254+
},
255+
)
256+
257+
export const hasDocumentLogsCached = cache(async (documentUuid: string) => {
258+
const { workspace } = await getCurrentUser()
259+
const repository = new DocumentLogsRepository(workspace.id)
260+
return await repository.hasLogs({ documentUuid }).then((r) => r.unwrap())
261+
})
262+
263+
export const hasDocumentLogsByProjectCached = cache(
264+
async (projectId: number) => {
265+
const { workspace } = await getCurrentUser()
266+
const repository = new DocumentLogsRepository(workspace.id)
267+
return await repository
268+
.hasLogsByProject({ projectId })
269+
.then((r) => r.unwrap())
270+
},
271+
)
272+
226273
export const getDocumentLogCached = cache(async (uuid: string) => {
227274
const { workspace } = await getCurrentUser()
228275
const repository = new DocumentLogsRepository(workspace.id)

apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/_components/DocumentEditor/Editor/Playground/DocumentParams/HistoryLogParams/useLogHistoryParams.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { useCallback, useState } from 'react'
22

3-
import { DocumentVersion } from '@latitude-data/core/browser'
4-
import { useCurrentProject } from '@latitude-data/web-ui/providers'
3+
import { useDefaultLogFilterOptions } from '$/hooks/logFilters/useDefaultLogFilterOptions'
54
import { useDocumentParameters } from '$/hooks/useDocumentParameters'
65
import useDocumentLogs from '$/stores/documentLogs'
76
import useDocumentLogWithPaginationPosition, {
87
LogWithPosition,
98
} from '$/stores/documentLogWithPaginationPosition'
109
import useDocumentLogsPagination from '$/stores/useDocumentLogsPagination'
11-
import { useDefaultLogFilterOptions } from '$/hooks/logFilters/useDefaultLogFilterOptions'
10+
import { DocumentVersion, LogSources } from '@latitude-data/core/browser'
11+
import { useCurrentProject } from '@latitude-data/web-ui/providers'
1212

1313
const ONLY_ONE_PAGE = '1'
1414

@@ -21,13 +21,17 @@ export function useLogHistoryParams({
2121
}) {
2222
const { project } = useCurrentProject()
2323
const {
24-
history: { setHistoryLog, logUuid, mapDocParametersToInputs },
24+
history: { setHistoryLog, logUuid, force, mapDocParametersToInputs },
2525
} = useDocumentParameters({
2626
document,
2727
commitVersionUuid,
2828
})
2929

30-
const filterOptions = useDefaultLogFilterOptions()
30+
const filterOptions = {
31+
...useDefaultLogFilterOptions(),
32+
// Note: Speeding up history logs as a best-effort basis
33+
...(!force && { logSources: [LogSources.Playground] }),
34+
}
3135
const { data: pagination, isLoading: isLoadingCounter } =
3236
useDocumentLogsPagination({
3337
projectId: project.id,
@@ -64,11 +68,11 @@ export function useLogHistoryParams({
6468
pageSize: ONLY_ONE_PAGE,
6569
excludeErrors: true,
6670
onFetched: (logs) => {
67-
const log = logs[0]
71+
const log = logs?.[0]
6872
if (!log) return
6973

7074
mapDocParametersToInputs({ parameters: log.parameters })
71-
setHistoryLog(log.uuid)
75+
setHistoryLog(log)
7276
},
7377
})
7478

apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/_components/DocumentEditor/Editor/Playground/index.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { memo, useCallback, useEffect, useState } from 'react'
22

33
import { useDocumentParameters } from '$/hooks/useDocumentParameters'
44
import useDocumentLogWithMetadata from '$/stores/documentLogWithMetadata'
5-
import { DocumentVersion } from '@latitude-data/core/browser'
5+
import { DocumentVersion, LogSources } from '@latitude-data/core/browser'
66
import { SplitPane } from '@latitude-data/web-ui/atoms/SplitPane'
77
import {
88
AppLocalStorage,
@@ -50,7 +50,12 @@ export const Playground = memo(
5050
useEffect(() => {
5151
setForcedSize(collapsed ? DOCUMENT_PLAYGROUND_COLLAPSED_SIZE : undefined)
5252
}, [collapsed])
53-
const { parameters, source, setSource } = useDocumentParameters({
53+
const {
54+
parameters,
55+
source,
56+
setSource,
57+
history: { setHistoryLog },
58+
} = useDocumentParameters({
5459
commitVersionUuid: commit.uuid,
5560
document,
5661
})
@@ -72,8 +77,9 @@ export const Playground = memo(
7277
if (!documentLogUuid || error) return
7378
setRunCount((prev) => prev + 1)
7479
setDocumentLogUuid(documentLogUuid)
80+
setHistoryLog({ uuid: documentLogUuid, source: LogSources.Playground })
7581
},
76-
[setRunCount, setDocumentLogUuid],
82+
[setRunCount, setDocumentLogUuid, setHistoryLog],
7783
)
7884
const clearChat = useCallback(() => setMode('preview'), [setMode])
7985
const runPrompt = useCallback(() => setMode('chat'), [setMode])

apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/_components/DocumentLogs/DocumentLogInfo/Metadata.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ function UseDocumentLogInPlaygroundButton({
374374
const navigate = useNavigate()
375375
const employLogAsDocumentParameters = useCallback(() => {
376376
setSource('history')
377-
setHistoryLog(documentLog.uuid)
377+
setHistoryLog(documentLog)
378378
navigate.push(
379379
ROUTES.projects
380380
.detail({ id: project.id })
@@ -390,7 +390,7 @@ function UseDocumentLogInPlaygroundButton({
390390
project.id,
391391
commit.uuid,
392392
documentUuid,
393-
documentLog.uuid,
393+
documentLog,
394394
])
395395
const hasError = 'error' in documentLog && !!documentLog.error.message
396396

apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/_components/DocumentLogs/DocumentLogsTable.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { KeysetTablePaginationFooter } from '$/components/TablePaginationFooter/
88
import { SelectableRowsHook } from '$/hooks/useSelectableRows'
99
import { relativeTime } from '$/lib/relativeTime'
1010
import {
11-
DocumentLogLimitedView,
11+
DocumentLogsLimitedView,
1212
DocumentLogWithMetadataAndError,
1313
EvaluationV2,
1414
LOG_FILTERS_ENCODED_PARAMS,
@@ -115,7 +115,7 @@ type Props = {
115115
setSelectedLog: (log: DocumentLogWithMetadataAndError | undefined) => void
116116
isLoading: boolean
117117
selectableState: SelectableRowsHook
118-
limitedView?: DocumentLogLimitedView
118+
limitedView?: DocumentLogsLimitedView
119119
limitedCursor?: string | null
120120
setLimitedCursor?: (cursor: string | null) => void
121121
}

apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/_components/DocumentLogs/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import useProviderLogs from '$/stores/providerLogs'
88
import useDocumentLogsPagination from '$/stores/useDocumentLogsPagination'
99
import {
1010
DocumentLogFilterOptions,
11-
DocumentLogLimitedView,
11+
DocumentLogsLimitedView,
1212
DocumentLogsAggregations,
1313
DocumentLogWithMetadataAndError,
1414
EvaluationV2,
@@ -63,7 +63,7 @@ export function DocumentLogs({
6363
evaluations: EvaluationV2[]
6464
annotateEvaluation: ReturnType<typeof useEvaluationsV2>['annotateEvaluation']
6565
isAnnotatingEvaluation: boolean
66-
limitedView?: DocumentLogLimitedView
66+
limitedView?: DocumentLogsLimitedView
6767
limitedCursor?: string | null
6868
setLimitedCursor?: (cursor: string | null) => void
6969
}) {

apps/web/src/app/(private)/projects/[projectId]/versions/[commitUuid]/documents/[documentUuid]/logs/_components/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import useEvaluationResultsV2ByDocumentLogs from '$/stores/evaluationResultsV2/b
1515
import { useEvaluationsV2 } from '$/stores/evaluationsV2'
1616
import {
1717
DocumentLogFilterOptions,
18-
DocumentLogLimitedView,
18+
DocumentLogsLimitedView,
1919
DocumentLogWithMetadataAndError,
2020
} from '@latitude-data/core/browser'
2121
import { Button } from '@latitude-data/web-ui/atoms/Button'
@@ -93,7 +93,7 @@ export function DocumentLogsPage({
9393
selectedLog?: DocumentLogWithMetadataAndError
9494
originalSelectedCommitsIds: number[]
9595
documentLogFilterOptions: DocumentLogFilterOptions
96-
limitedView?: DocumentLogLimitedView
96+
limitedView?: DocumentLogsLimitedView
9797
}) {
9898
const { project } = useCurrentProject()
9999
const { commit } = useCurrentCommit()
@@ -143,8 +143,8 @@ export function DocumentLogsPage({
143143
})
144144

145145
const documentLogs = useMemo(() => {
146-
if (limitedView) return documentLogsLimited.items
147-
return documentLogsNormal
146+
if (limitedView) return documentLogsLimited.items ?? []
147+
return documentLogsNormal ?? []
148148
}, [limitedView, documentLogsLimited, documentLogsNormal])
149149

150150
const mutate = useMemo(() => {
@@ -200,7 +200,7 @@ export function DocumentLogsPage({
200200
const isEvaluationsLoading =
201201
isEvaluationResultsV2Loading || isEvaluationsV2Loading
202202

203-
const [realtimeEnabled, setRealtimeEnabled] = useState(true)
203+
const [realtimeEnabled, setRealtimeEnabled] = useState(!limitedView)
204204
useDocumentLogSocket(document.documentUuid, mutate, realtimeEnabled)
205205

206206
return (

0 commit comments

Comments
 (0)