1- import React , { useState , useContext } from 'react' ;
1+ import React , { useState , useContext , useEffect , useRef } from 'react' ;
2+ import { useIsMount } from '../context/helpers' ;
23import 'antd/dist/antd.less' ;
34import { Switch , Table , Tag , Transfer , Button } from 'antd' ;
45import { PoweroffOutlined } from '@ant-design/icons' ;
@@ -63,13 +64,15 @@ const TableTransfer = ({ leftColumns, rightColumns, ...restProps }) => (
6364
6465const TransferColumns = React . memo ( ( ) => {
6566 const [ targetKeys , setTargetKeys ] = useState ( [ ] ) ;
67+ const [ metricsPool , setMetricsPool ] = useState < any [ ] > ( [ ] ) ;
68+ const [ listReady , setListReady ] = useState ( false ) ;
6669 const [ disabled , setDisabled ] = useState ( false ) ;
6770 const [ showSearch , setShowSearch ] = useState ( true ) ;
6871 const { setSelectedMetrics } = useContext ( QueryContext ) ;
69- // const {category_service_datalist } = useContext(HealthContext);
70- // const { datalist } = useContext(EventContext);
72+ // const {datalist } = useContext(HealthContext);
73+ const { eventDataList } = useContext ( EventContext ) ;
7174
72- const category_service_datalist = [
75+ const datalist = [
7376 {
7477 Memory : [
7578 { books : [ { disk_usage : [ 10 , 20 ] } , { clockSpeed : [ 8 , 16 ] } ] } ,
@@ -80,29 +83,10 @@ const TransferColumns = React.memo(() => {
8083 CPU : [ { books : [ { cpu_temp : [ 100 , 200 ] } ] } , { orders : [ { cpu_temp : [ 150 , 250 ] } ] } ] ,
8184 } ,
8285 ] ;
83- const datalist = [
84- {
85- Event : [
86- {
87- books : [
88- { broker_metric1 : [ 10 , 20 ] } ,
89- { broker_metric2 : [ 8 , 16 ] } ,
90- { broker_metric3 : [ 8 , 16 ] } ,
91- ] ,
92- } ,
93- {
94- orders : [
95- { broker_metric1 : [ 5 , 25 ] } ,
96- { broker_metric2 : [ 7 , 14 ] } ,
97- { broker_metric3 : [ 8 , 16 ] } ,
98- ] ,
99- } ,
100- ] ,
101- } ,
102- ] ;
10386
104- const appendMetrics = ( datalist , metricsPool ) => {
105- if ( datalist ) {
87+ const appendMetrics = ( eventDataList , datalist ) => {
88+ let pool : any [ ] = [ ] ;
89+ if ( datalist && datalist . length > 0 ) {
10690 datalist . forEach ( category => {
10791 const tag : string = Object . keys ( category ) [ 0 ] ; //Memory
10892 const serviceObj : { } = category [ tag ] [ 0 ] ; // { books: [{ disk_usage: [10, 20] }, { clockSpeed: [8, 16] }] }
@@ -113,19 +97,44 @@ const TransferColumns = React.memo(() => {
11397 const temp = { } ;
11498 const metricName : string = Object . keys ( element ) [ 0 ] ;
11599 const key = tag + ' | ' + metricName ;
116- const title = key ;
117100 temp [ 'key' ] = key ;
118- temp [ 'title' ] = title ;
101+ temp [ 'title' ] = key ;
119102 temp [ 'tag' ] = tag ;
120- metricsPool . push ( temp ) ;
103+ pool . push ( temp ) ;
121104 } ) ;
122105 } ) ;
106+
123107 }
124- return metricsPool ;
108+ if ( eventDataList && eventDataList . length > 0 ) {
109+ eventDataList . forEach ( metric => {
110+ const temp = { } ;
111+ const metricName : string = Object . keys ( metric ) [ 0 ] ;
112+ const key = 'Event | ' + metricName ;
113+ temp [ 'key' ] = key ;
114+ temp [ 'title' ] = key ;
115+ temp [ 'tag' ] = 'Event' ;
116+ pool . push ( temp ) ;
117+ } ) ;
118+
119+ }
120+
121+ setMetricsPool ( pool ) ;
125122 } ;
126- let metricsPool = [ ] ;
127- metricsPool = appendMetrics ( category_service_datalist , metricsPool ) ;
128- metricsPool = appendMetrics ( datalist , metricsPool ) ;
123+
124+ useEffect ( ( ) => {
125+ if ( datalist && datalist . length > 0 && eventDataList && eventDataList . length > 0 ) {
126+ setListReady ( true ) ;
127+ }
128+ } , [ datalist , eventDataList ] ) ;
129+
130+
131+ const isMount = useIsMount ( ) ;
132+
133+ useEffect ( ( ) => {
134+ if ( ! isMount ) {
135+ appendMetrics ( eventDataList , datalist ) ;
136+ }
137+ } , [ listReady ] ) ;
129138
130139 const leftTableColumns = [
131140 {
@@ -147,7 +156,6 @@ const TransferColumns = React.memo(() => {
147156
148157 const onChange = nextTargetKeys => {
149158 setTargetKeys ( nextTargetKeys ) ;
150- console . log ( 'nextTargetKeys' , nextTargetKeys ) ;
151159 } ;
152160
153161 const triggerDisable = checked => {
@@ -157,45 +165,43 @@ const TransferColumns = React.memo(() => {
157165 const triggerShowSearch = checked => {
158166 setShowSearch ( checked ) ;
159167 } ;
160- const handleClick = ( ) => {
168+ const handleClick = ( ) => {
161169 //setSelectedMetrics
162170 const temp : any [ ] = [ ] ;
163171 const categorySet = new Set ( ) ;
164- for ( let i = 0 ; i < targetKeys . length ; i ++ ) {
165- let str : string = targetKeys [ i ] ;
166- const strArr : string [ ] = str . split ( ' | ' ) ;
172+ for ( let i = 0 ; i < targetKeys . length ; i ++ ) {
173+ let str : string = targetKeys [ i ] ;
174+ const strArr : string [ ] = str . split ( ' | ' ) ;
167175 const categoryName = strArr [ 0 ] ;
168176 const metricName = strArr [ 1 ] ;
169- if ( categorySet . has ( categoryName ) ) {
177+ if ( categorySet . has ( categoryName ) ) {
170178 temp . forEach ( element => {
171- if ( Object . keys ( element ) [ 0 ] === categoryName ) {
179+ if ( Object . keys ( element ) [ 0 ] === categoryName ) {
172180 const metricsList : any [ ] = element [ categoryName ] ;
173181 metricsList . push ( metricName ) ;
174182 }
175183 } ) ;
176- }
177- else {
184+ } else {
178185 categorySet . add ( categoryName ) ;
179186 const newCategory = { } ;
180187 newCategory [ categoryName ] = [ metricName ] ;
181188 temp . push ( newCategory ) ;
182189 }
183190 }
184191 setSelectedMetrics ( temp ) ;
185- }
192+ } ;
186193
187194 return (
188195 < >
189196 < Button
190197 type = "primary"
191198 onClick = { handleClick }
192- shape = ' round'
193- size = ' middle'
199+ shape = " round"
200+ size = " middle"
194201 style = { {
195202 marginLeft : 16 ,
196203 marginTop : 330 ,
197- float : 'right'
198-
204+ float : 'right' ,
199205 } }
200206 >
201207 Get Charts
0 commit comments