-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogAnalyzer.java
More file actions
159 lines (141 loc) · 4.43 KB
/
LogAnalyzer.java
File metadata and controls
159 lines (141 loc) · 4.43 KB
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
/**
* Write a description of class LogAnalyzer here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.*;
import edu.duke.*;
public class LogAnalyzer
{
private ArrayList<LogEntry> records;
public LogAnalyzer() {
records = new ArrayList<LogEntry>();
}
public void readFile(String filename) {
FileResource fr = new FileResource(filename);
for(String line : fr.lines()){
records.add(WebLogParser.parseEntry(line));
}
}
public void printAll() {
for (LogEntry le : records) {
System.out.println(le);
}
}
public int countUniqueIPs(){
ArrayList<String> uniqueIPs = new ArrayList<String>();
for(LogEntry le : records){
String ipAddr = le.getIpAddress();
if(!uniqueIPs.contains(ipAddr)){
uniqueIPs.add(ipAddr);
}
}
return uniqueIPs.size();
}
public void printAllHigherThanNum(int num){
for(LogEntry le : records){
if(le.getStatusCode() > num){
System.out.println(le);
}
}
}
public void uniqueIPVisitsOnDay(String someday){
ArrayList<String> uniqueIPsOnDay = new ArrayList<String>();
for(LogEntry le : records){
Date d = le.getAccessTime();
String str = d.toString();
str = str.substring(4,10);
if(someday.equals(str)){
if(!uniqueIPsOnDay.contains(le.getIpAddress()))
uniqueIPsOnDay.add(le.getIpAddress());}
}
System.out.println(uniqueIPsOnDay);
System.out.println(uniqueIPsOnDay.size());
}
public int countUniqueIPsInRange(int low, int high){
ArrayList<String> uniqueIPsInRange = new ArrayList<String>();
for(LogEntry le : records){
if(le.getStatusCode() >= low && le.getStatusCode() <= high){
if(!uniqueIPsInRange.contains(le.getIpAddress())){
uniqueIPsInRange.add(le.getIpAddress());
}
}
}
return uniqueIPsInRange.size();
}
public HashMap<String,Integer> countVisitsPerIP(){
HashMap<String,Integer> counts = new HashMap<String,Integer>();
for(LogEntry le : records){
String ip = le.getIpAddress();
if(!counts.containsKey(ip)){
counts.put(ip,1);
}
else{
counts.put(ip,counts.get(ip)+1);
}
}
return counts;
}
public int mostNumberVisitsByIP(HashMap<String,Integer> counts){
int max = 0;
for(Integer v : counts.values()){
if(v > max){
max = v;
}
}
return max;
}
public ArrayList<String> iPsMostVisits(HashMap<String,Integer> counts ){
ArrayList<String> MostVisitIp = new ArrayList<String>();
int max = mostNumberVisitsByIP(counts);
for(String s : counts.keySet()){
if(counts.get(s) == max){
MostVisitIp.add(s);
}
}
return MostVisitIp;
}
public HashMap<String,ArrayList<String>> iPsForDays(){
HashMap<String,ArrayList<String>> ipDays = new HashMap<String,ArrayList<String>>();
for(LogEntry le : records){
ArrayList<String> myList = new ArrayList<String>();
String ip = le.getIpAddress();
String date = le.getAccessTime().toString().substring(4,10);
if(!ipDays.containsKey(date)){
myList.add(ip);
ipDays.put(date,myList);
}
else{
myList = ipDays.get(date);
myList.add(ip);
ipDays.put(date,myList);
}
}
return ipDays;
}
public String dayWithMostIPVisits(HashMap<String,ArrayList<String>> ipDays){
int max = 0;
String ans = "";
for(ArrayList<String> myList : ipDays.values()){
if(myList.size() > max){
max = myList.size();
}
}
for(String k : ipDays.keySet()){
if((ipDays.get(k)).size() == max ){
ans = k;
}
}
return ans;
}
public ArrayList<String> iPsWithMostVisitsOnDay(HashMap<String,ArrayList<String>> ipDays, String someday){
ArrayList<String> myList = new ArrayList<String>();
for(String k : ipDays.keySet()){
if(k.equals(someday)){
myList = ipDays.get(k);
}
}
return myList;
}
}