-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
1,772 additions
and
983 deletions.
There are no files selected for viewing
This file contains 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 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
66 changes: 66 additions & 0 deletions
66
packages/react/src/components/Metrics/Aggregate/AggregateBarChart.tsx
This file contains 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,66 @@ | ||
import React from 'react'; | ||
import { Bar } from 'react-chartjs-2'; | ||
|
||
import { | ||
AggregateMetricsRequest, | ||
AggregationType, | ||
MetricsBarChartComponentProps, | ||
} from '../types'; | ||
|
||
import { useAggregation } from './hooks'; | ||
|
||
type LabelAndData = { x: string; y: number }[]; | ||
export default function AggregateLineChart( | ||
props: { | ||
aggregation: AggregateMetricsRequest; | ||
} & MetricsBarChartComponentProps | ||
) { | ||
const { chartOptions, dataset } = props; | ||
|
||
const aggregationType: AggregationType = props.aggregation.aggregationType; | ||
const { isLoading, buckets } = useAggregation(props); | ||
|
||
const data = React.useMemo((): LabelAndData => { | ||
if (!buckets) { | ||
return []; | ||
} | ||
return buckets | ||
.map((bucket) => { | ||
return { | ||
y: Number(bucket[aggregationType]), | ||
x: bucket.timestamp?.toISOString() as string, | ||
}; | ||
}) | ||
.filter(Boolean); | ||
}, [aggregationType, buckets]); | ||
|
||
if (isLoading) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Bar | ||
options={{ | ||
...chartOptions, | ||
plugins: { | ||
legend: { | ||
display: false, | ||
}, | ||
}, | ||
}} | ||
data={{ | ||
datasets: [ | ||
{ | ||
label: props.aggregation.metricName, | ||
data, | ||
backgroundColor: 'rgb(111 226 255)', | ||
borderColor: 'rgb(77, 158, 178)', | ||
borderSkipped: true, | ||
barPercentage: 0.9, | ||
...dataset, | ||
}, | ||
], | ||
}} | ||
/> | ||
); | ||
} |
This file contains 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 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,55 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
import { UseAggreationReturn, UseAggregationProps } from '../types'; | ||
import { useInterval } from '../../../lib/hooks/useInterval'; | ||
import { useNile } from '../../../context'; | ||
import Queries from '../../../lib/queries'; | ||
|
||
/** | ||
* @example | ||
* import { useAggregation } from '@theniledev/react'; | ||
* function MyChart() { | ||
* const { buckets } = useAggregation({ | ||
* aggregation: { | ||
* aggregationRequest: { | ||
* startTime: new Date(), | ||
* organizationId: 'myOrganization', | ||
* bucketSize: '1h', | ||
* }, | ||
* metricName: 'my.metric', | ||
* }, | ||
* updateInterval: 4000 | ||
* }); | ||
* // rest of component | ||
* } | ||
* | ||
* @param props config object for hook and metrics request | ||
* @returns a boolean for the loading state and an array of Bucket objects | ||
*/ | ||
|
||
export const useAggregation = ( | ||
props: UseAggregationProps | ||
): UseAggreationReturn => { | ||
const nile = useNile(); | ||
|
||
const { updateInterval, queryKey, aggregation } = props; | ||
const qKey = queryKey || `aggregation:${aggregation.metricName}`; | ||
|
||
const { | ||
data = [], | ||
isLoading, | ||
refetch, | ||
} = useQuery( | ||
[Queries.FilterMetrics(qKey)], | ||
() => { | ||
return nile.metrics.aggregateMetrics(aggregation); | ||
}, | ||
{ enabled: Boolean(nile.workspace) } | ||
); | ||
|
||
useInterval(() => { | ||
refetch(); | ||
}, updateInterval); | ||
|
||
return { isLoading, buckets: data }; | ||
}; |
90 changes: 90 additions & 0 deletions
90
packages/react/src/components/Metrics/Filter/FilterBarChart.tsx
This file contains 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,90 @@ | ||
import React from 'react'; | ||
import { Bar } from 'react-chartjs-2'; | ||
import { FilterMetricsRequest } from '@theniledev/js'; | ||
import { | ||
Chart, | ||
CategoryScale, | ||
BarController, | ||
Title, | ||
Tooltip, | ||
Legend, | ||
TimeScale, | ||
ChartDataset, | ||
ChartOptions, | ||
BarElement, | ||
} from 'chart.js'; | ||
|
||
import { useFilter } from './hooks'; | ||
|
||
Chart.register( | ||
CategoryScale, | ||
BarController, | ||
BarElement, | ||
Title, | ||
Tooltip, | ||
Legend, | ||
TimeScale | ||
); | ||
|
||
export type MetricsComponentProps = { | ||
timeFormat?: string; | ||
dataset?: Omit<ChartDataset<'bar', number[]>, 'data'>; | ||
updateInterval?: number; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
chartOptions?: ChartOptions<any>; | ||
queryKey?: string; | ||
}; | ||
type LabelAndData = { x: string; y: number }[]; | ||
|
||
export default function FilterBarChart( | ||
props: FilterMetricsRequest & MetricsComponentProps | ||
) { | ||
const { filter, chartOptions, dataset } = props; | ||
|
||
const { isLoading, metrics } = useFilter(props); | ||
const metricName = filter.metricName; | ||
|
||
const data = React.useMemo<LabelAndData>((): LabelAndData => { | ||
if (!metrics) { | ||
return []; | ||
} | ||
return metrics.map((metric) => { | ||
return { y: metric.value, x: metric.timestamp.toISOString() }; | ||
}); | ||
}, [metrics]); | ||
|
||
const sets = React.useMemo(() => { | ||
return data.map((datum) => { | ||
return { | ||
label: metricName, | ||
data: [datum], | ||
borderColor: 'rgb(77, 158, 178)', | ||
borderSkipped: true, | ||
backgroundColor: 'rgb(77, 158, 178)', | ||
barPercentage: 0.9, | ||
grouped: true, | ||
...dataset, | ||
}; | ||
}); | ||
}, [data, dataset, metricName]); | ||
|
||
if (isLoading) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Bar | ||
options={{ | ||
...chartOptions, | ||
plugins: { | ||
legend: { | ||
display: false, | ||
}, | ||
}, | ||
}} | ||
data={{ | ||
datasets: sets, | ||
}} | ||
/> | ||
); | ||
} |
This file contains 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 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 @@ | ||
import React from 'react'; | ||
import { FilterMetricsRequest, Metric } from '@theniledev/js'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
import { useNile } from '../../../context'; | ||
import Queries from '../../../lib/queries'; | ||
import { useInterval } from '../../../lib/hooks/useInterval'; | ||
import { UseMetricsProps, UseMetricsReturn } from '../types'; | ||
|
||
/** | ||
* @example | ||
* ```typescript | ||
* import { useFilter } from '@theniledev/react'; | ||
* function MyChart() { | ||
* const filter = { | ||
* entityName: "clusters", | ||
* metric: "my.metric" | ||
* startTime: new Date(), | ||
* duration: 60 * 1000, | ||
* }; | ||
* | ||
* const { isLoading, metrics } = useFilter({ | ||
* filter, | ||
* }); | ||
* // rest of component | ||
* } | ||
* ``` | ||
* @param props config object for a metrics request | ||
* @returns a boolean for the loading state and metrics flattened into measurements | ||
*/ | ||
|
||
export const useFilter = (props: UseMetricsProps): UseMetricsReturn => { | ||
const nile = useNile(); | ||
|
||
const updateInterval = props?.updateInterval; | ||
const { filter } = props; | ||
|
||
const payload = React.useMemo<FilterMetricsRequest>(() => { | ||
// API does not like this currently | ||
if (filter && filter.metricName === '') { | ||
delete filter.metricName; | ||
} | ||
return { | ||
...props, | ||
filter: filter ? filter : {}, | ||
} as FilterMetricsRequest; | ||
}, [filter, props]); | ||
|
||
const queryKey = React.useMemo( | ||
() => props.queryKey || `filter:${filter.metricName}`, | ||
[filter.metricName, props.queryKey] | ||
); | ||
|
||
const { data, isLoading, refetch } = useQuery( | ||
[Queries.FilterMetrics(queryKey)], | ||
() => { | ||
return nile.metrics.filterMetrics(payload); | ||
}, | ||
{ enabled: Boolean(nile.workspace) } | ||
); | ||
|
||
const flatMetrics = React.useMemo( | ||
() => | ||
data?.flatMap((metric: Metric) => { | ||
return metric.measurements; | ||
}), | ||
[data] | ||
); | ||
|
||
useInterval(() => { | ||
refetch(); | ||
}, updateInterval); | ||
|
||
return { isLoading, metrics: flatMetrics }; | ||
}; |
Oops, something went wrong.
9e17eab
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
nile-js – ./
nile-js-git-master-theniledev.vercel.app
nile-js-theniledev.vercel.app
nile-js.vercel.app
9e17eab
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
react-storybook – ./
react-storybook-ten.vercel.app
react-storybook-theniledev.vercel.app
storybook.thenile.dev
react-storybook-git-master-theniledev.vercel.app