MCPcopy Create free account
hub / github.com/devaccuracy/ledgerforge / GetTotalCommittedTransactions

Method GetTotalCommittedTransactions

database/transaction.go:745–786  ·  view source on GitHub ↗

GetTotalCommittedTransactions calculates the total committed transaction amounts for a given parent transaction. It uses OpenTelemetry for tracing and returns the total or an error if the retrieval fails. Parameters: - ctx: Context for managing the request and tracing. - parentID: The ID of the pare

(ctx context.Context, parentID string)

Source from the content-addressed store, hash-verified

743// Returns:
744// - The total committed amount as *big.Int, or 0 if no transactions are found, along with an error if the retrieval fails.
745func (d Datasource) GetTotalCommittedTransactions(ctx context.Context, parentID string) (*big.Int, error) {
746 // Start a new tracing span for the operation
747 ctx, span := otel.Tracer("transaction.database").Start(ctx, "GetTotalCommittedTransactions")
748 defer span.End()
749
750 // SQL query to calculate the total precise amount for the given parent transaction
751 query := `
752 SELECT SUM(precise_amount) AS total_amount
753 FROM ledgerforge.transactions
754 WHERE parent_transaction = $1 AND status = 'APPLIED'
755 GROUP BY parent_transaction;
756 `
757
758 // Initialize the variable to store the total amount
759 var totalAmountStr string
760
761 // Execute the query and scan the result into totalAmount
762 err := d.Conn.QueryRowContext(ctx, query, parentID).Scan(&totalAmountStr)
763 if err != nil {
764 // If no rows are found, return 0 without error
765 if errors.Is(err, sql.ErrNoRows) {
766 return new(big.Int), nil
767 }
768 // Record the error in the tracing span and return the error
769 span.RecordError(err)
770 return big.NewInt(0), apierror.NewAPIError(apierror.ErrInternalServer, "Failed to get total committed transactions", err)
771 }
772
773 total, ok := new(big.Int).SetString(totalAmountStr, 10)
774 if !ok {
775 return nil, apierror.NewAPIError(apierror.ErrInternalServer, "Failed to parse precise amount", nil)
776 }
777
778 // Log the successful retrieval of the total amount
779 span.AddEvent("Total committed transactions retrieved", trace.WithAttributes(
780 attribute.String("parent_transaction.id", parentID),
781 attribute.String("total_amount", total.String()),
782 ))
783
784 // Return the total amount
785 return total, nil
786}
787
788// GetTransactionsPaginated retrieves a batch of transactions from the database with pagination support and caches the result.
789// If the data is found in cache, it is returned from there; otherwise, it is fetched from the database and then cached.

Calls 2

NewAPIErrorFunction · 0.92
StartMethod · 0.45