From e889dfdc5caab807f9a6c830df88ccc1d8d5eb58 Mon Sep 17 00:00:00 2001 From: Kevin Yu Date: Mon, 20 Jun 2022 06:28:29 -0700 Subject: [PATCH] Cloud Monitoring: Update GroupBy fields to use experimental UI components (#50541) * Cloud Monitoring: Update GroupBy fields to use experimental UI components * let group by field grow horizontally * remove fixed width constants from inputs * add test * Cloud Monitoring: Update GraphPeriod to use experimental UI components (#50545) * Cloud Monitoring: Update GraphPeriod to use experimental UI components * Cloud Monitoring: Update Preprocessing to use experimental UI components (#50548) * Cloud Monitoring: Update Preprocessing to use experimental UI components * add tests * make overrides optional * move preprocessor back into its own row --- package.json | 2 +- .../cloudMonitoringMetricDescriptor.ts | 15 +++ .../Experimental/Aggregation.test.tsx | 65 +++++++++++++ .../components/Experimental/Aggregation.tsx | 68 +++++++++++++ .../Experimental/GraphPeriod.test.tsx | 39 ++++++++ .../components/Experimental/GraphPeriod.tsx | 49 ++++++++++ .../components/Experimental/GroupBy.test.tsx | 42 ++++++++ .../components/Experimental/GroupBy.tsx | 64 +++++++++++++ .../Experimental/MetricQueryEditor.tsx | 2 +- .../Experimental/Preprocessor.test.tsx | 95 +++++++++++++++++++ .../components/Experimental/Preprocessor.tsx | 69 ++++++++++++++ .../Experimental/VisualMetricQueryEditor.tsx | 4 +- yarn.lock | 10 +- 13 files changed, 516 insertions(+), 8 deletions(-) create mode 100644 public/app/plugins/datasource/cloud-monitoring/__mocks__/cloudMonitoringMetricDescriptor.ts create mode 100644 public/app/plugins/datasource/cloud-monitoring/components/Experimental/Aggregation.test.tsx create mode 100644 public/app/plugins/datasource/cloud-monitoring/components/Experimental/Aggregation.tsx create mode 100644 public/app/plugins/datasource/cloud-monitoring/components/Experimental/GraphPeriod.test.tsx create mode 100644 public/app/plugins/datasource/cloud-monitoring/components/Experimental/GraphPeriod.tsx create mode 100644 public/app/plugins/datasource/cloud-monitoring/components/Experimental/GroupBy.test.tsx create mode 100644 public/app/plugins/datasource/cloud-monitoring/components/Experimental/GroupBy.tsx create mode 100644 public/app/plugins/datasource/cloud-monitoring/components/Experimental/Preprocessor.test.tsx create mode 100644 public/app/plugins/datasource/cloud-monitoring/components/Experimental/Preprocessor.tsx diff --git a/package.json b/package.json index 6f4bf821d6d..3b26cf81093 100644 --- a/package.json +++ b/package.json @@ -252,7 +252,7 @@ "@grafana/aws-sdk": "0.0.36", "@grafana/data": "workspace:*", "@grafana/e2e-selectors": "workspace:*", - "@grafana/experimental": "^0.0.2-canary.30", + "@grafana/experimental": "^0.0.2-canary.32", "@grafana/google-sdk": "0.0.3", "@grafana/lezer-logql": "^0.0.12", "@grafana/runtime": "workspace:*", diff --git a/public/app/plugins/datasource/cloud-monitoring/__mocks__/cloudMonitoringMetricDescriptor.ts b/public/app/plugins/datasource/cloud-monitoring/__mocks__/cloudMonitoringMetricDescriptor.ts new file mode 100644 index 00000000000..3cb1069169c --- /dev/null +++ b/public/app/plugins/datasource/cloud-monitoring/__mocks__/cloudMonitoringMetricDescriptor.ts @@ -0,0 +1,15 @@ +import { MetricDescriptor, MetricKind, ValueTypes } from '../types'; + +export const createMockMetricDescriptor = (overrides?: Partial): MetricDescriptor => { + return { + metricKind: MetricKind.CUMULATIVE, + valueType: ValueTypes.DOUBLE, + type: 'type', + unit: 'unit', + service: 'service', + serviceShortName: 'srv', + displayName: 'displayName', + description: 'description', + ...overrides, + }; +}; diff --git a/public/app/plugins/datasource/cloud-monitoring/components/Experimental/Aggregation.test.tsx b/public/app/plugins/datasource/cloud-monitoring/components/Experimental/Aggregation.test.tsx new file mode 100644 index 00000000000..4544201c624 --- /dev/null +++ b/public/app/plugins/datasource/cloud-monitoring/components/Experimental/Aggregation.test.tsx @@ -0,0 +1,65 @@ +import { render, screen } from '@testing-library/react'; +import React from 'react'; +import { openMenu } from 'react-select-event'; +import { TemplateSrvStub } from 'test/specs/helpers'; + +import { ValueTypes, MetricKind } from '../../types'; + +import { Aggregation, Props } from './Aggregation'; + +const props: Props = { + onChange: () => {}, + // @ts-ignore + templateSrv: new TemplateSrvStub(), + metricDescriptor: { + valueType: '', + metricKind: '', + } as any, + crossSeriesReducer: '', + groupBys: [], + templateVariableOptions: [], +}; + +describe('Aggregation', () => { + it('renders correctly', () => { + render(); + expect(screen.getByTestId('cloud-monitoring-aggregation')).toBeInTheDocument(); + }); + + describe('options', () => { + describe('when DOUBLE and GAUGE is passed as props', () => { + const nextProps = { + ...props, + metricDescriptor: { + valueType: ValueTypes.DOUBLE, + metricKind: MetricKind.GAUGE, + } as any, + }; + + it('should not have the reduce values', () => { + render(); + const label = screen.getByLabelText('Group by function'); + openMenu(label); + expect(screen.queryByText('count true')).not.toBeInTheDocument(); + expect(screen.queryByText('count false')).not.toBeInTheDocument(); + }); + }); + + describe('when MONEY and CUMULATIVE is passed as props', () => { + const nextProps = { + ...props, + metricDescriptor: { + valueType: ValueTypes.MONEY, + metricKind: MetricKind.CUMULATIVE, + } as any, + }; + + it('should have the reduce values', () => { + render(); + const label = screen.getByLabelText('Group by function'); + openMenu(label); + expect(screen.getByText('none')).toBeInTheDocument(); + }); + }); + }); +}); diff --git a/public/app/plugins/datasource/cloud-monitoring/components/Experimental/Aggregation.tsx b/public/app/plugins/datasource/cloud-monitoring/components/Experimental/Aggregation.tsx new file mode 100644 index 00000000000..6b50d22b735 --- /dev/null +++ b/public/app/plugins/datasource/cloud-monitoring/components/Experimental/Aggregation.tsx @@ -0,0 +1,68 @@ +import React, { FC, useMemo } from 'react'; + +import { SelectableValue } from '@grafana/data'; +import { EditorField } from '@grafana/experimental'; +import { Select } from '@grafana/ui'; + +import { getAggregationOptionsByMetric } from '../../functions'; +import { MetricDescriptor, MetricKind, ValueTypes } from '../../types'; + +export interface Props { + refId: string; + onChange: (metricDescriptor: string) => void; + metricDescriptor?: MetricDescriptor; + crossSeriesReducer: string; + groupBys: string[]; + templateVariableOptions: Array>; +} + +export const Aggregation: FC = (props) => { + const aggOptions = useAggregationOptionsByMetric(props); + const selected = useSelectedFromOptions(aggOptions, props); + + return ( + +