Skip to content

Commit 794b813

Browse files
authored
Merge pull request #32 from oslabs-beta/health-context-refactor
Health context refactor
2 parents 3c0222a + 6f8890a commit 794b813

15 files changed

Lines changed: 107 additions & 332 deletions

File tree

app/components/ApplicationsCard/ApplicationsCard.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import React from 'react';
2-
import { Card, CardHeader, IconButton, CardContent, Typography } from '@mui/material';
1+
2+
import React from "react";
3+
import { Card,CardHeader,IconButton,CardContent,Typography } from "@mui/material";
34
import HighlightOffIcon from '@mui/icons-material/HighlightOff';
45
import UpdateIcon from '@mui/icons-material/Update';
56
import './styles.scss';
@@ -12,8 +13,11 @@ interface ApplicationCardProps {
1213
classes: any;
1314
}
1415

15-
const ApplicationsCard: React.FC<ApplicationCardProps> = (props: ApplicationCardProps) => {
16-
const { application, i, setModal, classes } = props;
16+
import './styles.scss'
17+
18+
const ApplicationsCard:React.FC<ApplicationCardProps> = (props) => {
19+
20+
const { application, i, setModal, classes } = props
1721
const { handleClick, confirmDelete } = getEventHandlers({ application, setModal });
1822

1923
return (

app/components/GraphNavBar/GraphNavBar.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { ApplicationContext } from '../../context/ApplicationContext'
44
import { QueryContext } from '../../context/QueryContext'
55
import { CommsContext } from '../../context/CommsContext'
66
import { HealthContext } from '../../context/HealthContext'
7-
import { Link } from 'react-router-dom';
87

98
const GraphNavBar = (props) => {
109

@@ -50,7 +49,6 @@ const GraphNavBar = (props) => {
5049
);
5150
});
5251
}
53-
5452
return buttonList;
5553
};
5654

app/components/TransferColumns.tsx

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useState, useContext, useEffect } from 'react';
2+
import { ApplicationContext } from '../context/ApplicationContext';
23
import { useParams } from 'react-router-dom';
34
import { QueryContext } from '../context/QueryContext';
45
import { HealthContext } from '../context/HealthContext';
@@ -17,7 +18,9 @@ interface Params {
1718

1819
const TransferColumns = React.memo(() => {
1920
const [targetKeys, setTargetKeys] = useState<any[]>([]);
21+
// console.log({targetKeys})
2022
const [metricsPool, setMetricsPool] = useState<any[]>([]);
23+
// console.log({metricsPool})
2124
const [healthMetricsReady, setHealthMetricsReady] = useState(false);
2225
const [healthMetrics, setHealthMetrics] = useState<any[]>([]);
2326
const [eventMetricsReady, setEventMetricsReady] = useState(false);
@@ -31,11 +34,14 @@ const TransferColumns = React.memo(() => {
3134
const { mode } = useContext(DashboardContext.DashboardContext);
3235
const [searchTerm, setSearchTerm] = useState<string>('');
3336

37+
const { savedMetrics } = useContext(ApplicationContext)
38+
3439
const currentMode = mode === 'light' ? lightAndDark.lightModeText : lightAndDark.darkModeText;
3540
const currentStyle = mode === 'light' ? lightAndDark.lightModeData : lightAndDark.darkModeData;
3641

3742
useEffect(() => {
3843
if (healthData) {
44+
console.log({healthData})
3945
setHealthMetricsReady(true);
4046
}
4147
}, [healthData]);
@@ -103,7 +109,7 @@ const TransferColumns = React.memo(() => {
103109
}
104110
}
105111
} else {
106-
// iterate throught the healthData object to populate the `Metrics Query` tab with metrics options
112+
// iterate throughout the healthData object to populate the `Metrics Query` tab with metrics options
107113
// The pool array wants an object with a specific format in order to populate the selection table
108114
for (const service in dataObject) {
109115
const categoryObjects = dataObject[service];
@@ -124,9 +130,10 @@ const TransferColumns = React.memo(() => {
124130
};
125131

126132
const createSelectedMetricsList = () => {
133+
127134
const temp: any[] = [];
128135
const categorySet = new Set();
129-
// console.log('Inside TransferColumns.txs line 124 targetKeys: ', targetKeys)
136+
130137
for (let i = 0; i < targetKeys.length; i++) {
131138
const str: string = targetKeys[i];
132139
const strArr: string[] = str.split(' | ');
@@ -170,21 +177,31 @@ const TransferColumns = React.memo(() => {
170177
];
171178

172179
// makes the rows needed for the selection grid
173-
const rows: any[] = [];
174-
metricsPool.forEach((el, index) => {
175-
const row = {
176-
id: index,
177-
tag: el.tag,
178-
title: el.title.split(' | ')[1].replace(/.*\/.*\//g, ''),
179-
}; // gets rid of the full path
180-
rows.push(row);
181-
});
180+
const rows:any[] = []
181+
let id = 0
182+
for(let savedMetric of Object.keys(savedMetrics)) {
183+
184+
const { metric,category } = savedMetrics[savedMetric]
185+
rows.push({
186+
id:id++,
187+
tag:category,
188+
title:metric
189+
})
190+
}
182191

183192
// makes the Printed list of 'currently selected rows' on the page using targetKeys array
184193
const selectedRows: any[] = [];
185194
targetKeys.forEach(el => {
186195
selectedRows.push(
187-
<li style={{ marginLeft: '30px', marginTop: '5px', color: currentMode.color }}>{el}</li>
196+
<li
197+
style={{
198+
marginLeft: '30px',
199+
marginTop: '5px',
200+
color: currentMode.color
201+
}}
202+
>
203+
{el}
204+
</li>
188205
);
189206
});
190207

@@ -222,7 +239,8 @@ const TransferColumns = React.memo(() => {
222239
onRowSelectionModelChange={metricIndeces => {
223240
const metrics: any[] = [];
224241
metricIndeces.forEach(el => {
225-
metrics.push(metricsPool[el].key);
242+
console.log({rows})
243+
metrics.push(`${rows[el].tag} | ${rows[el].title}`);
226244
});
227245
setTargetKeys(metrics);
228246
}}
@@ -231,10 +249,20 @@ const TransferColumns = React.memo(() => {
231249

232250
</div>
233251
{selectedRows.length > 0 && (
234-
<h3 style={{ marginTop: '20px', color: 'currentStyle',}}
235-
>Selected Rows:</h3>
252+
<h3
253+
style={{
254+
marginTop: '20px',
255+
color: 'currentStyle'
256+
}}
257+
>
258+
Selected Rows:
259+
</h3>
236260
)}
237-
<ol id="selectedRows">{selectedRows}</ol>
261+
<ol
262+
id="selectedRows"
263+
>
264+
{selectedRows}
265+
</ol>
238266
</div>
239267
</>
240268
);

app/containers/GraphsContainer/GraphsContainer.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ const GraphsContainer: React.FC = React.memo(() => {
102102
inspect={inspect}
103103
setInspect={setInspect}
104104
/>
105-
<Header app={app} service={service} live={live} setLive={setLive} />
105+
<Header
106+
app={app}
107+
service={service}
108+
live={live}
109+
setLive={setLive}
110+
/>
106111

107112
{inspect && <Inspect />}
108113

@@ -115,11 +120,16 @@ const GraphsContainer: React.FC = React.memo(() => {
115120
<RouteChart />
116121
<LogsTable />
117122
</div>
118-
) : (
119-
<div className="graphs" style= {currentStyle}>
123+
) :
124+
(
125+
<div className="graphs">
120126
{chart === 'all' && (
121127
<div className="transferColumns">
122-
<h2 style={currentMode}>Search Your Metrics to Display</h2>
128+
<h2
129+
style={currentMode}
130+
>
131+
Search Your Metrics to Display
132+
</h2>
123133
<TransferColumns />
124134
</div>
125135
)}

app/containers/SidebarContainer/SidebarContainer.tsx

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useContext } from 'react';
2-
import { Link } from 'react-router-dom';
2+
import { Link,useNavigate } from 'react-router-dom';
33

44
import ListIcon from '@mui/icons-material/List';
55
import InfoIcon from '@mui/icons-material/Info';
@@ -12,6 +12,7 @@ import './styles.scss';
1212
import { ApplicationContext } from '../../context/ApplicationContext';
1313
import { AwsContext } from '../../context/AwsContext';
1414
import { DashboardContext } from '../../context/DashboardContext';
15+
import { QueryContext } from '../../context/QueryContext';
1516

1617
const SidebarContainer = React.memo(() => {
1718
// Extract invervalID from ApplicationContext. Initival value: intervalID = null
@@ -20,6 +21,9 @@ const SidebarContainer = React.memo(() => {
2021
const { isLoading, setLoadingState } = useContext(AwsContext);
2122
// clear interval and set loading state to true when leaving graph containers
2223
const { addApp,setApplications,deleteApp } = useContext(DashboardContext)
24+
const { setSelectedMetrics } = useContext(QueryContext)
25+
26+
const navigate = useNavigate();
2327

2428
/**
2529
* @function handleCLick - check if the 'intervalID' exists. If so, theres a timer running and the fuunction clears the timer using @function clearInterval - function.
@@ -41,13 +45,6 @@ const SidebarContainer = React.memo(() => {
4145
URI: "mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.5",
4246
name: 'Microservices-Mongo',
4347
description: 'Mongo Microservices Example'
44-
},
45-
microServicesSQLFields: {
46-
typeOfService: 'Microservices',
47-
database: 'SQL',
48-
URI: "mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.5",
49-
name: 'Microservices-SQL',
50-
description: 'SQL Microservices Example'
5148
}
5249
,
5350
dockerMongoData: {
@@ -56,27 +53,22 @@ const SidebarContainer = React.memo(() => {
5653
URI: "mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.5",
5754
name: 'Docker-Mongo',
5855
description: 'Docker Example'
59-
},
60-
dockerSQLData: {
61-
typeOfService: 'Docker',
62-
database: 'SQL',
63-
URI: "mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.5",
64-
name: 'Docker-SQL',
65-
description: 'Docker Example'
6656
}
6757
}
6858

6959
addApp(examplesData)
7060
}
7161

7262
const handleExitExample = () => {
63+
7364
setExample(false)
74-
// setApplications([])
7565
setAppIndex(0);
7666
setApp('');
7767
setServicesData([]);
78-
setChart('communications')
68+
setChart('all')
7969
deleteApp(0,'all')
70+
setSelectedMetrics([])
71+
navigate("/")
8072
}
8173

8274
return (

app/context/DashboardContext.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ const DashboardContextProvider = React.memo((props: any) => {
4747

4848
const [user, setUser] = useState<string>('guest');
4949
const [applications, setApplications] = useState<string[][]>([]);
50-
console.log({applications})
5150
const [mode, setMode] = useState<string>('light');
5251

5352
const getApplications = useCallback(() => {

app/context/HealthContext.tsx

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ interface HealthDataObject {
3434
const HealthContextProvider: React.FC<Props> = React.memo(({ children }) => {
3535
const [healthData, setHealthData] = useState<any>({ healthDataList: [], healthTimeList: [] });
3636
const [services, setServices] = useState<Array<string>>([]);
37+
console.log({services})
3738

3839
function tryParseJSON(jsonString: any) {
3940
try {
@@ -55,58 +56,41 @@ const HealthContextProvider: React.FC<Props> = React.memo(({ children }) => {
5556
*/
5657

5758
const fetchHealthData = useCallback(async servers => {
58-
ipcRenderer.removeAllListeners('healthResponse');
5959

6060
let temp: HealthDataObject[] = [];
61-
await Promise.all(servers.map( async (service: string) => {
62-
//NOT WORKING HERE
63-
try {
64-
const newPromise: any = await new Promise((resolve, reject) => {
65-
ipcRenderer.send('healthRequest', `${service}`);
66-
ipcRenderer.on('healthResponse', (event: Electron.Event, data: string) => {
67-
let result: object[];
68-
// console.log({data})
69-
if (JSON.stringify(data) !== '{}' && tryParseJSON(data)) {
70-
result = JSON.parse(data);
71-
// console.log({result})
72-
// console.log('HealthContext.tsx line 68 result: ', result, 'service', service, 'Obj key', Object.keys(result[0])[0]);
73-
//result exists, has a length prop, and the service name and database name are same
74-
if (result && result.length && `${service}` === Object.keys(result[0])[0]) {
75-
resolve(result[0]);
76-
}
77-
}
78-
});
79-
})
80-
temp.push(newPromise);
81-
// console.log('HealthContext.tsx line 80 temp populates?: ', temp, serv)
82-
if (checkServicesComplete(temp, [`${service}`])) {
61+
servers.map( async (service: string) => {
62+
63+
try {
64+
65+
ipcRenderer.removeAllListeners('healthResponse');
66+
ipcRenderer.send('healthRequest', `${service}`);
67+
ipcRenderer.on('healthResponse', (event: Electron.Event, data: string) => {
68+
69+
const response = JSON.parse(data);
70+
temp.push(response[0]);
71+
72+
if(temp.length === servers.length) {
73+
console.log(temp.length,servers.length)
8374
setServices([`${service}`]);
8475
let transformedData: any = {};
85-
// console.log('original healthData before transformation: ', temp);
86-
// transformedData = {
87-
// healthDataList: [1,2,3,4,5],
88-
// healthTimeList: [1,2,3,4,5]
89-
// } //testing typescript, transformedDATA of type 2 arrays with basic entries?
76+
console.log({temp})
9077
transformedData = healthTransformer(temp); //must match the setHealthData STATE format
91-
// console.log('healthData after tranformation: ', transformedData);
9278
setHealthData(transformedData);
9379
}
94-
} catch (err) {
95-
// console.log("healthcontext.tsx ERROR: ", err);
96-
};
97-
}
98-
))
99-
} , []);
80+
});
81+
} catch (err) {
82+
console.log("healthcontext.tsx ERROR: ", err);
83+
}})} , []);
10084

10185
const checkServicesComplete = (temp: any[], servers: string[]) => {
10286
if (temp.length !== servers.length) {
10387
return false;
10488
}
10589
const arr1: string[] = [];
10690
for (let i = 0; i < temp.length; i++) {
107-
arr1.push(Object.keys(temp[i])[0]);
91+
const [ serviceName ] = Object.keys(temp[i])
92+
arr1.push(serviceName);
10893
}
109-
// console.log('in checkServicesComplete line 139: ', arr1);
11094
return arr1.sort().toString() === servers.sort().toString();
11195
};
11296

app/context/helpers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ interface HealthDataObject {
3838

3939
export function healthTransformer(healthData: HealthDataObject[]) {
4040
// make an object for storing different services' metrics data
41+
console.log({healthData})
4142
const serviceMetricsObject = {};
4243
// loop through the services in the healthData array
4344
healthData.forEach(serviceObj => {
4445
// grab the key string from the current service object
4546
const serviceName = Object.keys(serviceObj)[0];
4647
const serviceElements = serviceObj[serviceName];
48+
console.log(serviceObj)
4749
// console.log('serviceElements: ', serviceElements);
4850
// add the serviceName as a key on the serviceMetricsObject and assign it an empty object
4951
serviceMetricsObject[serviceName] = {};

0 commit comments

Comments
 (0)