astra.database_manager#
Classes
|
Manages the SQLite database for an observatory, including creation, querying, and periodic backups. |
- class astra.database_manager.DatabaseManager(observatory_name: str, run_backup: bool = True, backup_time: datetime = datetime.strptime('12:00', '%H:%M'), logger=None)[source]#
Bases:
objectManages the SQLite database for an observatory, including creation, querying, and periodic backups.
- backup_time#
Scheduled time for daily backups.
- Type:
datetime
- logger#
Logger instance for logging messages.
- Type:
Examples
>>> from astra.config import ObservatoryConfig >>> from astra.database_manager import DatabaseManager >>> observatory_config = ObservatoryConfig.from_config() >>> db_manager = DatabaseManager(observatory_config.observatory_name) >>> db_manager.execute_select_to_df("SELECT * FROM polling", table="polling")
- property cursor: Sqlite3Worker#
- execute_select(query: str) list[tuple][source]#
Execute a SELECT query and return the result as a list of tuples. Only SELECT queries are allowed.
For static type checking to ensure the return type is always a list of tuples.
- execute_select_to_df(query: str, table: str | None = None) DataFrame[source]#
Execute a SELECT query and return the result as a pandas DataFrame. Only SELECT queries are allowed.
- create_database(max_queue_size: int = 2000) Sqlite3Worker[source]#
Create and initialize the observatory database.
Creates a SQLite database for storing observatory data including device polling information, image metadata, and log entries. The database includes three main tables: polling (device status data), images (image file information), and log (system log messages).
- Returns:
Sqlite3Worker – The database cursor object for executing queries and managing the database connection with a maximum queue size of 200.
Note
The database file is created in the logs directory using the observatory name as the filename with a .db extension.
- backup() None[source]#
Back up database tables from the previous 24 hours to CSV files.
Creates timestamped CSV backups of the main database tables (polling, log, autoguider_log, autoguider_info_log) and stores them in an archive directory. Also monitors disk usage and logs a warning if disk usage exceeds 90%.
- The backup process:
Checks available disk space and warns if usage > 90%
Creates an archive directory if it doesn’t exist
Exports specified database tables to timestamped CSV files
Logs the backup completion or any errors encountered
- Raises:
Exception – Any errors during the backup process are logged and added to the error_source list for monitoring.