useConsumptionHistory
neon-ui/use-consumption-historyFreeHook
Fetches consumption history through your own proxy route and shapes it into buckets and totals.
Install it
npx shadcn@latest add https://ui.neon.com/r/use-consumption-history.jsonSource
1"use client";23import { useCallback, useEffect, useRef, useState } from "react";45import type {6 ConsumptionBucket,7 ConsumptionGranularity,8 ConsumptionMetricName,9 ConsumptionPeriod,10 ConsumptionTotals,11} from "@/lib/consumption";12import {13 CONSUMPTION_METRICS,14 flattenConsumption,15 sumBuckets,16} from "@/lib/consumption";1718/**19 * Neon refreshes consumption roughly every 15 minutes and the endpoints20 * share a ~50 req/min bucket, so anything faster than this just burns21 * quota on numbers that haven't moved.22 */23export const MIN_POLL_INTERVAL_MS = 900_000;2425export interface UseConsumptionHistoryOptions {26 /**27 * Your own route handler that proxies the Neon consumption API. The key28 * is server-only, so the browser must never call console.neon.tech29 * directly. Defaults to "/api/consumption".30 */31 endpoint?: string;32 /** RFC 3339 start of the window. */33 from: string;34 /** RFC 3339 end of the window. */35 to: string;36 granularity: ConsumptionGranularity;37 /** Defaults to every metric the v2 project endpoint returns. */38 metrics?: readonly ConsumptionMetricName[];39 /** Narrow to specific projects; omit for the whole org. */40 projectIds?: readonly string[];41 /** Re-fetch on an interval. Clamped to 15 minutes. */42 pollIntervalMs?: number;43 /** Set false to hold the request until you're ready. */44 enabled?: boolean;45}4647export interface UseConsumptionHistoryResult {48 /** One entry per timeframe, oldest first, raw API units. */49 buckets: ConsumptionBucket[];50 /** Every metric summed across the window, raw API units. */51 totals: ConsumptionTotals;52 /** The untouched response, for per-project or per-branch splitting. */53 periods: ConsumptionPeriod[];54 isLoading: boolean;55 error: string | null;56 /** Fetched-at timestamp; pair it with a metering-lag notice. */57 updatedAt: Date | null;58 refresh: () => void;59}6061interface ConsumptionResponse {62 projects?: { project_id: string; periods: ConsumptionPeriod[] }[];63 branches?: { branch_id: string; periods: ConsumptionPeriod[] }[];64}6566const buildQuery = (options: UseConsumptionHistoryOptions) => {67 const params = new URLSearchParams({68 from: options.from,69 granularity: options.granularity,70 metrics: (options.metrics ?? CONSUMPTION_METRICS).join(","),71 to: options.to,72 });7374 if (options.projectIds?.length) {75 params.set("project_ids", options.projectIds.join(","));76 }7778 return params.toString();79};8081// … truncated
What it pulls in
Other registry items
Files it writes
More from neon-ui
All 77 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| useGatewayModelsuse-gateway-models | neon-ui | Hooks | Free | no deps | |
| useRegionPinguse-region-ping | neon-ui | Hooks | Free | no deps |
