-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis.R
128 lines (99 loc) · 3.23 KB
/
analysis.R
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
library(tidyverse)
library(lubridate)
# read and parse the access log
log_file <- read_log("access.log")
# drop the first two columns that are filled with NA's
log_file %>%
select(-c(X2,X3)) -> log_file
# set meaningful column names
colnames(log_file) <- c("client_ip","timestamp","request_type",
"status_code","size_of_reply","referer_URL","user_agent")
# convert timestamp to a proper date format
log_file$timestamp <- dmy_hms(log_file$timestamp)
log_file %>%
select(client_ip) %>%
group_by(client_ip) %>%
summarise(count = n()) %>%
arrange(desc(count))
# plot count by daily,weekly,monthly
log_file %>%
group_by(month = as.factor(month(timestamp)), year = as.factor(year(timestamp))) %>%
summarise(visits = n()) %>%
ggplot(., aes(month, year))+
geom_tile(aes(fill = visits)) +
scale_fill_gradient(low = "white", high = "red")
log_file %>%
group_by(day_of_week = as.factor(wday(timestamp))) %>%
summarise(visits = n()) %>%
ggplot(., aes(day_of_week,visits))+
geom_bar(aes(fill = day_of_week), stat = "identity")
log_file %>%
group_by(month = as.factor(month(timestamp))) %>%
summarise(visits = n()) %>%
ggplot(., aes(month,visits))+
geom_bar(aes(fill = month), stat = "identity") +
coord_flip() +
theme(legend.position = "bottom")
#
log_file %>%
group_by(date =date(timestamp)) %>%
summarise(visits = n()) %>%
ggplot(., aes(date,visits))+
geom_line(size = .2,alpha = .65) +
scale_y_log10()
#
log_file %>%
group_by(hour_of_day = (hour(timestamp)),day_of_week = as.factor(wday(timestamp))) %>%
summarise(visits = n()) %>%
ggplot(., aes(day_of_week,hour_of_day))+
geom_tile(aes(fill = visits))+
scale_fill_gradient(low = "yellow", high = "red")
log_file %>%
group_by(status_code) %>%
summarise(count = n()) %>%
mutate(proportion_in_percentage = (count/sum(count))*100) %>%
arrange(desc(count))
log_file %>%
filter(status_code == 404) %>%
group_by(client_ip) %>%
summarise(count =n()) %>%
arrange(desc(count)) %>%
head(n=25) %>%
ggplot(., aes(client_ip,count))+
geom_bar(stat = "identity") +
aes(x=reorder(client_ip,count))+
coord_flip()
log_file %>%
filter(status_code == 404) %>%
group_by(client_ip) %>%
summarise(count =n()) %>%
arrange(desc(count)) %>%
left_join(., log_file) %>%
group_by(request_type) %>%
summarise(count = n()) %>%
arrange(desc(count))
log_file %>%
filter(client_ip == "93.158.147.8") %>%
group_by(year = as.factor(year(timestamp)) ,month = as.factor(month(timestamp)),
request_type, status_code = as.factor(status_code)) %>%
summarise(count = n()) %>%
ggplot(., aes(month,count))+
geom_bar(aes(fill = year),stat = "identity")+
facet_wrap(~request_type, ncol = 2) +
theme(legend.position = "top",
axis.text.x = element_text(angle = 45, hjust = 1))
log_file %>%
filter(client_ip == "93.158.147.8") %>%
select(user_agent)
log_file %>%
filter(client_ip == "93.158.147.8") %>%
group_by(user_agent) %>%
summarise(count = n())
log_file %>%
filter(str_detect(log_file$user_agent,"[A-z]{1,}Bot|[A-z]{1,}bot")) %>%
pull(user_agent) %>%
str_extract(.,"[A-z]{1,}Bot|[A-z]{1,}bot") %>%
data_frame(botname =.) %>%
group_by(botname) %>%
summarise(count = n()) %>%
arrange(desc(count))