-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstat_class.py
More file actions
98 lines (88 loc) · 3.79 KB
/
Copy pathstat_class.py
File metadata and controls
98 lines (88 loc) · 3.79 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
90
91
92
93
94
95
96
97
98
import json
import datetime
class Stat:
def __init__(self):
with open('data.json', 'r') as f:
json_string = f.read()
try:
self.stats = json.loads(json_string)
except json.decoder.JSONDecodeError:
print('jsondecoder_exeption')
self.stats = {
'total_hosts_stat': {},
'date_hosts_stat': {}
}
# self.save_json()
def save_json(self):
with open('data.json', 'w') as f:
json.dump(self.stats, f, indent=2)
def update_stat(self, request):
print("+++++++++++++++++++++++++++++++++++++++++++++++")
host = request.json['host']
if host not in self.stats['total_hosts_stat'].keys():
Stat._create_dict_for_new_host(self.stats['total_hosts_stat'], host)
host_stat = self.stats['total_hosts_stat'][host]
Stat._update_host_stat(host_stat, request)
now_date = datetime.datetime.now().strftime("%d-%m-%Y")
if now_date not in self.stats['date_hosts_stat'].keys():
self.stats['date_hosts_stat'][now_date] = {}
Stat._create_dict_for_new_host(self.stats['date_hosts_stat'][now_date], host)
host_stat = self.stats['date_hosts_stat'][now_date][host]
Stat._update_host_stat(host_stat, request)
@staticmethod
def _update_host_stat(host_stat, request):
ip = request.remote_addr
path = request.json['pathname']
host_stat['total_visits_count'] += 1
depth = len(list(filter(lambda e: e != '', path.split('/'))))
host_stat['average_depth'] = (host_stat['average_depth'] * (host_stat['total_visits_count'] - 1) + depth) / \
host_stat['total_visits_count']
if ip not in host_stat['users_ip_stat'].keys():
host_stat['unique_users_count'] += 1
if path != '/':
if path not in host_stat['paths_visits_stat'].keys():
Stat._create_dict_for_new_path(host_stat, path)
path_stat = host_stat['paths_visits_stat'][path]
Stat._update_path_stat(path_stat, request)
Stat._increment_dict_counter(host_stat['browsers_stat'], request.headers['Sec-Ch-Ua'].split(', ')[1])
Stat._increment_dict_counter(host_stat['operation_systems_stat'], request.headers['Sec-Ch-Ua-Platform'])
Stat._increment_dict_counter(host_stat['languages_stat'], request.headers['Accept-Language'])
Stat._increment_dict_counter(host_stat['users_ip_stat'], ip)
@staticmethod
def _update_path_stat(path_stat, request):
ip = request.remote_addr
referrer = request.json['referrer']
path_stat['total_visits_count'] += 1
if ip not in path_stat['users_ip_stat'].keys():
path_stat['unique_users_count'] += 1
Stat._increment_dict_counter(path_stat['referrer_stat'], referrer)
Stat._increment_dict_counter(path_stat['users_ip_stat'], ip)
@staticmethod
def _create_dict_for_new_host(dict, host):
dict[host] = {
'total_visits_count': 0,
'average_depth': 0,
'unique_users_count': 0,
'paths_visits_stat': {},
'browsers_stat': {},
'operation_systems_stat': {},
'languages_stat': {},
'users_ip_stat': {}
}
@staticmethod
def _create_dict_for_new_path(host_stat, path):
host_stat['paths_visits_stat'][path] = {
'total_visits_count': 0,
'unique_users_count': 0,
'referrer_stat': {},
'users_ip_stat': {}
}
@staticmethod
def _increment_dict_counter(dict, key):
if key != '':
if key in dict.keys():
dict[key] += 1
else:
dict[key] = 1
def __del__(self):
self.save_json()