Skip to content

Commit

Permalink
feat(metrics-chart): add nullValue config
Browse files Browse the repository at this point in the history
  • Loading branch information
shhdgit committed Jul 19, 2022
1 parent 502174f commit 7ccb9a8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import {
processRawData,
PromMatrixData,
QueryOptions,
resolveQueryTemplate
resolveQueryTemplate,
TransformNullValue
} from '@lib/utils/prometheus'
import { AxiosPromise } from 'axios'
import { ReqConfig } from '@lib/types'
Expand Down Expand Up @@ -96,6 +97,7 @@ export interface IMetricChartProps {
queries: IQueryOption[]
unit?: string
type: GraphType
nullValue?: TransformNullValue

height?: number

Expand Down Expand Up @@ -126,7 +128,8 @@ export default function MetricChart({
height = 200,
onRangeChange,
onLoadingStateChange,
getMetrics
getMetrics,
nullValue = TransformNullValue.NULL
}: IMetricChartProps) {
const chartRef = useRef<Chart>(null)
const chartContainerRef = useRef<HTMLDivElement>(null)
Expand Down Expand Up @@ -204,10 +207,23 @@ export default function MetricChart({
if (data === null) {
return
}

// transform data according to nullValue config
const transformedData =
nullValue === TransformNullValue.AS_ZERO
? data.map((d) => {
if (d[1] !== null) {
return d
}
d[1] = 0
return d
})
: data

sd.push({
id: `${queryIdx}_${seriesIdx}`,
name: format(queries[queryIdx].name, promResult.metric),
data,
data: transformedData,
color: queries[queryIdx].color
})
})
Expand Down
6 changes: 3 additions & 3 deletions ui/packages/tidb-dashboard-lib/src/utils/prometheus/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ export function processRawData(
let dpValue: number | null = parseSampleValue(value[1])

if (isNaN(dpValue)) {
dpValue = 0
dpValue = null
}

const timestamp = value[0] * 1000
for (let t = baseTimestamp; t < timestamp; t += stepMs) {
dps.push([t, 0])
dps.push([t, null])
}
baseTimestamp = timestamp + stepMs
dps.push([timestamp, dpValue])
}

const endTimestamp = options.end * 1000
for (let t = baseTimestamp; t <= endTimestamp; t += stepMs) {
dps.push([t, 0])
dps.push([t, null])
}

return dps
Expand Down
5 changes: 5 additions & 0 deletions ui/packages/tidb-dashboard-lib/src/utils/prometheus/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ export type MatrixOrVectorResult =
| PromMatrixData['result'][0]
| PromVectorData['result'][0]

export enum TransformNullValue {
NULL = 'null',
AS_ZERO = 'as_zero'
}

// Our customized types

export interface QueryOptions {
Expand Down

0 comments on commit 7ccb9a8

Please sign in to comment.