Skip to content

Commit

Permalink
node_stats: support all runtimes
Browse files Browse the repository at this point in the history
  • Loading branch information
mitjat committed Feb 13, 2024
1 parent a2c2151 commit 698661b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
48 changes: 23 additions & 25 deletions analyzer/node_stats/node_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ const (
)

type processor struct {
layers []common.Layer
source nodeapi.ConsensusApiLite
emeraldSource nodeapi.RuntimeApiLite
sapphireSource nodeapi.RuntimeApiLite
target storage.TargetStorage
logger *log.Logger
layers []common.Layer
consensusSource nodeapi.ConsensusApiLite
runtimeSources map[common.Runtime]nodeapi.RuntimeApiLite
target storage.TargetStorage
logger *log.Logger
}

var _ item.ItemProcessor[common.Layer] = (*processor)(nil)

func NewAnalyzer(
cfg config.ItemBasedAnalyzerConfig,
layers []common.Layer,
sourceClient nodeapi.ConsensusApiLite,
consensusClient nodeapi.ConsensusApiLite,
emeraldClient nodeapi.RuntimeApiLite,
sapphireClient nodeapi.RuntimeApiLite,
pontusxClient nodeapi.RuntimeApiLite,
target storage.TargetStorage,
logger *log.Logger,
) (analyzer.Analyzer, error) {
Expand All @@ -51,12 +51,15 @@ func NewAnalyzer(
layers = []common.Layer{common.LayerConsensus, common.LayerEmerald, common.LayerSapphire}
}
p := &processor{
layers: layers,
source: sourceClient,
emeraldSource: emeraldClient,
sapphireSource: sapphireClient,
target: target,
logger: logger.With("analyzer", nodeStatsAnalyzerName),
layers: layers,
consensusSource: consensusClient,
runtimeSources: map[common.Runtime]nodeapi.RuntimeApiLite{
common.RuntimeEmerald: emeraldClient,
common.RuntimeSapphire: sapphireClient,
common.RuntimePontusx: pontusxClient,
},
target: target,
logger: logger.With("analyzer", nodeStatsAnalyzerName),
}

return item.NewAnalyzer[common.Layer](
Expand All @@ -74,27 +77,22 @@ func (p *processor) GetItems(ctx context.Context, limit uint64) ([]common.Layer,
func (p *processor) ProcessItem(ctx context.Context, batch *storage.QueryBatch, layer common.Layer) error {
p.logger.Debug("fetching node height", "layer", layer)
latestHeight := uint64(0) // will be fetched from the node
switch layer {
case common.LayerConsensus:
latestBlock, err := p.source.GetBlock(ctx, consensusAPI.HeightLatest)
if layer == common.LayerConsensus {
latestBlock, err := p.consensusSource.GetBlock(ctx, consensusAPI.HeightLatest)
if err != nil {
return fmt.Errorf("error fetching latest block height for layer %s: %w", layer, err)
}
latestHeight = uint64(latestBlock.Height)
case common.LayerEmerald:
latestBlock, err := p.emeraldSource.GetBlockHeader(ctx, runtimeAPI.RoundLatest)
if err != nil {
return fmt.Errorf("error fetching latest block height for layer %s: %w", layer, err)
} else { // the layer is a runtime
runtimeClient, ok := p.runtimeSources[common.Runtime(layer)]
if !ok || runtimeClient == nil {
return fmt.Errorf("unsupported layer %s", layer)
}
latestHeight = latestBlock.Round
case common.LayerSapphire:
latestBlock, err := p.sapphireSource.GetBlockHeader(ctx, runtimeAPI.RoundLatest)
latestBlock, err := runtimeClient.GetBlockHeader(ctx, runtimeAPI.RoundLatest)
if err != nil {
return fmt.Errorf("error fetching latest block height for layer %s: %w", layer, err)
}
latestHeight = latestBlock.Round
default:
return fmt.Errorf("unsupported layer %s", layer)
}
batch.Queue(queries.NodeHeightUpsert,
layer,
Expand Down
6 changes: 5 additions & 1 deletion cmd/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,11 @@ func NewService(cfg *config.AnalysisConfig) (*Service, error) { //nolint:gocyclo
if err1 != nil {
return nil, err1
}
return nodestats.NewAnalyzer(cfg.Analyzers.NodeStats.ItemBasedAnalyzerConfig, cfg.Analyzers.NodeStats.Layers, sourceClient, emeraldClient, sapphireClient, dbClient, logger)
pontusxClient, err1 := sources.Runtime(ctx, common.RuntimePontusx)
if err1 != nil {
return nil, err1
}
return nodestats.NewAnalyzer(cfg.Analyzers.NodeStats.ItemBasedAnalyzerConfig, cfg.Analyzers.NodeStats.Layers, sourceClient, emeraldClient, sapphireClient, pontusxClient, dbClient, logger)
})
}
if cfg.Analyzers.AggregateStats != nil {
Expand Down

0 comments on commit 698661b

Please sign in to comment.