-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem5_WebsiteAnalyticsDashboard.java
More file actions
89 lines (63 loc) · 2.74 KB
/
Problem5_WebsiteAnalyticsDashboard.java
File metadata and controls
89 lines (63 loc) · 2.74 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
import java.util.*;
class AnalyticsSystem {
HashMap<String, Integer> pageViews = new HashMap<String, Integer>();
HashMap<String, HashSet<String>> uniqueVisitors = new HashMap<String, HashSet<String>>();
HashMap<String, Integer> trafficSources = new HashMap<String, Integer>();
public void processEvent(String url, String userId, String source) {
if (!pageViews.containsKey(url)) {
pageViews.put(url, 0);
}
pageViews.put(url, pageViews.get(url) + 1);
if (!uniqueVisitors.containsKey(url)) {
uniqueVisitors.put(url, new HashSet<String>());
}
uniqueVisitors.get(url).add(userId);
if (!trafficSources.containsKey(source)) {
trafficSources.put(source, 0);
}
trafficSources.put(source, trafficSources.get(source) + 1);
}
public void getDashboard() {
ArrayList<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(pageViews.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) {
return b.getValue() - a.getValue();
}
});
System.out.println("Top Pages:");
int count = 0;
for (Map.Entry<String, Integer> entry : list) {
if (count == 10) {
break;
}
String url = entry.getKey();
int views = entry.getValue();
int unique = uniqueVisitors.get(url).size();
System.out.println((count + 1) + ". " + url + " - " + views + " views (" + unique + " unique)");
count++;
}
int total = 0;
for (int v : trafficSources.values()) {
total = total + v;
}
System.out.println();
System.out.println("Traffic Sources:");
for (String src : trafficSources.keySet()) {
int c = trafficSources.get(src);
double percent = (c * 100.0) / total;
System.out.println(src + ": " + percent + "%");
}
}
}
public class Problem5_WebsiteAnalyticsDashboard {
public static void main(String[] args) {
AnalyticsSystem system = new AnalyticsSystem();
system.processEvent("/article/breaking-news", "user_123", "google");
system.processEvent("/article/breaking-news", "user_456", "facebook");
system.processEvent("/sports/championship", "user_111", "google");
system.processEvent("/sports/championship", "user_222", "direct");
system.processEvent("/sports/championship", "user_333", "google");
system.processEvent("/article/breaking-news", "user_123", "google");
system.getDashboard();
}
}