REDROOM
PHP 8.2.30
Path:
Logout
Edit File
Size: 2.92 KB
Close
//opt/cloudlinux/venv/lib/python3.11/site-packages/wmt/db.py
Text
Base64
#!/opt/cloudlinux/venv/bin/python3 -bb # coding=utf-8 # # Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2020 All Rights Reserved # # Licensed under CLOUD LINUX LICENSE AGREEMENT # http://cloudlinux.com/docs/LICENCE.TXT # import os import contextlib import sqlite3 from datetime import datetime, timedelta from sqlalchemy import ( Column, Boolean, DateTime, Integer, String, create_engine, event ) from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.engine.reflection import Inspector from sqlalchemy.orm import Session from wmt.common.const import WMT_DB, RETENTION_TIME_DAYS Base = declarative_base() class ScrapeResult(Base): """ created_date is saved in local TZ format """ __tablename__ = 'scrape_result' id = Column(Integer, primary_key=True) website = Column(String) create_date = Column(DateTime, default=lambda: datetime.now(), index=True) is_finished = Column(Boolean) response_code = Column(Integer, nullable=True) response_time_ms = Column(Integer, nullable=True) class DomainAlerts(Base): """ alert_time is saved in local TZ format """ __tablename__ = 'domain_alerts' id = Column(Integer, primary_key=True) website = Column(String, index=True) alert_time = Column(DateTime, default=lambda: datetime.now(), index=True) is_resolved = Column(Boolean, default=False) def cleanup_old_data(engine): n_days_ago = datetime.today() - timedelta(days=RETENTION_TIME_DAYS) with session_scope(engine) as session: session.query(ScrapeResult)\ .filter(ScrapeResult.create_date < n_days_ago)\ .delete() session.query(DomainAlerts)\ .filter(DomainAlerts.alert_time < n_days_ago)\ .delete() def create_db_if_not_exist(engine): if not is_db_present(engine): Base.metadata.create_all(engine) def is_db_present(engine): if not os.path.isfile(WMT_DB): return False database_inspection = Inspector.from_engine(engine) tables = [table for table in database_inspection.get_table_names()] return len(tables) > 0 def setup_wal_mode(dbapi_con, con_record): dbapi_con.execute('PRAGMA journal_mode = WAL') def setup_database(readonly=False): connection_string = f'file:{WMT_DB}' if readonly: connection_string = f'{connection_string}?mode=ro' creator = lambda: sqlite3.connect(connection_string, uri=True) engine = create_engine( 'sqlite:////', creator=creator, echo=False, ) event.listen(engine, 'connect', setup_wal_mode) create_db_if_not_exist(engine) return engine @contextlib.contextmanager def session_scope(engine) -> Session: """Provide a transactional scope around a series of operations.""" session = Session(bind=engine) try: yield session session.commit() except: session.rollback() raise finally: session.close()
Save
Close
Exit & Reset
Text mode: syntax highlighting auto-detects file type.
Directory Contents
Dirs: 2 × Files: 4
Delete Selected
Select All
Select None
Sort:
Name
Size
Modified
Enable drag-to-move
Name
Size
Perms
Modified
Actions
common
DIR
-
drwxr-xr-x
2025-11-05 08:00:55
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
__pycache__
DIR
-
drwxr-xr-x
2025-11-05 08:00:55
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
db.py
2.92 KB
lrw-r--r--
2025-10-20 12:17:38
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
main.py
15.17 KB
lrwxr-xr-x
2025-10-20 12:17:38
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
wmt-api.py
5.11 KB
lrwxr-xr-x
2025-10-20 12:17:38
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
__init__.py
231 B
lrw-r--r--
2025-10-20 12:17:38
Edit
Download
Rename
Chmod
Change Date
Delete
OK
Cancel
recursive
OK
Cancel
recursive
OK
Cancel
Zip Selected
If ZipArchive is unavailable, a
.tar
will be created (no compression).