1 | package util;
|
---|
2 |
|
---|
3 | import java.io.BufferedReader;
|
---|
4 | import java.io.FileReader;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.time.LocalDate;
|
---|
7 | import java.time.LocalTime;
|
---|
8 | import java.time.format.DateTimeFormatter;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.regex.Matcher;
|
---|
11 | import java.util.regex.Pattern;
|
---|
12 |
|
---|
13 | //import MySQLAccess.Tuple;
|
---|
14 |
|
---|
15 |
|
---|
16 | public class MacroniserLogFileProcessor {
|
---|
17 | static boolean debug = false;
|
---|
18 | /** ARGUMENTS:
|
---|
19 | * /home/wjkw1/RESEARCH_2018-19/bash_test/loggingtest.log
|
---|
20 | * /home/wjkw1/comp520/MacroniserLogs/Week02/macron.log
|
---|
21 | */
|
---|
22 |
|
---|
23 | static final String directInputRegexp = "INFO : \\[\\d\\d\\d\\d\\-\\d\\d\\-\\d\\d \\d\\d:\\d\\d:\\d\\d\\] DirectInput.doPost\\(\\)";
|
---|
24 | static final String fileUploadRegexp = "ERROR: \\[\\d\\d\\d\\d\\-\\d\\d\\-\\d\\d \\d\\d:\\d\\d:\\d\\d\\] FileUpload.doPost\\(\\)";
|
---|
25 |
|
---|
26 | static ArrayList<MacroniserLogFileData> extractedEntries;
|
---|
27 |
|
---|
28 | public static void main(String[] args) {
|
---|
29 | checkArgs(args.length);
|
---|
30 | String filename = args[0];
|
---|
31 | //extracts using yesterdays date
|
---|
32 | extractFromLogFile(filename);
|
---|
33 |
|
---|
34 |
|
---|
35 | if(debug) {
|
---|
36 | int count = 0;
|
---|
37 | for (MacroniserLogFileData entry : extractedEntries) {
|
---|
38 | printMessage((++count) + ": " + entry.toString());
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | if(exportToDB()) {
|
---|
43 | System.out.println("Success. Move the log file " + filename + " to processed folder");
|
---|
44 | System.exit(0);
|
---|
45 | } else {
|
---|
46 | System.err.println("FAILED. Move the log file " + filename + " to reprocess folder");
|
---|
47 | System.exit(-1);
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | //Goal is to extract information from here to DB
|
---|
52 | private static boolean exportToDB() {
|
---|
53 |
|
---|
54 | boolean success = false;
|
---|
55 | MySQLAccess sqlAccess = new MySQLAccess();
|
---|
56 |
|
---|
57 | try {
|
---|
58 |
|
---|
59 | sqlAccess.makeConnection();
|
---|
60 |
|
---|
61 | //loop through all entries
|
---|
62 | for (MacroniserLogFileData entry : extractedEntries) {
|
---|
63 | //get the marked words from first entry
|
---|
64 | ArrayList<String> markedWords = getMarkedWordsFromOutput(entry.getOutputText());
|
---|
65 | // all these markedWords share the same date and time
|
---|
66 | LocalDate date = entry.getDate();
|
---|
67 | LocalTime time = entry.getTime();
|
---|
68 |
|
---|
69 | if (markedWords == null) { // no words, enter NULL into db
|
---|
70 | if(debug) {
|
---|
71 | printMessage("word=NULL");
|
---|
72 | printMessage(date.toString());
|
---|
73 | printMessage(time.format(DateTimeFormatter.ofPattern("HH:mm:ss")));
|
---|
74 | printMessage(""); // newline
|
---|
75 | }
|
---|
76 |
|
---|
77 | MySQLAccess.Tuple tuple = new MySQLAccess.Tuple(null,date,time);
|
---|
78 | // b. add to DB
|
---|
79 | sqlAccess.addNewEntry(tuple);
|
---|
80 | } else { // process all the words
|
---|
81 | for (String word : markedWords) {
|
---|
82 | if(debug) {
|
---|
83 | printMessage(word);
|
---|
84 | printMessage(date.toString());
|
---|
85 | printMessage(time.format(DateTimeFormatter.ofPattern("HH:mm:ss")));
|
---|
86 | printMessage(""); // newline
|
---|
87 | }
|
---|
88 |
|
---|
89 | //insert all into database:
|
---|
90 | // a. create the tuple: always entering into db as lowercase so we don't consider tÅ different from TÅ
|
---|
91 | // whether when inserting into DB or searching for the term
|
---|
92 | MySQLAccess.Tuple tuple = new MySQLAccess.Tuple(word.toLowerCase(),date,time);
|
---|
93 | // b. add to DB
|
---|
94 | sqlAccess.addNewEntry(tuple);
|
---|
95 | }
|
---|
96 |
|
---|
97 | }
|
---|
98 | }
|
---|
99 | success = true;
|
---|
100 |
|
---|
101 | } catch (Exception e) {
|
---|
102 | e.printStackTrace(); // goes to std.err, see https://stackoverflow.com/questions/12095378/difference-between-e-printstacktrace-and-system-out-printlne
|
---|
103 | success = false;
|
---|
104 |
|
---|
105 | } finally {
|
---|
106 | sqlAccess.closeConnection();
|
---|
107 |
|
---|
108 | }
|
---|
109 | return success;
|
---|
110 | }
|
---|
111 |
|
---|
112 | //returns an array list of all marked words, null if there are none
|
---|
113 | private static ArrayList<String> getMarkedWordsFromOutput(String outputText) {
|
---|
114 | if(outputText == null) { return null; }
|
---|
115 |
|
---|
116 | final Pattern TAG_REGEXP = Pattern.compile("<mark>(.+?)</mark>", Pattern.DOTALL);
|
---|
117 | final Matcher matcher = TAG_REGEXP.matcher(outputText);
|
---|
118 | ArrayList<String> markedWords = new ArrayList<>();
|
---|
119 | //find all values from matches
|
---|
120 | while (matcher.find()){
|
---|
121 | String s = matcher.group(1);
|
---|
122 | markedWords.add(s);
|
---|
123 | }
|
---|
124 | return markedWords;
|
---|
125 | }
|
---|
126 |
|
---|
127 | //reads the log file and creates a list of data
|
---|
128 | private static void extractFromLogFile(String filename) {
|
---|
129 |
|
---|
130 | BufferedReader br = null;
|
---|
131 | try {
|
---|
132 | br = new BufferedReader(new FileReader(filename));
|
---|
133 | extractedEntries = new ArrayList<>();
|
---|
134 |
|
---|
135 | boolean firstTagFound = false;
|
---|
136 | String line = "";
|
---|
137 | StringBuilder extractedSB = new StringBuilder();
|
---|
138 |
|
---|
139 | while ((line = br.readLine())!= null){
|
---|
140 | if(!firstTagFound){
|
---|
141 | //check for tag
|
---|
142 | firstTagFound = doesLineMatch(line);
|
---|
143 | //add to list if the first match
|
---|
144 | if(firstTagFound) {
|
---|
145 | extractedSB.append(line);
|
---|
146 | continue;
|
---|
147 | }
|
---|
148 | }else {
|
---|
149 | //if line is a match, remove and keep
|
---|
150 | if(doesLineMatch(line)){
|
---|
151 | //turn the string builder text into MacroniserLogFileData object
|
---|
152 | MacroniserLogFileData entry = transformLogFileStringToObject(extractedSB.toString());
|
---|
153 | //perform operations if not null, else error
|
---|
154 | if(entry != null) {
|
---|
155 | extractedEntries.add(entry);
|
---|
156 | //remove old content and keep new tag
|
---|
157 | extractedSB.setLength(0);
|
---|
158 | extractedSB.append(line);
|
---|
159 | } else{
|
---|
160 | //printErrorMsg("Parsing of entry in log file found an error, continuing on next lines...");
|
---|
161 | extractedSB.setLength(0);
|
---|
162 | extractedSB.append(line);
|
---|
163 | }
|
---|
164 |
|
---|
165 | }else {
|
---|
166 | //keep the line
|
---|
167 | extractedSB.append(line);
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | } catch (Exception ex) {
|
---|
173 | ex.printStackTrace();
|
---|
174 | } finally {
|
---|
175 | if(br != null){
|
---|
176 | try {
|
---|
177 | br.close();
|
---|
178 | } catch (IOException e) {
|
---|
179 | e.printStackTrace();
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | }
|
---|
185 |
|
---|
186 | //Changes the string into a MacorniserLogFileData object
|
---|
187 | private static MacroniserLogFileData transformLogFileStringToObject(String extractedString) {
|
---|
188 | MacroniserLogFileData entry;
|
---|
189 | LocalDate entryDate;
|
---|
190 | LocalTime entryTime;
|
---|
191 | String entryInput, entryOutput;
|
---|
192 |
|
---|
193 | //Get the input output portion of the string
|
---|
194 | String input_output = "";
|
---|
195 | if (extractedString.charAt(0) == 'I') {
|
---|
196 |
|
---|
197 | //Get the date and time of entry
|
---|
198 | int INDEX_STARTOF_DATE = 8, INDEX_ENDOF_DATE = 18, INDEX_STARTOF_TIME = 19, INDEX_ENDOF_TIME = 27;
|
---|
199 |
|
---|
200 | entryDate = LocalDate.parse(extractedString.substring(INDEX_STARTOF_DATE, INDEX_ENDOF_DATE));
|
---|
201 |
|
---|
202 | entryTime = LocalTime.parse(extractedString.substring(INDEX_STARTOF_TIME, INDEX_ENDOF_TIME));
|
---|
203 |
|
---|
204 | //Get the input and output
|
---|
205 | input_output = extractedString.replaceAll(directInputRegexp, "").trim();
|
---|
206 |
|
---|
207 | int INDEX_STARTOF_INPUT = 6;
|
---|
208 | int INDEX_ENDOF_INPUT = getEndofInputIndex(input_output);
|
---|
209 | int INDEX_STARTOF_OUTPUT = INDEX_ENDOF_INPUT + 8;
|
---|
210 | int INDEX_ENDOF_OUTPUT = input_output.length();
|
---|
211 | if (INDEX_ENDOF_INPUT == -1) {
|
---|
212 | entryInput = null;
|
---|
213 | entryOutput = null;
|
---|
214 |
|
---|
215 | } else {
|
---|
216 | entryInput = input_output.substring(INDEX_STARTOF_INPUT, INDEX_ENDOF_INPUT);
|
---|
217 | entryOutput = input_output.substring(INDEX_STARTOF_OUTPUT, INDEX_ENDOF_OUTPUT);
|
---|
218 | }
|
---|
219 | entry = new MacroniserLogFileData(entryDate, entryTime, entryInput, entryOutput);
|
---|
220 |
|
---|
221 | return entry;
|
---|
222 |
|
---|
223 | } else if (extractedString.charAt(0) == 'E') {
|
---|
224 | input_output = extractedString.replaceAll(fileUploadRegexp, "");
|
---|
225 | return null;
|
---|
226 | } else {
|
---|
227 | printErrorMsg("Unknown entry in log file.");
|
---|
228 | return null;
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | //gets the end of input index, protects against log file 'input' that includes the tag
|
---|
233 | private static int getEndofInputIndex(String str) {
|
---|
234 | ArrayList<Integer> indexes = new ArrayList<>();
|
---|
235 | String outputTag = "Output: ";
|
---|
236 |
|
---|
237 | //loop through and find all "Output: " Strings
|
---|
238 | int i = str.indexOf(outputTag);
|
---|
239 | while(i >= 0){
|
---|
240 | indexes.add(i);
|
---|
241 | i = str.indexOf(outputTag, i+1);
|
---|
242 | }
|
---|
243 |
|
---|
244 | //return the correct index
|
---|
245 | if(indexes.size() >= 1){
|
---|
246 | int middle_index = (indexes.size() / 2);
|
---|
247 | return indexes.get(middle_index);
|
---|
248 | } else {
|
---|
249 | printMessage("Warning: No output tag could be found. Probably NULL input.");
|
---|
250 | return -1;
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | //Does line match the regexp
|
---|
255 | private static boolean doesLineMatch(String line) {
|
---|
256 | return (line.matches(directInputRegexp) || line.matches(fileUploadRegexp));
|
---|
257 | }
|
---|
258 |
|
---|
259 | //checks the arguments
|
---|
260 | private static void checkArgs(int numArgs) {
|
---|
261 | if( numArgs != 1){
|
---|
262 | printErrorMsg("Incorrect number of arguments given - "+numArgs);
|
---|
263 | printUsage();
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | //Prints a message to std output
|
---|
268 | private static void printMessage(String msg){
|
---|
269 | System.out.println(msg);
|
---|
270 | }
|
---|
271 |
|
---|
272 | //Prints the error message to "std error"
|
---|
273 | private static void printErrorMsg(String msg){
|
---|
274 | System.err.println("ERROR: "+ msg);
|
---|
275 | }
|
---|
276 |
|
---|
277 | //Prints the usage of this program to "std error"
|
---|
278 | private static void printUsage(){
|
---|
279 | System.err.println("Usage: ");
|
---|
280 | System.err.println("\tjava LogFileExtraction <log file>");
|
---|
281 | System.err.println("\tWhere 'log file' is the log file of the Macroniser - macron.log");
|
---|
282 | }
|
---|
283 | }
|
---|