github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/export/handle_count.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/file"
    12  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/filter"
    13  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/monitor"
    14  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
    15  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
    16  )
    17  
    18  func (opts *ExportOptions) HandleCount(rCtx *output.RenderCtx, monitorArray []monitor.Monitor) error {
    19  	if opts.Globals.Verbose {
    20  		for i := 0; i < len(monitorArray); i++ {
    21  			_ = monitorArray[i].ReadMonitorHeader()
    22  			monitorArray[i].Close()
    23  		}
    24  	}
    25  
    26  	testMode := opts.Globals.TestMode
    27  	filter := filter.NewFilter(
    28  		opts.Reversed,
    29  		opts.Reverted,
    30  		opts.Fourbytes,
    31  		base.BlockRange{First: opts.FirstBlock, Last: opts.LastBlock},
    32  		base.RecordRange{First: opts.FirstRecord, Last: opts.GetMax()},
    33  	)
    34  
    35  	fetchData := func(modelChan chan types.Modeler, errorChan chan error) {
    36  		for _, mon := range monitorArray {
    37  			if apps, cnt, err := mon.ReadAndFilterAppearances(filter, true /* withCount */); err != nil {
    38  				errorChan <- err
    39  				return
    40  			} else if !opts.NoZero || cnt > 0 {
    41  				s := types.Monitor{
    42  					Address:     mon.Address,
    43  					NRecords:    int64(len(apps)),
    44  					FileSize:    file.FileSize(mon.Path()),
    45  					LastScanned: mon.LastScanned,
    46  					Deleted:     mon.Deleted,
    47  				}
    48  				if testMode {
    49  					s.FileSize = 0xdead
    50  					s.LastScanned = maxTestingBlock
    51  				}
    52  				modelChan <- &s
    53  			} else {
    54  				errorChan <- fmt.Errorf("no appearances found for %s", mon.Address.Hex())
    55  				continue
    56  			}
    57  			mon.Close()
    58  		}
    59  	}
    60  
    61  	return output.StreamMany(rCtx, fetchData, opts.Globals.OutputOpts())
    62  }
    63  
    64  const maxTestingBlock = 17000000