New creates a new DB rollup service that periodically runs rollup queries. It is the caller's responsibility to call Close on the returned instance. This is for e.g. generating insights data (template_usage_stats) from raw data (workspace_agent_stats, workspace_app_stats).
(logger slog.Logger, db database.Store, opts ...Option)
| 60 | // This is for e.g. generating insights data (template_usage_stats) from |
| 61 | // raw data (workspace_agent_stats, workspace_app_stats). |
| 62 | func New(logger slog.Logger, db database.Store, opts ...Option) *Rolluper { |
| 63 | ctx, cancel := context.WithCancel(context.Background()) |
| 64 | |
| 65 | r := &Rolluper{ |
| 66 | cancel: cancel, |
| 67 | closed: make(chan struct{}), |
| 68 | db: db, |
| 69 | logger: logger, |
| 70 | interval: DefaultInterval, |
| 71 | } |
| 72 | |
| 73 | for _, opt := range opts { |
| 74 | opt(r) |
| 75 | } |
| 76 | |
| 77 | //nolint:gocritic // The system rolls up database tables without user input. |
| 78 | ctx = dbauthz.AsSystemRestricted(ctx) |
| 79 | go r.start(ctx) |
| 80 | |
| 81 | return r |
| 82 | } |
| 83 | |
| 84 | func (r *Rolluper) start(ctx context.Context) { |
| 85 | defer close(r.closed) |