Skip to content

Commit

Permalink
Refactor series color resolution, fix nits
Browse files Browse the repository at this point in the history
  • Loading branch information
Bluefinger committed Dec 10, 2024
1 parent ac3ee18 commit 60973ef
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import React from 'react';
import { EuiFlexItem, useEuiTheme } from '@elastic/eui';
import { EuiFlexItem } from '@elastic/eui';
import type { DataView } from '@kbn/data-views-plugin/public';
import type { Filter, Query, TimeRange } from '@kbn/es-query';
import { Kpi } from './kpi';
Expand Down Expand Up @@ -79,10 +79,8 @@ const DockerKpiCharts = ({
searchSessionId,
loading = false,
}: ContainerKpiChartsProps) => {
const { euiTheme } = useEuiTheme();
const charts = useDockerContainerKpiCharts({
dataViewId: dataView?.id,
seriesColor: euiTheme.colors.backgroundLightText,
});

return (
Expand Down Expand Up @@ -112,10 +110,8 @@ const KubernetesKpiCharts = ({
searchSessionId,
loading = false,
}: ContainerKpiChartsProps) => {
const { euiTheme } = useEuiTheme();
const charts = useK8sContainerKpiCharts({
dataViewId: dataView?.id,
seriesColor: euiTheme.colors.backgroundLightText,
});

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import React from 'react';
import { EuiFlexItem, useEuiTheme } from '@elastic/eui';
import { EuiFlexItem } from '@elastic/eui';
import type { DataView } from '@kbn/data-views-plugin/public';
import type { Filter, Query, TimeRange } from '@kbn/es-query';
import { Kpi } from './kpi';
Expand All @@ -31,11 +31,9 @@ export const HostKpiCharts = ({
searchSessionId,
loading = false,
}: HostKpiChartsProps) => {
const { euiTheme } = useEuiTheme();
const charts = useHostKpiCharts({
dataViewId: dataView?.id,
getSubtitle,
seriesColor: euiTheme.colors.backgroundLightText,
});

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { useEuiTheme } from '@elastic/eui';
import { renderHook } from '@testing-library/react';
import { useChartSeriesColor } from './use_chart_series_color';

describe('useChartSeriesColor', () => {
let seriesDefaultColor: string;

beforeEach(() => {
const { result } = renderHook(() => useEuiTheme());

// Don't try to test a hardcoded value, just use what is provided by EUI.
// If in the future this value changes, the tests won't break.
seriesDefaultColor = result.current.euiTheme.colors.backgroundLightText;
});

it('returns a default color value if given no input', () => {
const { result } = renderHook(() => useChartSeriesColor());

expect(result.current).not.toBe('');
expect(result.current).toBe(seriesDefaultColor);
});

it('returns a default color value if given an empty string', () => {
const { result } = renderHook(() => useChartSeriesColor(''));

expect(result.current).not.toBe('');
expect(result.current).toBe(seriesDefaultColor);
});

it('returns the provided color input', () => {
const { result } = renderHook(() => useChartSeriesColor('#fff'));

expect(result.current).not.toBe(seriesDefaultColor);
expect(result.current).toBe('#fff');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { useEuiTheme } from '@elastic/eui';

/**
* Provides either the input color, or yields the default EUI theme
* color for use as the KPI chart series color.
* @param seriesColor A user-defined color value
* @returns Either the input `seriesColor` or the default color from EUI
*/
export const useChartSeriesColor = (seriesColor?: string): string => {
const { euiTheme } = useEuiTheme();

// Prevent empty string being used as a valid color
return seriesColor || euiTheme.colors.backgroundLightText;
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { i18n } from '@kbn/i18n';
import { findInventoryModel } from '@kbn/metrics-data-access-plugin/common';
import useAsync from 'react-use/lib/useAsync';
import { ContainerMetricTypes } from '../charts/types';
import { useChartSeriesColor } from './use_chart_series_color';

const getSubtitleFromFormula = (value: string) =>
value.startsWith('max')
Expand Down Expand Up @@ -106,6 +107,8 @@ export const useDockerContainerKpiCharts = ({
dataViewId?: string;
seriesColor?: string;
}) => {
seriesColor = useChartSeriesColor(seriesColor);

const { value: charts = [] } = useAsync(async () => {
const model = findInventoryModel('container');
const { cpu, memory } = await model.metrics.getCharts();
Expand Down Expand Up @@ -134,6 +137,8 @@ export const useK8sContainerKpiCharts = ({
dataViewId?: string;
seriesColor?: string;
}) => {
seriesColor = useChartSeriesColor(seriesColor);

const { value: charts = [] } = useAsync(async () => {
const model = findInventoryModel('container');
const { cpu, memory } = await model.metrics.getCharts();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { findInventoryModel } from '@kbn/metrics-data-access-plugin/common';
import { useMemo } from 'react';
import useAsync from 'react-use/lib/useAsync';
import { HostMetricTypes } from '../charts/types';
import { useChartSeriesColor } from './use_chart_series_color';

export const useHostCharts = ({
metric,
Expand Down Expand Up @@ -87,6 +88,8 @@ export const useHostKpiCharts = ({
seriesColor?: string;
getSubtitle?: (formulaValue: string) => string;
}) => {
seriesColor = useChartSeriesColor(seriesColor);

const { value: charts = [] } = useAsync(async () => {
const model = findInventoryModel('host');
const { cpu, memory, disk } = await model.metrics.getCharts();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { i18n } from '@kbn/i18n';
import { useMemo } from 'react';
import { LensConfig } from '@kbn/lens-embeddable-utils/config_builder';
import { useChartSeriesColor } from './use_chart_series_color';

const LOG_RATE = i18n.translate('xpack.infra.assetDetails.charts.logRate', {
defaultMessage: 'Log Rate',
Expand All @@ -17,7 +18,7 @@ const LOG_ERROR_RATE = i18n.translate('xpack.infra.assetDetails.charts.logErrorR
defaultMessage: 'Log Error Rate',
});

const logMetric: LensConfig & { id: string } = {
const logRateMetric: LensConfig & { id: string } = {
id: 'logMetric',
chartType: 'metric',
title: LOG_RATE,
Expand All @@ -29,7 +30,7 @@ const logMetric: LensConfig & { id: string } = {
normalizeByUnit: 's',
};

const logErrorMetric: LensConfig & { id: string } = {
const logErrorRateMetric: LensConfig & { id: string } = {
id: 'logErrorMetric',
chartType: 'metric',
title: LOG_ERROR_RATE,
Expand All @@ -49,6 +50,8 @@ export const useLogsCharts = ({
dataViewId?: string;
seriesColor?: string;
}) => {
seriesColor = useChartSeriesColor(seriesColor);

return useMemo(() => {
const dataset = dataViewId && {
dataset: {
Expand All @@ -59,12 +62,12 @@ export const useLogsCharts = ({
return {
charts: [
{
...logMetric,
...logRateMetric,
...dataset,
seriesColor,
},
{
...logErrorMetric,
...logErrorRateMetric,
...dataset,
seriesColor,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ const useTabs = (tabs: Tab[]) => {
const tabEntries: TabItem[] = useMemo(
() =>
tabs
.filter(isTabEnabled)
.filter(hasMetricsTab)
.filter((tab) => isTabEnabled(tab) && hasMetricsTab(tab))
.map(({ name, ...tab }) => {
return {
...tab,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import React, { useMemo } from 'react';
import type { DataView } from '@kbn/data-views-plugin/public';
import type { TimeRange } from '@kbn/es-query';
import { EuiFlexGroup, EuiFlexItem, useEuiTheme } from '@elastic/eui';
import { EuiFlexGroup, EuiFlexItem } from '@elastic/eui';
import {
findInventoryFields,
type InventoryItemType,
Expand Down Expand Up @@ -38,10 +38,8 @@ export const LogsContent = ({ assetId, assetType, dataView, dateRange }: Props)
];
}, [dataView, assetId, assetType]);

const { euiTheme } = useEuiTheme();
const { charts } = useLogsCharts({
dataViewId: dataView?.id,
seriesColor: euiTheme.colors.backgroundLightText,
});

return (
Expand Down

0 comments on commit 60973ef

Please sign in to comment.