github.com/annchain/OG@v0.0.9/rpc/status_controller.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package rpc
    15  
    16  import (
    17  	"fmt"
    18  	"github.com/annchain/OG/p2p"
    19  	"github.com/annchain/OG/p2p/ioperformance"
    20  	"github.com/annchain/OG/types"
    21  	"github.com/gin-gonic/gin"
    22  	"github.com/spf13/viper"
    23  	"net/http"
    24  	"time"
    25  )
    26  
    27  type SyncStatus struct {
    28  	Id                       string `json:"id"`
    29  	SyncMode                 string `json:"syncMode"`
    30  	CatchupSyncerStatus      string `json:"catchupSyncerStatus"`
    31  	CatchupSyncerEnabled     bool   `json:"catchupSyncerEnabled"`
    32  	IncrementalSyncerEnabled bool   `json:"incrementalSyncerEnabled"`
    33  	Height                   uint64 `json:"height"`
    34  	LatestHeight             uint64 `json:"latestHeight"`
    35  	BestPeer                 string `json:"bestPeer"`
    36  	Error                    string `json:"error"`
    37  	Txid                     uint32 `json:"txid"`
    38  }
    39  
    40  //NodeStatus
    41  type NodeStatus struct {
    42  	NodeInfo  *p2p.NodeInfo   `json:"node_info"`
    43  	PeersInfo []*p2p.PeerInfo `json:"peers_info"`
    44  }
    45  
    46  //Status node status
    47  func (r *RpcController) SyncStatus(c *gin.Context) {
    48  	status := r.syncStatus()
    49  	cors(c)
    50  	c.JSON(http.StatusOK, status)
    51  }
    52  
    53  func (r *RpcController) syncStatus() SyncStatus {
    54  	var status SyncStatus
    55  
    56  	status = SyncStatus{
    57  		Id:                       r.P2pServer.Self().ID().TerminalString(),
    58  		SyncMode:                 r.SyncerManager.Status.String(),
    59  		CatchupSyncerStatus:      r.SyncerManager.CatchupSyncer.WorkState.String(),
    60  		CatchupSyncerEnabled:     r.SyncerManager.CatchupSyncer.Enabled,
    61  		IncrementalSyncerEnabled: r.SyncerManager.IncrementalSyncer.Enabled,
    62  		Height:                   r.SyncerManager.NodeStatusDataProvider.GetCurrentNodeStatus().CurrentId,
    63  	}
    64  
    65  	peerId, _, seqId, err := r.SyncerManager.Hub.BestPeerInfo()
    66  	if err != nil {
    67  		status.Error = err.Error()
    68  	} else {
    69  		status.LatestHeight = seqId
    70  		status.BestPeer = peerId
    71  
    72  	}
    73  	status.Txid = r.Og.TxPool.GetTxNum()
    74  	return status
    75  }
    76  
    77  func (r *RpcController) Performance(c *gin.Context) {
    78  	cd := r.PerformanceMonitor.CollectData()
    79  	cors(c)
    80  	c.JSON(http.StatusOK, cd)
    81  }
    82  
    83  func (r *RpcController) NetIo(c *gin.Context) {
    84  	cors(c)
    85  	type transportData struct {
    86  		*ioperformance.IoDataInfo `json:"transport_data"`
    87  	}
    88  	data := transportData{ioperformance.GetNetPerformance()}
    89  	Response(c, http.StatusOK, nil, data)
    90  	return
    91  }
    92  
    93  func (r *RpcController) OgPeersInfo(c *gin.Context) {
    94  	info := r.Og.Manager.Hub.PeersInfo()
    95  
    96  	Response(c, http.StatusOK, nil, info)
    97  	return
    98  }
    99  
   100  type Monitor struct {
   101  	Port    string     `json:"port"`
   102  	ShortId string     `json:"short_id"`
   103  	Peers   []Peer     `json:"peers,omitempty"`
   104  	SeqId   uint64     `json:"seq_id"`
   105  	Tps     *Tps       `json:"tps"`
   106  	Status  SyncStatus `json:"status"`
   107  }
   108  
   109  type Peer struct {
   110  	Addr    string `json:"addr"`
   111  	ShortId string `json:"short_id"`
   112  	Link    bool   `json:"link"`
   113  }
   114  
   115  func (r *RpcController) Monitor(c *gin.Context) {
   116  	var m Monitor
   117  	seq := r.Og.Dag.LatestSequencer()
   118  	if seq != nil {
   119  		m.SeqId = seq.Height
   120  	}
   121  	peersinfo := r.Og.Manager.Hub.PeersInfo()
   122  	for _, p := range peersinfo {
   123  		/*
   124  			if p.Network.Inbound {
   125  				addr = p.Network.LocalAddress
   126  			}else {
   127  				addr = p.Network.RemoteAddress
   128  			}
   129  				ipPort :=strings.Split(addr,":")
   130  				if len(ipPort) ==2 {
   131  					m.PeerPipeIns = append(m.PeerPipeIns ,ipPort[1])
   132  				}
   133  		*/
   134  		var peer Peer
   135  		peer.Addr = p.Addrs
   136  		peer.ShortId = p.ShortId
   137  		peer.Link = p.Link
   138  		m.Peers = append(m.Peers, peer)
   139  	}
   140  	m.Port = viper.GetString("p2p.port")
   141  	m.ShortId = r.P2pServer.NodeInfo().ShortId
   142  	m.Tps, _ = r.getTps()
   143  	m.Status = r.syncStatus()
   144  
   145  	Response(c, http.StatusOK, nil, m)
   146  	return
   147  }
   148  
   149  func (r *RpcController) BftStatus(c *gin.Context) {
   150  	cors(c)
   151  	if r.AnnSensus != nil {
   152  		s := r.AnnSensus.GetBftStatus()
   153  		Response(c, http.StatusOK, nil, s)
   154  	} else {
   155  		Response(c, http.StatusOK, nil, nil)
   156  	}
   157  
   158  	return
   159  }
   160  
   161  func (r *RpcController) GetPoolHashes(c *gin.Context) {
   162  	cors(c)
   163  	buffer := c.Query("buffer")
   164  	if buffer == "dependency" {
   165  		r.BufferDependencyCache(c)
   166  		return
   167  	} else if buffer == "known" {
   168  		r.BufferKnownCache(c)
   169  		return
   170  	}
   171  	s := r.Og.TxPool.GetOrder()
   172  	Response(c, http.StatusOK, nil, s.String())
   173  	return
   174  }
   175  
   176  func (r *RpcController) BufferDependencyCache(c *gin.Context) {
   177  	cors(c)
   178  	s := r.Og.TxBuffer.Dump()
   179  	Response(c, http.StatusOK, nil, s)
   180  	return
   181  }
   182  func (r *RpcController) BufferKnownCache(c *gin.Context) {
   183  	cors(c)
   184  	s := r.Og.TxBuffer.DumpKnownCache()
   185  	Response(c, http.StatusOK, nil, s)
   186  	return
   187  }
   188  
   189  func (r *RpcController) Validator(c *gin.Context) {
   190  	cors(c)
   191  	Response(c, http.StatusOK, nil, "validator")
   192  	return
   193  }
   194  
   195  type Tps struct {
   196  	Num     int     `json:"num"`
   197  	TxCount int     `json:"tx_num"`
   198  	Seconds float64 `json:"duration"`
   199  }
   200  
   201  func (r *RpcController) getTps() (t *Tps, err error) {
   202  	var tps Tps
   203  	lseq := r.Og.Dag.LatestSequencer()
   204  	if lseq == nil {
   205  		return nil, fmt.Errorf("not found")
   206  	}
   207  	if lseq.Height < 3 {
   208  		return
   209  	}
   210  
   211  	var cfs []types.ConfirmTime
   212  	for id := lseq.Height; id > 0 && id > lseq.Height-5; id-- {
   213  		cf := r.Og.Dag.GetConfirmTime(id)
   214  		if cf == nil {
   215  			return nil, fmt.Errorf("db error")
   216  		}
   217  		cfs = append(cfs, *cf)
   218  	}
   219  	var start, end time.Time
   220  	for i, cf := range cfs {
   221  		if i == 0 {
   222  			end, err = time.Parse(time.RFC3339Nano, cf.ConfirmTime)
   223  			if err != nil {
   224  				return nil, err
   225  			}
   226  		}
   227  		if i == len(cfs)-1 {
   228  			start, err = time.Parse(time.RFC3339Nano, cf.ConfirmTime)
   229  			if err != nil {
   230  				return nil, err
   231  			}
   232  		} else {
   233  			tps.TxCount += int(cf.TxNum)
   234  		}
   235  	}
   236  
   237  	if !end.After(start) {
   238  		return nil, fmt.Errorf("time server error")
   239  	}
   240  	sub := end.Sub(start)
   241  	sec := sub.Seconds()
   242  	if sec != 0 {
   243  		num := float64(tps.TxCount) / sec
   244  		tps.Num = int(num)
   245  	}
   246  	tps.Seconds = sec
   247  	return &tps, nil
   248  }
   249  
   250  func (r *RpcController) Tps(c *gin.Context) {
   251  	cors(c)
   252  	t, err := r.getTps()
   253  	if err != nil {
   254  		Response(c, http.StatusBadRequest, err, nil)
   255  		return
   256  	}
   257  	Response(c, http.StatusOK, nil, t)
   258  	return
   259  }
   260  
   261  //Status node status
   262  func (r *RpcController) Status(c *gin.Context) {
   263  	var status NodeStatus
   264  	status.NodeInfo = r.P2pServer.NodeInfo()
   265  	status.PeersInfo = r.P2pServer.PeersInfo()
   266  	cors(c)
   267  	Response(c, http.StatusOK, nil, status)
   268  }
   269  
   270  //PeersInfo network information
   271  func (r *RpcController) NetInfo(c *gin.Context) {
   272  	info := r.P2pServer.NodeInfo()
   273  	cors(c)
   274  	Response(c, http.StatusOK, nil, info)
   275  }
   276  
   277  //PeersInfo  peers information
   278  func (r *RpcController) PeersInfo(c *gin.Context) {
   279  	peersInfo := r.P2pServer.PeersInfo()
   280  	cors(c)
   281  	Response(c, http.StatusOK, nil, peersInfo)
   282  }