Skip to content

Commit eaff885

Browse files
authored
Add files via upload
1 parent c14498a commit eaff885

File tree

18 files changed

+1835
-0
lines changed

18 files changed

+1835
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
/**
3+
* Write a description of class LogAnalyzer here.
4+
*
5+
* @author (your name)
6+
* @version (a version number or a date)
7+
*/
8+
9+
import java.util.*;
10+
import edu.duke.*;
11+
12+
public class LogAnalyzer
13+
{
14+
private ArrayList<LogEntry> records;
15+
16+
public LogAnalyzer() {
17+
// complete constructor
18+
}
19+
20+
public void readFile(String filename) {
21+
// complete method
22+
}
23+
24+
public void printAll() {
25+
for (LogEntry le : records) {
26+
System.out.println(le);
27+
}
28+
}
29+
30+
31+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
/**
3+
* Write a description of class LogRecord here.
4+
*
5+
* @author (your name)
6+
* @version (a version number or a date)
7+
*/
8+
9+
import java.util.*;
10+
public class LogEntry
11+
{
12+
private String ipAddress;
13+
private Date accessTime;
14+
private String request;
15+
private int statusCode;
16+
private int bytesReturned;
17+
18+
public LogEntry(String ip, Date time, String req, int status, int bytes) {
19+
ipAddress = ip;
20+
accessTime = time;
21+
request = req;
22+
statusCode = status;
23+
bytesReturned = bytes;
24+
25+
}
26+
27+
public String getIpAddress() {
28+
return ipAddress;
29+
}
30+
public Date getAccessTime() {
31+
return accessTime;
32+
}
33+
public String getRequest() {
34+
return request;
35+
}
36+
public int getStatusCode() {
37+
return statusCode;
38+
}
39+
public int getBytesReturned() {
40+
return bytesReturned;
41+
}
42+
43+
public String toString() {
44+
return ipAddress + " " + accessTime + " " + request
45+
+ " " + statusCode + " " + bytesReturned;
46+
}
47+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
/**
3+
* Write a description of class Tester here.
4+
*
5+
* @author (your name)
6+
* @version (a version number or a date)
7+
*/
8+
9+
import java.util.*;
10+
11+
public class Tester
12+
{
13+
public void testLogEntry() {
14+
LogEntry le = new LogEntry("1.2.3.4", new Date(), "example request", 200, 500);
15+
System.out.println(le);
16+
LogEntry le2 = new LogEntry("1.2.100.4", new Date(), "example request 2", 300, 400);
17+
System.out.println(le2);
18+
}
19+
20+
public void testLogAnalyzer() {
21+
// complete method
22+
}
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.text.*;
2+
import java.util.*;
3+
4+
public class WebLogParser {
5+
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MMM/yyyy:kk:mm:ss Z", Locale.US);
6+
private static String munchTo(StringBuilder sb, String delim) {
7+
int x = sb.indexOf(delim);
8+
if (x == -1) {
9+
x = sb.length();
10+
}
11+
String ans = sb.substring(0,x);
12+
sb.delete(0, x + delim.length());
13+
return ans;
14+
}
15+
public static LogEntry parseEntry(String line) {
16+
//Assumes line is vald and in this format:
17+
//110.76.104.12 - - [30/Sep/2015:07:47:11 -0400] "GET //favicon.ico HTTP/1.1" 200 3426
18+
StringBuilder sb = new StringBuilder(line);
19+
String ip = munchTo(sb, " ");
20+
munchTo(sb, " "); //ignore -
21+
munchTo(sb, " ["); //ignore -, and eat the leading [
22+
String dateStr = munchTo(sb, "] \""); //]-space is intentional: eat both
23+
Date date = parseDate(dateStr);
24+
String request = munchTo(sb, "\" "); // quote-space is intentional: eat both
25+
String statusStr = munchTo(sb, " ");
26+
int status = Integer.parseInt(statusStr);
27+
String byteStr = munchTo(sb, " ");
28+
int bytes = Integer.parseInt(byteStr);
29+
return new LogEntry(ip, date, request, status, bytes);
30+
}
31+
public static Date parseDate(String dateStr) {
32+
ParsePosition pp = new ParsePosition(0);
33+
return dateFormat.parse(dateStr, pp);
34+
}
35+
36+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import edu.duke.FileResource;
2+
import java.util.ArrayList;
3+
import java.util.Date;
4+
import java.util.HashMap;
5+
6+
public class LogAnalyzer
7+
{
8+
private ArrayList<LogEntry> records;
9+
10+
public LogAnalyzer()
11+
{
12+
records = new ArrayList<LogEntry>();
13+
}
14+
15+
public void readFile(String filename)
16+
{
17+
FileResource fr = new FileResource(filename);
18+
for(String line: fr.lines())
19+
{
20+
records.add(WebLogParser.parseEntry(line));
21+
}
22+
23+
}
24+
25+
public int countUniqueIPs()
26+
{
27+
ArrayList<String> uniqueIPs = new ArrayList<String>();
28+
for(LogEntry le: records)
29+
{
30+
String ipAddress = le.getIpAddress();
31+
if(!uniqueIPs.contains(ipAddress))
32+
{
33+
uniqueIPs.add(ipAddress);
34+
}
35+
}
36+
return uniqueIPs.size();
37+
}
38+
39+
public void printAllHigherThanNum(int num)
40+
{
41+
for(LogEntry le: records)
42+
{
43+
int statusCode = le.getStatusCode();
44+
if(statusCode > num)
45+
{
46+
System.out.println("Status Code greater than " +num+ ": " + statusCode);
47+
}
48+
}
49+
}
50+
51+
public ArrayList uniqueIPVisitsOnDay (String someday)
52+
{
53+
ArrayList<String> uniqueIPs = new ArrayList<String>();
54+
for(LogEntry le: records)
55+
{
56+
Date d = le.getAccessTime();
57+
String date = d.toString();
58+
String ipAddress = le.getIpAddress();
59+
if(date.contains(someday) && !uniqueIPs.contains(ipAddress))
60+
{
61+
uniqueIPs.add(ipAddress);
62+
//System.out.println("Unique IPs on " +someday+ ": " + ipAddress);
63+
}
64+
}
65+
System.out.println("UNIQUE IPs on " + someday+ ": " + uniqueIPs.size());
66+
return uniqueIPs;
67+
}
68+
69+
public int countUniqueIPsInRange(int low, int high)
70+
{
71+
ArrayList<String> uniqueIPs = new ArrayList<String>();
72+
for(LogEntry le: records)
73+
{
74+
int statusCode = le.getStatusCode();
75+
String ipAddress = le.getIpAddress();
76+
if((statusCode >= low) && (statusCode<= high) && !uniqueIPs.contains(ipAddress))
77+
{
78+
uniqueIPs.add(ipAddress);
79+
}
80+
}
81+
System.out.println("UNIQUE IPs in range: " + uniqueIPs.size());
82+
return uniqueIPs.size();
83+
}
84+
85+
public void printAll()
86+
{
87+
for(LogEntry le: records)
88+
{
89+
System.out.println(le);
90+
}
91+
}
92+
93+
94+
95+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.util.Date;
2+
3+
public class LogEntry {
4+
private final String ipAddress;
5+
private final Date accessTime;
6+
private final String request;
7+
private final int statusCode;
8+
private final int bytesReturned;
9+
10+
public LogEntry(String ip, Date time, String req, int status, int bytes) {
11+
ipAddress = ip;
12+
accessTime = time;
13+
request = req;
14+
statusCode = status;
15+
bytesReturned = bytes;
16+
17+
}
18+
19+
public String getIpAddress() {
20+
return ipAddress;
21+
}
22+
23+
public Date getAccessTime() {
24+
return accessTime;
25+
}
26+
27+
public String getRequest() {
28+
return request;
29+
}
30+
31+
public int getStatusCode() {
32+
return statusCode;
33+
}
34+
35+
public int getBytesReturned() {
36+
return bytesReturned;
37+
}
38+
39+
public String toString() {
40+
return ipAddress + " " + accessTime + " " + request
41+
+ " " + statusCode + " " + bytesReturned;
42+
}
43+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import java.util.*;
2+
3+
public class Tester
4+
{
5+
public void testLogEntry()
6+
{
7+
LogEntry le = new LogEntry("1.2.3.4", new Date(), "example request", 200, 500);
8+
System.out.println(le);
9+
LogEntry le2 = new LogEntry("1.2.100.4", new Date(), "example request 2", 300, 400);
10+
System.out.println(le2);
11+
}
12+
13+
public void testLogAnalyzer()
14+
{
15+
LogAnalyzer test = new LogAnalyzer();
16+
test.readFile("short-test_log");
17+
test.printAll();
18+
}
19+
20+
public void testUniqueIP()
21+
{
22+
LogAnalyzer test = new LogAnalyzer();
23+
test.readFile("short-test_log");
24+
test.countUniqueIPs();
25+
test.uniqueIPVisitsOnDay("Sep 14");
26+
27+
}
28+
public void testprintAllHigherThanNum()
29+
{
30+
LogAnalyzer test = new LogAnalyzer();
31+
test.readFile("weblog1_log");
32+
test.printAllHigherThanNum(300);
33+
34+
}
35+
public void testuniqueIPVisitsOnDay()
36+
{
37+
LogAnalyzer test = new LogAnalyzer();
38+
test.readFile("weblog1_log");
39+
test.uniqueIPVisitsOnDay("Mar 24");
40+
//test.uniqueIPVisitsOnDay("Sep 30");
41+
}
42+
public void testcountUniqueIPsInRange()
43+
{
44+
LogAnalyzer test = new LogAnalyzer();
45+
test.readFile("weblog1_log");
46+
//test.countUniqueIPsInRange(200, 299);
47+
test.countUniqueIPsInRange(300, 399);
48+
}
49+
50+
public static void main (String []args)
51+
{
52+
Tester testMe = new Tester();
53+
testMe.testLogAnalyzer();
54+
testMe.testUniqueIP();
55+
testMe.testuniqueIPVisitsOnDay();
56+
testMe.testcountUniqueIPsInRange();
57+
testMe.testprintAllHigherThanNum();
58+
}
59+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.text.*;
2+
import java.util.*;
3+
4+
public class WebLogParser {
5+
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MMM/yyyy:kk:mm:ss Z", Locale.US);
6+
private static String munchTo(StringBuilder sb, String delim) {
7+
int x = sb.indexOf(delim);
8+
if (x == -1) {
9+
x = sb.length();
10+
}
11+
String ans = sb.substring(0,x);
12+
sb.delete(0, x + delim.length());
13+
return ans;
14+
}
15+
public static LogEntry parseEntry(String line) {
16+
//Assumes line is vald and in this format:
17+
//110.76.104.12 - - [30/Sep/2015:07:47:11 -0400] "GET //favicon.ico HTTP/1.1" 200 3426
18+
StringBuilder sb = new StringBuilder(line);
19+
String ip = munchTo(sb, " ");
20+
munchTo(sb, " "); //ignore -
21+
munchTo(sb, " ["); //ignore -, and eat the leading [
22+
String dateStr = munchTo(sb, "] \""); //]-space is intentional: eat both
23+
Date date = parseDate(dateStr);
24+
String request = munchTo(sb, "\" "); // quote-space is intentional: eat both
25+
String statusStr = munchTo(sb, " ");
26+
int status = Integer.parseInt(statusStr);
27+
String byteStr = munchTo(sb, " ");
28+
int bytes = Integer.parseInt(byteStr);
29+
return new LogEntry(ip, date, request, status, bytes);
30+
}
31+
public static Date parseDate(String dateStr) {
32+
ParsePosition pp = new ParsePosition(0);
33+
return dateFormat.parse(dateStr, pp);
34+
}
35+
36+
}

0 commit comments

Comments
 (0)