-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataExtraction.py
144 lines (103 loc) · 3.77 KB
/
dataExtraction.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from datetime import datetime
import numpy as np
import pandas as pd
import re
from sklearn.neighbors import KNeighborsRegressor
def getStatusCodeCount(df, q):
x = df['status'].value_counts()
x = pd.DataFrame({'status': x.index, 'count': x.values})
x = x.head(5)
q.put(x)
return x
def getOverallStats(df, q):
allg = {}
x, y = df.shape
allg["length"] = x
top = df.head(1)
bottom= df.tail(1)
start = datetime.strptime(str(top.iloc[0]['year']) + '-' + str(top.iloc[0]['month']) + '-' + str(top.iloc[0]['day']), "%Y-%m-%d").date()
end = datetime.strptime(str(bottom.iloc[0]['year']) + '-' + str(bottom.iloc[0]['month'])+ '-' + str(bottom.iloc[0]['day']), "%Y-%m-%d").date()
allg["start"] = start
allg["end"] = end
delta = end - start
delta = delta.days
allg["delta"] = delta
q.put(allg)
return allg
def getStatusCodeTimeLine(df, q):
x = df.groupby(['year', 'month','day', 'status']).size().unstack(level=-1).fillna(0)
x = x.reset_index()
x = x.reindex(index=range(0, len(x)))
x['date'] = x['year'].apply(str) + '-' + x['month'].apply(str) + '-' + x['day'].apply(str)
x = x.sort_values(by=['date'])
x.drop(['year', 'month', 'day'], axis=1, inplace=True)
q.put(x)
return x
def getUsageHours(df,q):
top = df.head(1)
bottom= df.tail(1)
start = datetime.strptime(str(top.iloc[0]['year']) + '-' + str(top.iloc[0]['month']) + '-' + str(top.iloc[0]['day']), "%Y-%m-%d").date()
end = datetime.strptime(str(bottom.iloc[0]['year']) + '-' + str(bottom.iloc[0]['month'])+ '-' + str(bottom.iloc[0]['day']), "%Y-%m-%d").date()
delta = end - start
delta = delta.days
x = df
x['minute'] = x['minute'].apply(lambda x: _minutesToQuarters(x))
x = x.groupby(['hour', 'minute']).size()
x = x.reset_index(name='counts')
x['time'] = x['hour'].apply(str) + ':' + x['minute'].apply(str)
x = x.sort_values(by=['time'])
x.drop(['hour', 'minute'], axis=1, inplace=True)
x['counts'] = x['counts'].div(delta)
### Add Trendline
reg = KNeighborsRegressor(n_neighbors=10).fit(np.vstack(x.index), x['counts'])
x['bestfit'] = reg.predict(np.vstack(x.index))
q.put(x)
return x
def getUsageDays(df, q):
x = df.groupby(['year', 'month','day']).size()
x = x.reset_index(name='counts')
x = x.reindex(index=range(0, len(x)))
x['date'] = x['year'].apply(str) + '-' + x['month'].apply(str) + '-' + x['day'].apply(str)
x = x.sort_values(by=['date'])
x.drop(['year', 'month', 'day'], axis=1, inplace=True)
### Add Trendline
reg = KNeighborsRegressor(n_neighbors=5).fit(np.vstack(x.index), x['counts'])
x['bestfit'] = reg.predict(np.vstack(x.index))
q.put(x)
return x
def getRequestCount(df,q):
x = df.groupby("request").size()
x = x.reset_index(name='counts')
x = x.sort_values(by=['counts'], ascending=False)
x = x[x['request'].str.contains("GET")]
x = x.head(10)
x = x.sort_values(by=['counts'], ascending=True)
q.put(x)
return x
def getReferrer(df, q):
df['referrer'] = df['referrer'].apply(lambda x: _regex(x))
df = df.groupby("referrer").size()
x = df.reset_index(name='counts')
x = x.sort_values(by=['counts'], ascending=False)
x.to_csv("ref_list", sep='\t', encoding='utf-8')
x = x.head(5)
q.put(x)
return x
def _regex(ref):
if ("www" in ref or "http" in ref):
re.findall
return re.findall('^(?:https?:\/\/)?(?:[^@\/\n]+@)?(?:www\.)?([^:\/?\n]+)', ref)[0]
else:
return ref
def _minutesToQuarters(x):
x = int(x)
if(x >= 0 and x <=15):
return 0
elif(x >= 16 and x <=30):
return 1
elif(x >= 31 and x <=45):
return 2
elif(x >= 46 and x <=60):
return 3
else:
return -1