github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/export/handle_appearances.go (about)

     1  // Copyright 2021 The TrueBlocks Authors. All rights reserved.
     2  // Use of this source code is governed by a license that can
     3  // be found in the LICENSE file.
     4  
     5  package exportPkg
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/filter"
    12  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/monitor"
    13  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
    14  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/tslib"
    15  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
    16  )
    17  
    18  func (opts *ExportOptions) HandleAppearances(rCtx *output.RenderCtx, monitorArray []monitor.Monitor) error {
    19  	chain := opts.Globals.Chain
    20  	filter := filter.NewFilter(
    21  		opts.Reversed,
    22  		false,
    23  		[]string{},
    24  		base.BlockRange{First: opts.FirstBlock, Last: opts.LastBlock},
    25  		base.RecordRange{First: opts.FirstRecord, Last: opts.GetMax()},
    26  	)
    27  
    28  	fetchData := func(modelChan chan types.Modeler, errorChan chan error) {
    29  		currentBn := uint32(0)
    30  		for _, mon := range monitorArray {
    31  			if apps, cnt, err := mon.ReadAndFilterAppearances(filter, true /* withCount */); err != nil {
    32  				errorChan <- err
    33  				return
    34  			} else if !opts.NoZero || cnt > 0 {
    35  				for _, app := range apps {
    36  					if opts.Globals.Verbose {
    37  						if app.BlockNumber == 0 || app.BlockNumber != currentBn {
    38  							app.Timestamp, _ = tslib.FromBnToTs(chain, base.Blknum(app.BlockNumber))
    39  						}
    40  						currentBn = app.BlockNumber
    41  					}
    42  					modelChan <- &app
    43  				}
    44  			} else {
    45  				errorChan <- fmt.Errorf("no appearances found for %s", mon.Address.Hex())
    46  				continue
    47  			}
    48  		}
    49  	}
    50  
    51  	extraOpts := map[string]any{
    52  		"export": true,
    53  	}
    54  
    55  	return output.StreamMany(rCtx, fetchData, opts.Globals.OutputOptsWithExtra(extraOpts))
    56  }
    57  
    58  func (opts *ExportOptions) IsMax(cnt uint64) bool {
    59  	max := opts.MaxRecords
    60  	if max == 250 && !opts.Globals.IsApiMode() {
    61  		max = base.NOPOS
    62  	}
    63  	return cnt >= max
    64  }
    65  
    66  func (opts *ExportOptions) GetMax() uint64 {
    67  	if opts.MaxRecords == 250 && !opts.Globals.IsApiMode() {
    68  		return base.NOPOS
    69  	}
    70  	return opts.MaxRecords
    71  }