Skip to content

Commit

Permalink
feat: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutgon committed Sep 19, 2024
1 parent bc8b3af commit 8160dff
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 134 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
import { ReactNode, useMemo } from 'react'
import { ReactNode } from 'react'

import {
Commit,
Evaluation,
EvaluationResultableType,
} from '@latitude-data/core/browser'
import { EvaluationResultWithMetadata } from '@latitude-data/core/repositories'
import {
Icon,
RangeBadge,
Skeleton,
Text,
Tooltip,
} from '@latitude-data/web-ui'
import { EvaluationAggregationTotals } from '@latitude-data/core/browser'
import { Icon, Text, Tooltip } from '@latitude-data/web-ui'
import { formatCostInMillicents } from '$/app/_lib/formatUtils'

function Panel({
Expand Down Expand Up @@ -50,127 +39,23 @@ function Panel({
return panel
}

function TypeSpecificPanel({
evaluation,
evaluationResults,
commit,
}: {
evaluation: Evaluation
evaluationResults: EvaluationResultWithMetadata[]
commit?: Commit
}) {
const label =
evaluation.configuration.type == EvaluationResultableType.Number
? 'Current average'
: 'Current modal'

const additionalInfo =
evaluation.configuration.type == EvaluationResultableType.Number
? 'The mean value of all the evaluated results from the current version.'
: 'The most common result from the current version.'

if (!commit) {
return (
<Panel label={label} additionalInfo={additionalInfo}>
<Skeleton className='w-16 h-4 bg-muted animate-pulse' />
</Panel>
)
}

const value = useMemo<ReactNode>(() => {
const resultsFromCommit = evaluationResults.filter(
(e) => e.commit.id === commit.id,
)

if (resultsFromCommit.length == 0) {
return <Text.H4 color='foregroundMuted'>No data</Text.H4>
}

if (evaluation.configuration.type == EvaluationResultableType.Number) {
const { to: maxValue, from: minValue } =
evaluation.configuration.detail!.range
const sumValue = resultsFromCommit.reduce((acc, result) => {
return acc + Number(result.result)
}, 0)

const meanValue = sumValue / resultsFromCommit.length

return (
<div className='w-fit'>
<RangeBadge
minValue={minValue}
maxValue={maxValue}
value={meanValue}
/>
</div>
)
}

const resultCounts = resultsFromCommit.reduce(
(acc, result) => {
const value = result.result as string
if (!acc[value]) {
acc[value] = 0
}
acc[value] += 1
return acc
},
{} as Record<string, number>,
)

const mostCommonResult = Object.keys(resultCounts).reduce((a, b) =>
resultCounts[a]! > resultCounts[b]! ? a : b,
)

const resultPresence =
(resultCounts[mostCommonResult]! / resultsFromCommit.length) * 100

return (
<>
<Text.H3B>{mostCommonResult}</Text.H3B>
<Text.H3 color='foregroundMuted'> ({resultPresence} %)</Text.H3>
</>
)
}, [evaluation, evaluationResults, commit])

return (
<Panel label={label} additionalInfo={additionalInfo}>
{value}
</Panel>
)
}

export function BigNumberPanels({
evaluation,
evaluationResults,
commit,
aggregationTotals,
documentUuid: _d,
evaluationId: _e,
}: {
evaluation: Evaluation
evaluationResults: EvaluationResultWithMetadata[]
commit?: Commit
aggregationTotals: EvaluationAggregationTotals
documentUuid: string
evaluationId: number
}) {
const totalCost = useMemo(() => {
return evaluationResults.reduce((acc, result) => {
return acc + (result.costInMillicents ?? 0)
}, 0)
}, [evaluationResults])

const totalTokens = useMemo(() => {
return evaluationResults.reduce((acc, result) => {
return acc + (result.tokens ?? 0)
}, 0)
}, [evaluationResults])

return (
<div className='flex flex-wrap gap-6'>
<Panel label='Total logs' value={String(evaluationResults.length)} />
<Panel label='Total cost' value={formatCostInMillicents(totalCost)} />
<Panel label='Total tokens' value={String(totalTokens)} />
<TypeSpecificPanel
evaluation={evaluation}
evaluationResults={evaluationResults}
commit={commit}
<Panel label='Total logs' value={String(aggregationTotals.totalCount)} />
<Panel
label='Total cost'
value={formatCostInMillicents(aggregationTotals.costInMillicents)}
/>
<Panel label='Total tokens' value={String(aggregationTotals.tokens)} />
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import { ReactNode, useMemo } from 'react'

import {
Commit,
Evaluation,
EvaluationResultableType,
} from '@latitude-data/core/browser'
import { EvaluationResultWithMetadata } from '@latitude-data/core/repositories'
import {
Icon,
RangeBadge,
Skeleton,
Text,
Tooltip,
} from '@latitude-data/web-ui'
import { formatCostInMillicents } from '$/app/_lib/formatUtils'

function Panel({
label,
value,
additionalInfo,
children,
}: {
label: string
value?: string
additionalInfo?: string
children?: ReactNode
}) {
const panel = (
<div className='min-w-44 flex-1 flex flex-col gap-1 p-4 rounded-lg border border-border'>
<div className='flex flex-row justify-between items-center gap-1'>
<Text.H5 color='foregroundMuted'>{label}</Text.H5>
{additionalInfo && (
<Icon name='info' className='text-muted-foreground' />
)}
</div>
{value && <Text.H3B>{value}</Text.H3B>}
{children}
</div>
)

if (additionalInfo) {
return (
<Tooltip side='left' trigger={panel}>
<Text.H5 color='white'>{additionalInfo}</Text.H5>
</Tooltip>
)
}

return panel
}

function TypeSpecificPanel({
evaluation,
evaluationResults,
commit,
}: {
evaluation: Evaluation
evaluationResults: EvaluationResultWithMetadata[]
commit?: Commit
}) {
const label =
evaluation.configuration.type == EvaluationResultableType.Number
? 'Current average'
: 'Current modal'

const additionalInfo =
evaluation.configuration.type == EvaluationResultableType.Number
? 'The mean value of all the evaluated results from the current version.'
: 'The most common result from the current version.'

if (!commit) {
return (
<Panel label={label} additionalInfo={additionalInfo}>
<Skeleton className='w-16 h-4 bg-muted animate-pulse' />
</Panel>
)
}

const value = useMemo<ReactNode>(() => {
const resultsFromCommit = evaluationResults.filter(
(e) => e.commit.id === commit.id,
)

if (resultsFromCommit.length == 0) {
return <Text.H4 color='foregroundMuted'>No data</Text.H4>
}

if (evaluation.configuration.type == EvaluationResultableType.Number) {
const { to: maxValue, from: minValue } =
evaluation.configuration.detail!.range
const sumValue = resultsFromCommit.reduce((acc, result) => {
return acc + Number(result.result)
}, 0)

const meanValue = sumValue / resultsFromCommit.length

return (
<div className='w-fit'>
<RangeBadge
minValue={minValue}
maxValue={maxValue}
value={meanValue}
/>
</div>
)
}

const resultCounts = resultsFromCommit.reduce(
(acc, result) => {
const value = result.result as string
if (!acc[value]) {
acc[value] = 0
}
acc[value] += 1
return acc
},
{} as Record<string, number>,
)

const mostCommonResult = Object.keys(resultCounts).reduce((a, b) =>
resultCounts[a]! > resultCounts[b]! ? a : b,
)

const resultPresence =
(resultCounts[mostCommonResult]! / resultsFromCommit.length) * 100

return (
<>
<Text.H3B>{mostCommonResult}</Text.H3B>
<Text.H3 color='foregroundMuted'> ({resultPresence} %)</Text.H3>
</>
)
}, [evaluation, evaluationResults, commit])

return (
<Panel label={label} additionalInfo={additionalInfo}>
{value}
</Panel>
)
}

export function BigNumberPanelsOld({
evaluation,
evaluationResults,
commit,
}: {
evaluation: Evaluation
evaluationResults: EvaluationResultWithMetadata[]
commit?: Commit
}) {
const totalCost = useMemo(() => {
return evaluationResults.reduce((acc, result) => {
return acc + (result.costInMillicents ?? 0)
}, 0)
}, [evaluationResults])

const totalTokens = useMemo(() => {
return evaluationResults.reduce((acc, result) => {
return acc + (result.tokens ?? 0)
}, 0)
}, [evaluationResults])

return (
<div className='flex flex-wrap gap-6'>
<Panel label='Total logs' value={String(evaluationResults.length)} />
<Panel label='Total cost' value={formatCostInMillicents(totalCost)} />
<Panel label='Total tokens' value={String(totalTokens)} />
<TypeSpecificPanel
evaluation={evaluation}
evaluationResults={evaluationResults}
commit={commit}
/>
</div>
)
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
'use client'

import { Commit, Evaluation } from '@latitude-data/core/browser'
import { Commit, Evaluation, EvaluationAggregationTotals } from '@latitude-data/core/browser'
import { EvaluationResultWithMetadata } from '@latitude-data/core/repositories'
import { useCurrentCommit } from '@latitude-data/web-ui'

import { BigNumberPanelsOld } from './BigNumberPanelsOld'
import { BigNumberPanels } from './BigNumberPanels'
import { EvaluationResultsCharts } from './Charts'

export function MetricsSummary({
evaluation,
documentUuid,
evaluationResults,
aggregationTotals,
}: {
evaluation: Evaluation
documentUuid: string
evaluationResults: EvaluationResultWithMetadata[]
aggregationTotals: EvaluationAggregationTotals
}) {
const { commit } = useCurrentCommit()

Expand All @@ -25,11 +28,16 @@ export function MetricsSummary({
documentUuid={documentUuid}
/>
<div className='min-w-[400px] flex-1'>
<BigNumberPanels
<BigNumberPanelsOld
evaluation={evaluation}
evaluationResults={evaluationResults}
commit={commit as Commit}
/>
<BigNumberPanels
documentUuid={documentUuid}
evaluationId={evaluation.id}
aggregationTotals={aggregationTotals}
/>
</div>
</div>
)
Expand Down
Loading

0 comments on commit 8160dff

Please sign in to comment.