github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/monitors/handle_clean.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 monitorsPkg
     6  
     7  import (
     8  	"sort"
     9  
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/monitor"
    12  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
    13  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
    14  )
    15  
    16  // HandleClean handles the chifra monitors --clean command.
    17  func (opts *MonitorsOptions) HandleClean(rCtx *output.RenderCtx) error {
    18  	chain := opts.Globals.Chain
    19  	testMode := opts.Globals.TestMode
    20  
    21  	fetchData := func(modelChan chan types.Modeler, errorChan chan error) {
    22  		_, monArray := GetMonitorMap(chain)
    23  		for _, mon := range monArray {
    24  			addr := mon.Address.Hex()
    25  			s := types.MonitorClean{
    26  				Address: mon.Address,
    27  				Staged:  mon.Staged,
    28  			}
    29  			if testMode {
    30  				if addr == "0x001d14804b399c6ef80e64576f657660804fec0b" ||
    31  					addr == "0x0029218e1dab069656bfb8a75947825e7989b987" {
    32  					s.SizeThen = 10
    33  					s.SizeNow = 8
    34  					s.Dups = 10 - 8
    35  				}
    36  			} else {
    37  				then, now, err := mon.RemoveDups()
    38  				if err != nil {
    39  					errorChan <- err
    40  					return
    41  				}
    42  				s.SizeThen = int64(then)
    43  				s.SizeNow = int64(now)
    44  				s.Dups = s.SizeThen - s.SizeNow
    45  				if opts.Staged && mon.Staged {
    46  					s.Removed = true
    47  					mon.Close()
    48  					_ = mon.Delete()
    49  					_, _ = mon.Remove()
    50  				}
    51  			}
    52  
    53  			if s.SizeThen > 0 {
    54  				modelChan <- &s
    55  			} else if s.SizeNow == 0 {
    56  				mon.Close()
    57  				_ = mon.Delete()
    58  				_, _ = mon.Remove()
    59  			}
    60  		}
    61  	}
    62  
    63  	extraOpts := map[string]any{
    64  		"staged": opts.Staged,
    65  	}
    66  	return output.StreamMany(rCtx, fetchData, opts.Globals.OutputOptsWithExtra(extraOpts))
    67  }
    68  
    69  func GetMonitorMap(chain string) (map[base.Address]*monitor.Monitor, []*monitor.Monitor) {
    70  	monitorChan := make(chan monitor.Monitor)
    71  
    72  	go monitor.ListExistingMonitors(chain, monitorChan)
    73  
    74  	monMap := make(map[base.Address]*monitor.Monitor)
    75  	monArray := []*monitor.Monitor{}
    76  	for mon := range monitorChan {
    77  		switch mon.Address {
    78  		case base.NotAMonitor:
    79  			close(monitorChan)
    80  		default:
    81  			monMap[mon.Address] = &mon
    82  			monArray = append(monArray, &mon)
    83  		}
    84  	}
    85  
    86  	sort.Slice(monArray, func(i, j int) bool {
    87  		return monArray[i].Address.Hex() < monArray[j].Address.Hex()
    88  	})
    89  
    90  	return monMap, monArray
    91  }