PersistenceService class
持久化环形缓冲服务 / Persistence ring-buffer service
所有采集数据原本只存在于内存(ListQueue + 单例),App 崩溃或面板长期不开就会丢失。 本服务把日志 / 网络 / 异常异步落盘到 SQLite,按"行数上限 + 保留时长"双重 滚动裁剪,形成磁盘环形缓冲,从而支持崩溃后复盘与"导出本次会话完整存档"。 Collected data previously lived only in memory (ListQueue + singleton) and was lost when the app crashed or the panel stayed closed. This service async-flushes logs / network / errors into SQLite, rolling by both row cap and retention window (a disk ring buffer) — enabling crash post-mortems and a full "export this session" archive.
全部操作都在 try/catch 内并优雅降级:数据库不可用(如桌面端未配置 FFI) 时 isEnabled 为 false,所有写入变为空操作,绝不影响宿主 App。 Everything is guarded by try/catch and degrades gracefully: if the DB is unavailable (e.g. desktop without FFI) isEnabled is false and all writes become no-ops — never affecting the host app.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- isEnabled → bool
-
是否已启用(DB 可用)/ Whether enabled (DB available)
no setter
- maxRowsPerTable → int
-
每张表的行数上限(环形缓冲裁剪线),由 init 的 maxRowsPerTable 决定。
Row cap per table (the ring-buffer trim line), set via init's
maxRowsPerTable.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
buildSessionArchiveJson(
{int logLimit = 2000, int netLimit = 500, int errLimit = 200, int alertLimit = 100}) → Future< String> - 构建"本次会话完整存档"JSON(日志 + 异常 + 网络) Build the full-session archive JSON (logs + errors + network)
-
clearAll(
) → Future< void> - 清空磁盘上的持久化数据 / Clear persisted data on disk
-
dispose(
) → Future< void> - 关闭(先刷盘)/ Close (flush first)
-
enqueueAlert(
AlertEvent e) → void - 入队一条告警事件(异步落盘,崩溃后可在 Alerts 标签回看) Enqueue an alert event (async flush; recoverable in the Alerts tab later)
-
enqueueError(
ErrorRecord e) → void - 入队一条聚合异常 / Enqueue an aggregated error
-
enqueueLog(
LogEntry e) → void - 入队一条日志(异步落盘)/ Enqueue a log (async flush)
-
enqueueNetwork(
NetworkRequest r) → void - 入队一条网络记录 / Enqueue a network record
- 导出并分享"本次会话完整存档"/ Export & share the full-session archive
-
flush(
) → Future< void> - 把缓冲区写入磁盘并按环形缓冲裁剪 / Flush buffer to disk then trim
-
init(
{int maxRowsPerTable = _defaultMaxRows, Duration retention = _defaultRetention, Duration flushInterval = _defaultFlushInterval}) → Future< void> - 初始化数据库(失败时优雅降级)/ Initialize the DB (degrades gracefully)
-
loadAlerts(
{int limit = 100}) → Future< List< AlertEvent> > - 读取已持久化的告警事件(最新在前)/ Load persisted alerts (newest first)
-
loadErrors(
{int limit = 200}) → Future< List< ErrorRecord> > - 读取已持久化的聚合异常(最新在前)/ Load persisted errors (newest first)
-
loadLogs(
{int limit = 500}) → Future< List< LogEntry> > - 读取已持久化的日志(最新在前)/ Load persisted logs (newest first)
-
loadNetworkJson(
{int limit = 500}) → Future< List< Map< >String, dynamic> > - 读取已持久化的网络记录(原始 JSON,最新在前) Load persisted network records (raw JSON, newest first)
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- instance → PersistenceService
-
单例实例 / Singleton instance
final