TakeBalanceSnapshots creates daily snapshots of balances in batches. It uses the PostgreSQL function to process balances in chunks to avoid memory issues with large datasets. Parameters: - ctx: Context for the operation, allowing for timeouts and cancellation - batchSize: The number of balances to
(ctx context.Context, batchSize int)
| 1394 | // - int: The total number of snapshots created |
| 1395 | // - error: Returns an APIError if the operation fails |
| 1396 | func (d Datasource) TakeBalanceSnapshots(ctx context.Context, batchSize int) (int, error) { |
| 1397 | var totalProcessed int |
| 1398 | |
| 1399 | // Call the PostgreSQL function to take snapshots in batches |
| 1400 | err := d.Conn.QueryRowContext(ctx, ` |
| 1401 | SELECT ledgerforge.take_daily_balance_snapshots_batched($1) |
| 1402 | `, batchSize).Scan(&totalProcessed) |
| 1403 | if err != nil { |
| 1404 | return 0, apierror.NewAPIError( |
| 1405 | apierror.ErrInternalServer, |
| 1406 | "Failed to take balance snapshots", |
| 1407 | err, |
| 1408 | ) |
| 1409 | } |
| 1410 | |
| 1411 | return totalProcessed, nil |
| 1412 | } |
| 1413 | |
| 1414 | // validateBalanceTimeParams validates the input parameters for GetBalanceAtTime |
| 1415 | func validateBalanceTimeParams(balanceID string, targetTime time.Time) error { |