-
Notifications
You must be signed in to change notification settings - Fork 670
Replace plotly line chart with pf-react area chart #1448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
openshift-merge-robot
merged 1 commit into
openshift:master-next
from
TheRealJon:console-1365
May 8, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import * as _ from 'lodash-es'; | ||
| import * as React from 'react'; | ||
| import { Chart, ChartArea, ChartVoronoiContainer, ChartAxis, ChartTheme, ChartGroup } from '@patternfly/react-charts'; | ||
|
|
||
| import { humanizeNumber } from '../utils'; | ||
| import { twentyFourHourTime } from '../utils/datetime'; | ||
| import { areaStyles, cartesianChartStyles } from './themes'; | ||
| import { useRefWidth } from '../utils/use-ref-width'; | ||
| import { usePrometheusPoll, PrometheusQuery } from './use-prometheus-poll'; | ||
| import { PrometheusGraph } from './prometheus-graph'; | ||
|
|
||
| export const Area: React.FC<AreaProps> = ({ | ||
| basePath, | ||
| className, | ||
| height = 90, | ||
| humanizeTime = twentyFourHourTime, | ||
| humanizeValue = humanizeNumber, | ||
| namespace, | ||
| numSamples, | ||
| query, | ||
| theme = ChartTheme.light.multi, | ||
| tickCount = 3, | ||
| timeout, | ||
| timeSpan, | ||
| title, | ||
| }) => { | ||
| const [containerRef, width] = useRefWidth(); | ||
| const [data] = usePrometheusPoll({basePath, namespace, numSamples, query, timeout, timeSpan, defaultQueryName: title}); | ||
TheRealJon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Override theme. Once PF React Charts is released and we update, there should be much less we need to override here. | ||
| const _theme = _.merge(theme, cartesianChartStyles, areaStyles); | ||
| const getLabel = ({name, y}) => (data.length > 1) ? `${name}: ${humanizeValue(y)}` : humanizeValue(y); | ||
| const container = <ChartVoronoiContainer voronoiDimension="x" labels={getLabel} />; | ||
| return <PrometheusGraph className={className} query={query} title={title} > | ||
| <div ref={containerRef} style={{width: '100%'}}> | ||
| <Chart | ||
| containerComponent={container} | ||
| domainPadding={{y: 20}} | ||
| height={height} | ||
| width={width} | ||
| theme={_theme} | ||
| scale={{x: 'time', y: 'linear'}} | ||
| > | ||
| <ChartAxis tickCount={tickCount} tickFormat={humanizeTime} /> | ||
| <ChartAxis dependentAxis tickCount={tickCount} tickFormat={humanizeValue} /> | ||
| <ChartGroup> | ||
| { _.map(data, (values, i) => <ChartArea key={i} data={values} />) } | ||
| </ChartGroup> | ||
| </Chart> | ||
| </div> | ||
| </PrometheusGraph>; | ||
| }; | ||
|
|
||
| type AreaProps = { | ||
| basePath: string; | ||
| className: string; | ||
| height: number, | ||
| humanizeTime: (date: Date) => string; | ||
| humanizeValue: (value: string | number) => string; | ||
| namespace: string; | ||
| numSamples: number; | ||
| query: PrometheusQuery[] | string; | ||
| theme: any; | ||
| tickCount: number; | ||
| timeout: number; | ||
| timeSpan: number; | ||
| title: string; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| export { Bar } from './bar'; | ||
| export { Gauge } from './gauge'; | ||
| export { Area } from './area'; | ||
| export { Line } from './line'; | ||
| export { QueryBrowser } from './query-browser'; | ||
| export { Donut } from './donut'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* eslint-disable no-undef, no-unused-vars */ | ||
| import * as _ from 'lodash-es'; | ||
| import * as React from 'react'; | ||
| import * as classNames from 'classnames'; | ||
|
|
||
| import { connectToURLs, MonitoringRoutes } from '../../monitoring'; | ||
|
|
||
| const getPrometheusUrl = (urls: string[], query: PrometheusQuery[] | string): string => { | ||
| const base = urls && urls[MonitoringRoutes.Prometheus]; | ||
| if (!base || !query) { | ||
| return null; | ||
| } | ||
|
|
||
| const queries = _.isArray(query) ? query : [{query}]; | ||
spadgett marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const params = new URLSearchParams(); | ||
| _.each(queries, (q, i) => { | ||
| params.set(`g${i}.range_input`, '1h'); | ||
| params.set(`g${i}.expr`, q.query); | ||
| params.set(`g${i}.tab`, '0'); | ||
| }); | ||
|
|
||
| return `${base}/graph?${params.toString()}`; | ||
| }; | ||
|
|
||
| const PrometheusGraphLink = connectToURLs(MonitoringRoutes.Prometheus)( | ||
| ({children, query, urls}: React.PropsWithChildren<PrometheusGraphLinkProps>) => { | ||
| const url = getPrometheusUrl(urls, query); | ||
| return <React.Fragment> | ||
| { | ||
| url | ||
| ? <a href={url} target="_blank" rel="noopener noreferrer" style={{ textDecoration: 'none' }}> | ||
| {children} | ||
| </a> | ||
| : {children} | ||
| } | ||
| </React.Fragment>; | ||
| } | ||
| ); | ||
|
|
||
| export const PrometheusGraph: React.FunctionComponent<PrometheusGraphProps> = ({children, title, className, query}) => { | ||
| return <div className={classNames('graph-wrapper', className)}> | ||
| { title && <h5 className="graph-title graph-title--left">{title}</h5> } | ||
| <PrometheusGraphLink query={query}> | ||
| {children} | ||
| </PrometheusGraphLink> | ||
| </div>; | ||
| }; | ||
|
|
||
| type PrometheusQuery = { | ||
| name: string; | ||
| query: string; | ||
| }; | ||
|
|
||
| type PrometheusGraphLinkProps = { | ||
| query: PrometheusQuery[] | string; | ||
| urls?: string[]; | ||
| }; | ||
|
|
||
| type PrometheusGraphProps = { | ||
| className?: string; | ||
| query: PrometheusQuery[] | string; | ||
| title?: string; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* eslint-disable camelcase */ | ||
|
|
||
| import { global_FontFamily_sans_serif } from '@patternfly/react-tokens'; | ||
|
|
||
| const independentAxisStyles = { | ||
| independentAxis: { | ||
| style: { | ||
| axis: { stroke: '#D1D1D1' }, | ||
| tickLabels: { fontFamily: global_FontFamily_sans_serif.value }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const dependentAxisStyles = { | ||
| dependentAxis: { | ||
| style: { | ||
| axis: { stroke: '#D1D1D1' }, | ||
| grid: { stroke: '#EDEDED' }, | ||
| tickLabels: { fontFamily: global_FontFamily_sans_serif.value }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const tooltipStyles = { | ||
| // General tooltip style | ||
| tooltip: { | ||
| flyoutStyle: { | ||
| fill: '#151515', | ||
| }, | ||
| style: { | ||
| labels: { | ||
| fontFamily: global_FontFamily_sans_serif.value, | ||
| fill: '#FFF', | ||
| }, | ||
| }, | ||
| }, | ||
|
|
||
| // Voronoi container tooltip theme, overrides general tooltip style | ||
| voronoi: { | ||
| style: { | ||
| flyout: { | ||
| fill: '#151515', | ||
| }, | ||
| labels: { | ||
| fontFamily: global_FontFamily_sans_serif.value, | ||
| fill: '#FFF', | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export const areaStyles = { | ||
| area: { | ||
| style: { | ||
| data: { | ||
| labels: global_FontFamily_sans_serif.value, | ||
| fillOpacity: .15, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export const cartesianChartStyles = { | ||
| chart: { | ||
| padding: { | ||
| bottom: 30, | ||
| left: 60, | ||
| right: 0, | ||
| top: 0, | ||
| }, | ||
| }, | ||
| ...independentAxisStyles, | ||
| ...dependentAxisStyles, | ||
| ...tooltipStyles, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import * as _ from 'lodash-es'; | ||
| import { useEffect, useRef } from 'react'; | ||
| import { prometheusTenancyBasePath, prometheusBasePath } from '.'; | ||
| import { coFetchJSON } from '../../co-fetch'; | ||
| import { useSafetyFirst } from '../safety-first'; | ||
|
|
||
| // TODO This effect only handles polling prometheus data range queries. For now, | ||
| // area charts are the only component using this, so that's okay, but will need | ||
| // to be expanded to handle singular vector queries as well. | ||
| export const usePrometheusPoll = ({ | ||
| basePath, | ||
| defaultQueryName = '', | ||
| namespace, | ||
| numSamples, | ||
| query, | ||
| timeout, | ||
| timeSpan = 60 * 60 * 1000, | ||
| }: PrometheusPollProps ) => { | ||
| const [data, setData] = useSafetyFirst([]); | ||
| const interval = useRef(null); | ||
|
|
||
| useEffect(() => { | ||
| const fetch = () => { | ||
| const end = Date.now(); | ||
| const start = end - timeSpan; | ||
| const baseUrl = basePath || (namespace ? prometheusTenancyBasePath : prometheusBasePath); | ||
| const pollFrequency = Math.max(timeSpan / 120, 3000); // Update every 3 seconds at most | ||
| const stepSize = (numSamples ? (timeSpan / numSamples) : pollFrequency) / 1000; | ||
| const timeoutParam = timeout ? `&timeout=${encodeURIComponent(timeout.toString())}` : ''; | ||
| const queries = !_.isArray(query) ? [{query, name: defaultQueryName}] : query; | ||
| const promises = queries.map(q => { | ||
| const nsParam = namespace ? `&namespace=${encodeURIComponent(namespace)}` : ''; | ||
| const url = `${baseUrl}/api/v1/query_range?query=${encodeURIComponent(q.query)}&start=${start / 1000}&end=${end / 1000}&step=${stepSize}${nsParam}${timeoutParam}`; | ||
| return coFetchJSON(url).then(result => { | ||
| const values = _.get(result, 'data.result[0].values'); | ||
| return _.map(values, v => ({ | ||
| name: q.name, | ||
| x: new Date(v[0] * 1000), | ||
| y: parseFloat(v[1]), | ||
| })); | ||
| }); | ||
| }); | ||
| Promise.all(promises) | ||
| .then(d => setData(d)) | ||
| /* eslint-disable-next-line no-console */ | ||
| .catch(e => console.warn(`Error retrieving Prometheus data: ${e}`)) | ||
| .then(() => { | ||
| interval.current = setTimeout(fetch, pollFrequency); | ||
| }); | ||
| }; | ||
|
|
||
| if (query) { | ||
| fetch(); | ||
| } | ||
| return () => { | ||
| clearInterval(interval.current); | ||
| }; | ||
| }, [basePath, defaultQueryName, namespace, numSamples, query, timeout, timeSpan, setData]); | ||
|
|
||
| return [data] as [GraphDataPoint[][]]; | ||
| }; | ||
|
|
||
| export type PrometheusQuery = { | ||
| name: string; | ||
| query: string; | ||
| }; | ||
|
|
||
| type PrometheusPollProps = { | ||
| basePath?: string; | ||
| defaultQueryName?: string; | ||
| namespace?: string; | ||
| numSamples?: number; | ||
| query: PrometheusQuery[] | string; | ||
| timeout?: number; | ||
| timeSpan?: number; | ||
| } | ||
|
|
||
| type GraphDataPoint = { | ||
| name?: string; | ||
| x: Date; | ||
| y: number; | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.