Skip to content

Commit da2481a

Browse files
committed
fetching aws data to frontend
1 parent 459ca89 commit da2481a

File tree

10 files changed

+807
-9
lines changed

10 files changed

+807
-9
lines changed

app/components/Occupied.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import '../stylesheets/Occupied.scss';
4646
import { DashboardContext } from '../context/DashboardContext';
4747
import { ApplicationContext } from '../context/ApplicationContext';
4848
import { CommsContext } from '../context/CommsContext';
49+
import { AwsContext } from '../context/AwsContext';
4950

5051
// TYPESCRIPT
5152
interface StyleProps {
@@ -55,6 +56,7 @@ interface StyleProps {
5556

5657

5758
const Occupied = React.memo(() => {
59+
const { awsData, fetchAwsData } = useContext(AwsContext);
5860
const { setServicesData, app, setApp } = useContext(ApplicationContext);
5961
const { user, applications, getApplications, deleteApp, mode } = useContext(DashboardContext);
6062
const { commsData } = useContext(CommsContext);
@@ -89,6 +91,7 @@ const Occupied = React.memo(() => {
8991
setApp(selectedApp);
9092
setServicesData([]);
9193
setServiceModalOpen(true);
94+
fetchAwsData();
9295
}
9396
};
9497

app/components/Splash.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import '../stylesheets/Splash.scss';
33

44
const Splash: React.FC = React.memo((props) => {
55
const [visible, setVisible] = useState(false);
6-
// useEffect(() => {
7-
// setTimeout(() => setVisible(false), 4000);
8-
// }, []);
6+
useEffect(() => {
7+
setTimeout(() => setVisible(false), 4000);
8+
}, []);
99

1010
return (
1111
<>

app/containers/DashboardContainer.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import DashboardContextProvider from '../context/DashboardContext';
99
import DockerContextProvider from '../context/DockerContext';
1010
import EventContextProvider from '../context/EventContext';
1111
import QueryContextProvider from '../context/QueryContext';
12+
import AwsContextProvider from '../context/AwsContext';
1213
import '../stylesheets/Dashboard.scss';
1314

1415
const DashboardContainer = React.memo(() => {
1516
const [visible, setVisible] = useState(true);
1617

17-
// useEffect(() => {
18-
// setTimeout(() => setVisible(true), 4000);
19-
// }, []);
18+
useEffect(() => {
19+
setTimeout(() => setVisible(true), 4000);
20+
}, []);
2021

2122
return (
2223
<>
@@ -30,8 +31,10 @@ const DashboardContainer = React.memo(() => {
3031
<HealthContextProvider>
3132
<EventContextProvider>
3233
<QueryContextProvider>
33-
<SidebarContainer />
34-
<MainContainer />
34+
<AwsContextProvider>
35+
<SidebarContainer />
36+
<MainContainer />
37+
</AwsContextProvider>
3538
</QueryContextProvider>
3639
</EventContextProvider>
3740
</HealthContextProvider>

app/context/AwsContext.tsx

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React, { useCallback, useState } from 'react';
2+
import Electron from 'electron';
3+
import { transformData } from './helpers';
4+
const { ipcRenderer } = window.require('electron');
5+
6+
export const AwsContext = React.createContext<any>(null);
7+
8+
interface Props {
9+
children: any;
10+
}
11+
12+
const AwsContextProvider: React.FC<Props> = React.memo(({ children }) => {
13+
const [awsData, setAwsData] = useState<any>({ CPUUtilization: [], NetworkIn: [], NetworkOut: [], DiskReadBytes: [] })
14+
15+
const fetchAwsData = useCallback(() => {
16+
console.log('i am here in fetchawsdata!!!')
17+
ipcRenderer.send('awsMetricsRequest');
18+
19+
console.log('i am between the send and on!!!!!!!')
20+
ipcRenderer.on('awsMetricsResponse', (event: Electron.Event, data: any) => {
21+
console.log('data fetched from awsContext', data);
22+
// setAwsData(data);
23+
})
24+
25+
console.log('i am after the ipcrender!!!!')
26+
},[]);
27+
28+
return (
29+
<AwsContext.Provider
30+
value={{ awsData, fetchAwsData }}
31+
>
32+
{children}
33+
</AwsContext.Provider>
34+
);
35+
});
36+
37+
export default AwsContextProvider;

app/context/helpers.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/* eslint-disable camelcase */
33
import { useRef, useEffect } from 'react';
44

5-
console.log('category list at start is: ', categoryList);
65
let categoryList = [];
76

87
export function transformData(healthData) {

electron/routes/data.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import MetricsModel from '../models/MetricsModel';
1717

1818
const mongoFetch = fetchData.mongoFetch;
1919
const postgresFetch = fetchData.postgresFetch;
20+
const AWS = require('aws-sdk');
2021

2122
require('dotenv').config();
2223
// Initiate pool variable for SQL setup
@@ -307,6 +308,73 @@ ipcMain.on('kubernetesRequest', async (message) => {
307308
console.log('Error in "kubernetesRequest" event', message);
308309
message.sender.send('kubernetesResponse', {});
309310
}
311+
})
312+
313+
ipcMain.on('awsMetricsRequest', async (message: Electron.IpcMainEvent) => {
314+
try {
315+
// message.sender.send('awsMetricsResponse', 'hello from chronos team')
316+
// console.log('i am inside the ipcmain')
317+
318+
const cloudwatch = new AWS.CloudWatch({
319+
region: 'us-west-1',
320+
accessKeyId: 'AKIAU4VDDZOHUKZLQREU',
321+
secretAccessKey: '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'
322+
});
323+
324+
const metricsNamesArray = ['CPUUtilization', 'NetworkIn', 'NetworkOut', 'DiskReadBytes'];
325+
const awsData = {};
326+
327+
metricsNamesArray.forEach(metric => {
328+
const params = {
329+
EndTime: new Date(),
330+
MetricName: metric,
331+
Namespace: 'AWS/EC2',
332+
Period: 60,
333+
StartTime: new Date(new Date().getTime() - 60*60*1000),
334+
Statistics: ['Average'],
335+
Dimensions: [{
336+
Name: 'InstanceId',
337+
Value: 'i-0c5656a0366bc6027'
338+
}]
339+
}
340+
341+
const fetchData = async () => {
342+
try {
343+
const data = await cloudwatch.getMetricStatistics(params).promise();
344+
// console.log(data)
345+
346+
const newData = data.Datapoints.map((el, i) => {
347+
let transformedData = {};
348+
349+
transformedData['time'] = data.Datapoints[i].Timestamp,
350+
transformedData['metric'] = data.Label,
351+
transformedData['value'] = data.Datapoints[i].Average,
352+
transformedData['unit'] = data.Datapoints[i].Unit
353+
354+
// console.log(transformedData);
355+
// arrayTest.push(transformedData);
356+
return transformedData;
357+
})
358+
359+
awsData[metric] = newData;
360+
// console.log(awsData)
361+
362+
return awsData; // final returned data
363+
} catch (err) {
364+
console.log(err);
365+
}
366+
}
367+
368+
fetchData().then(data => {
369+
console.log(data)
370+
371+
message.sender.send('awsMetricsResponse', JSON.stringify(data)) // send data to frontend
372+
})
373+
})
374+
} catch (err) {
375+
console.log('Error in "awsMetricsRequest" event', message);
376+
message.sender.send('awsMetricsResponse', { CPUUtilization: [], NetworkIn: [], NetworkOut: [], DiskReadBytes: [] });
377+
}
310378
});
311379

312380

examples/aws/data.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
const AWS = require('aws-sdk');
2+
const path = require('path');
3+
require('dotenv').config({
4+
path: path.resolve(__dirname, './.env')
5+
});
6+
7+
8+
const awsHelpers = {};
9+
10+
awsHelpers.getAwsMetrics = async () => {
11+
12+
const cloudwatch = new AWS.CloudWatch({
13+
region: 'us-west-1',
14+
accessKeyId: process.env.AWS_ACCESS_KEY,
15+
secretAccessKey: process.env.AWS_SECRET_KEY
16+
});
17+
18+
const metricsNamesArray = ['CPUUtilization', 'NetworkIn', 'NetworkOut', 'DiskReadBytes'];
19+
const awsData = {};
20+
21+
metricsNamesArray.forEach(metric => {
22+
const params = {
23+
EndTime: new Date(),
24+
MetricName: metric,
25+
Namespace: 'AWS/EC2',
26+
Period: 60,
27+
StartTime: new Date(new Date().getTime() - 60*60*1000),
28+
Statistics: ['Average'],
29+
Dimensions: [{
30+
Name: 'InstanceId',
31+
Value: 'i-0c5656a0366bc6027'
32+
}]
33+
}
34+
35+
const fetchData = async () => {
36+
try {
37+
const data = await cloudwatch.getMetricStatistics(params).promise();
38+
// console.log(data)
39+
40+
const newData = data.Datapoints.map((el, i) => {
41+
let transformedData = {};
42+
43+
transformedData['time'] = data.Datapoints[i].Timestamp,
44+
transformedData['metric'] = data.Label,
45+
transformedData['value'] = data.Datapoints[i].Average,
46+
transformedData['unit'] = data.Datapoints[i].Unit
47+
48+
// console.log(transformedData);
49+
// arrayTest.push(transformedData);
50+
return transformedData;
51+
})
52+
53+
awsData[metric] = newData;
54+
// console.log(awsData)
55+
56+
return awsData; // final returned data
57+
} catch (err) {
58+
console.log(err);
59+
}
60+
}
61+
62+
fetchData().then(data => {
63+
console.log(data)
64+
65+
// send data to frontend
66+
})
67+
})
68+
}
69+
70+
71+
72+
// awsHelpers.getAwsMetrics();
73+
// console.log(Promise.resolve(final))
74+
// console.log(final);
75+
76+
77+
78+
79+
// const metricStatisticsObject = {};
80+
81+
// awsHelpers.getMetricStatistics = async () => {
82+
// try{
83+
// const data = await cloudwatch.getMetricStatistics(CPUUtilization).promise();
84+
85+
// metricStatisticsObject[CPUUtilization.MetricName] = data;
86+
// return metricStatisticsObject;
87+
// }catch(err){
88+
// console.log(err);
89+
// }
90+
// };
91+
// getMetricStatistics().then(metricStatisticsObject =>{
92+
// console.log(metricStatisticsObject[CPUUtilization.MetricName].Datapoints.map(datapoint => datapoint.Average))
93+
// })
94+
95+
96+
module.exports = awsHelpers;

0 commit comments

Comments
 (0)