This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflow_parse.js
242 lines (218 loc) · 7.67 KB
/
flow_parse.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
//Outputs Date and Time in a more readable format
var logFolder = __dirname+"/logs";
const fs = require("fs");
const { time } = require("console");
var flow_log;
const readline = require("readline-sync");
const path = require('path');
function date_time(date) {
var arr = date.split("T");
return "Date: " + arr[0] + " Time: " + arr[1];
}
//Function made by Daroush that allows the user to select a certain log by number from the logs folder
function selectLog(){
let count = 1
let files = fs.readdirSync(logFolder)
var folder_log = []
files.forEach(function(folder){
var fromPath = path.join(logFolder,folder)
stat_folder = fs.statSync(fromPath)
if(!stat_folder.isFile()){
console.log(count+". "+fromPath)
count++
folder_log.push(fromPath)
}
});
var selection = readline.question("Select the number of the folder you want to analyze from: \n")
logFolder = folder_log[selection-1]
count=0;
var files2={};
fs.readdirSync(logFolder).forEach(file => {
if(file.includes('.json')){
files2[count]=file;
console.log(count+". "+file);
count++;
}
});
var selection = readline.question(`\nselect log\n`);
if(selection=='q'){
return
}
while(selection<0||selection>=count){
selection = readline.question("Not a valid log, enter again\n")
if(selection=='q'){
return
}
}
if(selection>=0 && selection<count){
return logFolder+"/"+files2[selection];
}
}
//Outputs flow logs that match the filter requirement of an attribute given by the user
//TODO: filter by multiple attributes
function filter_by(flow_log, attributes, filter) {
var allowed = true;
var arr = flow_log.flow_logs;
var valid_arr = [];
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < attributes.length; j++) {
if (!(arr[i][attributes[j]].toString() === filter[j].toString())) {
allowed = false;
}
}
if (allowed) {
valid_arr.push(arr[i]);
}
allowed = true;
}
for (var i = 0; i < valid_arr.length; i++) {
console.log(valid_arr[i]);
}
}
//returns time elasped in seconds from two inputted dates
function time_elapsed(start_date, end_date) {
var end_time = new Date(end_date);
var start_time = new Date(start_date);
var sec_elapsed = (end_time.getTime() - start_time.getTime()) / 1000;
return (
"Time elapsed in seconds: " +
(end_time.getTime() - start_time.getTime()) / 1000
);
}
//reads in json file
//TODO: add implementation which filters through each of the json files rather than individual ones
function input() {
let input_files = fs.readFileSync(selectLog())
flow_log = JSON.parse(input_files);
main()
}
function for_mat(flog, tabs) {
var s = "";
for (k in flog) {
if (typeof flog[k] === "object") {
s += k + "\n" + for_mat(flog[k], "\t");
} else {
s += tabs + k + ": " + flog[k] + "\n";
}
}
return s;
}
function output(file_name) {
var format_flow = for_mat(flow_log, "");
console.log(flow_log);
//let write_file = fs.writeFileSync()
fs.writeFile(file_name, format_flow, err => {
if (err) {
console.log("Error reading file from disk:", err);
return;
}
try {
return;
} catch (err) {
console.log("Error parsing JSON string:", err);
return;
}
});
}
function main() {
do {
var option;
option = readline.question(`choose option
1. print flowlogs
2. filter flow logs by attributes
3. save to a file
q. return to home prompt
\n`);
switch(option) {
case "1":
console.log("you've selected option 1:\n\n");
console.log(flow_log)
break;
case "2":
var breakout = false
var keys = []
console.log("Attributes to filter by: \n\n");
var count = 0;
for(var k in flow_log.flow_logs[0]){
count+=1;
console.log(count+". "+k)
keys.push(k)
}
var attributes = [];
var filters = [];
var amt = readline.question("How many attributes do you want to filter by? Press q to quit\n")
if(amt=='q'){
break;
}
for(var i=0;i<amt;i++){
var attribute = readline.question(i+1+". Choose an attribute to filter by (type out the name or choose a number): \n")
if(attribute=='q'){
break;
}
if(attribute.length<=2){
while(attribute>=22||attribute<=0){
attribute = readline.question("Invalid number choice, choose a new number or q to quit\n")
if(attribute==='q'){
break
}
}
attribute = keys[attribute-1]
}
while(!keys.includes(attribute)){
attribute = readline.question("Please retype the attribute you want to filter by or press q to quit\n")
if(attribute==='q'){
break
}
}
if(attribute==='q'){
break
}
var list_val = []
attributes.push(attribute)
for(var j=0;j<flow_log.flow_logs.length;j++){
list_val.push(flow_log.flow_logs[j][attribute])
}
var unique = list_val.filter((v, s, a) => a.indexOf(v) === s);
for(var j=0;j<unique.length;j++){
console.log(String.fromCharCode(97 + j)+". "+unique[j])
}
var filter = readline.question(i+1+". Choose the value of that attribute you want to filter by: \n")
if(filter>='a'&&filter<=String.fromCharCode(97 + j)){
filter = unique[filter.charCodeAt(0) - 97]
}
while(!unique.includes(filter)){
filter = readline.question("Please retype the filter or press q to quit\n")
if(filter==='q'){
breakout = true
break
}
}
if(filter==='q'){
breakout = true
break;
}
filters.push(filter)
}
if(breakout){
break;
}
filter_by(flow_log, attributes,filters);
break;
case "3":
var file_name = readline.question("Type in the file you want to save to: \n")
if(file_name=='q'){
break
}
output(file_name)
break;
case "q":
console.log("exiting...\n\n\n\n\n");
option=-1;
break;
default:
console.log("invalid option\n\n\n\n\n");
}
} while (option != -1);
}
module.exports.input = input;
module.exports.filter_by = filter_by;