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