Skip to content

MeterUsage

A usage overview component: fetches data from analysis/query and renders a total plus a bar trend.

Loading interactive preview…

View codeHide code

Actual React source for this preview, including demo controls, shared styles and isolated mocks. The docs host supplies the client and theme; application installation and React / Vue usage follow below.

import {
MeterUsage,
MeterChart,
} from "@meterry/component-react"
import type {
MeterThemeInput,
MeterFilterValues,
} from "@meterry/component"
import { METRICS, type PreviewEnvironment } from "./preview-data"
import { EMPTY_FILTERS, styleProps } from "./preview-shared"
export function UsagePreview({
environment,
theme,
filters = EMPTY_FILTERS,
}: {
environment: PreviewEnvironment
theme: MeterThemeInput
filters?: MeterFilterValues
}) {
return (
<div data-testid="usage-preview">
<MeterUsage
{...styleProps}
title="Token usage · last 7 days"
query={{
startTime: environment.startTime,
endTime: environment.endTime,
metrics: [...METRICS],
measures: ["quantity"],
filters,
}}
slots={{
value: ({ total, formattedTotal }) => (
<div
className="docs-preview-value"
data-testid="usage-total"
data-value={total}
>
{formattedTotal}
<span className="docs-preview-unit">tokens</span>
</div>
),
chart: ({ model }) => (
<MeterChart
unstyled
type="bar"
theme={theme}
data={model.series[0]?.points ?? []}
x="label"
series={[{ key: "usage", label: "Tokens", value: "value" }]}
height={180}
showLegend={false}
slots={{ header: () => null }}
/>
),
}}
/>
</div>
)
}
Terminal
npm install @meterry/component @meterry/component-react echarts echarts-for-react

For default styles, optionally install @meterry/component-tailwindcss. For authentication, shared clients and theming, see Getting Started.

The examples below show how to fetch usage through a shared client and apply dimension filters. See Getting Started for a complete React or Vue integration.

// React: recommended — mount under MeterClientProvider
<MeterClientProvider getJwt={getSubjectToken}>
<MeterUsage title="Usage" rangeDays={7} />
</MeterClientProvider>
// Compatibility mode: component-level getJwt creates a client automatically
<MeterUsage getJwt={getSubjectToken} tokenScope="meter:current-user" />
<!-- Vue 3 -->
<script setup>
provideMeterClient({ getJwt: getSubjectToken })
</script>
<template>
<MeterUsage title="Usage" :range-days="7" measure="quantity" />
</template>
Prop Type Default Description
client SubjectClient — Explicit shared client; takes precedence over context and getJwt
getJwt GetJwt — Compatibility mode: component-level Subject Token issuance, creates a client automatically
tokenScope string — Scope of the shared token provider in compatibility mode (isolate per user/tenant/environment)
tokenStorageKey string meter:subject-token Override the localStorage key
baseUrl string '' API prefix
query UsageWidgetQuery — Overrides bucketSize / metrics / groupBy / measures / time range and supplies dimension filters
rangeDays number 7 Time window in days
title string messages.usageTitle Title
measure string first measure Measure used for the total and the chart (e.g. quantity, amount)
loading / empty ReactNode / VNode messages defaults Loading and empty state content
error (error: Error) => Node — Custom error rendering
onError (error: Error) => void — Error callback
slots MeterUsageSlots — Replace the header / value / chart rendering
messages Partial<MeterMessages> — Copy overrides, see Internationalization
seriesRegistry EChartsSeriesRegistry global registry Scoped chart type registry
className / classNames / unstyled — — Styling, see Theming & Styles

Pass active dimension values through query.filters (Record<string, string>):

import { MeterUsage, useMeterFilters } from '@meterry/component-react'
import type { MeterFilterController } from '@meterry/component'
function FilteredUsage({ controller }: { controller: MeterFilterController }) {
const filters = useMeterFilters(controller)
return <MeterUsage query={{ metrics: ['tokens'], filters }} />
}
<template>
<!-- filters is the ref returned by useMeterFilterValues(controller). -->
<MeterUsage :query="{ metrics: ['tokens'], filters }" />
</template>

These snippets assume the corresponding React/Vue bindings and a page-level filter controller; see Shared Filters for the complete setup. The component normalizes empty strings away before sending the analysis request and omits filters if none are active. Keys and non-empty values are otherwise passed unchanged; the service decides matching semantics. Metric and time selectors may need to update query.metrics, startTime, or endTime instead of becoming dimensions.

The filter bar debounces text/number input by default. MeterUsage itself does not debounce programmatic query changes. It cancels superseded requests using an AbortSignal and ignores stale responses/errors, including after unmounting. Custom fetch implementations should honor the signal.

<MeterUsage
client={client}
slots={{
header: ({ caption, title }) => <MyHeader caption={caption} title={title} />,
value: ({ total, formattedTotal, measure }) => (
<strong>{formattedTotal} {measure}</strong>
),
chart: ({ model, option }) => <MyChart option={option} />, // option feeds echarts directly
}}
/>

Changing query or rangeDays loads usage for the new selection. Use loading, empty, and error to provide helpful messages while loading, when no usage matches, or when a request fails.