github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/when/handle_ts_show.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 whenPkg 6 7 import ( 8 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base" 9 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/identifiers" 10 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output" 11 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/tslib" 12 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types" 13 ) 14 15 // HandleTimestampsShow handles chifra when --timestamps 16 func (opts *WhenOptions) HandleTimestampsShow(rCtx *output.RenderCtx) error { 17 chain := opts.Globals.Chain 18 19 bnMap, err := identifiers.GetBlockNumberMap(chain, opts.BlockIds) 20 if err != nil { 21 return err 22 } 23 24 cnt, err := tslib.NTimestamps(chain) 25 if err != nil { 26 return err 27 } 28 29 prev := base.Timestamp(0) 30 fetchData := func(modelChan chan types.Modeler, errorChan chan error) { 31 for bn := base.Blknum(0); bn < cnt; bn++ { 32 if len(bnMap) == 0 || bnMap[bn] { 33 ts, err := tslib.FromBn(chain, bn) 34 if err != nil { 35 errorChan <- err 36 } 37 s := types.Timestamp{ 38 BlockNumber: base.Blknum(ts.Bn), 39 Timestamp: base.Timestamp(ts.Ts), 40 Diff: int64(base.Timestamp(ts.Ts) - prev), 41 } 42 if bn == 0 { 43 s.Diff = 0 44 } 45 modelChan <- &s 46 prev = s.Timestamp 47 } 48 } 49 } 50 51 return output.StreamMany(rCtx, fetchData, opts.Globals.OutputOpts()) 52 }