Compare commits

...

1 Commits

Author SHA1 Message Date
Gaurav Tewari
1dd887f7fd feat: add config page initial draft 2026-06-11 20:30:32 +05:30
18 changed files with 2072 additions and 0 deletions

View File

@@ -323,3 +323,10 @@ export const AIAssistantPage = Loadable(
/* webpackChunkName: "AI Assistant Page" */ 'pages/AIAssistantPage/AIAssistantPage'
),
);
export const LLMObservabilityModelPricingPage = Loadable(
() =>
import(
/* webpackChunkName: "LLM Observability Model Pricing Page" */ 'pages/LLMObservabilityModelPricing'
),
);

View File

@@ -22,6 +22,7 @@ import {
IntegrationsDetailsPage,
LicensePage,
ListAllALertsPage,
LLMObservabilityModelPricingPage,
LiveLogs,
Login,
Logs,
@@ -507,6 +508,13 @@ const routes: AppRoutes[] = [
key: 'AI_ASSISTANT',
isPrivate: true,
},
{
path: ROUTES.LLM_OBSERVABILITY_MODEL_PRICING,
exact: true,
component: LLMObservabilityModelPricingPage,
key: 'LLM_OBSERVABILITY_MODEL_PRICING',
isPrivate: true,
},
];
export const SUPPORT_ROUTE: AppRoutes = {

View File

@@ -91,6 +91,7 @@ const ROUTES = {
AI_ASSISTANT_BASE: '/ai-assistant',
AI_ASSISTANT_ICON_PREVIEW: '/ai-assistant-icon-preview',
MCP_SERVER: '/settings/mcp-server',
LLM_OBSERVABILITY_MODEL_PRICING: '/llm-observability/model-pricing',
} as const;
export default ROUTES;

View File

@@ -0,0 +1,140 @@
.llm-observability-model-pricing {
display: flex;
flex-direction: column;
gap: 16px;
padding: 24px 32px;
.page-header {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 16px;
&__title {
h1 {
margin: 0;
font-size: 22px;
font-weight: 600;
}
p {
margin: 4px 0 0;
color: var(--bg-vanilla-400);
font-size: 13px;
}
}
&__actions {
display: flex;
gap: 8px;
}
}
.page-tabs {
.ant-tabs-nav {
margin-bottom: 0;
}
}
.filters-bar {
display: flex;
gap: 12px;
align-items: center;
&__search {
flex: 1;
max-width: 360px;
}
&__source,
&__currency {
min-width: 160px;
}
&__add {
margin-left: auto;
}
}
.page-error {
padding: 12px 16px;
border-radius: 4px;
background: rgba(255, 90, 90, 0.08);
color: var(--bg-cherry-400);
font-size: 13px;
}
.page-footer {
color: var(--bg-vanilla-400);
font-size: 12px;
}
}
.model-costs-table {
.model-cell {
display: flex;
flex-direction: column;
gap: 2px;
&__name {
font-weight: 600;
}
&__canonical-id {
color: var(--bg-vanilla-400);
font-family: var(--code-font-family, monospace);
font-size: 12px;
}
}
.price-cell {
font-family: var(--code-font-family, monospace);
}
.extra-buckets {
display: flex;
flex-wrap: wrap;
gap: 6px;
&__chip {
display: inline-flex;
align-items: center;
gap: 6px;
margin: 0;
}
&__key {
font-family: var(--code-font-family, monospace);
font-size: 12px;
}
&__price {
font-family: var(--code-font-family, monospace);
font-weight: 600;
}
}
.muted {
color: var(--bg-vanilla-400);
}
.source-badge {
margin: 0;
&--auto {
background: rgba(78, 116, 248, 0.12);
color: var(--bg-robin-400);
border-color: rgba(78, 116, 248, 0.24);
}
&--override {
background: rgba(245, 175, 25, 0.12);
color: var(--bg-amber-400);
border-color: rgba(245, 175, 25, 0.24);
}
}
&__row--selected {
background: rgba(78, 116, 248, 0.06);
}
}

View File

@@ -0,0 +1,140 @@
import { useMemo, useState } from 'react';
import { Button, Input, Select, Tabs } from 'antd';
import { Plus, Search } from '@signozhq/icons';
import { useListLLMPricingRules } from 'api/generated/services/llmpricingrules';
import ModelCostDrawer from './ModelCostDrawer';
import ModelCostsTable from './ModelCostsTable';
import { useModelCostDrawer } from './useModelCostDrawer';
import { filterRules, type PricingRule, type SourceFilter } from './utils';
import './LLMObservabilityModelPricing.styles.scss';
const SOURCE_OPTIONS: { value: SourceFilter; label: string }[] = [
{ value: 'all', label: 'Source: All' },
{ value: 'auto', label: 'Auto-populated' },
{ value: 'override', label: 'User override' },
];
const CURRENCY_OPTIONS = [
{ value: 'USD', label: 'USD' },
{ value: 'EUR', label: 'EUR', disabled: true },
{ value: 'INR', label: 'INR', disabled: true },
];
const PAGE_SIZE = 100;
function LLMObservabilityModelPricing(): JSX.Element {
const [search, setSearch] = useState<string>('');
const [source, setSource] = useState<SourceFilter>('all');
const [currency, setCurrency] = useState<string>('USD');
const { data, isLoading, isError } = useListLLMPricingRules({
offset: 0,
limit: PAGE_SIZE,
});
const rules: PricingRule[] = useMemo(() => data?.data?.items || [], [data]);
const filteredRules = useMemo(
() => filterRules(rules, search, source),
[rules, search, source],
);
const drawer = useModelCostDrawer();
return (
<div
className="llm-observability-model-pricing"
data-testid="llm-observability-model-pricing-page"
>
<header className="page-header">
<div className="page-header__title">
<h1>Configuration</h1>
<p>Model pricing and cost estimation settings</p>
</div>
</header>
<Tabs
className="page-tabs"
defaultActiveKey="model-costs"
items={[
{ key: 'model-costs', label: 'Model costs' },
{
key: 'unpriced-models',
label: 'Unpriced models',
disabled: true,
},
]}
/>
<div className="filters-bar">
<Input
className="filters-bar__search"
placeholder="Search by model or provider…"
prefix={<Search size={14} />}
value={search}
onChange={(event): void => setSearch(event.target.value)}
data-testid="search-input"
allowClear
/>
<Select<SourceFilter>
className="filters-bar__source"
value={source}
onChange={(value): void => setSource(value)}
options={SOURCE_OPTIONS}
data-testid="source-select"
/>
<Select<string>
className="filters-bar__currency"
value={currency}
onChange={(value): void => setCurrency(value)}
options={CURRENCY_OPTIONS}
data-testid="currency-select"
/>
<Button
type="primary"
className="filters-bar__add"
icon={<Plus size={14} />}
onClick={(): void => drawer.openForAdd()}
data-testid="add-model-cost-btn"
>
Add model cost
</Button>
</div>
{isError && (
<div className="page-error" role="alert">
Failed to load pricing rules. Please try again.
</div>
)}
<ModelCostsTable
rules={filteredRules}
isLoading={isLoading}
selectedRuleId={drawer.selectedRuleId}
onEdit={drawer.openForEdit}
/>
<footer className="page-footer">
Showing {filteredRules.length} model{filteredRules.length === 1 ? '' : 's'}
{' · '}All prices per 1M tokens (USD)
</footer>
<ModelCostDrawer
isOpen={drawer.isOpen}
mode={drawer.mode}
draft={drawer.draft}
setDraft={drawer.setDraft}
onClose={drawer.close}
onSave={drawer.save}
onDelete={drawer.deleteRule}
isSaving={drawer.isSaving}
isDeleting={drawer.isDeleting}
saveError={drawer.saveError}
/>
</div>
);
}
export default LLMObservabilityModelPricing;

View File

@@ -0,0 +1,256 @@
.model-cost-drawer {
.ant-drawer-body {
padding: 20px 24px;
display: flex;
flex-direction: column;
gap: 18px;
}
&__title {
h3 {
margin: 0;
font-size: 16px;
font-weight: 600;
}
p {
margin: 4px 0 0;
color: var(--bg-vanilla-400);
font-size: 12px;
}
}
&__footer {
display: flex;
align-items: center;
justify-content: space-between;
padding: 12px 16px;
border-top: 1px solid var(--bg-slate-300);
&-right {
display: flex;
gap: 8px;
margin-left: auto;
}
}
.drawer-section {
display: flex;
flex-direction: column;
gap: 6px;
label,
.field-label {
font-size: 12px;
font-weight: 600;
color: var(--bg-vanilla-200);
}
.help {
margin: 0;
font-size: 11px;
}
}
.muted {
color: var(--bg-vanilla-400);
}
.required {
color: var(--bg-cherry-400);
}
.full-width {
width: 100%;
}
.drawer-surface {
padding: 14px;
border-radius: 6px;
background: rgba(255, 255, 255, 0.02);
border: 1px solid var(--bg-slate-300);
&__head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
h4 {
margin: 0;
font-size: 13px;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.04em;
color: var(--bg-vanilla-300);
}
}
}
.managed-label {
display: inline-flex;
align-items: center;
gap: 4px;
font-size: 11px;
color: var(--bg-vanilla-400);
}
.pattern-chips {
display: flex;
flex-wrap: wrap;
gap: 6px;
min-height: 28px;
}
.pattern-chip {
display: inline-flex;
align-items: center;
gap: 4px;
&__remove {
background: transparent;
border: none;
padding: 0;
margin-left: 2px;
cursor: pointer;
color: inherit;
display: inline-flex;
align-items: center;
&:hover {
color: var(--bg-cherry-400);
}
}
}
.pattern-add {
display: flex;
gap: 6px;
}
.pattern-test {
display: flex;
gap: 10px;
align-items: center;
flex-wrap: wrap;
margin-top: 6px;
&__result {
font-size: 12px;
&--match {
color: var(--bg-forest-400);
}
&--no-match {
color: var(--bg-cherry-400);
}
}
}
.source-radio-group {
display: flex;
flex-direction: column;
gap: 8px;
width: 100%;
}
.source-radio {
display: flex;
align-items: flex-start;
gap: 8px;
padding: 10px 12px;
border-radius: 4px;
border: 1px solid transparent;
background: rgba(255, 255, 255, 0.02);
margin: 0;
width: 100%;
&__title {
font-weight: 600;
font-size: 13px;
}
&__desc {
font-size: 12px;
color: var(--bg-vanilla-400);
}
&.ant-radio-wrapper-checked.source-radio--auto {
background: rgba(78, 116, 248, 0.1);
border-color: rgba(78, 116, 248, 0.3);
}
&.ant-radio-wrapper-checked.source-radio--override {
background: rgba(245, 175, 25, 0.1);
border-color: rgba(245, 175, 25, 0.3);
}
}
.reset-confirm {
margin-top: 12px;
padding: 12px;
border-radius: 4px;
background: rgba(78, 116, 248, 0.06);
border: 1px solid rgba(78, 116, 248, 0.2);
p {
margin: 0 0 10px;
font-size: 12px;
}
&__actions {
display: flex;
gap: 8px;
justify-content: flex-end;
}
}
.pricing-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 12px;
}
.pricing-field {
display: flex;
flex-direction: column;
gap: 4px;
.ant-input-number {
width: 100%;
}
}
.cache-mode-field {
margin-top: 10px;
}
.extras-divider {
margin-top: 14px;
margin-bottom: 6px;
font-size: 11px;
text-transform: uppercase;
letter-spacing: 0.04em;
color: var(--bg-vanilla-400);
}
.cost-preview {
&__line {
font-family: var(--code-font-family, monospace);
font-size: 12px;
strong {
margin-left: 4px;
}
}
}
.drawer-error {
padding: 10px 12px;
border-radius: 4px;
background: rgba(255, 90, 90, 0.08);
color: var(--bg-cherry-400);
font-size: 12px;
}
}

View File

@@ -0,0 +1,413 @@
import { useMemo, useState } from 'react';
import { Button, Drawer, Input, InputNumber, Select, Tooltip } from 'antd';
import { Badge } from '@signozhq/ui/badge';
import { RadioGroup, RadioGroupItem } from '@signozhq/ui/radio-group';
import { Lock, Trash2, X } from '@signozhq/icons';
import { LlmpricingruletypesLLMPricingRuleCacheModeDTO as CacheModeDTO } from 'api/generated/services/sigNoz.schemas';
import {
CACHE_MODE_OPTIONS,
computeCostPreview,
matchesAnyPattern,
PROVIDER_OPTIONS,
validateDraft,
type DrawerDraft,
type DrawerMode,
} from './drawerUtils';
import './ModelCostDrawer.styles.scss';
interface ModelCostDrawerProps {
isOpen: boolean;
mode: DrawerMode;
draft: DrawerDraft;
setDraft: (next: DrawerDraft) => void;
onClose: () => void;
onSave: () => void;
onDelete: () => void;
isSaving: boolean;
isDeleting: boolean;
saveError: string | null;
}
function ModelCostDrawer({
isOpen,
mode,
draft,
setDraft,
onClose,
onSave,
onDelete,
isSaving,
isDeleting,
saveError,
}: ModelCostDrawerProps): JSX.Element {
const [patternInput, setPatternInput] = useState<string>('');
const [testInput, setTestInput] = useState<string>('');
const [showResetConfirm, setShowResetConfirm] = useState<boolean>(false);
const isReadOnly = !draft.isOverride;
const validation = validateDraft(draft, mode);
const preview = useMemo(() => computeCostPreview(draft), [draft]);
const testMatch = useMemo(
() => (testInput ? matchesAnyPattern(testInput, draft.patterns) : null),
[testInput, draft.patterns],
);
const update = (patch: Partial<DrawerDraft>): void => {
setDraft({ ...draft, ...patch });
};
const updatePricing = (patch: Partial<DrawerDraft['pricing']>): void => {
setDraft({ ...draft, pricing: { ...draft.pricing, ...patch } });
};
const addPattern = (): void => {
const next = patternInput.trim();
if (!next || draft.patterns.includes(next)) {
setPatternInput('');
return;
}
update({ patterns: [...draft.patterns, next] });
setPatternInput('');
};
const removePattern = (pattern: string): void => {
update({ patterns: draft.patterns.filter((p) => p !== pattern) });
};
const handleSourceChange = (value: 'auto' | 'override'): void => {
if (value === 'auto' && draft.isOverride) {
setShowResetConfirm(true);
return;
}
if (value === 'override' && !draft.isOverride) {
update({ isOverride: true });
}
};
const confirmReset = (): void => {
update({ isOverride: false });
setShowResetConfirm(false);
};
const hasCacheBucket =
draft.pricing.cacheRead !== null || draft.pricing.cacheWrite !== null;
return (
<Drawer
width={520}
open={isOpen}
onClose={onClose}
placement="right"
className="model-cost-drawer"
destroyOnClose
closeIcon={<X size={16} />}
title={
<div className="model-cost-drawer__title">
<h3>{mode === 'edit' ? 'Edit model cost' : 'Add model cost'}</h3>
<p>Pricing computes gen_ai.estimated_total_cost at ingest.</p>
</div>
}
footer={
<div className="model-cost-drawer__footer">
{mode === 'edit' && (
<Button
danger
type="text"
icon={<Trash2 size={14} />}
onClick={onDelete}
loading={isDeleting}
data-testid="drawer-delete-btn"
>
Delete
</Button>
)}
<div className="model-cost-drawer__footer-right">
<Button onClick={onClose} data-testid="drawer-cancel-btn">
Cancel
</Button>
<Tooltip title={validation.ok ? '' : validation.message}>
<Button
type="primary"
onClick={onSave}
loading={isSaving}
disabled={!validation.ok}
data-testid="drawer-save-btn"
>
Save
</Button>
</Tooltip>
</div>
</div>
}
>
<div className="drawer-section">
<label htmlFor="billing-model-id">Billing model ID</label>
<Input
id="billing-model-id"
placeholder="e.g. openai:gpt-4o"
value={draft.modelName}
disabled={mode === 'edit'}
onChange={(e): void => update({ modelName: e.target.value })}
data-testid="drawer-model-id-input"
/>
</div>
<div className="drawer-section">
<label htmlFor="provider-select">Provider</label>
<Select
id="provider-select"
value={draft.provider}
onChange={(value): void => update({ provider: value })}
options={PROVIDER_OPTIONS}
disabled={isReadOnly}
className="full-width"
data-testid="drawer-provider-select"
/>
</div>
<div className="drawer-section">
<span className="field-label">
Model name patterns <span className="muted">(prefix match)</span>
</span>
<div className="pattern-chips">
{draft.patterns.map((pattern) => (
<Badge
key={pattern}
color="forest"
variant="outline"
className="pattern-chip"
>
{pattern}*
{!isReadOnly && (
<button
type="button"
aria-label={`Remove pattern ${pattern}`}
className="pattern-chip__remove"
onClick={(): void => removePattern(pattern)}
>
<X size={10} />
</button>
)}
</Badge>
))}
</div>
{!isReadOnly && (
<div className="pattern-add">
<Input
placeholder="Add pattern…"
value={patternInput}
onChange={(e): void => setPatternInput(e.target.value)}
onPressEnter={addPattern}
data-testid="drawer-pattern-input"
/>
<Button onClick={addPattern} data-testid="drawer-pattern-add-btn">
+ Add
</Button>
</div>
)}
<p className="muted help">
Each pattern uses prefix matching against gen_ai.request.model.
</p>
{!isReadOnly && (
<div className="pattern-test">
<Input
placeholder="Test: type a model name…"
value={testInput}
onChange={(e): void => setTestInput(e.target.value)}
data-testid="drawer-pattern-test-input"
/>
{testInput && (
<span
className={`pattern-test__result ${
testMatch
? 'pattern-test__result--match'
: 'pattern-test__result--no-match'
}`}
data-testid="drawer-pattern-test-result"
>
{testMatch ? `Matched: ${testMatch}*` : 'No matching pattern'}
</span>
)}
</div>
)}
</div>
<div className="drawer-section drawer-surface">
<div className="drawer-surface__head">
<h4>Source</h4>
{isReadOnly && (
<span className="managed-label">
<Lock size={12} />
Managed by SigNoz
</span>
)}
</div>
<RadioGroup
value={draft.isOverride ? 'override' : 'auto'}
onChange={(value): void =>
handleSourceChange(value as 'auto' | 'override')
}
className="source-radio-group"
>
<RadioGroupItem
value="auto"
className="source-radio source-radio--auto"
testId="drawer-source-auto"
>
<div className="source-radio__title">Auto-populated</div>
<div className="source-radio__desc">
Default pricing from SigNoz. Updated automatically.
</div>
</RadioGroupItem>
<RadioGroupItem
value="override"
className="source-radio source-radio--override"
testId="drawer-source-override"
>
<div className="source-radio__title">User override</div>
<div className="source-radio__desc">
Custom pricing. Takes precedence.
</div>
</RadioGroupItem>
</RadioGroup>
{showResetConfirm && (
<div
className="reset-confirm"
role="dialog"
aria-label="Reset to default pricing"
>
<p>Reset to default pricing? Custom values will be discarded.</p>
<div className="reset-confirm__actions">
<Button
onClick={(): void => setShowResetConfirm(false)}
data-testid="drawer-reset-keep-btn"
>
Keep
</Button>
<Button
type="primary"
onClick={confirmReset}
data-testid="drawer-reset-confirm-btn"
>
Reset
</Button>
</div>
</div>
)}
</div>
<div className="drawer-section drawer-surface">
<div className="drawer-surface__head">
<h4>Pricing (per 1M tokens, USD)</h4>
{isReadOnly && (
<span className="managed-label">
<Lock size={12} />
Read-only
</span>
)}
</div>
<div className="pricing-grid">
<div className="pricing-field">
<label htmlFor="input-cost">
Input cost <span className="required">*</span>
</label>
<InputNumber
id="input-cost"
min={0}
step={0.01}
value={draft.pricing.input}
onChange={(v): void => updatePricing({ input: Number(v) || 0 })}
disabled={isReadOnly}
data-testid="drawer-input-cost"
/>
</div>
<div className="pricing-field">
<label htmlFor="output-cost">
Output cost <span className="required">*</span>
</label>
<InputNumber
id="output-cost"
min={0}
step={0.01}
value={draft.pricing.output}
onChange={(v): void => updatePricing({ output: Number(v) || 0 })}
disabled={isReadOnly}
data-testid="drawer-output-cost"
/>
</div>
</div>
<div className="extras-divider">Extra pricing buckets</div>
<div className="pricing-grid">
<div className="pricing-field">
<label htmlFor="cache-read">cache_read</label>
<InputNumber
id="cache-read"
min={0}
step={0.01}
value={draft.pricing.cacheRead ?? undefined}
placeholder="—"
onChange={(v): void =>
updatePricing({ cacheRead: v === null ? null : Number(v) })
}
disabled={isReadOnly}
data-testid="drawer-cache-read-cost"
/>
</div>
<div className="pricing-field">
<label htmlFor="cache-write">cache_write</label>
<InputNumber
id="cache-write"
min={0}
step={0.01}
value={draft.pricing.cacheWrite ?? undefined}
placeholder="—"
onChange={(v): void =>
updatePricing({ cacheWrite: v === null ? null : Number(v) })
}
disabled={isReadOnly}
data-testid="drawer-cache-write-cost"
/>
</div>
</div>
{hasCacheBucket && (
<div className="pricing-field cache-mode-field">
<label htmlFor="cache-mode">Cache mode</label>
<Select
id="cache-mode"
value={draft.pricing.cacheMode}
options={CACHE_MODE_OPTIONS}
onChange={(v): void => updatePricing({ cacheMode: v as CacheModeDTO })}
disabled={isReadOnly}
className="full-width"
data-testid="drawer-cache-mode"
/>
</div>
)}
<p className="muted help">Image tokens may be priced differently (v2).</p>
</div>
<div className="drawer-section drawer-surface cost-preview">
<div className="drawer-surface__head">
<h4>Cost preview</h4>
</div>
<div className="cost-preview__line">
{preview.breakdown.map((part) => part.label).join(' + ')} ={' '}
<strong> ${preview.total.toFixed(4)}</strong>
</div>
<p className="muted help">
Write-time attribution. Changes only affect new spans.
</p>
</div>
{saveError && (
<div className="drawer-error" role="alert">
{saveError}
</div>
)}
</Drawer>
);
}
export default ModelCostDrawer;

View File

@@ -0,0 +1,146 @@
import { Button, Table, type TableColumnsType } from 'antd';
import { Badge } from '@signozhq/ui/badge';
import { ChevronDown } from '@signozhq/icons';
import {
formatPricePerMillion,
getCanonicalId,
getExtraBuckets,
getRelativeLastSeen,
getSourceLabel,
type PricingRule,
} from './utils';
interface ModelCostsTableProps {
rules: PricingRule[];
isLoading: boolean;
selectedRuleId: string | null;
onEdit: (rule: PricingRule) => void;
}
function ModelCostsTable({
rules,
isLoading,
selectedRuleId,
onEdit,
}: ModelCostsTableProps): JSX.Element {
const columns: TableColumnsType<PricingRule> = [
{
title: 'Model',
dataIndex: 'modelName',
key: 'model',
render: (_value, rule): JSX.Element => (
<div className="model-cell">
<div className="model-cell__name">{rule.modelName}</div>
<div className="model-cell__canonical-id">{getCanonicalId(rule)}</div>
</div>
),
},
{
title: 'Provider',
dataIndex: 'provider',
key: 'provider',
},
{
title: 'Input / 1M',
key: 'input',
render: (_value, rule): JSX.Element => (
<span className="price-cell">
{formatPricePerMillion(rule.pricing?.input)}
</span>
),
},
{
title: 'Output / 1M',
key: 'output',
render: (_value, rule): JSX.Element => (
<span className="price-cell">
{formatPricePerMillion(rule.pricing?.output)}
</span>
),
},
{
title: 'Extra buckets',
key: 'extra-buckets',
render: (_value, rule): JSX.Element => {
const buckets = getExtraBuckets(rule);
if (buckets.length === 0) {
return <span className="muted"></span>;
}
return (
<div className="extra-buckets">
{buckets.map((bucket) => (
<Badge
key={bucket.key}
color="vanilla"
variant="outline"
className="extra-buckets__chip"
>
<span className="extra-buckets__key">{bucket.key}</span>
<span className="extra-buckets__price">
{formatPricePerMillion(bucket.pricePerMillion)}
</span>
</Badge>
))}
</div>
);
},
},
{
title: 'Source',
dataIndex: 'isOverride',
key: 'source',
render: (_value, rule): JSX.Element => {
const label = getSourceLabel(rule);
return (
<Badge
color={rule.isOverride ? 'amber' : 'robin'}
variant="outline"
className="source-badge"
data-testid={`source-badge-${rule.id}`}
>
{label}
</Badge>
);
},
},
{
title: 'Last seen',
key: 'last-seen',
render: (_value, rule): string => getRelativeLastSeen(rule),
},
{
title: '',
key: 'actions',
width: 80,
render: (_value, rule): JSX.Element => (
<Button
type="text"
size="small"
data-testid={`edit-rule-${rule.id}`}
onClick={(): void => onEdit(rule)}
>
Edit
<ChevronDown size={14} />
</Button>
),
},
];
return (
<Table<PricingRule>
className="model-costs-table"
rowKey="id"
columns={columns}
dataSource={rules}
loading={isLoading}
pagination={false}
rowClassName={(row): string =>
row.id === selectedRuleId ? 'model-costs-table__row--selected' : ''
}
data-testid="model-costs-table"
/>
);
}
export default ModelCostsTable;

View File

@@ -0,0 +1,108 @@
import {
LlmpricingruletypesLLMPricingRuleUnitDTO as UnitDTO,
type LlmpricingruletypesLLMPricingRuleDTO,
} from 'api/generated/services/sigNoz.schemas';
import { rest, server } from 'mocks-server/server';
import { fireEvent, render, screen } from 'tests/test-utils';
import LLMObservabilityModelPricing from '../LLMObservabilityModelPricing';
const ENDPOINT = '*/api/v1/llm_pricing_rules';
const mockRules: LlmpricingruletypesLLMPricingRuleDTO[] = [
{
id: 'rule-gpt4o',
orgId: 'org-1',
modelName: 'gpt-4o',
provider: 'OpenAI',
modelPattern: ['gpt-4o'],
isOverride: false,
enabled: true,
unit: UnitDTO.per_million_tokens,
pricing: { input: 15, output: 60 },
},
{
id: 'rule-llama',
orgId: 'org-1',
modelName: 'llama-3.1-70b',
provider: 'Self-hosted',
modelPattern: ['llama-3.1'],
isOverride: true,
enabled: true,
unit: UnitDTO.per_million_tokens,
pricing: { input: 0, output: 0 },
},
];
describe('LLMObservabilityModelPricing', () => {
beforeEach(() => {
server.use(
rest.get(ENDPOINT, (_, res, ctx) =>
res(
ctx.status(200),
ctx.json({
status: 'success',
data: {
items: mockRules,
limit: 100,
offset: 0,
total: mockRules.length,
},
}),
),
),
);
});
afterEach(() => {
server.resetHandlers();
});
it('renders the page header and both rules', async () => {
render(<LLMObservabilityModelPricing />);
await screen.findByText('gpt-4o');
expect(screen.getByText('Configuration')).toBeInTheDocument();
expect(screen.getByText('llama-3.1-70b')).toBeInTheDocument();
expect(screen.getByText('openai:gpt-4o')).toBeInTheDocument();
});
it('filters rules by the search input', async () => {
render(<LLMObservabilityModelPricing />);
await screen.findByText('gpt-4o');
fireEvent.change(screen.getByTestId('search-input'), {
target: { value: 'llama' },
});
expect(screen.queryByText('gpt-4o')).not.toBeInTheDocument();
expect(screen.getByText('llama-3.1-70b')).toBeInTheDocument();
});
it('opens the drawer in Add mode when the Add button is clicked', async () => {
render(<LLMObservabilityModelPricing />);
await screen.findByText('gpt-4o');
fireEvent.click(screen.getByTestId('add-model-cost-btn'));
const input = (await screen.findByTestId(
'drawer-model-id-input',
)) as HTMLInputElement;
expect(input).toBeInTheDocument();
expect(input.value).toBe('');
});
it('opens the drawer in Edit mode with prefilled values when a row Edit is clicked', async () => {
render(<LLMObservabilityModelPricing />);
await screen.findByText('gpt-4o');
fireEvent.click(screen.getByTestId('edit-rule-rule-gpt4o'));
const input = (await screen.findByTestId(
'drawer-model-id-input',
)) as HTMLInputElement;
expect(input).toBeInTheDocument();
expect(input.value).toBe('gpt-4o');
});
});

View File

@@ -0,0 +1,141 @@
import { useState } from 'react';
import userEvent from '@testing-library/user-event';
import { fireEvent, render, screen } from 'tests/test-utils';
import { EMPTY_DRAFT, type DrawerDraft } from '../drawerUtils';
import ModelCostDrawer from '../ModelCostDrawer';
interface HarnessProps {
initialDraft?: DrawerDraft;
mode?: 'add' | 'edit';
onSave?: () => void;
onDelete?: () => void;
}
function Harness({
initialDraft = { ...EMPTY_DRAFT, modelName: 'gpt-4o' },
mode = 'add',
onSave = jest.fn(),
onDelete = jest.fn(),
}: HarnessProps): JSX.Element {
const [draft, setDraft] = useState<DrawerDraft>(initialDraft);
return (
<ModelCostDrawer
isOpen
mode={mode}
draft={draft}
setDraft={setDraft}
onClose={jest.fn()}
onSave={onSave}
onDelete={onDelete}
isSaving={false}
isDeleting={false}
saveError={null}
/>
);
}
describe('ModelCostDrawer', () => {
it('adds a pattern chip when the user types and presses Enter', () => {
render(<Harness />);
fireEvent.change(screen.getByTestId('drawer-pattern-input'), {
target: { value: 'gpt-4o-mini' },
});
fireEvent.keyDown(screen.getByTestId('drawer-pattern-input'), {
key: 'Enter',
code: 'Enter',
});
expect(screen.getByText('gpt-4o-mini*')).toBeInTheDocument();
});
it('shows a match result when the test input matches an existing pattern', () => {
render(
<Harness
initialDraft={{
...EMPTY_DRAFT,
modelName: 'gpt-4o',
patterns: ['gpt-4o'],
isOverride: true,
}}
/>,
);
fireEvent.change(screen.getByTestId('drawer-pattern-test-input'), {
target: { value: 'gpt-4o-2024-08-06' },
});
expect(screen.getByTestId('drawer-pattern-test-result')).toHaveTextContent(
/matched: gpt-4o\*/i,
);
});
it('shows a no-match result when nothing matches', () => {
render(
<Harness
initialDraft={{
...EMPTY_DRAFT,
modelName: 'gpt-4o',
patterns: ['gpt-4o'],
isOverride: true,
}}
/>,
);
fireEvent.change(screen.getByTestId('drawer-pattern-test-input'), {
target: { value: 'claude' },
});
expect(screen.getByTestId('drawer-pattern-test-result')).toHaveTextContent(
/no matching pattern/i,
);
});
it('shows a reset confirmation when switching from Override to Auto', async () => {
const user = userEvent.setup({ pointerEventsCheck: 0 });
render(
<Harness
initialDraft={{
...EMPTY_DRAFT,
modelName: 'gpt-4o',
isOverride: true,
}}
/>,
);
await user.click(screen.getByTestId('drawer-source-auto'));
expect(screen.getByTestId('drawer-reset-confirm-btn')).toBeInTheDocument();
expect(screen.getByTestId('drawer-reset-keep-btn')).toBeInTheDocument();
});
it('hides the Delete action in Add mode', () => {
render(<Harness mode="add" />);
expect(screen.queryByTestId('drawer-delete-btn')).not.toBeInTheDocument();
});
it('shows the Delete action in Edit mode', () => {
render(
<Harness
mode="edit"
initialDraft={{
...EMPTY_DRAFT,
id: 'rule-1',
modelName: 'gpt-4o',
isOverride: true,
}}
/>,
);
expect(screen.getByTestId('drawer-delete-btn')).toBeInTheDocument();
});
it('calls onSave when the Save button is clicked', async () => {
const user = userEvent.setup({ pointerEventsCheck: 0 });
const onSave = jest.fn();
render(<Harness onSave={onSave} />);
await user.click(screen.getByTestId('drawer-save-btn'));
expect(onSave).toHaveBeenCalledTimes(1);
});
});

View File

@@ -0,0 +1,160 @@
import {
LlmpricingruletypesLLMPricingRuleCacheModeDTO as CacheModeDTO,
LlmpricingruletypesLLMPricingRuleUnitDTO as UnitDTO,
} from 'api/generated/services/sigNoz.schemas';
import {
buildPricingPayload,
buildRulePayload,
computeCostPreview,
draftFromRule,
EMPTY_DRAFT,
matchesAnyPattern,
validateDraft,
type DrawerDraft,
} from '../drawerUtils';
import type { PricingRule } from '../utils';
const makeRule = (overrides: Partial<PricingRule> = {}): PricingRule => ({
id: 'rule-1',
orgId: 'org-1',
modelName: 'gpt-4o',
provider: 'OpenAI',
modelPattern: ['gpt-4o'],
isOverride: false,
enabled: true,
unit: UnitDTO.per_million_tokens,
pricing: { input: 15, output: 60 },
...overrides,
});
describe('drawerUtils', () => {
describe('draftFromRule', () => {
it('maps a rule to a draft with cache values when present', () => {
const rule = makeRule({
pricing: {
input: 3,
output: 15,
cache: {
mode: CacheModeDTO.additive,
read: 0.3,
write: 3.75,
},
},
});
const draft = draftFromRule(rule);
expect(draft.modelName).toBe('gpt-4o');
expect(draft.pricing.input).toBe(3);
expect(draft.pricing.cacheRead).toBe(0.3);
expect(draft.pricing.cacheWrite).toBe(3.75);
expect(draft.pricing.cacheMode).toBe(CacheModeDTO.additive);
});
it('falls back to defaults when cache is missing', () => {
const draft = draftFromRule(makeRule());
expect(draft.pricing.cacheRead).toBeNull();
expect(draft.pricing.cacheWrite).toBeNull();
expect(draft.pricing.cacheMode).toBe(CacheModeDTO.unknown);
});
});
describe('buildPricingPayload', () => {
it('omits the cache block when no cache values are set', () => {
const payload = buildPricingPayload(EMPTY_DRAFT);
expect(payload.cache).toBeUndefined();
});
it('includes only the cache values that are > 0', () => {
const draft: DrawerDraft = {
...EMPTY_DRAFT,
pricing: {
...EMPTY_DRAFT.pricing,
cacheRead: 1.5,
cacheWrite: 0,
cacheMode: CacheModeDTO.subtract,
},
};
const payload = buildPricingPayload(draft);
expect(payload.cache?.read).toBe(1.5);
expect(payload.cache?.write).toBeUndefined();
expect(payload.cache?.mode).toBe(CacheModeDTO.subtract);
});
});
describe('buildRulePayload', () => {
it('uses the modelName as a default pattern when no patterns are supplied', () => {
const draft: DrawerDraft = {
...EMPTY_DRAFT,
modelName: 'gpt-4o',
patterns: [],
provider: 'OpenAI',
};
const payload = buildRulePayload(draft);
expect(payload.modelPattern).toStrictEqual(['gpt-4o']);
expect(payload.unit).toBe(UnitDTO.per_million_tokens);
expect(payload.enabled).toBe(true);
});
it('omits id and sourceId for an Add draft', () => {
const payload = buildRulePayload(EMPTY_DRAFT);
expect(payload.id).toBeUndefined();
expect(payload.sourceId).toBeUndefined();
});
});
describe('validateDraft', () => {
it('requires a model name in Add mode', () => {
const result = validateDraft(EMPTY_DRAFT, 'add');
expect(result.ok).toBe(false);
expect(result.message).toMatch(/billing model id/i);
});
it('rejects negative pricing', () => {
const draft: DrawerDraft = {
...EMPTY_DRAFT,
modelName: 'gpt-4o',
pricing: { ...EMPTY_DRAFT.pricing, input: -1 },
};
expect(validateDraft(draft, 'add').ok).toBe(false);
});
it('accepts a valid Add draft', () => {
const draft: DrawerDraft = {
...EMPTY_DRAFT,
modelName: 'gpt-4o',
pricing: { ...EMPTY_DRAFT.pricing, input: 1, output: 2 },
};
expect(validateDraft(draft, 'add').ok).toBe(true);
});
});
describe('matchesAnyPattern', () => {
it('returns the matching prefix pattern, case-insensitive', () => {
expect(matchesAnyPattern('GPT-4o-2024', ['gpt-4o'])).toBe('gpt-4o');
});
it('returns null when nothing matches', () => {
expect(matchesAnyPattern('claude', ['gpt-4o'])).toBeNull();
});
});
describe('computeCostPreview', () => {
it('adds cache buckets when they are set', () => {
const draft: DrawerDraft = {
...EMPTY_DRAFT,
pricing: {
...EMPTY_DRAFT.pricing,
input: 10,
output: 30,
cacheRead: 5,
},
};
const preview = computeCostPreview(draft);
const labels = preview.breakdown.map((part) => part.label);
expect(labels).toContain('2000 input');
expect(labels).toContain('500 output');
expect(labels).toContain('1000 cache_read');
expect(preview.total).toBeGreaterThan(0);
});
});
});

View File

@@ -0,0 +1,119 @@
import {
LlmpricingruletypesLLMPricingRuleCacheModeDTO as CacheModeDTO,
LlmpricingruletypesLLMPricingRuleUnitDTO as UnitDTO,
} from 'api/generated/services/sigNoz.schemas';
import {
filterRules,
formatPricePerMillion,
getCanonicalId,
getExtraBuckets,
getRelativeLastSeen,
getSourceLabel,
type PricingRule,
} from '../utils';
const makeRule = (overrides: Partial<PricingRule> = {}): PricingRule => ({
id: 'rule-1',
orgId: 'org-1',
modelName: 'gpt-4o',
provider: 'OpenAI',
modelPattern: ['gpt-4o'],
isOverride: false,
enabled: true,
unit: UnitDTO.per_million_tokens,
pricing: { input: 15, output: 60 },
...overrides,
});
describe('utils', () => {
describe('formatPricePerMillion', () => {
it('formats numbers with 2 decimals and dollar prefix', () => {
expect(formatPricePerMillion(15)).toBe('$15.00');
expect(formatPricePerMillion(0.15)).toBe('$0.15');
});
it('returns em-dash for nullish or NaN', () => {
expect(formatPricePerMillion(undefined)).toBe('—');
expect(formatPricePerMillion(Number.NaN)).toBe('—');
});
});
describe('getExtraBuckets', () => {
it('returns an empty array when there is no cache pricing', () => {
expect(getExtraBuckets(makeRule())).toStrictEqual([]);
});
it('returns only buckets with values > 0', () => {
const rule = makeRule({
pricing: {
input: 3,
output: 15,
cache: {
mode: CacheModeDTO.additive,
read: 0.3,
write: 0,
},
},
});
const buckets = getExtraBuckets(rule);
expect(buckets).toStrictEqual([{ key: 'cache_read', pricePerMillion: 0.3 }]);
});
});
describe('getSourceLabel', () => {
it('returns "Auto" for non-override and "User override" otherwise', () => {
expect(getSourceLabel(makeRule({ isOverride: false }))).toBe('Auto');
expect(getSourceLabel(makeRule({ isOverride: true }))).toBe('User override');
});
});
describe('getCanonicalId', () => {
it('lowercases the provider and joins with the model name', () => {
expect(getCanonicalId(makeRule({ provider: 'OpenAI' }))).toBe(
'openai:gpt-4o',
);
});
});
describe('getRelativeLastSeen', () => {
it('returns em-dash when no timestamp is present', () => {
expect(getRelativeLastSeen(makeRule())).toBe('—');
});
it('formats minutes-old timestamps', () => {
const recent = new Date(Date.now() - 5 * 60 * 1000).toISOString();
expect(getRelativeLastSeen(makeRule({ updatedAt: recent }))).toMatch(
/min ago/,
);
});
});
describe('filterRules', () => {
const auto = makeRule({ id: 'r1', modelName: 'gpt-4o', isOverride: false });
const override = makeRule({
id: 'r2',
modelName: 'llama-3',
provider: 'Self-hosted',
modelPattern: ['llama-3'],
isOverride: true,
});
it('returns everything when no filters are applied', () => {
expect(filterRules([auto, override], '', 'all')).toHaveLength(2);
});
it('narrows by source = override', () => {
expect(filterRules([auto, override], '', 'override')).toStrictEqual([
override,
]);
});
it('narrows by free-text search across model and provider', () => {
expect(filterRules([auto, override], 'self', 'all')).toStrictEqual([
override,
]);
expect(filterRules([auto, override], 'gpt-4', 'all')).toStrictEqual([auto]);
});
});
});

View File

@@ -0,0 +1,191 @@
import {
LlmpricingruletypesLLMPricingRuleCacheModeDTO as CacheModeDTO,
LlmpricingruletypesLLMPricingRuleUnitDTO as UnitDTO,
type LlmpricingruletypesLLMPricingCacheCostsDTO,
type LlmpricingruletypesLLMRulePricingDTO,
type LlmpricingruletypesUpdatableLLMPricingRuleDTO,
} from 'api/generated/services/sigNoz.schemas';
import type { PricingRule } from './utils';
export const PROVIDER_OPTIONS = [
{ value: 'OpenAI', label: 'OpenAI' },
{ value: 'Anthropic', label: 'Anthropic' },
{ value: 'Azure OpenAI', label: 'Azure OpenAI' },
{ value: 'Google', label: 'Google' },
{ value: 'Self-hosted', label: 'Self-hosted' },
{ value: 'Other', label: 'Other' },
];
export const CACHE_MODE_OPTIONS = [
{
value: CacheModeDTO.subtract,
label: 'Subtract (OpenAI style)',
},
{
value: CacheModeDTO.additive,
label: 'Additive (Anthropic style)',
},
{
value: CacheModeDTO.unknown,
label: 'Unknown',
},
];
export type DrawerMode = 'add' | 'edit';
export interface DrawerDraft {
id: string | null;
sourceId: string | null;
modelName: string;
provider: string;
patterns: string[];
isOverride: boolean;
pricing: {
input: number;
output: number;
cacheMode: CacheModeDTO;
cacheRead: number | null;
cacheWrite: number | null;
};
}
export const EMPTY_DRAFT: DrawerDraft = {
id: null,
sourceId: null,
modelName: '',
provider: 'OpenAI',
patterns: [],
isOverride: true,
pricing: {
input: 0,
output: 0,
cacheMode: CacheModeDTO.unknown,
cacheRead: null,
cacheWrite: null,
},
};
export const draftFromRule = (rule: PricingRule): DrawerDraft => ({
id: rule.id,
sourceId: rule.sourceId ?? null,
modelName: rule.modelName,
provider: rule.provider || 'OpenAI',
patterns: rule.modelPattern || [],
isOverride: !!rule.isOverride,
pricing: {
input: rule.pricing?.input ?? 0,
output: rule.pricing?.output ?? 0,
cacheMode: rule.pricing?.cache?.mode ?? CacheModeDTO.unknown,
cacheRead: rule.pricing?.cache?.read ?? null,
cacheWrite: rule.pricing?.cache?.write ?? null,
},
});
const hasCacheValue = (value: number | null): boolean =>
typeof value === 'number' && value > 0;
export const buildPricingPayload = (
draft: DrawerDraft,
): LlmpricingruletypesLLMRulePricingDTO => {
const pricing: LlmpricingruletypesLLMRulePricingDTO = {
input: draft.pricing.input,
output: draft.pricing.output,
};
if (
hasCacheValue(draft.pricing.cacheRead) ||
hasCacheValue(draft.pricing.cacheWrite)
) {
const cache: LlmpricingruletypesLLMPricingCacheCostsDTO = {
mode: draft.pricing.cacheMode,
};
if (hasCacheValue(draft.pricing.cacheRead)) {
cache.read = draft.pricing.cacheRead as number;
}
if (hasCacheValue(draft.pricing.cacheWrite)) {
cache.write = draft.pricing.cacheWrite as number;
}
pricing.cache = cache;
}
return pricing;
};
export const buildRulePayload = (
draft: DrawerDraft,
): LlmpricingruletypesUpdatableLLMPricingRuleDTO => ({
id: draft.id || undefined,
sourceId: draft.sourceId || undefined,
modelName: draft.modelName.trim(),
provider: draft.provider.trim(),
modelPattern:
draft.patterns.length > 0 ? draft.patterns : [draft.modelName.trim()],
isOverride: draft.isOverride,
enabled: true,
unit: UnitDTO.per_million_tokens,
pricing: buildPricingPayload(draft),
});
export interface ValidationResult {
ok: boolean;
message?: string;
}
export const validateDraft = (
draft: DrawerDraft,
mode: DrawerMode,
): ValidationResult => {
if (mode === 'add' && !draft.modelName.trim()) {
return { ok: false, message: 'Billing model ID is required.' };
}
if (!draft.provider.trim()) {
return { ok: false, message: 'Provider is required.' };
}
if (draft.pricing.input < 0 || draft.pricing.output < 0) {
return { ok: false, message: 'Pricing values must be non-negative.' };
}
return { ok: true };
};
export const matchesAnyPattern = (
candidate: string,
patterns: string[],
): string | null => {
const lowered = candidate.toLowerCase();
const match = patterns.find((pattern) =>
lowered.startsWith(pattern.toLowerCase()),
);
return match || null;
};
const EXAMPLE_INPUT_TOKENS = 2000;
const EXAMPLE_OUTPUT_TOKENS = 500;
const EXAMPLE_CACHE_TOKENS = 1000;
const PER_MILLION = 1_000_000;
export interface CostPreviewParts {
total: number;
breakdown: { label: string; cost: number }[];
}
export const computeCostPreview = (draft: DrawerDraft): CostPreviewParts => {
const breakdown: { label: string; cost: number }[] = [];
const inputCost = (EXAMPLE_INPUT_TOKENS / PER_MILLION) * draft.pricing.input;
const outputCost =
(EXAMPLE_OUTPUT_TOKENS / PER_MILLION) * draft.pricing.output;
breakdown.push({ label: `${EXAMPLE_INPUT_TOKENS} input`, cost: inputCost });
breakdown.push({ label: `${EXAMPLE_OUTPUT_TOKENS} output`, cost: outputCost });
let total = inputCost + outputCost;
if (hasCacheValue(draft.pricing.cacheRead)) {
const cost =
(EXAMPLE_CACHE_TOKENS / PER_MILLION) * (draft.pricing.cacheRead as number);
breakdown.push({ label: `${EXAMPLE_CACHE_TOKENS} cache_read`, cost });
total += cost;
}
if (hasCacheValue(draft.pricing.cacheWrite)) {
const cost =
(EXAMPLE_CACHE_TOKENS / PER_MILLION) * (draft.pricing.cacheWrite as number);
breakdown.push({ label: `${EXAMPLE_CACHE_TOKENS} cache_write`, cost });
total += cost;
}
return { total, breakdown };
};

View File

@@ -0,0 +1,125 @@
import { useCallback, useState } from 'react';
import { useQueryClient } from 'react-query';
import {
getListLLMPricingRulesQueryKey,
useCreateOrUpdateLLMPricingRules,
useDeleteLLMPricingRule,
} from 'api/generated/services/llmpricingrules';
import {
buildRulePayload,
draftFromRule,
EMPTY_DRAFT,
type DrawerDraft,
type DrawerMode,
} from './drawerUtils';
import type { PricingRule } from './utils';
interface UseModelCostDrawerResult {
isOpen: boolean;
mode: DrawerMode;
draft: DrawerDraft;
setDraft: (next: DrawerDraft) => void;
openForAdd: (prefillModelName?: string) => void;
openForEdit: (rule: PricingRule) => void;
close: () => void;
save: () => Promise<void>;
deleteRule: () => Promise<void>;
isSaving: boolean;
isDeleting: boolean;
saveError: string | null;
selectedRuleId: string | null;
}
export function useModelCostDrawer(): UseModelCostDrawerResult {
const queryClient = useQueryClient();
const [isOpen, setIsOpen] = useState<boolean>(false);
const [mode, setMode] = useState<DrawerMode>('add');
const [draft, setDraft] = useState<DrawerDraft>(EMPTY_DRAFT);
const [selectedRuleId, setSelectedRuleId] = useState<string | null>(null);
const [saveError, setSaveError] = useState<string | null>(null);
const { mutateAsync: createOrUpdate, isLoading: isSaving } =
useCreateOrUpdateLLMPricingRules();
const { mutateAsync: deleteRuleApi, isLoading: isDeleting } =
useDeleteLLMPricingRule();
const invalidateList = useCallback(async (): Promise<void> => {
await queryClient.invalidateQueries({
queryKey: getListLLMPricingRulesQueryKey(),
});
}, [queryClient]);
const openForAdd = useCallback((prefillModelName?: string): void => {
setMode('add');
setDraft({
...EMPTY_DRAFT,
modelName: prefillModelName || '',
patterns: prefillModelName ? [prefillModelName] : [],
});
setSelectedRuleId(null);
setSaveError(null);
setIsOpen(true);
}, []);
const openForEdit = useCallback((rule: PricingRule): void => {
setMode('edit');
setDraft(draftFromRule(rule));
setSelectedRuleId(rule.id);
setSaveError(null);
setIsOpen(true);
}, []);
const close = useCallback((): void => {
setIsOpen(false);
setSelectedRuleId(null);
setSaveError(null);
}, []);
const save = useCallback(async (): Promise<void> => {
setSaveError(null);
try {
await createOrUpdate({
data: { rules: [buildRulePayload(draft)] },
});
await invalidateList();
setIsOpen(false);
setSelectedRuleId(null);
} catch (error) {
const message = error instanceof Error ? error.message : 'Save failed';
setSaveError(message);
}
}, [createOrUpdate, draft, invalidateList]);
const deleteRule = useCallback(async (): Promise<void> => {
if (!draft.id) {
return;
}
setSaveError(null);
try {
await deleteRuleApi({ pathParams: { id: draft.id } });
await invalidateList();
setIsOpen(false);
setSelectedRuleId(null);
} catch (error) {
const message = error instanceof Error ? error.message : 'Delete failed';
setSaveError(message);
}
}, [deleteRuleApi, draft.id, invalidateList]);
return {
isOpen,
mode,
draft,
setDraft,
openForAdd,
openForEdit,
close,
save,
deleteRule,
isSaving,
isDeleting,
saveError,
selectedRuleId,
};
}

View File

@@ -0,0 +1,101 @@
import type { LlmpricingruletypesLLMPricingRuleDTO } from 'api/generated/services/sigNoz.schemas';
export type PricingRule = LlmpricingruletypesLLMPricingRuleDTO;
export type SourceFilter = 'all' | 'auto' | 'override';
export interface ExtraBucket {
key: string;
pricePerMillion: number;
}
export const formatPricePerMillion = (value: number | undefined): string => {
if (value === undefined || value === null || Number.isNaN(value)) {
return '—';
}
return `$${value.toFixed(2)}`;
};
export const getExtraBuckets = (rule: PricingRule): ExtraBucket[] => {
const cache = rule.pricing?.cache;
if (!cache) {
return [];
}
const buckets: ExtraBucket[] = [];
if (typeof cache.read === 'number' && cache.read > 0) {
buckets.push({ key: 'cache_read', pricePerMillion: cache.read });
}
if (typeof cache.write === 'number' && cache.write > 0) {
buckets.push({ key: 'cache_write', pricePerMillion: cache.write });
}
return buckets;
};
export const getSourceLabel = (rule: PricingRule): 'Auto' | 'User override' =>
rule.isOverride ? 'User override' : 'Auto';
const MINUTE = 60;
const HOUR = 60 * MINUTE;
const DAY = 24 * HOUR;
const MONTH = 30 * DAY;
const YEAR = 365 * DAY;
export const getRelativeLastSeen = (rule: PricingRule): string => {
const ts = rule.updatedAt || rule.syncedAt || rule.createdAt;
if (!ts) {
return '—';
}
const now = Date.now();
const target = new Date(ts).getTime();
if (Number.isNaN(target)) {
return '—';
}
const seconds = Math.max(0, Math.round((now - target) / 1000));
if (seconds < MINUTE) {
return 'just now';
}
if (seconds < HOUR) {
return `${Math.floor(seconds / MINUTE)} min ago`;
}
if (seconds < DAY) {
return `${Math.floor(seconds / HOUR)} hr ago`;
}
if (seconds < MONTH) {
return `${Math.floor(seconds / DAY)} days ago`;
}
if (seconds < YEAR) {
return `${Math.floor(seconds / MONTH)} mo ago`;
}
return `${Math.floor(seconds / YEAR)} yr ago`;
};
const lc = (value: string): string => value.toLowerCase();
export const filterRules = (
rules: PricingRule[],
search: string,
source: SourceFilter,
): PricingRule[] => {
const normalized = lc(search.trim());
return rules.filter((rule) => {
if (source === 'auto' && rule.isOverride) {
return false;
}
if (source === 'override' && !rule.isOverride) {
return false;
}
if (!normalized) {
return true;
}
return (
lc(rule.modelName).includes(normalized) ||
lc(rule.provider).includes(normalized) ||
(rule.modelPattern || []).some((pattern) => lc(pattern).includes(normalized))
);
});
};
export const getCanonicalId = (rule: PricingRule): string => {
const provider = rule.provider?.trim() || 'unknown';
return `${lc(provider)}:${rule.modelName}`;
};

View File

@@ -11,6 +11,7 @@ import {
Building2,
ChartArea,
Cloudy,
Coins,
DraftingCompass,
FileKey2,
Github,
@@ -365,6 +366,13 @@ export const settingsNavSections: SettingsNavSection[] = [
isEnabled: false,
itemKey: 'mcp-server',
},
{
key: ROUTES.LLM_OBSERVABILITY_MODEL_PRICING,
label: 'Model Pricing',
icon: <Coins size={16} />,
isEnabled: true,
itemKey: 'llm-observability-model-pricing',
},
],
},

View File

@@ -0,0 +1,7 @@
import LLMObservabilityModelPricing from 'container/LLMObservabilityModelPricing/LLMObservabilityModelPricing';
function LLMObservabilityModelPricingPage(): JSX.Element {
return <LLMObservabilityModelPricing />;
}
export default LLMObservabilityModelPricingPage;

View File

@@ -136,4 +136,5 @@ export const routePermission: Record<keyof typeof ROUTES, ROLES[]> = {
AI_ASSISTANT_ICON_PREVIEW: ['ADMIN', 'EDITOR', 'VIEWER'],
MCP_SERVER: ['ADMIN', 'EDITOR', 'VIEWER'],
AI_ASSISTANT_BASE: ['ADMIN', 'EDITOR', 'VIEWER'],
LLM_OBSERVABILITY_MODEL_PRICING: ['ADMIN', 'EDITOR', 'VIEWER'],
};