1
- /* eslint-disable no-undef */
1
+ //-----------Import External Modules-----------
2
2
import React , { useEffect , useState } from "react" ;
3
3
import { Grid , Typography } from "@mui/material" ;
4
4
import {
@@ -13,37 +13,39 @@ import 'chartjs-adapter-moment';
13
13
import { CardContent } from "@mui/material" ;
14
14
15
15
16
- // import config from './chartConfig.js';
17
16
17
+ //-----------Import Internal Modules-----------
18
+ import { getUnixRange , getDuration } from "../utils/utilityFunctions.js" ;
18
19
19
20
Chart . register ( ChartStreaming ) ;
20
21
22
+
21
23
const client = new ApolloClient ( {
22
24
uri : "http://localhost:5001/graphql" ,
23
25
cache : new InMemoryCache ( ) ,
24
26
} ) ;
25
27
26
- // console.log('test: ', Math.round(new Date().getTime() / 1000));
27
-
28
- export default function LineChart ( { metric, description } ) {
28
+ export default function LineChart ( { metric, description, metricsState } ) {
29
29
useEffect ( ( ) => {
30
- let delayed ;
31
30
let initialData ;
31
+
32
32
// define chart context
33
33
const ctx = document . getElementById ( metric ) . getContext ( "2d" ) ;
34
34
35
+ // make initial fetch of graph data
36
+ const [ unixStart , unixEnd ] = getUnixRange ( metricsState . duration . days , metricsState . duration . hours , metricsState . duration . minutes ) ;
37
+
35
38
initialData = client . query ( {
36
39
query : gql `
37
- query testQuery {
38
- ksqlDBMetrics(metric: "${ metric } ", resolution: 2 , start: ${ Math . round ( new Date ( ) . getTime ( ) / 1000 ) - 500 } , end: ${ Math . round ( new Date ( ) . getTime ( ) / 1000 ) } ) {
39
- x,
40
- y
41
- }
42
- }
43
- `
44
- } )
40
+ query fetchMetric {
41
+ ksqlDBMetrics(prometheusURL: " ${ metricsState . prometheusURL } " metric: "${ metric } ", resolution: ${ metricsState . refreshRate } , start: ${ unixStart } , end: ${ unixEnd } ) {
42
+ x,
43
+ y
44
+ }
45
+ }
46
+ `
47
+ } )
45
48
. then ( res => {
46
- // chart.data.datasets[0].data.push(...[{x: new Date(), y: 1}]);
47
49
const data = res . data . ksqlDBMetrics . map ( ( queryObj ) => {
48
50
return {
49
51
x : new Date ( queryObj . x * 1000 ) ,
@@ -65,69 +67,65 @@ export default function LineChart({ metric, description }) {
65
67
data : {
66
68
datasets : [ {
67
69
data : initialData ,
70
+ // data: [{x: new Date(), y: '0'}],
68
71
// data: [], // empty at the beginning,
69
72
borderColor : 'rgba(58, 123, 213, 1)' ,
70
73
pointRadius : 0 ,
71
74
hitRadius : 30 ,
72
75
hoverRadius : 5 ,
73
76
fill : true ,
74
77
backgroundColor : gradient ,
75
- } ]
78
+ } ]
79
+ } ,
80
+ options : {
81
+ responsive : true ,
82
+ elements : {
83
+ line : {
84
+ tension : .4
85
+ }
76
86
} ,
77
- options : {
78
- responsive : true ,
79
- animation : {
80
- onComplete : ( ) => {
81
- delayed = true ;
87
+ scales : {
88
+ x : {
89
+ type : 'realtime' ,
90
+ ticks : {
91
+ // minTicksLimit: 24
92
+ autoskip : true ,
93
+ autoSkipPadding : 30 ,
94
+ maxRotation : 0 ,
95
+ steps : 10
82
96
} ,
83
- // delay: (context) => {
84
- // let delay = 0;
85
- // if (context.type === "data" && context.mode === "default" && !delayed) {
86
- // delay = context.dataIndex * 300 + context.datasetIndex * 100;
87
- // }
88
- // return delay;
89
- // }
90
- delay : 3 ,
91
- } ,
92
- elements : {
93
- line : {
94
- tension : .4
95
- }
96
- } ,
97
- scales : {
98
- x : {
99
- type : 'realtime' , // x axis will auto-scroll from right to left
100
- realtime : { // per-axis options
101
- duration : 200000 , // data in the past 20000 ms will be displayed
102
- refresh : 2000 , // onRefresh callback will be called every 1000 ms
103
- delay : 2000 , // delay of 1000 ms, so upcoming values are known before plotting a line
104
- pause : false , // chart is not paused
105
- ttl : undefined , // data will be automatically deleted as it disappears off the chart
106
- frameRate : 30 , // data points are drawn 30 times every second
107
-
108
- // a callback to update datasets
109
- onRefresh : chart => {
110
-
111
- // query your data source and get the array of {x: timestamp, y: value} objects
112
- client . query ( {
113
- query : gql `
97
+ realtime : {
98
+ duration : getDuration ( metricsState . duration . days , metricsState . duration . hours , metricsState . duration . minutes ) , // data in the past duration # of ms will be displayed
99
+ refresh : metricsState . refreshRate * 1000 , // onRefresh callback will be called every refresh # ms
100
+ delay : 1000 , // delay of 1000 ms, so upcoming values are known before plotting a line
101
+ pause : false , // chart is not paused
102
+ ttl : undefined , // data will be automatically deleted as it disappears off the chart
103
+ frameRate : 30 , // data points are drawn 30 times every second
104
+
105
+ // a callback to update datasets
106
+ onRefresh : chart => {
107
+ const [ unixStart , unixEnd ] = getUnixRange ( metricsState . duration . days , metricsState . duration . hours , metricsState . duration . minutes ) ;
108
+
109
+ // query your data source and get the array of {x: timestamp, y: value} objects
110
+ client . query ( {
111
+ query : gql `
114
112
query testQuery {
115
- ksqlDBMetrics(metric: "${ metric } ", resolution: 2 , start: ${ Math . round ( new Date ( ) . getTime ( ) / 1000 ) - 500 } , end: ${ Math . round ( new Date ( ) . getTime ( ) / 1000 ) } ) {
113
+ ksqlDBMetrics(prometheusURL: " ${ metricsState . prometheusURL } " metric: "${ metric } ", resolution: ${ metricsState . refreshRate } , start: ${ unixStart } , end: ${ unixEnd } ) {
116
114
x,
117
115
y
118
116
}
119
117
}
120
118
`
121
119
} )
122
120
. then ( res => {
123
- // chart.data.datasets[0].data.push(...[{x: new Date(), y: 1}]);
124
121
const data = res . data . ksqlDBMetrics . map ( ( queryObj ) => {
125
122
return {
126
123
x : new Date ( queryObj . x * 1000 ) ,
127
124
y : queryObj . y
128
125
}
129
126
} ) ;
130
127
chart . data . datasets [ 0 ] . data = data ;
128
+ // console.log('this is the data: ', data);
131
129
} )
132
130
. catch ( error => console . log ( error ) ) ;
133
131
}
@@ -160,41 +158,16 @@ export default function LineChart({ metric, description }) {
160
158
// instantiate new instance of a chart
161
159
const realTimeChart = new Chart ( ctx , config ) ;
162
160
163
- // populate initial data to avoid having to wait for first refresh
164
- // client.query({
165
- // query: gql`
166
- // query testQuery {
167
- // ksqlDBMetrics(metric: "numActiveQueries", resolution: 1, start: ${Math.round(new Date().getTime() / 1000) - 500}, end: ${Math.round(new Date().getTime() / 1000)}) {
168
- // x,
169
- // y
170
- // }
171
- // }
172
- // `
173
- // })
174
- // .then(res => {
175
- // // chart.data.datasets[0].data.push(...[{x: new Date(), y: 1}]);
176
- // const data = res.data.ksqlDBMetrics.map((queryObj) => {
177
- // return {
178
- // x: new Date(queryObj.x * 1000),
179
- // y: queryObj.y
180
- // }
181
- // });
182
- // console.log('this is a test');
183
- // realTimeChart.data.datasets[0].data = data;
184
- // realTimeChart.update();
185
- // })
186
- // .catch(error => console.log(error));
187
-
188
161
189
162
// chart teardown on unmount
190
163
return ( ) => {
191
164
realTimeChart . destroy ( ) ;
192
165
}
193
- } , [ ] ) ;
166
+ } , [ metricsState ] ) ;
194
167
195
168
return (
196
169
< Grid item xs = { 3 } md = { 3 } lg = { 3 } >
197
- < CardContent sx = { { bgcolor :"chartColor.background ", boxShadow : 1 , borderRadius : '16px' } } >
170
+ < CardContent sx = { { bgcolor : "white ", boxShadow : 1 , borderRadius : '16px' } } >
198
171
< canvas id = { metric } width = "100%" height = "100%" > </ canvas >
199
172
</ CardContent >
200
173
</ Grid >
0 commit comments