-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
267 lines (237 loc) · 9.92 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
const AWS = require('@aws-sdk/client-sqs');
const AWS_EC2 = require('@aws-sdk/client-ec2');
const { StaleIpPermission } = require('@aws-sdk/client-ec2');
let mainWindow;
let QURL;
function startTimer() {
timer.start();
setTimeout(stopTimer, 60);
}
function stopTimer() {
timer.stop();
}
function createWindow() {
// Get the screen size
const { screen } = require('electron');
const { width: screenWidth, height: screenHeight } = screen.getPrimaryDisplay().workAreaSize;
// Create the browser window.
mainWindow = new BrowserWindow({
height: 830,
width: 1123,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
devTools: !app.isPackaged,
},
});
// and load the index.html of the app.
mainWindow.loadFile('index.html');
// Open the DevTools.
//mainWindow.webContents.openDevTools();
// Open URLS external browser
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
// config.fileProtocol is my custom file protocol
if (url.startsWith(config.fileProtocol)) {
return { action: 'allow' };
}
// open url in a browser and prevent default
shell.openExternal(url);
return { action: 'deny' };
});
}
function onLoad() {
//console.log('server process is ready');
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow();
onLoad();
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
ipcMain.on('connectivity', function (event, region, profile) {
try {
const sqs = new AWS.SQS({ region: region, profile: profile });
var params = {
MaxResults: 1,
};
sqs.listQueues(params, function (err, data) {
if (err) {
mainWindow.webContents.send('exception', err); // Send the response to the renderer
} // an error occurred
else {
mainWindow.webContents.send('connectivity', data.$metadata);
} // successful response
});
} catch (error) {
mainWindow.webContents.send('exception', err); // Send the response to the renderer
}
});
ipcMain.on('getqueueslist', function (event, region, profile) {
try {
const sqs = new AWS.SQS({ region: region, profile: profile });
var params = {
MaxResults: 1000,
};
sqs.listQueues(params, function (err, data) {
if (err) {
//console.log('Error in SQS GetQueueURL');
mainWindow.webContents.send('exception', err); // Send the response to the renderer
} // an error occurred
else {
////console.log(data);
mainWindow.webContents.send('getqueueslist', data.QueueUrls);
} // successful response
});
} catch (error) {
//console.log('Inside getqueueslist Catch');
mainWindow.webContents.send('exception', err); // Send the response to the renderer
}
});
ipcMain.on('queueStats', function (event, region, profile) {
});
ipcMain.on('listqueue', function (event, msgno, sqsqueue, region, profile, filter) {
var NoOfMessages;
var payload;
try {
const sqs = new AWS.SQS({ region: region, profile: profile });
var queuename = sqsqueue.replace(/\s+/g, ' ').trim();
var params = {
QueueName: queuename,
};
sqs.getQueueUrl(params, function (err, data) {
if (err) {
//console.log('Error in SQS GetQueueURL');
mainWindow.webContents.send('exception', err); // Send the response to the renderer
} else {
QURL = data.QueueUrl;
var para = {
AttributeNames: ['All'],
MaxNumberOfMessages: 10,
MessageAttributeNames: ['All'],
QueueUrl: QURL,
WaitTimeSeconds: 20, // Long Polling Enabled
};
try {
sqs.getQueueAttributes(para, async function (err, data) {
if (err) {
//console.log('Error Getting Queue Attributes');
mainWindow.webContents.send('exception', err); // Send the response to the renderer
} else {
NoOfMessages = data.Attributes.ApproximateNumberOfMessages;
approx_notvisible = data.Attributes.ApproximateNumberOfMessagesNotVisible;
message = 'Approximate Messages Can be read/Visible:' + NoOfMessages + '\nApproximate Messages In Transit/Not Visible:' + approx_notvisible;
mainWindow.webContents.send('queueinfo', message); // Send the response to the renderer
// wait for 2 seconds
await new Promise(r => setTimeout(r, 2000));
var stats = {
"OnTransit": approx_notvisible,
"TotalMessages": NoOfMessages
};
// If the msg no is -1, then set it to the number of messages in the queue
if (msgno == -1){
msgno = parseInt(NoOfMessages);
}
// Case1: If the queue is empty
if (parseInt(NoOfMessages) == 0 && approx_notvisible == 0) {
message = 'Queue must be empty, No messages in Transit or available state';
mainWindow.webContents.send('noMessages', message);
}
// Case2: If the queue has messages but not visible
else if (parseInt(NoOfMessages) == 0 && approx_notvisible > 0) {
message = 'Your messages are in Transit state : ' + approx_notvisible + '\n\nYou need to wait until the Default visibilty timeout, Default 30s';
mainWindow.webContents.send('noMessages', message);
}
// Case3: If the queue has messages less than the requested number
else if (parseInt(NoOfMessages) < parseInt(msgno)) {
// set the message number to the number of messages in the queue
msgno = parseInt(NoOfMessages);
var TotalReceivedCount = 0;
payload = {};
// Add the Approx Not Visible count and the visible messages count to the payload
payload['stats'] = stats;
payload['messages'] = [];
var barWidth = 'width: 0%';
while (parseInt(TotalReceivedCount) < parseInt(NoOfMessages)) {
TotalReceivedCount += 1;
try {
const data = await sqs.receiveMessage(para);
if (data) {
barWidth = 'width: ' + (payload['messages'].length / msgno) * 100 + '%';
mainWindow.webContents.send('barProgress', barWidth);
data.Messages.forEach((e, index) => {
payload['messages'].push(data.Messages[index]);
});
if (payload['messages'].length == parseInt(NoOfMessages)) {
message = 'Queue has less messages than you asked, So printing all the messages available.';
mainWindow.webContents.send('finalList', message);
barWidth = 'width: 100%';
mainWindow.webContents.send('barProgress', barWidth);
mainWindow.webContents.send('listqueue', payload);
TotalReceivedCount = 10;
return;
} else {
//console.log('Enters Else');
}
}
} catch (error) {
mainWindow.webContents.send('exception', err);
break;
}
}
}
// Case 4: If the queue has messages more than the requested number
else {
var TotalReceivedCount = 0;
payload = {};
payload['stats'] = stats;
payload['messages'] = [];
// payload.push(stats);
var barWidth = 'width: 0%';
while (parseInt(TotalReceivedCount) < parseInt(NoOfMessages)) {
TotalReceivedCount += 1;
try {
const data = await sqs.receiveMessage(para);
if (data) {
barWidth = 'width: ' + (payload['messages'].length / msgno) * 100 + '%';
mainWindow.webContents.send('barProgress', barWidth);
data.Messages.forEach((e, index) => {
payload['messages'].push(data.Messages[index]);
});
if (payload['messages'].length >= msgno) {
barWidth = 'width: 100%';
mainWindow.webContents.send('barProgress', barWidth);
mainWindow.webContents.send('listqueue', payload);
TotalReceivedCount = 10;
return;
} else {
//console.log('Enters Else');
}
}
} catch (error) {
mainWindow.webContents.send('exception', "Unexpected Error has occured. Possibly due to less number of messages"); // Send the response to the renderer
break;
}
} //Close While loop
}
}
});
} catch (error) {
mainWindow.webContents.send('exception', error); // Send the response to the renderer
}
}
});
} catch (error) {
mainWindow.webContents.send('exception', err); // Send the response to the renderer
}
});