7
7
from sqlalchemy import create_engine , Column , Integer , String , LargeBinary
8
8
from sqlalchemy .ext .declarative import declarative_base
9
9
from sqlalchemy .orm import sessionmaker
10
+ from sqlalchemy .orm .decl_api import DeclarativeMeta
10
11
11
12
from core import logger , scheduler , utils
12
13
@@ -42,13 +43,14 @@ class ResponseStatistics:
42
43
forbidden : int = 0
43
44
error : int = 0
44
45
redirect : int = 0
46
+ partial : int = 0
45
47
ip_tables : defaultdict [str , int ] = field (default_factory = lambda : defaultdict (int ))
46
48
user_agents : defaultdict [str , int ] = field (default_factory = lambda : defaultdict (int ))
47
49
48
50
49
51
50
52
engine = create_engine ('sqlite:///database.db' )
51
- Base = declarative_base ()
53
+ Base : DeclarativeMeta = declarative_base ()
52
54
53
55
class ClusterStatisticsTable (Base ):
54
56
__tablename__ = 'ClusterStatistics'
@@ -71,6 +73,7 @@ class ResponseTable(Base):
71
73
id = Column (Integer , primary_key = True )
72
74
hour = Column (Integer , nullable = False )
73
75
success = Column (String , nullable = False )
76
+ partial = Column (String , nullable = False )
74
77
forbidden = Column (String , nullable = False )
75
78
not_found = Column (String , nullable = False )
76
79
error = Column (String , nullable = False )
@@ -80,6 +83,7 @@ class ResponseTable(Base):
80
83
81
84
class StatusType (Enum ):
82
85
SUCCESS = "success"
86
+ PARTIAL = "partial"
83
87
FORBIDDEN = "forbidden"
84
88
NOT_FOUND = "not_found"
85
89
ERROR = "error"
@@ -181,12 +185,12 @@ def _commit_cluster(hour: int, cluster: str, hits: int, bytes: int):
181
185
)
182
186
return True
183
187
184
- def _commit_response (hour : int , ip_tables : defaultdict [str , int ], user_agents : defaultdict [str , int ], success : int = 0 , forbidden : int = 0 , redirect : int = 0 , not_found : int = 0 , error : int = 0 ):
188
+ def _commit_response (hour : int , ip_tables : defaultdict [str , int ], user_agents : defaultdict [str , int ], success : int = 0 , forbidden : int = 0 , redirect : int = 0 , not_found : int = 0 , error : int = 0 , partial : int = 0 ):
185
189
if ip_tables == {}:
186
190
return False
187
191
session = SESSION .get_session ()
188
192
q = session .query (ResponseTable ).filter_by (hour = hour )
189
- r = q .first () or ResponseTable (hour = hour , ip_tables = b'' , user_agents = b'' , success = str (0 ), forbidden = str (0 ), redirect = str (0 ), not_found = str (0 ), error = str (0 ))
193
+ r = q .first () or ResponseTable (hour = hour , ip_tables = b'' , user_agents = b'' , success = str (0 ), forbidden = str (0 ), redirect = str (0 ), not_found = str (0 ), error = str (0 ), partial = str ( 0 ) )
190
194
if q .count () == 0 :
191
195
session .add (r )
192
196
origin_ip_tables : defaultdict [str , int ] = defaultdict (lambda : 0 )
@@ -235,7 +239,8 @@ def _commit_response(hour: int, ip_tables: defaultdict[str, int], user_agents: d
235
239
'forbidden' : str (int (r .forbidden ) + forbidden ), # type: ignore
236
240
'redirect' : str (int (r .redirect ) + redirect ), # type: ignore
237
241
'not_found' : str (int (r .not_found ) + not_found ), # type: ignore
238
- 'error' : str (int (r .error ) + error ) # type: ignore
242
+ 'error' : str (int (r .error ) + error ), # type: ignore
243
+ 'partial' : str (int (r .partial ) + partial ) # type: ignore
239
244
}
240
245
)
241
246
return True
@@ -265,7 +270,7 @@ def commit():
265
270
_commit_cluster (cluster [0 ], cluster [1 ], value .hits , value .bytes )
266
271
267
272
for hour , value in response_cache .items ():
268
- _commit_response (hour , value .ip_tables , value .user_agents , value .success , value .forbidden , value .redirect , value .not_found , value .error )
273
+ _commit_response (hour , value .ip_tables , value .user_agents , value .success , value .forbidden , value .redirect , value .not_found , value .error , value . partial )
269
274
270
275
session .commit ()
271
276
old_keys = []
0 commit comments