Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(logs): tone down the sparkline visible-range window and give it sub-bar precision (#67382)

Frank Hamand1ffd45145e9f
diff --git a/frontend/src/lib/components/Sparkline.tsx b/frontend/src/lib/components/Sparkline.tsx
index ed415e7aaecb..f26985d37857 100644
--- a/frontend/src/lib/components/Sparkline.tsx
+++ b/frontend/src/lib/components/Sparkline.tsx
@@ -20,6 +20,8 @@ import { LemonSkeleton } from '../lemon-ui/LemonSkeleton'
// AlertHistoryChart, which also uses the annotation plugin) are safe.
Chart.register(annotationPlugin)
+const HIGHLIGHT_COLOR = '#8f8f8f'
+
export interface SparklineReferenceLine {
/** Y-axis value the dashed line is drawn at, in the same units as the series data. */
value: number
@@ -77,12 +79,13 @@ export interface SparklineProps {
/** Format the per-series tooltip value. Defaults to `humanFriendlyNumber`. */
renderTooltipValue?: (value: number) => string
/**
- * Inclusive label-index range to highlight as a translucent box behind the bars.
- * Used to mirror an external selection (e.g. the rows currently visible in a
- * paired virtualized list) onto the chart. Out-of-range or inverted ranges are
- * clamped; pass `null`/`undefined` to clear.
+ * X-axis value range to highlight as a translucent box behind the bars. Values are
+ * in the x-axis's own units: epoch ms for a time scale (positioned with sub-bar
+ * precision), or a label for a category scale. Used to mirror an external selection
+ * (e.g. the rows currently visible in a paired virtualized list) onto the chart.
+ * Callers pass an already-ordered `xMin <= xMax`; pass `null`/`undefined` to clear.
*/
- highlightedRange?: { startIndex: number; endIndex: number } | null
+ highlightedRange?: { xMin: number | string; xMax: number | string } | null
/**
* Bar indices that are still being ingested (incomplete). Those bars render with a faded
* diagonal-hatch fill, and hovering one adds `tooltip` to the hover tooltip. Used to flag the
@@ -333,31 +336,16 @@ export function Sparkline({
}
if (highlightedRange && labels && labels.length > 0) {
- const lastIdx = labels.length - 1
- const lo = Math.max(0, Math.min(highlightedRange.startIndex, highlightedRange.endIndex))
- const hi = Math.min(
- lastIdx,
- Math.max(highlightedRange.startIndex, highlightedRange.endIndex)
- )
- if (lo <= hi) {
- // Match the cursor-row highlight hue (`--primary-highlight`):
- // orange in light mode, amber in dark. Read the concrete
- // per-theme token since the semantic var is a nested `var()`
- // that doesn't resolve off-DOM (e.g. on the chart canvas).
- const isDark = document.body.getAttribute('theme') === 'dark'
- const primary = getColorVar(isDark ? 'primary-3000-dark' : 'primary-3000-light')
- annotations.highlightedRange = {
- type: 'box',
- xMin: labels[lo],
- // Extend to the next bucket's start so the last bar is fully enclosed.
- xMax: labels[hi + 1] ?? labels[hi],
- // Drawn under the bars so the data stays legible.
- drawTime: 'beforeDatasetsDraw',
- // 10% fill mirrors the cursor row; a stronger border marks the window edges.
- backgroundColor: hexToRGBA(primary, 0.1),
- borderColor: hexToRGBA(primary, 0.8),
- borderWidth: 1,
- }
+ annotations.highlightedRange = {
+ type: 'box',
+ xMin: highlightedRange.xMin,
+ xMax: highlightedRange.xMax,
+ // Drawn under the bars so the data stays legible.
+ drawTime: 'beforeDatasetsDraw',
+ // Faint fill with a stronger border to mark the window edges.
+ backgroundColor: hexToRGBA(HIGHLIGHT_COLOR, 0.1),
+ borderColor: hexToRGBA(HIGHLIGHT_COLOR, 0.8),
+ borderWidth: 1,
}
}
diff --git a/products/logs/frontend/components/LogsViewer/LogsViewerSparkline/index.tsx b/products/logs/frontend/components/LogsViewer/LogsViewerSparkline/index.tsx
index bdd1ff7ff56c..a6ee853a3d67 100644
--- a/products/logs/frontend/components/LogsViewer/LogsViewerSparkline/index.tsx
+++ b/products/logs/frontend/components/LogsViewer/LogsViewerSparkline/index.tsx
@@ -113,36 +113,19 @@ export function LogsSparkline({
const { visibleRowDateRange } = useValues(logsViewerLogic)
- // Map the visible-row date range onto bucket indices in `dates`. Buckets are
- // anchored at their start time; the date_to range edge belongs to the bucket
- // whose start is the last one <= date_to.
const highlightedRange = useMemo(() => {
if (!visibleRowDateRange || sparklineData.dates.length === 0) {
return null
}
- const fromMs = dayjs(visibleRowDateRange.date_from).valueOf()
- const toMs = dayjs(visibleRowDateRange.date_to).valueOf()
- let startIndex = -1
- let endIndex = -1
- for (let i = 0; i < sparklineData.dates.length; i++) {
- const bucketMs = dayjs(sparklineData.dates[i]).valueOf()
- if (bucketMs <= fromMs) {
- startIndex = i
- }
- if (bucketMs <= toMs) {
- endIndex = i
- } else {
- break
- }
- }
- // Fall back to the first bucket when the range starts before any bucket.
- if (startIndex === -1) {
- startIndex = 0
- }
- if (endIndex === -1 || endIndex < startIndex) {
+ const firstMs = dayjs(sparklineData.dates[0]).valueOf()
+ const lastMs = dayjs(sparklineData.dates[sparklineData.dates.length - 1]).valueOf()
+ const clamp = (ms: number): number => Math.min(Math.max(ms, firstMs), lastMs)
+ const xMin = clamp(dayjs(visibleRowDateRange.date_from).valueOf())
+ const xMax = clamp(dayjs(visibleRowDateRange.date_to).valueOf())
+ if (xMax <= xMin) {
return null
}
- return { startIndex, endIndex }
+ return { xMin, xMax }
}, [visibleRowDateRange, sparklineData.dates])
const onSelectionChange = useCallback(
diff --git a/products/tracing/frontend/TracingSparkline.tsx b/products/tracing/frontend/TracingSparkline.tsx
index b8f06919c46a..fcdc513cca4b 100644
--- a/products/tracing/frontend/TracingSparkline.tsx
+++ b/products/tracing/frontend/TracingSparkline.tsx
@@ -133,14 +133,17 @@ export function TracingSparkline({
if (!durationHistogram || !visibleRowDurationRange || durationHistogram.bucketsNs.length === 0) {
return null
}
- const { bucketsNs } = durationHistogram
+ const { bucketsNs, labels } = durationHistogram
// An edge missing from the axis can only mean it's outside the rendered range (the axis
// spans the data's min..max bucket), so clamp it to the nearest end.
const startIndexRaw = bucketsNs.indexOf(snapDurationToBucket(visibleRowDurationRange.minNs))
const endIndexRaw = bucketsNs.indexOf(snapDurationToBucket(visibleRowDurationRange.maxNs))
const startIndex = startIndexRaw === -1 ? 0 : startIndexRaw
const endIndex = endIndexRaw === -1 ? bucketsNs.length - 1 : endIndexRaw
- return startIndex <= endIndex ? { startIndex, endIndex } : null
+ if (startIndex > endIndex) {
+ return null
+ }
+ return { xMin: labels[startIndex], xMax: labels[endIndex + 1] ?? labels[endIndex] }
}, [visibleRowDurationRange, durationHistogram])
// Map the visible-row date range onto bucket indices in `dates`. Buckets are anchored at
@@ -171,8 +174,8 @@ export function TracingSparkline({
if (endIndex === -1 || endIndex < startIndex) {
return null
}
- return { startIndex, endIndex }
- }, [compare, visibleRowDateRange, sparklineData.dates])
+ return { xMin: sparklineLabels[startIndex], xMax: sparklineLabels[endIndex + 1] ?? sparklineLabels[endIndex] }
+ }, [compare, visibleRowDateRange, sparklineData.dates, sparklineLabels])
const onSelectionChange = useCallback(
(selection: { startIndex: number; endIndex: number }): void => {