Skip to content

Commit 2b11762

Browse files
authored
Merge pull request #37 from oslabs-beta/contTSConvert
Cont ts convert
2 parents b78aba1 + dce47f2 commit 2b11762

File tree

7 files changed

+65
-109
lines changed

7 files changed

+65
-109
lines changed

src/components/tabs/ImagesUser.js renamed to src/components/tabs/ImagesUser.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
/* eslint-disable react/prop-types */
2-
import React, { useState } from 'react';
2+
import React, { useState, MouseEvent } from 'react';
33
import * as helper from '../helper/commands';
4+
import { ImagesProps } from '../../../types';
45

56
/**
67
* Render Images of the user has
78
*
89
* @param {*} props
910
*/
10-
const Images = (props) => {
11+
const Images = (props: ImagesProps) => {
1112
const [repo, setRepo] = useState('');
1213

13-
const handleClick = (e) => {
14+
const handleClick = (e: MouseEvent) => {
1415
e.preventDefault();
1516
helper.pullImage(repo);
1617
};

src/components/tabs/Metrics.js renamed to src/components/tabs/Metrics.tsx

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
/* eslint-disable no-tabs */
22
/* eslint-disable react/prop-types */
3-
import React from 'react'
4-
import { convertToMetricsArr } from '../helper/parseContainerFormat'
5-
import { Chart } from 'react-chartjs-2'
6-
import LineChartDisplay from '../display/LineChartDisplay'
7-
import { useSelector } from 'react-redux'
3+
import React from 'react';
4+
import { Chart } from 'react-chartjs-2';
5+
import LineChartDisplay from '../display/LineChartDisplay';
6+
import { useSelector } from 'react-redux';
7+
import { MetricsProps } from '../../../types';
88

99
/**
1010
* Display general metrics
1111
*
1212
* @param {*} props
1313
*/
14-
const Metrics = (props) => {
15-
const hostStats = useSelector((state) => state.containersList.hostStats)
16-
const fullRunningList = props.runningList
17-
// const result = convertToMetricsArr(props.runningList)
18-
const result = hostStats
19-
const cpuData = 100 - result.cpuPerc // .toFixed(2) // 60%
20-
const memoryData = 100 - result.memPerc // .toFixed(2)
21-
const cpuThreshold = props.threshold[0]
22-
const memThreshold = props.threshold[1]
14+
const Metrics = (props: MetricsProps) => {
15+
const fullRunningList = props.runningList;
16+
const hostStats = useSelector((state: any) => state.containersList.hostStats);
17+
const cpuData = 100 - hostStats.cpuPerc;
18+
const memoryData = 100 - hostStats.memPerc;
19+
const cpuThreshold = props.threshold[0];
20+
const memThreshold = props.threshold[1];
2321

2422
let cpuFails = 0
2523
let memFails = 0
@@ -33,23 +31,23 @@ const Metrics = (props) => {
3331
}
3432

3533
const cpu = {
36-
labels: [`Available: ${cpuData}%`, `Usage: ${result.cpuPerc}%`],
34+
labels: [`Available: ${cpuData}%`, `Usage: ${hostStats.cpuPerc}%`],
3735
datasets: [
3836
{
3937
label: 'CPU',
4038
backgroundColor: ['#4594ce', '#67f267'],
41-
data: [cpuData, result.cpuPerc],
39+
data: [cpuData, hostStats.cpuPerc],
4240
},
4341
],
4442
}
4543

4644
const memory = {
47-
labels: [`Available: ${memoryData}%`, `Usage: ${result.memPerc}%`],
45+
labels: [`Available: ${memoryData}%`, `Usage: ${hostStats.memPerc}%`],
4846
datasets: [
4947
{
5048
label: 'Memory',
5149
backgroundColor: ['#4594ce', '#67f267'],
52-
data: [memoryData, result.memPerc],
50+
data: [memoryData, hostStats.memPerc],
5351
},
5452
],
5553
}
@@ -62,14 +60,14 @@ const Metrics = (props) => {
6260
tooltips: { enabled: true, mode: 'index' },
6361
legend: { display: false },
6462
datalabels: {
65-
formatter: (value, ctx) => {
66-
let sum = 0
67-
const dataArr = ctx.chart.data.datasets[0].data
68-
dataArr.map((data) => {
69-
sum += data
70-
})
71-
const percentage = (value * 100) / sum + '%'
72-
return percentage
63+
formatter: (value: number, ctx: any) => {
64+
let sum = 0;
65+
const dataArr = ctx.chart.data.datasets[0].data;
66+
dataArr.map((data: any) => {
67+
sum += data;
68+
});
69+
const percentage = (value * 100) / sum + '%';
70+
return percentage;
7371
},
7472
color: '#fff',
7573
},
@@ -84,14 +82,14 @@ const Metrics = (props) => {
8482
tooltips: { enabled: false },
8583
legend: { display: false },
8684
datalabels: {
87-
formatter: (value, ctx) => {
88-
let sum = 0
89-
const dataArr = ctx.chart.data.datasets[0].data
90-
dataArr.map((data) => {
91-
sum += data
92-
})
93-
const percentage = (value * 100) / sum + '%'
94-
return percentage
85+
formatter: (value: number, ctx: any) => {
86+
let sum = 0;
87+
const dataArr = ctx.chart.data.datasets[0].data;
88+
dataArr.map((data: any) => {
89+
sum += data;
90+
});
91+
const percentage = (value * 100) / sum + '%';
92+
return percentage;
9593
},
9694
color: '#fff',
9795
},
@@ -123,10 +121,7 @@ const Metrics = (props) => {
123121
</div>
124122
<div className="legend-section">
125123
<div className="usage-box"></div>
126-
<p className="legend-text">
127-
{' '}
128-
Usage {Math.round(result.cpuPerc)}%
129-
</p>
124+
<p className="legend-text"> Usage {Math.round(hostStats.cpuPerc)}%</p>
130125
</div>
131126
</div>
132127
</div>
@@ -150,10 +145,7 @@ const Metrics = (props) => {
150145
</div>
151146
<div className="legend-section">
152147
<div className="usage-box"></div>
153-
<p className="legend-text">
154-
{' '}
155-
Usage {Math.round(result.memPerc)}%
156-
</p>
148+
<p className="legend-text"> Usage {Math.round(hostStats.memPerc)}%</p>
157149
</div>
158150
</div>
159151
</div>

src/components/tabs/ProcessLogs.js renamed to src/components/tabs/ProcessLogs.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
/* eslint-disable react/prop-types */
2-
import React from 'react'
3-
import ProcessLogsCard from '../display/ProcessLogsCard'
2+
import { AnyAsyncThunk } from '@reduxjs/toolkit/dist/matchers';
3+
import React from 'react';
4+
import ProcessLogsCard from '../display/ProcessLogsCard';
5+
import { ContainerType } from '../../../types';
46

57
/**
68
* Display all running and stopped containers. Each box can be selected to view process log tables.
79
*
810
* @param {*} props
911
*/
1012

11-
const ProcessLogs = (props) => {
12-
const renderRunningList = []
13-
props.runningList.map((container, index) => {
13+
const ProcessLogs = (props: any) => {
14+
const renderRunningList: any[] = [];
15+
props.runningList.map((container: ContainerType, index: number) => {
1416
renderRunningList.push(
1517
<ProcessLogsCard
1618
key={index}
@@ -21,8 +23,8 @@ const ProcessLogs = (props) => {
2123
)
2224
})
2325

24-
const renderStoppedList = []
25-
props.stoppedList.map((container, index) => {
26+
const renderStoppedList: any[] = [];
27+
props.stoppedList.map((container: ContainerType, index: number) => {
2628
renderStoppedList.push(
2729
<ProcessLogsCard
2830
key={index}

src/components/tabs/Stopped.js

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/components/views/Admin.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const AdminView = () => {
5757
const runningList = useSelector((state: StateType) => state.containersList.runningList);
5858
const stoppedList = useSelector((state: StateType) => state.containersList.stoppedList);
5959
const imagesList = useSelector((state: StateType) => state.images.imagesList);
60+
const { mem_threshold, cpu_threshold } = useSelector((state: StateType) => state.session);
6061
// const networkList = useSelector((state: StateType) => state.networkList.networkList);
6162

6263
const arrayOfVolumeNames = useSelector(
@@ -249,7 +250,7 @@ const AdminView = () => {
249250
<Route
250251
path='/metrics'
251252
element={
252-
<Metrics runningList={runningList} />
253+
<Metrics runningList={runningList} threshold={[cpu_threshold, mem_threshold]}/>
253254
}
254255
/>
255256
<Route

src/components/views/UserView.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const UserView = () => {
5656
const runningList = useSelector((state: StateType) => state.containersList.runningList);
5757
const stoppedList = useSelector((state: StateType) => state.containersList.stoppedList);
5858
const imagesList = useSelector((state: StateType) => state.images.imagesList);
59+
const { mem_threshold, cpu_threshold } = useSelector((state: StateType) => state.session);
5960
// const networkList = useSelector((state: StateType) => state.networkList.networkList);
6061
const arrayOfVolumeNames = useSelector((state: StateType) => state.volumeList.arrayOfVolumeNames);
6162
const volumeContainersList = useSelector((state: StateType) => state.volumeList.volumeContainersList);
@@ -237,7 +238,7 @@ const UserView = () => {
237238
<Route
238239
path='/metrics'
239240
element={
240-
<Metrics runningList={runningList} />
241+
<Metrics runningList={runningList} threshold={[cpu_threshold, mem_threshold]}/>
241242
}
242243
/>
243244

types.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ export interface RunningListType {
2525
RunningFor: string,
2626
}
2727

28+
export interface hostStats {
29+
cpuPerc: string,
30+
memPerc: string,
31+
}
32+
2833
// for more info review actions.ts file and Settings.ts
2934
export type ContainerProps = {
3035
stoppedList: StoppedListType[];
@@ -36,8 +41,14 @@ export type ContainerProps = {
3641
stop: (id: string, refreshStoppedContainers: (data: StoppedContainerObj[]) => void) => void;
3742
runningList: RunningListType[];
3843
runIm: (id: ContainerType, runningList: RunningListType, callback_1: () => void, callback_2: () => void) => void;
44+
hostStats?: hostStats[];
3945
}
4046

47+
export type MetricsProps = {
48+
runningList: any[];
49+
threshold: any[];
50+
}
51+
4152
// Stopped containers have a Names key and running containers have a Name key
4253
export type ContainerType = {
4354
Name?: string;
@@ -242,9 +253,10 @@ interface session {
242253
}
243254

244255
// "any" has been used below since strict typing was used to define these props in the tabs types
245-
interface containersList {
256+
export interface containersList {
246257
runningList: any[],
247-
stoppedList: any[]
258+
stoppedList: any[],
259+
hostStats: string[],
248260
}
249261

250262
interface imagesList {

0 commit comments

Comments
 (0)