From 4fa07bb3a15f045e993ae91c77fb67e2c19488ac Mon Sep 17 00:00:00 2001 From: AymarN Date: Mon, 26 Aug 2019 07:16:15 +0200 Subject: [PATCH] Assignement Website visits --- Week3/Counting Website Visits Quizz.txt | 27 + Week3/Web Logs server quizz. txt | 137 ++++ .../WebLogProgram/CountTester.class | Bin 0 -> 3716 bytes .../WebLogProgram/CountTester.ctxt | 17 + .../WebLogProgram/CountTester.java | 88 +++ .../WebLogProgram/LogAnalyzer.class | Bin 0 -> 7642 bytes .../WebLogProgram/LogAnalyzer.ctxt | 31 + .../WebLogProgram/LogAnalyzer.java | 250 +++++++ .../WebLogProgram/LogEntry.class | Bin 0 -> 1427 bytes .../WebLogProgram/LogEntry.ctxt | 17 + .../WebLogProgram/LogEntry.java | 47 ++ .../Website Visits/WebLogProgram/Tester.class | Bin 0 -> 2124 bytes .../Website Visits/WebLogProgram/Tester.ctxt | 15 + .../Website Visits/WebLogProgram/Tester.java | 56 ++ .../WebLogProgram/WebLogParser.class | Bin 0 -> 2049 bytes .../WebLogProgram/WebLogParser.ctxt | 9 + .../WebLogProgram/WebLogParser.java | 36 + .../WebLogProgram/short-test_log | 7 + .../WebLogProgram/weblog-short_log | 12 + .../Website Visits/WebLogProgram/weblog1_log | 639 ++++++++++++++++++ .../WebLogProgram/weblog2-short_log | 11 + .../Website Visits/WebLogProgram/weblog2_log | 381 +++++++++++ .../WebLogProgram/weblog3-short_log | 10 + 23 files changed, 1790 insertions(+) create mode 100644 Week3/Counting Website Visits Quizz.txt create mode 100644 Week3/Web Logs server quizz. txt create mode 100644 Week3/Website Visits/WebLogProgram/CountTester.class create mode 100644 Week3/Website Visits/WebLogProgram/CountTester.ctxt create mode 100644 Week3/Website Visits/WebLogProgram/CountTester.java create mode 100644 Week3/Website Visits/WebLogProgram/LogAnalyzer.class create mode 100644 Week3/Website Visits/WebLogProgram/LogAnalyzer.ctxt create mode 100755 Week3/Website Visits/WebLogProgram/LogAnalyzer.java create mode 100644 Week3/Website Visits/WebLogProgram/LogEntry.class create mode 100644 Week3/Website Visits/WebLogProgram/LogEntry.ctxt create mode 100755 Week3/Website Visits/WebLogProgram/LogEntry.java create mode 100644 Week3/Website Visits/WebLogProgram/Tester.class create mode 100644 Week3/Website Visits/WebLogProgram/Tester.ctxt create mode 100755 Week3/Website Visits/WebLogProgram/Tester.java create mode 100644 Week3/Website Visits/WebLogProgram/WebLogParser.class create mode 100644 Week3/Website Visits/WebLogProgram/WebLogParser.ctxt create mode 100755 Week3/Website Visits/WebLogProgram/WebLogParser.java create mode 100644 Week3/Website Visits/WebLogProgram/short-test_log create mode 100644 Week3/Website Visits/WebLogProgram/weblog-short_log create mode 100644 Week3/Website Visits/WebLogProgram/weblog1_log create mode 100644 Week3/Website Visits/WebLogProgram/weblog2-short_log create mode 100644 Week3/Website Visits/WebLogProgram/weblog2_log create mode 100644 Week3/Website Visits/WebLogProgram/weblog3-short_log diff --git a/Week3/Counting Website Visits Quizz.txt b/Week3/Counting Website Visits Quizz.txt new file mode 100644 index 0000000..f328a0f --- /dev/null +++ b/Week3/Counting Website Visits Quizz.txt @@ -0,0 +1,27 @@ + +1. Question 1 +Run the method mostNumberVisitsByIP after a HashMap has been created from the method countVisitsPerIP on the file weblog1_log. + +What number is returned? + +133 + +2. Question 2 +Run the method iPsMostVisits after a HashMap has been created from the method countVisitsPerIP on the file weblog1_log. + +What single IP address is returned in the ArrayList? + +84.190.182.222 + +3. Question 3 +Run the method dayWithMostIPVisits with a HashMap has been created from the method iPsForDays on the file weblog1_log. + +What day is returned? + +4. Question 4 +Run the method iPsWithMostVisitsOnDay with two parameters—one, a HashMap that has been created from the method iPsForDays on the file weblog1_log and two, the day “Mar 17”. + +One IP Address is returned in the ArrayList—what is it? + +200.129.163.70 + diff --git a/Week3/Web Logs server quizz. txt b/Week3/Web Logs server quizz. txt new file mode 100644 index 0000000..66064d0 --- /dev/null +++ b/Week3/Web Logs server quizz. txt @@ -0,0 +1,137 @@ + +1. Question 1 +Suppose a web log is modified to now have a sixth piece of information, a priority, that can be represented as a String. + +Which one of the following is the least likely change to the LogEntry class to accommodate this new part of a web log? + +A new String parameter is added to the constructor. + +A new private variable named priority is created. + +A new String field is initialized in the constructor. + +A new getPriority method is created to return the priority. + +The toString method is modified to include a String parameter. + +The toString method is modified to include the new priority as part of the return String. + +2. Question 2 + +Consider the following code for the readFile method of the LogAnalyzer class. + +public void readFile(String filename) { + FileResource fr = new FileResource(filename); + for (String line : fr.lines()) { + LogEntry le = WebLogParser.parseEntry(line); + } +} + +In the Tester class, readFile is called with a correct filename, and then printAll is called, but nothing is printed. + +Which one of the following is likely the best reason why? + +In readFile, the log entries were not stored in records. + +3. Question 3 +Consider the following code for the method printAllHigherThanNum with one integer parameter num. This method should print all the logs that have a status code higher than num. + +Which one of the following would be the best choice for suitable code for this method? + + +for (LogEntry le : records) { + if (le.getStatusCode() > num) { + System.out.println(le); + } +} + + +if (le.getStatusCode() > num) { + for (LogEntry le : records) { + if (le.getStatusCode() > num) { + System.out.println(le); + } + } +} + + +for (LogEntry le : records) { + if (le.getStatusCode() > num) { + System.out.println(le); + } + else { + System.out.println(); + } +} + + +if (le.getStatusCode() > num) { + for (LogEntry le : records) { + System.out.println(le); + } +} + + +if (le.getStatusCode() > num) { + for (LogEntry le : records) { + System.out.println(le); + } +} +else { + System.out.println(); +} + +4. Question 4 +Run the method countUniqueIPs on the file weblog2_log. + +How many unique IP addresses are in the file? + +45 + +5. Question 5 +Run the method uniqueIPVisitsOnDay(“Sep 27”) on the file weblog2_log. +What size is the ArrayList that is returned? + +8 + +6. Question 6 +Run the method countUniqueIPsInRange(400,499) on the file weblog2_log. +What number is returned? + +23 + +7. Question 7 +Run the method mostNumberVisitsByIP after a HashMap has been created from the method countVisitsPerIP on the file weblog2_log. +What number is returned? + +63 + +8. Question 8 +Run the method iPsMostVisits after a HashMap has been created from the method countVisitsPerIP on the file weblog2_log. + +What single IP address is returned in the ArrayList? + +188.162.84.63 + +9. Question 9 +Run the method dayWithMostIPVisits with a HashMap has been created from the method iPsForDays on the file weblog2_log. + +What day is returned? + +Sep 24 + +Sep 26 + +Sep 28 + +Sep 30 + +10. Question 10 +Run the method iPsWithMostVisitsOnDay with two parameters—one, a HashMap that has been created from the method iPsForDays on the file weblog2_log and two, the day “Sep 29”. +One IP address is returned in the ArrayList—what is it? + +212.128.74.248 + + + + diff --git a/Week3/Website Visits/WebLogProgram/CountTester.class b/Week3/Website Visits/WebLogProgram/CountTester.class new file mode 100644 index 0000000000000000000000000000000000000000..0f5c7d930b9049f389514986d6468b9f79eb32a4 GIT binary patch literal 3716 zcmb7HYjYdr6@FgJ_R7j8PUOVFjvba;Vk=IJ9a@su7@R;rjqE7M2_Y@GYiqGLk#<#S zWulZ8TJCpRE-k%7FX?^SP7y;h)9Kgt3;%$be(P`G1N1q&D`{nWXqimjefQ|R=RD7I z&U+;P>yy9z9l&k)V*)8$(D3C1I&h%}qxgy}4{3Nffo@!gV^N-8mBsAA5|$Il!;0gP z1l?A|ysE)YV7vHUl;wJ?sNsq%UytK4 zF@HnOe^b03kK>6rz9rjl$MK|wr!+jRkSLm$?P9f<&Q=t5WiFa)=6Kb!3*)opvRTX6 z6>mzRCugrXrdKUn3Zt9(Ob5n->8yWh$So#}4}Q7bio z9NXx=i2@N#(vD}XFr|nxy&1y)|M+#nR&)*YI%ijGuX4PW&I(5=*TaG?u9<}@_bRQh zL;US*WsXMzXWB2Z06Djy5HHpm%5BYg=JF+~<*Uuc)$okMw($5#x6A@kD#icF2(_@> z%$}KLwxU^D%$v1EyJY6`Wfoo``#IEVZ@o=anW2Uno>kZ_z?kU)+glZG>1=~t53PZP z4JHLw7z|l7oCDX>4k;;g6>Ib&cEN2r`6%{8&aIZ0t&_G$yFJo(hvcT{xD^xZo=t9z zjsqCeF^(~XEs?Hcnn$grg1d6~U}e=UdyCxacn;6&cmYQ=yr| zb2gct%X-AimYEM4XxGTbiz8!jyFZ75rp#Jpxyw;0YaDb%-dMNFyy1+t-SI5vtjFtT znMID3pb6W}3R+2=3XTlMNbZ2dl;qwia&DX@{h_;mAsZucl=Gz9ER`%LuW&FLFq{J6 z?A_Rw1UNL3{;ZGVo*M)qq{f7d#9@G~>!w(0Q;3ma6a5P7tk_q0h55BZ4zU$WlxznS zYQpam=4p<*?n_oJC*C_6pnOGW5=5J`DktcP4>g;AI zVl%_3zoN5_>)yp!1l|DQ$p^!A4E_Uq_!a^i>t0U{l0#h;YZL&#lyU zf(bI{X3lD$F1J0M{$LjoY{}6v*pXXN&UyRp?9Jz-3IfH`Z}e z!1?N@|7;Q73ypbhC*dO`e3XQb5ycc12>k)JNrUVJZlVBleg)1q$!0MhlD!9~*O3)V zHYB@4$PNc&HJtgFi4?VLT&#Dfp_KM;NOx{5DSbw!L-S(?>$o}7qLsXbjCd!)0t%Dh zLJabc4zPpb?3ryWOdks|K*1*|cY_5}gxCkzV>$BSS0~H8Ko%W5VfZX!k?SFrEHfya zZNmKz!3x~7t?KyKf^el1ll$t}{~o%6UhLY?iz5WjUhMi`y_iCRNlp>qJPDm9yff^% z`w2IPI|=y|pYwcQz_}(tBhr&+n`N4#M{xg-v#9O&Uo4F>`8{d0_2hX%=)i;j1tGV2 AxBvhE literal 0 HcmV?d00001 diff --git a/Week3/Website Visits/WebLogProgram/CountTester.ctxt b/Week3/Website Visits/WebLogProgram/CountTester.ctxt new file mode 100644 index 0000000..9115442 --- /dev/null +++ b/Week3/Website Visits/WebLogProgram/CountTester.ctxt @@ -0,0 +1,17 @@ +#BlueJ class context +comment0.target=CountTester +comment1.params= +comment1.target=CountTester() +comment2.params= +comment2.target=java.util.HashMap\ testCounts() +comment3.params= +comment3.target=void\ testmostNumberVisitsByIP() +comment4.params= +comment4.target=void\ testiPsMostVisits() +comment5.params= +comment5.target=void\ testiPsForDays() +comment6.params= +comment6.target=void\ testdayWithMostIPVisits() +comment7.params= +comment7.target=void\ testiPsWithMostVisitsOnDay() +numComments=8 diff --git a/Week3/Website Visits/WebLogProgram/CountTester.java b/Week3/Website Visits/WebLogProgram/CountTester.java new file mode 100644 index 0000000..f1a6f66 --- /dev/null +++ b/Week3/Website Visits/WebLogProgram/CountTester.java @@ -0,0 +1,88 @@ + +/** + * Website Visits + * In this assignement you will continue to build on the LogEntry and LogAnalyser classes + * that you worked on inth last lesson.You will continue to use the method parseEntry from + * the WebLogParser class, and you should not modify this class.You will write several + * methods to solve problems about web logs. + * Write a description of CountTester here. + * @author (Aymar N.) + * @version (08.08.2019) + */ + +import java.util.*; + +public class CountTester { + + private ArrayList maximumIPs; + private ArrayList mostAccessesDay; + + public CountTester(){ + maximumIPs = new ArrayList(); + mostAccessesDay = new ArrayList(); + } + + public HashMap testCounts() { + LogAnalyzer la = new LogAnalyzer(); + la.readFile("weblog3-short_log"); + HashMap counts = la.countVisitsPerIP(); + System.out.println(counts); + return counts; + } + + public void testmostNumberVisitsByIP() { + LogAnalyzer le = new LogAnalyzer(); + //le.readFile("weblog3-short_log"); + //le.readFile("weblog1_log"); + le.readFile("weblog2_log"); + int max_value = le.mostNumberVisitsByIP(testCounts()); + System.out.println("max value in the HashMap "+ max_value); + } + + public void testiPsMostVisits() { + LogAnalyzer Sol = new LogAnalyzer(); + //Sol.readFile("weblog3-short_log"); + //Sol.readFile("weblog1_log"); + Sol.readFile("weblog2_log"); + HashMap myCounts = Sol.countVisitsPerIP(); + maximumIPs = Sol.iPsMostVisits(myCounts); + for (int k=0;k> map_day_ipaddress = LA.iPsForDays(); + for (String s: map_day_ipaddress.keySet()) { + System.out.println(s+" maps to"+"\t"+map_day_ipaddress.get(s)); + } + } + + public void testdayWithMostIPVisits() { + String dayMostIP; + LogAnalyzer LogA = new LogAnalyzer(); + //LogA.readFile("weblog3-short_log"); + //LogA.readFile("weblog1_log"); + LogA.readFile("weblog2_log"); + HashMap> map_day_ipaddress = LogA.iPsForDays(); + dayMostIP = LogA.dayWithMostIPVisits(map_day_ipaddress); + System.out.println("The day that has the most IP address"+ dayMostIP); + } + + public void testiPsWithMostVisitsOnDay() { + LogAnalyzer myLog = new LogAnalyzer(); + //myLog.readFile("weblog3-short_log"); + //myLog.readFile("weblog1_log"); + myLog.readFile("weblog2_log"); + HashMap> day_and_ipaddress = myLog.iPsForDays(); + //mostAccessesDay = myLog.iPsWithMostVisitsOnDay(day_and_ipaddress,"Sep 30"); + mostAccessesDay = myLog.iPsWithMostVisitsOnDay(day_and_ipaddress,"Sep 29"); + for (int k=0;kG#Hd5^NJf%LoSCSx)mFu7 zTdl3Nwy3pkZQK=?VXJ8CzSO1Ey40oBR$HxBYg;u6>38mXGxG+fzv3Uf+tuk_(JZ_iA9Xjp|ARi<0xGNub zV`Dzhve_CHplHRQENdZOXPP|VaYJ=dJZ=H`K(#nD7;cX^Q8w5P5;5sO57)G8lWh1zI_-aot~W}ePg<^NkAiYCk+Gp5!X9p)OE z(H4%y&6tMq&CWip@-;@Hk;Hrr?%5S9HFy>cbko8K&5@|tI@G_$j2#zV)5np*=7Fwo z-^y?-B7J+%ljx1mpmE}8Q#9N+d^S(j;E$Q%?j;df7MyJv>vT(<)5im3*FBu(^0izV z4fpdscZv@T#k$y>+-!Ca)pZZ8GwWpQW9=6_Zd`jJ+_kPHJgBT9Pj$RaPrC+&qKV_9 zk<*7v1t1*^(cj2mQ+GF!aEW<=Oo$lJq0HpsYd(obz2|8;Aa=y)%L z;wx_?+HIailEer4&2HJ)+dr(l_V*84Y+{ZoZg1A{XASfA_T0ujBP3)FW}_R59!uU5 zQ19$eNMLrinrHIaH^8T-mpIXEwZ!ti&5VWGgdNUQSQ?J^GK$>Y6&Ylt)O^yb$l-5h zPQ#v~Ic#-UpUDbJhla*nXYcI}BweH}-ZBtRSpG06Z9tVwq^xv$=adeOUKNB%Lo*(? zvn(QXSIIuem;N56P{NESG?+Oy-di(so}Xi}L^ME$mJGzmN}g|!L!m)3JE2(G_7$+s z*2$cQ$#KViCHVJpQ++M-pe4*i@FjXp)$ogcTFFsFr#d2uUI|;MEfu&~Iq;op1LssN z@^~%vms<-qqR9E&dYL(VO1BbqH+veobL^G_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(); + + public LogAnalyzer() { + records = new ArrayList(); + maxDate = new ArrayList(); + maxIPs = new ArrayList(); + myFreqs = new ArrayList(); + } + + //Complete the readFile method to create a FileResource and + //iterate over all lines,create a LogEntry and store it in the + //records ArrayList. + public void readFile(String filename) { + FileResource resource = new FileResource(); + for(String line: resource.lines()){ + WebLogParser.parseEntry(line); + records.add(WebLogParser.parseEntry(line)); + } + } + + public int countUniqueIPs() { + // uniqueIPs starts as an empty list + ArrayList uniqueIPs = new ArrayList(); + // for each element le in records + for (LogEntry le: records) { + // ipAddr is le's ipAddress + String ipAddr = le.getIpAddress(); + //if ipAddr is not in uniqueIPs + if(!uniqueIPs.contains(ipAddr)) { + //add ipAddr to uniqueIps + uniqueIPs.add(ipAddr); + } + } + // return the size of UniqueIPs + return uniqueIPs.size(); + } + + public void printAllHigherThanNum(int Num) { + + for(LogEntry le: records) { + // Status code in LogEntrys + int statusCode = le.getStatusCode(); + //if StatusCode greater than Num + if(statusCode > Num) { + //print StatusCode + System.out.println("StatusCode greater than "+Num+": "+statusCode); + } + } + + } + + //This method accesses the web logs in records and returns an ArrayList + //of Strings of unique IP addresses that had access on the given day. + + public ArrayList uniqueIPVisitsOnDay(String someday){ + ArrayList myIPs = new ArrayList(); + String myString = null; + for(LogEntry le: records) { + Date d = le.getAccessTime(); + String ipAddr = le.getIpAddress(); + myString = d.toString(); + int index = myIPs.indexOf(ipAddr); + if((myString.contains(someday)) && (!myIPs.contains(ipAddr))){ + myIPs.add(ipAddr); + myFreqs.add(1); + } + + for (int k =0; k < myIPs.size();k++) { + System.out.println(myIPs.get(k)+"\t"); + } + + System.out.println("Array size: "+myIPs.size()); + } + return myIPs; + } + + public int countUniqueIPsInRange(int low, int high){ + // uniqueIPs starts as an empty list + ArrayList uniqueIPs = new ArrayList(); + for(LogEntry New: records) { + // Status code in LogEntrys + int statusCode = New.getStatusCode(); + String ipAddr = New.getIpAddress(); + //if StatusCode greater than Num + if((statusCode >= low) && (statusCode <= high)) { + if(!uniqueIPs.contains(ipAddr)) { + //add ipAddr to uniqueIps + uniqueIPs.add(ipAddr); + } + } + } + return uniqueIPs.size(); + } + + public HashMap countVisitsPerIP() { + // Make an empty HashMap counts + HashMap counts = new HashMap(); + // For each le in records + for (LogEntry le: records) { + //ip is le's ipAddress + String ip = le.getIpAddress(); + //Check if ip is in counts + if (!counts.containsKey(ip)) { + // If not put ip in with a value of 1 + counts.put(ip,1); + } + // If so; update the value to be 1 more + else { + counts.put(ip,counts.get(ip) + 1); + } + } + //counts is the answer + return counts; + } + + //In the Log Analyser class, write the method iPsMostVisits which has one + //parameter , a HashMap that maps an IP address to the number of + // times that IP address appears in the web log file. This method returns an + // ArrayList of Strings of IP addresses that all have the maximum number of visits + // to this website. For example, the call iPsMostVisits on a HashMap formed using + // the file weblog3-short_log returns the ArrayList with these two IP addresses, + //61.15.121.171 and 84.133.195.161. Both of them visited the site three times, which + //is the maximum number of times any IP address visited the site. + + public ArrayList iPsMostVisits(HashMap addressNumberTime){ + ArrayList maxIps = new ArrayList(); + int greatest; + greatest = mostNumberVisitsByIP(addressNumberTime); + for (String s: addressNumberTime.keySet()) { + if (addressNumberTime.get(s) == greatest) { + maxIps.add(s); + } + } + + return maxIps; + } + + //In the LogAnalyser class, write the method iPsForDays which has no parameters. This + // method returns a HashMap> that uses records and maps days + // from web logs to an ArrayList of IP addresses that occured on that day.A day is in + //format "MMM DD" where MMM is the first three characters on the month name with the + // first letter capital and the others in lowercase, and DD is the day in two digits. + // For example, for the file weblog3-short_log, after building this HashMap, if you + // print it out, you will see that Sep 14 maps to one IP address, Sep 21 maps to four + //IP addresses and Sep 30 maps to five IP addresses. + + public HashMap> iPsForDays(){ + HashMap> dayIpThatDay = new HashMap>(); + ArrayList myIPs = new ArrayList(); + String myString = null; + for (LogEntry le: records) { + Date d = le.getAccessTime(); + String ipAddr = le.getIpAddress(); + myString = d.toString(); + myIPs = uniqueIPVisitsOnDay(myString); + dayIpThatDay.put(myString,myIPs); + } + return dayIpThatDay; + } + + public int findMax(){ + int theMax = myFreqs.get(0); + int maxIndex = 0; + for(int k=0; k < myFreqs.size(); k++){ + if (myFreqs.get(k) > theMax){ + theMax = myFreqs.get(k); + maxIndex = k; + } + } + return maxIndex; + } + + public String dayWithMostIPVisits(HashMap> dayIPs){ + String date; + String maxKey_date= null; + for (String s : dayIPs.keySet()) { + int index = maxDate.indexOf(s); + if (index == -1) { + maxDate.add(s); + myFreqs.add(1); + } + else { + int freq = myFreqs.get(index); + myFreqs.set(index,freq+1); + } + } + + int max = findMax(); + System.out.println("max Date: "+ maxDate.get(max)+" max Freq: "+ myFreqs.get(max)); + //myFreqs.clear(); + return maxDate.get(max); + } + + public ArrayList iPsWithMostVisitsOnDay(HashMap> dayandIPs, String aDay){ + myFreqs.clear(); + ArrayList mostIPs = new ArrayList(); + mostIPs = uniqueIPVisitsOnDay(aDay); + HashMap counts = new HashMap(); + + for (int k=0;k myCounts){ + int max = 0; + for (String s: myCounts.keySet()){ + int currentNum = myCounts.get(s); + if (currentNum > max) { + max = currentNum; + } + } + return max; + } + + public void printAll() { + for(LogEntry le : records) { + System.out.println(le); + } + } + +} diff --git a/Week3/Website Visits/WebLogProgram/LogEntry.class b/Week3/Website Visits/WebLogProgram/LogEntry.class new file mode 100644 index 0000000000000000000000000000000000000000..b85ebe68da28173c41af9f031ac43b9302184a7d GIT binary patch literal 1427 zcmaJ=X>Sry6g>}S*qm;3vsx{!R$;o}QkU9V1($?`Xwo)K{5Zf1PD25Q!KDAgpWz2h zgeIE!1N>3O^9HbV80Evcm;27$&V^sUzyAcVg?s`N$i^{_I|}Y9xToO0f~_bXBoIYb z!NV9HDf?K#wt^>7>_o9E5VxDTO2v__mO!$2Y<;w{HLG6D9=MKOukHvWta6#JhxUmS znCvmyu3gK%v|P#es3SkLWy>YAK#PBdKIn`ud@Q!Lt~dXwH~L`)2zHewU>yC zeBJBS#5Jq0afgYHxVUZTZ$Y{{zeopou$>w9(A{ZQ^hB;ezmUzRDz+7Trr>jd?mM-m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--git a/Week3/Website Visits/WebLogProgram/Tester.ctxt b/Week3/Website Visits/WebLogProgram/Tester.ctxt new file mode 100644 index 0000000..d3fa1e6 --- /dev/null +++ b/Week3/Website Visits/WebLogProgram/Tester.ctxt @@ -0,0 +1,15 @@ +#BlueJ class context +comment0.target=Tester +comment1.params= +comment1.target=void\ testLogEntry() +comment2.params= +comment2.target=void\ testLogAnalyzer() +comment3.params= +comment3.target=void\ testUniqIP() +comment4.params= +comment4.target=void\ testprintAllHigherthanNum() +comment5.params= +comment5.target=void\ testuniqueIPVisitsOnDay() +comment6.params= +comment6.target=void\ testcountUniqueIPsInRange() +numComments=7 diff --git a/Week3/Website Visits/WebLogProgram/Tester.java b/Week3/Website Visits/WebLogProgram/Tester.java new file mode 100755 index 0000000..be97ce5 --- /dev/null +++ b/Week3/Website Visits/WebLogProgram/Tester.java @@ -0,0 +1,56 @@ + +/** + * Write a description of class Tester here. + * @author (Aymar N.) + * @version (02.08.2019) + */ + +import java.util.*; + +public class Tester +{ + + public void testLogEntry() { + LogEntry le = new LogEntry("1.2.3.4", new Date(), "example request", 200, 500); + System.out.println(le); + LogEntry le2 = new LogEntry("1.2.100.4", new Date(), "example request 2", 300, 400); + System.out.println(le2); + } + + public void testLogAnalyzer() { + LogAnalyzer LogAnalyzer = new LogAnalyzer(); + LogAnalyzer.readFile("short-test_log"); + LogAnalyzer.printAll(); + } + + public void testUniqIP() { + LogAnalyzer la = new LogAnalyzer(); + la.readFile("short-test_log"); + int uniqueIPS = la.countUniqueIPs(); + System.out.println("There are " + uniqueIPS + " IPs"); + } + + public void testprintAllHigherthanNum() { + LogAnalyzer MyLogAnalyser = new LogAnalyzer(); + MyLogAnalyser.readFile("weblog1_log"); + MyLogAnalyser.printAllHigherThanNum(400); + } + + public void testuniqueIPVisitsOnDay() { + LogAnalyzer MyAnalyser = new LogAnalyzer(); + MyAnalyser.readFile("weblog1_log"); + //MyAnalyser.uniqueIPVisitsOnDay("Sep 14"); + //MyAnalyser.uniqueIPVisitsOnDay("Sep 30"); + MyAnalyser.uniqueIPVisitsOnDay("Mar 17"); + + } + + public void testcountUniqueIPsInRange() { + LogAnalyzer Analyser = new LogAnalyzer(); + Analyser.readFile("weblog1_log"); + int countinRange_first = Analyser.countUniqueIPsInRange(200, 299); + //int countinRange_second = Analyser.countUniqueIPsInRange(300, 399); + System.out.println("There are firstly " + countinRange_first + " IPs"); + //System.out.println("There are secondly " + countinRange_second + " IPs"); + } +} diff --git a/Week3/Website Visits/WebLogProgram/WebLogParser.class b/Week3/Website Visits/WebLogProgram/WebLogParser.class new file mode 100644 index 0000000000000000000000000000000000000000..df27f268e0ef094ccc17ebe2297a5691e36c4add GIT binary patch literal 2049 zcmZ`)jZza=6#j1h_J>6w@)txKQAd-KnK@Baqy1)4gpVnK(3C-Up5{CcKi z(U-nc@V&GwsaV!AjvrKHRpg|&qGDCY8lEe7p<@7f1?xJZc&X93p<@J_3Vu{k(C`Yc zrT&wGHwu0h(95Q4E!G<~(-oM=?wY@vY1cY%(|NnLU$vf|duIhAbGBo$H>}r|Yx*W69-6nxRvnUYj@|;Bx2$!v@U6Tksq8*ut zL>%yciR?^=K>PEqS=!5)`)&OCzC`@YaT|vM*Snz-5ovjT#tKzg6aupM$xxPEU>IX!+P7Yn(TqrbWOL_^!7)5neO6YJ49GYgr|9M#at~TC`<92RmDKSH?AP9iJHZ1zQG86b+P6 zR$v*}Mnz!s!{W9ZAW!YE#&H9;@hPhlf6Z#YiBB4s#T@m?xB}b2F185fteW!F-dWIY z+|pe&FoA0ZYH$R`z3e?_SYV`FPUmvD^dbK;dwZE$Ez@kqUn!^?*vDHLjD0dX3A0}p zd{&FQR*79wY^Q7;tZoYo_bNW8C{neYic7k@EL62-tJw5rnjjKOOFieeBauJ<48w`F zWRtJ;JajSIJ#p}s4qkv?a$Kuok)P@f4^@GOJ>Y*hp52#)_~iqT@RqO_Ot8e6jm5$}~68?nTqyN<6>}GnK?m! zCX|X^qLK;wTQZSY3Qh<}v0$8aAp-t=)Sup`Ut#&h;V7I@o7iqFXiJYDdZ^WxzJ?$H)tgn6Epd{g*} zKb2pyLifq`0c~N%oT4?!IxMg)U$a6Ay{ZSAkm&8S_mHF zO7