@@ -4,18 +4,10 @@ import { Key } from '@styled-icons/fa-solid/Key'
44import clone from 'clone'
55import moment from 'moment'
66import { Button } from 'react-bootstrap'
7- import {
8- XYPlot ,
9- XAxis ,
10- YAxis ,
11- VerticalGridLines ,
12- HorizontalGridLines ,
13- VerticalRectSeries ,
14- RectSeriesPoint ,
15- RVValueEventHandler
16- } from 'react-vis'
177
18- import { GraphValue , Requests , Plan } from '../types/graph'
8+ import { Requests , Plan , GraphNumberValue } from '../types/graph'
9+
10+ import Chart from './Chart'
1911
2012export type Props = {
2113 aggregatedView ?: boolean
@@ -27,18 +19,7 @@ export type Props = {
2719 * Renders a chart showing API Key usage (requests over time) for a particular
2820 * API key.
2921 */
30- class ApiKeyUsageChart extends Component < Props , { value : GraphValue | null } > {
31- constructor ( props : Props ) {
32- super ( props )
33- this . state = {
34- value : null
35- }
36- }
37-
38- handleClearValue = ( ) => {
39- this . setState ( { value : null } )
40- }
41-
22+ class ApiKeyUsageChart extends Component < Props > {
4223 _renderChartTitle = ( ) => {
4324 const { aggregatedView, isAdmin } = this . props
4425 // Do not show chart title for non-admin users.
@@ -140,12 +121,6 @@ class ApiKeyUsageChart extends Component<Props, { value: GraphValue | null }> {
140121 ? null
141122 : ( this . props . id && this . props ?. plan ?. apiUsers ?. [ this . props . id ] ) || null
142123
143- handleSetValue : RVValueEventHandler < RectSeriesPoint > = (
144- data : RectSeriesPoint
145- ) => {
146- this . setState ( { value : data } )
147- }
148-
149124 handleViewApiKey = ( ) => {
150125 const { id, plan } = this . props
151126 if ( ! id || ! plan ) return
@@ -164,8 +139,6 @@ class ApiKeyUsageChart extends Component<Props, { value: GraphValue | null }> {
164139
165140 render ( ) {
166141 const { aggregatedView, id, plan } = this . props
167- const { value } = this . state
168- const ONE_DAY_MILLIS = 86400000
169142 const startDate = moment ( plan ?. result . startDate )
170143 if ( ! aggregatedView && ! id ) {
171144 console . warn ( 'Cannot show non-aggregated view if id prop is undefined.' )
@@ -174,55 +147,24 @@ class ApiKeyUsageChart extends Component<Props, { value: GraphValue | null }> {
174147 // Render the # of requests per API key on each day beginning
175148 // with the start date.
176149 const requestData = this . _getRequestData ( )
177- const timestamp = startDate . valueOf ( )
178- let rangeMax = 0
179150 // Format request data for chart component.
180- const CHART_DATA : RectSeriesPoint [ ] =
151+ const CHART_DATA : GraphNumberValue [ ] =
181152 requestData ?. map ( ( value , i ) => {
182153 if ( i > 0 ) startDate . add ( 1 , 'days' )
183- const begin = startDate . valueOf ( )
184154 // @ts -ignore TYPESCRIPT TODO: what is going on here?
185- const y = value [ 0 ]
186- const end = begin + ONE_DAY_MILLIS
187- if ( y > rangeMax ) rangeMax = y
188- return { x : end , x0 : begin , y, y0 : 0 }
155+ return { x : startDate . valueOf ( ) , y : value [ 0 ] }
189156 } ) || [ ]
190- const maxY = rangeMax === 0 ? 10 : Math . ceil ( rangeMax / 10 ) * 10
191157 return (
192- < div className = "usage-list" style = { { display : 'inline-block' } } >
193- { this . _renderChartTitle ( ) }
194- { this . _renderKeyInfo ( ) }
195- < XYPlot
196- height = { 300 }
197- style = { { overflow : 'initial' } }
198- width = { 600 } // Round up max y value to the nearest 10
199- xDomain = { [
200- timestamp - 2 * ONE_DAY_MILLIS ,
201- timestamp + 30 * ONE_DAY_MILLIS
202- ] }
203- yDomain = { [ 0 , maxY ] }
204- >
205- < VerticalGridLines />
206- < HorizontalGridLines />
207- < XAxis tickFormat = { ( d ) => moment ( d ) . format ( 'MMM DD' ) } />
208- < YAxis />
209- < VerticalRectSeries
210- data = { CHART_DATA }
211- onValueMouseOut = { this . handleClearValue } // Update value on mouse over/out.
212- onValueMouseOver = { this . handleSetValue }
213- style = { { stroke : '#fff' } }
214- />
215- </ XYPlot >
216- < p style = { { textAlign : 'center' } } >
217- { value ? (
218- < >
219- { moment ( value . x ) . format ( 'MMM DD' ) } : { value . y } requests
220- </ >
221- ) : (
222- < > [Hover over bars to see values.]</ >
223- ) }
224- </ p >
225- </ div >
158+ < Chart
159+ data = { CHART_DATA }
160+ entityType = "requests"
161+ title = {
162+ < >
163+ { this . _renderChartTitle ( ) }
164+ { this . _renderKeyInfo ( ) }
165+ </ >
166+ }
167+ />
226168 )
227169 }
228170}
0 commit comments