Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
insmac committed Dec 2, 2024
1 parent 9b16745 commit bc507c8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 39 deletions.
42 changes: 3 additions & 39 deletions packages/web-console/src/scenes/Editor/Metrics/metric.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import React, {
import { Metric as MetricItem } from "../../../store/buffers"
import {
durationInMinutes,
graphDataConfigs,
MetricDuration,
MetricType,
Latency,
Expand Down Expand Up @@ -54,43 +55,6 @@ const sqlValueToFixed = (value: string, decimals: number = 2) => {
return Number(parsed.toFixed(decimals)) as unknown as number
}

const graphDataConfigs = {
[MetricType.LATENCY]: {
getData: (latency: Latency[]): uPlot.AlignedData => [
latency.map((l) => new Date(l.time).getTime()),
latency.map((l) => sqlValueToFixed(l.avg_latency)),
],
yValue: (rawValue: number) => {
if (rawValue >= 1000) {
const seconds = rawValue / 1000
return `${seconds.toFixed(2)} s`
}
return `${rawValue} ms`
},
},
[MetricType.ROWS_APPLIED]: {
getData: (rowsApplied: RowsApplied[]): uPlot.AlignedData => [
rowsApplied.map((l) => new Date(l.time).getTime()),
rowsApplied.map((l) => sqlValueToFixed(l.numOfRowsApplied)),
],
yValue: (rawValue: number) => {
if (rawValue >= 1e6) {
return (rawValue / 1e6).toFixed(1).replace(/\.0$/, "") + " M"
} else if (rawValue >= 1e3) {
return (rawValue / 1e3).toFixed(1).replace(/\.0$/, "") + " k"
}
return rawValue.toString()
},
},
[MetricType.WRITE_AMPLIFICATION]: {
getData: (rowsApplied: RowsApplied[]): uPlot.AlignedData => [
rowsApplied.map((l) => new Date(l.time).getTime()),
rowsApplied.map((l) => sqlValueToFixed(l.avgWalAmplification)),
],
yValue: (rawValue: number) => `${rawValue} x`,
},
}

export const Metric = ({
metric,
metricDuration,
Expand Down Expand Up @@ -173,7 +137,7 @@ export const Metric = ({
])

if (responses[0] && responses[0].type === QuestDB.Type.DQL) {
const alignedData = graphDataConfigs[metric.metricType].getData(
const alignedData = graphDataConfigs[metric.metricType].alignData(
responses[0].data as any,
)
setData(alignedData)
Expand Down Expand Up @@ -266,7 +230,7 @@ export const Metric = ({
loading={loading}
duration={metricDuration}
label={metricTypeLabel[metric.metricType]}
yValue={graphDataConfigs[metric.metricType].yValue}
yValue={graphDataConfigs[metric.metricType].mapYValue}
beforeLabel={
<TableSelector
loading={loading}
Expand Down
43 changes: 43 additions & 0 deletions packages/web-console/src/scenes/Editor/Metrics/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { utcToLocal } from "../../../utils/dateTime"
import uPlot from "uplot"

export enum MetricType {
ROWS_APPLIED = "Rows applied",
Expand Down Expand Up @@ -88,3 +89,45 @@ export const xAxisFormat = {
[MetricDuration.SEVEN_DAYS]: (rawValue: number) =>
utcToLocal(rawValue, "dd/MM"),
}

const sqlValueToFixed = (value: string, decimals: number = 2) => {
const parsed = parseFloat(value)
return Number(parsed.toFixed(decimals)) as unknown as number
}

export const graphDataConfigs = {
[MetricType.LATENCY]: {
alignData: (latency: Latency[]): uPlot.AlignedData => [
latency.map((l) => new Date(l.time).getTime()),
latency.map((l) => sqlValueToFixed(l.avg_latency)),
],
mapYValue: (rawValue: number) => {
if (rawValue >= 1000) {
const seconds = rawValue / 1000
return `${seconds.toFixed(2)} s`
}
return `${rawValue} ms`
},
},
[MetricType.ROWS_APPLIED]: {
alignData: (rowsApplied: RowsApplied[]): uPlot.AlignedData => [
rowsApplied.map((l) => new Date(l.time).getTime()),
rowsApplied.map((l) => sqlValueToFixed(l.numOfRowsApplied)),
],
mapYValue: (rawValue: number) => {
if (rawValue >= 1e6) {
return (rawValue / 1e6).toFixed(1).replace(/\.0$/, "") + " M"
} else if (rawValue >= 1e3) {
return (rawValue / 1e3).toFixed(1).replace(/\.0$/, "") + " k"
}
return rawValue.toString()
},
},
[MetricType.WRITE_AMPLIFICATION]: {
alignData: (rowsApplied: RowsApplied[]): uPlot.AlignedData => [
rowsApplied.map((l) => new Date(l.time).getTime()),
rowsApplied.map((l) => sqlValueToFixed(l.avgWalAmplification)),
],
mapYValue: (rawValue: number) => `${rawValue} x`,
},
}

0 comments on commit bc507c8

Please sign in to comment.