package nsqd

import (
    "sort"
    "sync/atomic"

    "github.com/nsqio/nsq/internal/quantile"
)

type TopicStats struct {
    TopicName    string         `json:"topic_name"`
    Channels     []ChannelStats `json:"channels"`
    Depth        int64          `json:"depth"`
    BackendDepth int64          `json:"backend_depth"`
    MessageCount uint64         `json:"message_count"`
    Paused       bool           `json:"paused"`

    E2eProcessingLatency *quantile.Result `json:"e2e_processing_latency"`
}

func NewTopicStats(t *Topic, channels []ChannelStats) TopicStats {
    return TopicStats{
        TopicName:    t.name,
        Channels:     channels,
        Depth:        t.Depth(),
        BackendDepth: t.backend.Depth(),
        MessageCount: atomic.LoadUint64(&t.messageCount),
        Paused:       t.IsPaused(),

        E2eProcessingLatency: t.AggregateChannelE2eProcessingLatency().Result(),
    }
}

type ChannelStats struct {
    ChannelName   string        `json:"channel_name"`
    Depth         int64         `json:"depth"`
    BackendDepth  int64         `json:"backend_depth"`
    InFlightCount int           `json:"in_flight_count"`
    DeferredCount int           `json:"deferred_count"`
    MessageCount  uint64        `json:"message_count"`
    RequeueCount  uint64        `json:"requeue_count"`
    TimeoutCount  uint64        `json:"timeout_count"`
    Clients       []ClientStats `json:"clients"`
    Paused        bool          `json:"paused"`

    E2eProcessingLatency *quantile.Result `json:"e2e_processing_latency"`
}

func NewChannelStats(c *Channel, clients []ClientStats) ChannelStats {
    return ChannelStats{
        ChannelName:   c.name,
        Depth:         c.Depth(),
        BackendDepth:  c.backend.Depth(),
        InFlightCount: len(c.inFlightMessages),
        DeferredCount: len(c.deferredMessages),
        MessageCount:  atomic.LoadUint64(&c.messageCount),
        RequeueCount:  atomic.LoadUint64(&c.requeueCount),
        TimeoutCount:  atomic.LoadUint64(&c.timeoutCount),
        Clients:       clients,
        Paused:        c.IsPaused(),

        E2eProcessingLatency: c.e2eProcessingLatencyStream.Result(),
    }
}

type ClientStats struct {
    // TODO: deprecated, remove in 1.0
    Name string `json:"name"`

    ClientID        string `json:"client_id"`
    Hostname        string `json:"hostname"`
    Version         string `json:"version"`
    RemoteAddress   string `json:"remote_address"`
    State           int32  `json:"state"`
    ReadyCount      int64  `json:"ready_count"`
    InFlightCount   int64  `json:"in_flight_count"`
    MessageCount    uint64 `json:"message_count"`
    FinishCount     uint64 `json:"finish_count"`
    RequeueCount    uint64 `json:"requeue_count"`
    ConnectTime     int64  `json:"connect_ts"`
    SampleRate      int32  `json:"sample_rate"`
    Deflate         bool   `json:"deflate"`
    Snappy          bool   `json:"snappy"`
    UserAgent       string `json:"user_agent"`
    Authed          bool   `json:"authed,omitempty"`
    AuthIdentity    string `json:"auth_identity,omitempty"`
    AuthIdentityURL string `json:"auth_identity_url,omitempty"`

    TLS                           bool   `json:"tls"`
    CipherSuite                   string `json:"tls_cipher_suite"`
    TLSVersion                    string `json:"tls_version"`
    TLSNegotiatedProtocol         string `json:"tls_negotiated_protocol"`
    TLSNegotiatedProtocolIsMutual bool   `json:"tls_negotiated_protocol_is_mutual"`
}

type Topics []*Topic

func (t Topics) Len() int      { return len(t) }
func (t Topics) Swap(i, j int) { t[i], t[j] = t[j], t[i] }

type TopicsByName struct {
    Topics
}

func (t TopicsByName) Less(i, j int) bool { return t.Topics[i].name < t.Topics[j].name }

type Channels []*Channel

func (c Channels) Len() int      { return len(c) }
func (c Channels) Swap(i, j int) { c[i], c[j] = c[j], c[i] }

type ChannelsByName struct {
    Channels
}

func (c ChannelsByName) Less(i, j int) bool { return c.Channels[i].name < c.Channels[j].name }

func (n *NSQD) GetStats() []TopicStats {
    n.RLock()
    realTopics := make([]*Topic, 0, len(n.topicMap))
    for _, t := range n.topicMap {
        realTopics = append(realTopics, t)
    }
    n.RUnlock()
    sort.Sort(TopicsByName{realTopics})
    topics := make([]TopicStats, 0, len(realTopics))
    for _, t := range realTopics {
        t.RLock()
        realChannels := make([]*Channel, 0, len(t.channelMap))
        for _, c := range t.channelMap {
            realChannels = append(realChannels, c)
        }
        t.RUnlock()
        sort.Sort(ChannelsByName{realChannels})
        channels := make([]ChannelStats, 0, len(realChannels))
        for _, c := range realChannels {
            c.RLock()
            clients := make([]ClientStats, 0, len(c.clients))
            for _, client := range c.clients {
                clients = append(clients, client.Stats())
            }
            c.RUnlock()
            channels = append(channels, NewChannelStats(c, clients))
        }
        topics = append(topics, NewTopicStats(t, channels))
    }
    return topics
}

stats.go的更多相关文章

  1. [转] MemCached 的 stats 命令

    Memcached有个stats命令,通过它可以查看Memcached服务的许多状态信息.使用方法如下:先在命令行直接输入telnet 主机名端口号,连接到memcached服务器,然后再连接成功后, ...

  2. [原创博文] 用Python做统计分析 (Scipy.stats的文档)

    [转自] 用Python做统计分析 (Scipy.stats的文档) 对scipy.stats的详细介绍: 这个文档说了以下内容,对python如何做统计分析感兴趣的人可以看看,毕竟Python的库也 ...

  3. Process Stats:了解你的APP如何使用内存(转)

    原文地址:http://android-developers.blogspot.com/2014/01/process-stats-understanding-how-your.html?m=1 原作 ...

  4. Unity 5 Stats窗口

    Unity5的 Statistics上的统计信息和Unity4 有一些区别, Statistics窗口,全称叫做 Rendering Statistics Window,即渲染统计窗口(或渲染数据统计 ...

  5. memcached实战系列(四)memcached stats命令 memcached优化

    memcached提供一系列的命令进行优化的查看,方便我们调整我们的存储策略,查看我们的使用率,内存的使用率以及浪费情况.常用的命令有stats.stats settings.stats items. ...

  6. x264 n-th pass编码时候Stats文件的含义

    x264 n-th pass(一般是2pass)编码时所用的文件包括下述x264参数生成.stats文件 options: 1280x816 fps=2997/125 timebase=125/299 ...

  7. Scipy教程 - 统计函数库scipy.stats

    http://blog.csdn.net/pipisorry/article/details/49515215 统计函数Statistical functions(scipy.stats) Pytho ...

  8. dat.gui stats.js 通用参数配置及图像统计工具

    在网上看到了一个非常好的JS烟雾效果 https://paveldogreat.github.io/WebGL-Fluid-Simulation/看源码时发现了dat.gui很好用. dat.gui ...

  9. scipy.stats.multivariate_normal的使用

    参考:https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.multivariate_normal.html ...

随机推荐

  1. 使用vs2010 opencv2.4.4编译release版本程序

    大体上要注意一下几点内容: 1.vc++目录的选择上,库目录选择为opencv目录中的staticlib目录 2.在链接->输入->附加依赖库,中添加,相应的staticlib库目录中的所 ...

  2. javascript类和原型学习笔记

    js中类的所有实例对象都从同一个原型对象上继承属性.我们可以自己写一个对象创建的工厂方法来来"模拟"这种继承行为: //inherit()返回一个继承自原型对象p的属性的性对象 / ...

  3. MQ队列与哪些机器连接

    1.使用MQ安装用户登录Linux,例如:su - mqm 2.runmqsc Qm1 #Queue 代表要查询的队列3.DISPLAY CONN(*) WHERE(OBJNAME EQ Queue) ...

  4. 小dai浅谈通信网络(一)——引子

    说起通信网络,首先来看一个场景: 场景模式: 小明和小刚在闹市碰面. 小明对小刚大声喊道:"小刚,你好啊!" 小刚摇手答到:"你好,小明!" 就这么几句简单的话 ...

  5. 检查Json格式工具

    在线JSON校验格式化工具(Be JSON) 地址:http://www.bejson.com/

  6. 百度AI技术QQ群

    百度语音QQ群 648968704 视频分析QQ群 632473158 DuerOSQQ群 604592023 图像识别QQ群 649285136 文字识别QQ群 631977213 理解与交互技术U ...

  7. 解决Android SDK Manager更新失败问题

    from:http://www.ztyhome.com/android-sdk-update/ 问题描述: 使用SDK Manager更新时无法完成更新ADT时无法解析https://dl-ssl.g ...

  8. JS实现数组去重的6种方法总结

    方法一: 双层循环,外层循环元素,内层循环时比较值,如果有相同的值则跳过,不相同则push进数组. Array.prototype.distinct = function(){ var arr = t ...

  9. Yii2快速总结

    1.项目目录 后面的总结都是基于这个项目目录,注意:这个Yii2框架的basic版本原始的目录结构,未作任何更改. 2.新建控制器以及视图 controllers这个目录下的SiteControlle ...

  10. Python2和Python3比较分析

    一直有看到网上有讨论Python2和Python3的比较,最近公司也在考虑是否在spark-python大数据开发环境中升级到python3.通过本篇博文记录Python2.7.13和Pthon3.5 ...