Language: English | 简体中文
The history module is responsible for storing, querying, and managing application history records. It uses a PostgreSQL database for data storage and provides automatic cleanup for old records.
- Store history records - Store various application operations and system events
- Conditional query - Query history records by type, application, time range, and other conditions
- Automatic cleanup - Periodically clean up records older than 1 month
- Pagination support - Support paginated queries for large datasets
- Health check - Provide database connection health check functionality
type HistoryRecord struct {
ID int64 `json:"id" db:"id"` // Primary key
Type HistoryType `json:"type" db:"type"` // Record type (enum)
Message string `json:"message" db:"message"` // Description message
Time int64 `json:"time" db:"time"` // Unix timestamp
App string `json:"app" db:"app"` // Application name
Account string `json:"account" db:"account"` // Account name
Extended string `json:"extended" db:"extended"` // JSON string for additional data
}Predefined record types include:
ACTION_INSTALL- Application installation operationACTION_UNINSTALL- Application uninstallation operationACTION_CANCEL- Application operation cancelledACTION_UPGRADE- Application upgrade operationSYSTEM_INSTALL_SUCCEED- System installation succeededSYSTEM_INSTALL_FAILED- System installation failedaction- Generic action typesystem- Generic system type
type QueryCondition struct {
Type HistoryType `json:"type,omitempty"` // Filter by type
App string `json:"app,omitempty"` // Filter by app
Account string `json:"account,omitempty"` // Filter by account
StartTime int64 `json:"start_time,omitempty"` // Time range start
EndTime int64 `json:"end_time,omitempty"` // Time range end
Limit int `json:"limit,omitempty"` // Max records to return
Offset int `json:"offset,omitempty"` // Pagination offset
}The module automatically creates the following database table:
CREATE TABLE IF NOT EXISTS history_records (
id BIGSERIAL PRIMARY KEY,
type VARCHAR(100) NOT NULL,
message TEXT NOT NULL,
time BIGINT NOT NULL,
app VARCHAR(255) NOT NULL,
account VARCHAR(255) NOT NULL DEFAULT '',
extended TEXT DEFAULT '',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Indexes for better query performance
CREATE INDEX IF NOT EXISTS idx_history_type ON history_records(type);
CREATE INDEX IF NOT EXISTS idx_history_app ON history_records(app);
CREATE INDEX IF NOT EXISTS idx_history_account ON history_records(account);
CREATE INDEX IF NOT EXISTS idx_history_time ON history_records(time);
CREATE INDEX IF NOT EXISTS idx_history_created_at ON history_records(created_at);The module automatically starts a background cleanup routine that periodically deletes records older than 1 month:
- Runs cleanup every 24 hours by default
- Deletes records where the
timefield is less than the current time minus 1 month - The cleanup interval can be configured via the
HISTORY_CLEANUP_INTERVAL_HOURSenvironment variable
You can easily add new record types (the following is an example; these constants may not exist in the current code base):
const (
TypeActionRestart HistoryType = "ACTION_RESTART"
TypeSystemBackup HistoryType = "SYSTEM_BACKUP"
TypeSystemRestore HistoryType = "SYSTEM_RESTORE"
)- The
Extendedfield should contain valid JSON; if it is not valid JSON, the module logs a warning but still stores the original string. - In public environments (as determined by
utils.IsPublicEnvironment()), the history module is not initialized to avoid unnecessary database connections in certain deployment scenarios.