Skip to content

Commit

Permalink
Added Analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
IliyanID committed Sep 19, 2021
1 parent d150ace commit 29adfb9
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 12 deletions.
85 changes: 85 additions & 0 deletions Back-End/src/main/java/com/tco/Server/Analytics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.tco.Server;
import java.util.ArrayList;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Analytics {
private final Logger log = LoggerFactory.getLogger(Analytics.class);

private int NumOfInstaRequests = 0;
private int NumOfEtsyRequests = 0;


public ArrayList<String> userAgents = new ArrayList<>();
public ArrayList<String> ips = new ArrayList<>();
public ArrayList<String> times = new ArrayList<>();

public String returnHTML(){
int hour = 0,day = 0,week = 0,month = 0;

LocalDateTime now = LocalDateTime.now();

for(int i = 0; i < times.size(); i++){
log.info(times.get(i));
LocalDateTime input = LocalDateTime.parse(times.get(i).replace("/","-").replace(" ","T"));
if(input.plusHours(1).isAfter(now))
hour++;
if(input.plusDays(1).isAfter(now))
day++;
if(input.plusWeeks(1).isAfter(now))
week++;
if(input.plusMonths(1).isAfter(now))
month++;
}

String response=
"Number of Total Visits: " + Math.min(NumOfEtsyRequests,NumOfInstaRequests) + "<br/>"+
"Number of Total Requests for Etsy Images: " + NumOfEtsyRequests + "<br/>"+
"Number of Total Requests for Instagram Images: " + NumOfInstaRequests + "<br/>" + "<br/>"+
"Number of Vists in last hour: " + hour +"<br/>" +
"Number of Vists in last day: "+ day + "<br/>" +
"Number of Vists in last week: "+ week + "<br/>" +
"Number of Vists in last month: "+ month + "<br/>" +
"<br/><br/><br/><br/>"+
"Latest 10 People to Access: " + "<br/><br/>";

int max = 0;
for(int i = userAgents.size() - 1;i >= 0;i--){
if(max >=10)
break;
else
max++;

response += (
"User-Agent: " + userAgents.get(i)+"<br/>"+
"IP: " + ips.get(i) + "<br/>"+
"Time Accessed At: " + times.get(i) + "<br/><br/>"
);
}
return response;
}

public void newEtsyRequest(String userAgent, String ip){
userAgents.add(userAgent);
ips.add(ip);

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
times.add(dtf.format(now));

NumOfEtsyRequests++;

log.info("New Request");
log.info("User Agent: + " + userAgent);
log.info("IP: " + ip);
log.info("Time: " + dtf.format(now));

}
public void newInstaRequest(){
NumOfInstaRequests++;
}
}
23 changes: 11 additions & 12 deletions Back-End/src/main/java/com/tco/Server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.tco.misc.RefreshToken;
import com.tco.misc.Config;


public class Server
{
private final int period = 3600000; // repeat every hour.
Expand All @@ -26,8 +27,8 @@ public class Server
private Service https;
private Service http;

private int NumOfInstaRequests = 0;
private int NumOfEtsyRequests = 0;

private Analytics analytics;

public Server()
{
Expand All @@ -40,7 +41,7 @@ private void configureRestfulApiServer() {
http = Service.ignite().port(80).threadPool(10);
System.out.println("Server configured to listen on port 80 and 443");


analytics = new Analytics();


String keyStoreLocation = new Config().getRootDirectory() + "/Back-End/src/main/resources/mykeystore.jks";
Expand All @@ -53,8 +54,8 @@ private void configureRestfulApiServer() {
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
log.info("Refreshing Resources");
etsy = new Etsy();
instagram = new Instagram();
//etsy = new Etsy();
//instagram = new Instagram();

}
}, 0, period);
Expand All @@ -74,18 +75,16 @@ private void processRestfulApiRequests()
response.type("text/html");
response.header("Access-Control-Allow-Origin","*");
response.status(200); //Success
return (
"Number of Visits: " + Math.min(NumOfEtsyRequests,NumOfInstaRequests) + "<br/>"+
"Number of Requests for Etsy Images: " + NumOfEtsyRequests + "<br/>"+
"Number of Requests for Instagram Images: " + NumOfInstaRequests + "<br/>"
);
return analytics.returnHTML();
});

https.get("/EtsyImages",(request,response)->{
response.type("application/json");
response.header("Access-Control-Allow-Origin","*");
response.status(200); //Success
NumOfEtsyRequests++;

analytics.newEtsyRequest(request.userAgent(), request.ip());

return etsy.getJSONResponse();
});

Expand All @@ -94,7 +93,7 @@ private void processRestfulApiRequests()
response.header("Access-Control-Allow-Origin","*");
response.status(200); //Success

NumOfInstaRequests++;
analytics.newInstaRequest();
return instagram.getJSONResponse();
});

Expand Down

0 comments on commit 29adfb9

Please sign in to comment.