Skip to content

Commit

Permalink
Remove sample by selector, use automated sampling at all times
Browse files Browse the repository at this point in the history
  • Loading branch information
insmac committed Dec 18, 2024
1 parent 844d222 commit b78e4ad
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 109 deletions.
52 changes: 3 additions & 49 deletions packages/web-console/src/scenes/Editor/Metrics/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import {
RefreshRate,
autoRefreshRates,
refreshRatesInSeconds,
defaultSampleByForDuration,
getRollingAppendRowLimit,
MetricViewMode,
FetchMode,
SampleBy,
durationInMinutes,
MetricsRefreshPayload,
} from "./utils"
Expand All @@ -23,7 +21,6 @@ import {
Refresh,
World,
} from "@styled-icons/boxicons-regular"
import { Soundwave } from "@styled-icons/bootstrap"
import { AddMetricDialog } from "./add-metric-dialog"
import type { Metric } from "../../../store/buffers"
import { Metric as MetricComponent } from "./metric"
Expand Down Expand Up @@ -109,21 +106,13 @@ const formatRefreshRateLabel = (
return rate
}

const formatSampleByLabel = (sampleBy: SampleBy, duration: MetricDuration) => {
if (sampleBy === SampleBy.AUTO) {
return `${SampleBy.AUTO} (${defaultSampleByForDuration[duration]})`
}
return sampleBy
}

export const Metrics = () => {
const { activeBuffer, updateBuffer, buffers } = useEditor()
const [metricDuration, setMetricDuration] = useState<MetricDuration>()
const [metricViewMode, setMetricViewMode] = useState<MetricViewMode>(
MetricViewMode.GRID,
)
const [refreshRate, setRefreshRate] = useState<RefreshRate>()
const [sampleBy, setSampleBy] = useState<SampleBy>()
const [dialogOpen, setDialogOpen] = useState(false)
const [metrics, setMetrics] = useState<Metric[]>([])
const telemetryConfig = useSelector(selectors.telemetry.getConfig)
Expand All @@ -148,9 +137,7 @@ export const Metrics = () => {

const rollingAppendLimit = getRollingAppendRowLimit(
refreshRateInSec,
sampleBy && sampleBy !== SampleBy.AUTO
? sampleBy
: defaultSampleByForDuration[duration],
duration,
)

const updateMetrics = (metrics: Metric[]) => {
Expand Down Expand Up @@ -244,19 +231,12 @@ export const Metrics = () => {
const metricDuration = buffer?.metricsViewState?.metricDuration
const refreshRate = buffer?.metricsViewState?.refreshRate
const metricViewMode = buffer?.metricsViewState?.viewMode
const sampleBy = buffer?.metricsViewState?.sampleBy

if (metrics) {
setMetrics(metrics)
}
if (metricDuration) {
setMetricDuration(metricDuration)
if (!sampleBy) {
setSampleBy(defaultSampleByForDuration[metricDuration])
}
}
if (sampleBy) {
setSampleBy(sampleBy)
}
if (refreshRate) {
setRefreshRate(refreshRate)
Expand All @@ -280,9 +260,6 @@ export const Metrics = () => {
...(metricViewMode !== buffer?.metricsViewState?.viewMode && {
viewMode: metricViewMode,
}),
...(sampleBy !== buffer?.metricsViewState?.sampleBy && {
sampleBy,
}),
},
})
if (metricDuration) {
Expand All @@ -291,13 +268,13 @@ export const Metrics = () => {
setupListeners()
}
}
if (metricDuration && refreshRate && metricViewMode && sampleBy) {
if (metricDuration && refreshRate && metricViewMode) {
updateBuffer(buffer.id, merged)
setFetchMode(FetchMode.OVERWRITE)
refreshMetricsData()
}
}
}, [metricDuration, refreshRate, metricViewMode, sampleBy])
}, [metricDuration, refreshRate, metricViewMode])

useEffect(() => {
if (refreshRate) {
Expand Down Expand Up @@ -360,24 +337,6 @@ export const Metrics = () => {
<World size="14px" />
<Text color="foreground">{getLocalTimeZone()}</Text>
</Box>
<Box gap="0.5rem" style={{ flexShrink: 0 }}>
<IconWithTooltip
icon={
<Select
name="sample_by"
value={sampleBy}
options={Object.values(SampleBy).map((rate) => ({
label: formatSampleByLabel(rate, duration),
value: rate,
}))}
prefixIcon={<Soundwave size="18px" />}
onChange={(e) => setSampleBy(e.target.value as SampleBy)}
/>
}
tooltip="Data sample rate"
placement="bottom"
/>
</Box>
<Box gap="0.5rem" style={{ flexShrink: 0 }}>
<IconWithTooltip
icon={
Expand Down Expand Up @@ -471,11 +430,6 @@ export const Metrics = () => {
key={index}
metric={metric}
metricDuration={duration}
sampleBy={
sampleBy && sampleBy !== SampleBy.AUTO
? sampleBy
: defaultSampleByForDuration[duration]
}
onRemove={handleRemoveMetric}
onTableChange={handleTableChange}
onColorChange={handleColorChange}
Expand Down
8 changes: 2 additions & 6 deletions packages/web-console/src/scenes/Editor/Metrics/metric.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import {
hasData,
FetchMode,
mergeRollingData,
SampleBy,
getTimeFilter,
MetricsRefreshPayload,
metricDurationToDate,
getSamplingRateForPeriod,
} from "./utils"
import { widgets } from "./widgets"
import { QuestContext } from "../../../providers"
Expand Down Expand Up @@ -53,7 +52,6 @@ export const Metric = ({
onMetricDurationChange,
fetchMode,
rollingAppendLimit,
sampleBy,
}: {
metric: MetricItem
metricDuration: MetricDuration
Expand All @@ -63,7 +61,6 @@ export const Metric = ({
onMetricDurationChange: (duration: MetricDuration) => void
fetchMode: FetchMode
rollingAppendLimit: number
sampleBy: SampleBy
}) => {
const { quest } = useContext(QuestContext)
const [loading, setLoading] = useState(metric.tableId !== undefined)
Expand Down Expand Up @@ -106,8 +103,7 @@ export const Metric = ({
quest.query<ResultType[MetricType]>(
widgetConfig.getQuery({
tableId: metric.tableId,
metricDuration,
sampleBy,
sampleBy: `${getSamplingRateForPeriod(subtracted, dateTo)}s`,
timeFilter,
...(isRollingAppendEnabled && {
limit: -rollingAppendLimit,
Expand Down
49 changes: 18 additions & 31 deletions packages/web-console/src/scenes/Editor/Metrics/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ export type Widget = {
isTableMetric: boolean
getQuery: ({
tableId,
metricDuration,
sampleBy,
limit,
timeFilter,
}: {
tableId?: number
metricDuration: MetricDuration
sampleBy?: SampleBy
sampleBy: string
limit?: number
timeFilter?: string
}) => string
Expand Down Expand Up @@ -114,32 +113,6 @@ export const durationInMinutes: Record<MetricDuration, number> = {
[MetricDuration.SEVEN_DAYS]: 60 * 168,
}

export const defaultSampleByForDuration: Record<
MetricDuration,
Exclude<SampleBy, SampleBy.AUTO>
> = {
[MetricDuration.FIVE_MINUTES]: SampleBy.ONE_SECOND,
[MetricDuration.FIFTEEN_MINUTES]: SampleBy.ONE_SECOND,
[MetricDuration.ONE_HOUR]: SampleBy.ONE_SECOND,
[MetricDuration.THREE_HOURS]: SampleBy.ONE_MINUTE,
[MetricDuration.SIX_HOURS]: SampleBy.ONE_MINUTE,
[MetricDuration.TWELVE_HOURS]: SampleBy.ONE_MINUTE,
[MetricDuration.TWENTY_FOUR_HOURS]: SampleBy.FIVE_MINUTES,
[MetricDuration.THREE_DAYS]: SampleBy.FIFTEEN_MINUTES,
[MetricDuration.SEVEN_DAYS]: SampleBy.FIFTEEN_MINUTES,
}

export const sampleByInSeconds: Record<
Exclude<SampleBy, SampleBy.AUTO>,
number
> = {
[SampleBy.ONE_SECOND]: 1,
[SampleBy.ONE_MINUTE]: 60,
[SampleBy.FIVE_MINUTES]: 60 * 5,
[SampleBy.FIFTEEN_MINUTES]: 60 * 15,
[SampleBy.ONE_HOUR]: 60 * 60,
}

export type CommitRate = {
created: string
commit_rate: string
Expand Down Expand Up @@ -233,10 +206,24 @@ export const getTimeFilter = (from: Date | string, to: Date | string) => {
return `FROM '${formatToISOIfNeeded(from)}' TO '${formatToISOIfNeeded(to)}'`
}

export const getSamplingRateForPeriod = (
from: Date,
to: Date,
pointsToPlot = 600,
) => {
const seconds = (to.getTime() - from.getTime()) / 1000
return Math.ceil(seconds / pointsToPlot)
}

export const getRollingAppendRowLimit = (
refreshRateInSeconds: number,
sampleBy: Exclude<SampleBy, SampleBy.AUTO>,
) => Math.ceil(refreshRateInSeconds / sampleByInSeconds[sampleBy])
duration: MetricDuration,
) => {
const dateNow = new Date()
const subtracted = subMinutes(dateNow, durationInMinutes[duration])
const sampleRate = getSamplingRateForPeriod(subtracted, dateNow)
return Math.ceil(refreshRateInSeconds / sampleRate)
}

export const hasData = (data?: uPlot.AlignedData) => {
if (!data || data[1].length === 0) return false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import uPlot from "uplot"
import type { Widget } from "../utils"
import { sqlValueToFixed, formatNumbers } from "../utils"
import { CommitRate, defaultSampleByForDuration } from "../utils"
import { CommitRate } from "../utils"
import { TelemetryTable } from "../../../../consts"

export const commitRate: Widget = {
label: "Commit rate",
iconUrl: "/assets/metric-commit-rate.svg",
isTableMetric: true,
querySupportsRollingAppend: true,
getQuery: ({ tableId, metricDuration, sampleBy, limit, timeFilter }) => {
getQuery: ({ tableId, sampleBy, limit, timeFilter }) => {
return `
select
created,
Expand All @@ -33,7 +33,7 @@ export const commitRate: Widget = {
event = 103
-- it is important this is 1s, should this value change
-- the "commit_rate" value will have to be adjusted to rate/s
sample by ${sampleBy ?? defaultSampleByForDuration[metricDuration]}
sample by ${sampleBy}
${timeFilter ? timeFilter : ""}
fill(0)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import uPlot from "uplot"
import type { Widget } from "../utils"
import { Latency, defaultSampleByForDuration, sqlValueToFixed } from "../utils"
import { Latency, sqlValueToFixed } from "../utils"
import { TelemetryTable } from "../../../../consts"

export const latency: Widget = {
label: "WAL apply latency in ms",
iconUrl: "/assets/metric-read-latency.svg",
isTableMetric: true,
querySupportsRollingAppend: true,
getQuery: ({ tableId, metricDuration, sampleBy, limit, timeFilter }) => {
getQuery: ({ tableId, sampleBy, limit, timeFilter }) => {
return `
select created, approx_percentile(latency, 0.9, 3) latency
from ${TelemetryTable.WAL}
where
event = 105 -- event is fixed
and rowCount > 0 -- this is fixed clause, we have rows with - rowCount logged
${tableId ? `and tableId = ${tableId}` : ""}
sample by ${sampleBy ?? defaultSampleByForDuration[metricDuration]}
sample by ${sampleBy}
${timeFilter ? timeFilter : ""}
fill(0)
${limit ? `limit ${limit}` : ""}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import uPlot from "uplot"
import type { Widget } from "../utils"
import { WriteAmplification } from "../utils"
import {
defaultSampleByForDuration,
sqlValueToFixed,
formatNumbers,
} from "../utils"
import { sqlValueToFixed, formatNumbers } from "../utils"
import { TelemetryTable } from "../../../../consts"

export const writeAmplification: Widget = {
label: "Write amplification",
iconUrl: "/assets/metric-write-amplification.svg",
isTableMetric: true,
querySupportsRollingAppend: true,
getQuery: ({ tableId, metricDuration, sampleBy, limit, timeFilter }) => {
getQuery: ({ tableId, sampleBy, limit, timeFilter }) => {
return `
select
created,
Expand All @@ -33,7 +29,7 @@ from (
where ${tableId ? `tableId = ${tableId} and ` : ""}
event = 105
and rowCount > 0 -- this is fixed clause, we have rows with - rowCount logged
sample by ${sampleBy ?? defaultSampleByForDuration[metricDuration]}
sample by ${sampleBy}
${timeFilter ? timeFilter : ""}
-- fill with null to avoid spurious values and division by 0
fill(null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import uPlot from "uplot"
import type { Widget } from "../utils"
import {
RowsApplied,
defaultSampleByForDuration,
sqlValueToFixed,
formatNumbers,
} from "../utils"
import { RowsApplied, sqlValueToFixed, formatNumbers } from "../utils"
import { TelemetryTable } from "../../../../consts"

export const writeThroughput: Widget = {
label: "Write throughput",
iconUrl: "/assets/metric-rows-applied.svg",
isTableMetric: true,
querySupportsRollingAppend: true,
getQuery: ({ tableId, metricDuration, sampleBy, limit, timeFilter }) => {
getQuery: ({ tableId, sampleBy, limit, timeFilter }) => {
return `
select
created time,
Expand All @@ -23,7 +18,7 @@ select
from ${TelemetryTable.WAL}
where ${tableId ? `tableId = ${tableId} and ` : ""}
event = 105
sample by ${sampleBy ?? defaultSampleByForDuration[metricDuration]}
sample by ${sampleBy}
${timeFilter ? timeFilter : ""}
fill(null)
${limit ? `limit ${limit}` : ""}`
Expand Down
2 changes: 0 additions & 2 deletions packages/web-console/src/scenes/Schema/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ import {
MetricDuration,
MetricViewMode,
RefreshRate,
SampleBy,
} from "../../scenes/Editor/Metrics/utils"

type Props = Readonly<{
Expand Down Expand Up @@ -284,7 +283,6 @@ const Schema = ({
metricDuration: MetricDuration.ONE_HOUR,
refreshRate: RefreshRate.AUTO,
viewMode: MetricViewMode.GRID,
sampleBy: SampleBy.AUTO,
},
})
}
Expand Down

0 comments on commit b78e4ad

Please sign in to comment.