Skip to content
Merged
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
16 changes: 3 additions & 13 deletions apps/dashboard/components/logs/chart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import { formatNumber } from "@/lib/fmt";
import { Grid } from "@unkey/icons";
import { useEffect, useRef, useState } from "react";
import { Bar, BarChart, ReferenceArea, ResponsiveContainer, YAxis } from "recharts";
import { createTimeIntervalFormatter } from "../overview-charts/utils";
import { LogsChartError } from "./components/logs-chart-error";
import { LogsChartLoading } from "./components/logs-chart-loading";
import { calculateTimePoints } from "./utils/calculate-timepoints";
import { formatTimestampLabel, formatTimestampTooltip } from "./utils/format-timestamp";
import { formatTimestampLabel } from "./utils/format-timestamp";

type Selection = {
start: string | number;
Expand Down Expand Up @@ -183,18 +184,7 @@ export function LogsTimeseriesBarChart({
</div>
}
className="rounded-lg shadow-lg border border-gray-4"
labelFormatter={(_, tooltipPayload) => {
const originalTimestamp = tooltipPayload[0]?.payload?.originalTimestamp;
return originalTimestamp ? (
<div>
<span className="font-mono text-accent-9 text-xs px-4">
{formatTimestampTooltip(originalTimestamp)}
</span>
</div>
) : (
""
);
}}
labelFormatter={(_, payload) => createTimeIntervalFormatter(data)(payload)}
/>
);
}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
"use client";
import { calculateTimePoints } from "@/components/logs/chart/utils/calculate-timepoints";
import {
formatTimestampLabel,
formatTimestampTooltip,
} from "@/components/logs/chart/utils/format-timestamp";
import { formatTimestampLabel } from "@/components/logs/chart/utils/format-timestamp";
import {
type ChartConfig,
ChartContainer,
Expand All @@ -24,6 +21,7 @@ import {
import { OverviewAreaChartError } from "./overview-area-chart-error";
import { OverviewAreaChartLoader } from "./overview-area-chart-loader";
import type { Selection, TimeseriesData } from "./types";
import { createTimeIntervalFormatter } from "./utils";

export type ChartMetric = {
key: string;
Expand Down Expand Up @@ -241,18 +239,9 @@ export const OverviewAreaChart = ({
label={label}
active={active}
className="rounded-lg shadow-lg border border-gray-4"
labelFormatter={(_, tooltipPayload) => {
const originalTimestamp = tooltipPayload[0]?.payload?.originalTimestamp;
return originalTimestamp ? (
<div>
<span className="font-mono text-accent-9 text-xs px-4">
{formatTimestampTooltip(originalTimestamp)}
</span>
</div>
) : (
""
);
}}
labelFormatter={(_, tooltipPayload) =>
createTimeIntervalFormatter(data)(tooltipPayload)
}
/>
);
}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
"use client";

import { calculateTimePoints } from "@/components/logs/chart/utils/calculate-timepoints";
import {
formatTimestampLabel,
formatTimestampTooltip,
} from "@/components/logs/chart/utils/format-timestamp";
import { formatTimestampLabel } from "@/components/logs/chart/utils/format-timestamp";
import {
type ChartConfig,
ChartContainer,
Expand All @@ -18,6 +15,7 @@ import { Bar, BarChart, CartesianGrid, ReferenceArea, ResponsiveContainer, YAxis
import { OverviewChartError } from "./overview-bar-chart-error";
import { OverviewChartLoader } from "./overview-bar-chart-loader";
import type { Selection, TimeseriesData } from "./types";
import { createTimeIntervalFormatter } from "./utils";

type ChartTooltipItem = {
label: string;
Expand Down Expand Up @@ -244,18 +242,9 @@ export function OverviewBarChart({
</div>
}
className="rounded-lg shadow-lg border border-gray-4"
labelFormatter={(_, tooltipPayload) => {
const originalTimestamp = tooltipPayload[0]?.payload?.originalTimestamp;
return originalTimestamp ? (
<div>
<span className="font-mono text-accent-9 text-xs px-4">
{formatTimestampTooltip(originalTimestamp)}
</span>
</div>
) : (
""
);
}}
labelFormatter={(_, tooltipPayload) =>
createTimeIntervalFormatter(data)(tooltipPayload)
}
/>
);
}}
Expand Down
70 changes: 70 additions & 0 deletions apps/dashboard/components/logs/overview-charts/utils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// components/logs/chart/utils/format-interval.ts

import { formatTimestampTooltip } from "../chart/utils/format-timestamp";
import type { TimeseriesData } from "./types";

/**
* Creates a tooltip formatter that displays time intervals between data points
*
* @param data - The chart data array containing timestamp information
* @returns A formatter function for use with chart tooltips
*/
export function createTimeIntervalFormatter(data?: TimeseriesData[]) {
return (tooltipPayload: any[]) => {
// Basic validation checks
if (!tooltipPayload?.[0]?.payload) {
return "";
}

const currentPayload = tooltipPayload[0].payload;
const currentTimestamp = currentPayload.originalTimestamp;
const currentDisplayX = currentPayload.displayX;

// If we don't have necessary data, fallback to displaying just the current point
if (!currentTimestamp || !currentDisplayX || !data?.length) {
return (
<div>
<span className="font-mono text-accent-9 text-xs px-4">
{currentDisplayX || formatTimestampTooltip(currentTimestamp)}
</span>
</div>
);
}

// Find position in the data array
const currentIndex = data.findIndex((item) => item?.originalTimestamp === currentTimestamp);

// If this is the last item or not found, just show current timestamp
if (currentIndex === -1 || currentIndex >= data.length - 1) {
return (
<div>
<span className="font-mono text-accent-9 text-xs px-4">{currentDisplayX}</span>
</div>
);
}

// Get the next point in the sequence
const nextPoint = data[currentIndex + 1];
if (!nextPoint) {
return (
<div>
<span className="font-mono text-accent-9 text-xs px-4">{currentDisplayX}</span>
</div>
);
}

// Format the next timestamp
const nextDisplayX =
nextPoint.displayX ||
(nextPoint.originalTimestamp ? formatTimestampTooltip(nextPoint.originalTimestamp) : "");

// Return formatted interval
return (
<div>
<span className="font-mono text-accent-9 text-xs px-4">
{currentDisplayX} - {nextDisplayX}
</span>
</div>
);
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { formatTimestampTooltip } from "@/components/logs/chart/utils/format-timestamp";
import { createTimeIntervalFormatter } from "@/components/logs/overview-charts/utils";
import {
type ChartConfig,
ChartContainer,
Expand Down Expand Up @@ -97,18 +97,7 @@ export function StatsTimeseriesBarChart<T extends BaseTimeseriesData>({
</div>
}
className="rounded-lg shadow-lg border border-gray-4"
labelFormatter={(_, tooltipPayload) => {
const originalTimestamp = tooltipPayload[0]?.payload?.originalTimestamp;
return originalTimestamp ? (
<div>
<span className="font-mono text-accent-9 text-xs px-4">
{formatTimestampTooltip(originalTimestamp)}
</span>
</div>
) : (
""
);
}}
labelFormatter={(_, payload) => createTimeIntervalFormatter(data)(payload)}
/>
);
}}
Expand Down
2 changes: 1 addition & 1 deletion internal/clickhouse/src/ratelimits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function createTimeseriesQuery(interval: TimeInterval, whereClause: string) {
ORDER BY x ASC
WITH FILL
FROM toUnixTimestamp64Milli(CAST(toStartOfInterval(toDateTime(fromUnixTimestamp64Milli({startTime: Int64})), INTERVAL ${interval.stepSize} ${interval.step}) AS DateTime64(3)))
TO toUnixTimestamp64Milli(CAST(toStartOfInterval(toDateTime(fromUnixTimestamp64Milli({endTime: Int64})), INTERVAL ${interval.stepSize} ${intervalUnit}) AS DateTime64(3))) + ${stepMs}
TO toUnixTimestamp64Milli(CAST(toStartOfInterval(toDateTime(fromUnixTimestamp64Milli({endTime: Int64})), INTERVAL ${interval.stepSize} ${interval.step}) AS DateTime64(3))) + ${stepMs}
STEP ${stepMs}`;
}

Expand Down
Loading