Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions console/src/lib/format.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, expect, it } from 'vitest';
import { pluralize } from './format';

describe('pluralize', () => {
it('returns singular when n is 1', () => {
expect(pluralize(1, 'call')).toBe('1 call');
});

it('returns plural when n is 0', () => {
expect(pluralize(0, 'call')).toBe('0 calls');
});

it('returns plural when n is greater than 1', () => {
expect(pluralize(2, 'call')).toBe('2 calls');
expect(pluralize(100, 'call')).toBe('100 calls');
});

it('uses a custom plural form for irregular plurals', () => {
expect(pluralize(1, 'person', 'people')).toBe('1 person');
expect(pluralize(3, 'person', 'people')).toBe('3 people');
});

it('handles negative numbers as plural', () => {
expect(pluralize(-1, 'call')).toBe('-1 calls');
});
});
8 changes: 8 additions & 0 deletions console/src/lib/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ export function formatUptime(totalSeconds: number): string {
return parts.join(' ');
}

export function pluralize(
n: number,
word: string,
plural = `${word}s`,
): string {
return `${n} ${n === 1 ? word : plural}`;
}

export function parseStringList(value: string): string[] {
return value
.split(/[\n,]/g)
Expand Down
4 changes: 1 addition & 3 deletions console/src/routes/channels-catalog.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { AdminConfig } from '../api/types';
import { pluralize } from '../lib/format';

export type ChannelKind =
| 'discord'
Expand Down Expand Up @@ -51,9 +52,6 @@ export function countTeamsOverrides(config: AdminConfig): number {
}, 0);
}

function pluralize(count: number, singular: string, plural = `${singular}s`) {
return `${count} ${count === 1 ? singular : plural}`;
}

function describeDiscord(
config: AdminConfig,
Expand Down
7 changes: 4 additions & 3 deletions console/src/routes/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
formatTokenBreakdown,
formatUptime,
formatUsd,
pluralize,
} from '../lib/format';
import { compareDateTime, compareNumber, compareText } from '../lib/sort';

Expand Down Expand Up @@ -170,7 +171,7 @@ export function DashboardPage() {
</small>
<small>
{formatUsd(overview.usage.daily.totalCostUsd)} across{' '}
{overview.usage.daily.callCount} calls
{pluralize(overview.usage.daily.callCount, 'call')}
</small>
</div>
<div className="usage-stack">
Expand All @@ -186,7 +187,7 @@ export function DashboardPage() {
</small>
<small>
{formatUsd(overview.usage.monthly.totalCostUsd)} across{' '}
{overview.usage.monthly.callCount} calls
{pluralize(overview.usage.monthly.callCount, 'call')}
</small>
</div>
</div>
Expand All @@ -205,7 +206,7 @@ export function DashboardPage() {
inputTokens: row.totalInputTokens ?? 0,
outputTokens: row.totalOutputTokens ?? 0,
})}{' '}
· {row.callCount} calls this month
· {pluralize(row.callCount, 'call')} this month
</small>
</div>
<span>{formatUsd(row.totalCostUsd)}</span>
Expand Down
7 changes: 4 additions & 3 deletions console/src/routes/models.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
formatRelativeTime,
formatTokenBreakdown,
formatUsd,
pluralize,
} from '../lib/format';
import { compareNumber, compareText } from '../lib/sort';

Expand Down Expand Up @@ -308,7 +309,7 @@ export function ModelsPage() {

<Panel
title="Catalog"
subtitle={`${models.length} model${models.length === 1 ? '' : 's'} visible`}
subtitle={`${pluralize(models.length, 'model')} visible`}
>
{modelsQuery.isLoading ? (
<div className="empty-state">Loading model catalog...</div>
Expand Down Expand Up @@ -376,7 +377,7 @@ export function ModelsPage() {
</small>
<small>
{formatUsd(model.usageMonthly.totalCostUsd)} ·{' '}
{model.usageMonthly.callCount} calls
{pluralize(model.usageMonthly.callCount, 'call')}
</small>
</>
) : (
Expand Down Expand Up @@ -421,7 +422,7 @@ export function ModelsPage() {
inputTokens: model.usageDaily.totalInputTokens ?? 0,
outputTokens: model.usageDaily.totalOutputTokens ?? 0,
})}{' '}
· {model.usageDaily.callCount} calls today
· {pluralize(model.usageDaily.callCount, 'call')} today
</small>
</div>
<span>{formatUsd(model.usageDaily.totalCostUsd ?? 0)}</span>
Expand Down
Loading