feat: add task to flag stale IPs for reevaluation
This commit is contained in:
@@ -812,6 +812,47 @@ class DatabaseManager:
|
||||
finally:
|
||||
self.close_session()
|
||||
|
||||
def flag_stale_ips_for_reevaluation(self) -> int:
|
||||
"""
|
||||
Flag IPs for reevaluation where:
|
||||
- last_seen is between 15 and 30 days ago
|
||||
- last_analysis is more than 10 days ago (or never analyzed)
|
||||
|
||||
Returns:
|
||||
Number of IPs flagged for reevaluation
|
||||
"""
|
||||
session = self.session
|
||||
try:
|
||||
now = datetime.now()
|
||||
last_seen_lower = now - timedelta(days=30)
|
||||
last_seen_upper = now - timedelta(days=15)
|
||||
last_analysis_cutoff = now - timedelta(days=10)
|
||||
|
||||
count = (
|
||||
session.query(IpStats)
|
||||
.filter(
|
||||
IpStats.last_seen >= last_seen_lower,
|
||||
IpStats.last_seen <= last_seen_upper,
|
||||
or_(
|
||||
IpStats.last_analysis <= last_analysis_cutoff,
|
||||
IpStats.last_analysis.is_(None),
|
||||
),
|
||||
IpStats.need_reevaluation == False,
|
||||
IpStats.manual_category == False,
|
||||
)
|
||||
.update(
|
||||
{IpStats.need_reevaluation: True},
|
||||
synchronize_session=False,
|
||||
)
|
||||
)
|
||||
session.commit()
|
||||
return count
|
||||
except Exception as e:
|
||||
session.rollback()
|
||||
raise
|
||||
finally:
|
||||
self.close_session()
|
||||
|
||||
def get_access_logs(
|
||||
self,
|
||||
limit: int = 100,
|
||||
|
||||
33
src/tasks/flag_stale_ips.py
Normal file
33
src/tasks/flag_stale_ips.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from database import get_database
|
||||
from logger import get_app_logger
|
||||
|
||||
# ----------------------
|
||||
# TASK CONFIG
|
||||
# ----------------------
|
||||
|
||||
TASK_CONFIG = {
|
||||
"name": "flag-stale-ips",
|
||||
"cron": "0 2 * * *", # Run daily at 2 AM
|
||||
"enabled": True,
|
||||
"run_when_loaded": False,
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
app_logger = get_app_logger()
|
||||
db = get_database()
|
||||
|
||||
try:
|
||||
count = db.flag_stale_ips_for_reevaluation()
|
||||
if count > 0:
|
||||
app_logger.info(
|
||||
f"[Background Task] flag-stale-ips: Flagged {count} stale IPs for reevaluation"
|
||||
)
|
||||
else:
|
||||
app_logger.debug(
|
||||
"[Background Task] flag-stale-ips: No stale IPs found to flag"
|
||||
)
|
||||
except Exception as e:
|
||||
app_logger.error(
|
||||
f"[Background Task] flag-stale-ips: Error flagging stale IPs: {e}"
|
||||
)
|
||||
Reference in New Issue
Block a user