77from sqlalchemy import create_engine , Column , Integer , String , LargeBinary
88from sqlalchemy .ext .declarative import declarative_base
99from sqlalchemy .orm import sessionmaker
10+ from sqlalchemy .orm .decl_api import DeclarativeMeta
1011
1112from core import logger , scheduler , utils
1213
@@ -42,13 +43,14 @@ class ResponseStatistics:
4243 forbidden : int = 0
4344 error : int = 0
4445 redirect : int = 0
46+ partial : int = 0
4547 ip_tables : defaultdict [str , int ] = field (default_factory = lambda : defaultdict (int ))
4648 user_agents : defaultdict [str , int ] = field (default_factory = lambda : defaultdict (int ))
4749
4850
4951
5052engine = create_engine ('sqlite:///database.db' )
51- Base = declarative_base ()
53+ Base : DeclarativeMeta = declarative_base ()
5254
5355class ClusterStatisticsTable (Base ):
5456 __tablename__ = 'ClusterStatistics'
@@ -71,6 +73,7 @@ class ResponseTable(Base):
7173 id = Column (Integer , primary_key = True )
7274 hour = Column (Integer , nullable = False )
7375 success = Column (String , nullable = False )
76+ partial = Column (String , nullable = False )
7477 forbidden = Column (String , nullable = False )
7578 not_found = Column (String , nullable = False )
7679 error = Column (String , nullable = False )
@@ -80,6 +83,7 @@ class ResponseTable(Base):
8083
8184class StatusType (Enum ):
8285 SUCCESS = "success"
86+ PARTIAL = "partial"
8387 FORBIDDEN = "forbidden"
8488 NOT_FOUND = "not_found"
8589 ERROR = "error"
@@ -181,12 +185,12 @@ def _commit_cluster(hour: int, cluster: str, hits: int, bytes: int):
181185 )
182186 return True
183187
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 ):
185189 if ip_tables == {}:
186190 return False
187191 session = SESSION .get_session ()
188192 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 ) )
190194 if q .count () == 0 :
191195 session .add (r )
192196 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
235239 'forbidden' : str (int (r .forbidden ) + forbidden ), # type: ignore
236240 'redirect' : str (int (r .redirect ) + redirect ), # type: ignore
237241 '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
239244 }
240245 )
241246 return True
@@ -265,7 +270,7 @@ def commit():
265270 _commit_cluster (cluster [0 ], cluster [1 ], value .hits , value .bytes )
266271
267272 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 )
269274
270275 session .commit ()
271276 old_keys = []
0 commit comments