github.com/gogf/gf/v2@v2.7.4/database/gdb/gdb_core_stats.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  //
     7  
     8  package gdb
     9  
    10  import (
    11  	"context"
    12  	"database/sql"
    13  )
    14  
    15  type localStatsItem struct {
    16  	node  *ConfigNode
    17  	stats sql.DBStats
    18  }
    19  
    20  // Node returns the configuration node info.
    21  func (item *localStatsItem) Node() ConfigNode {
    22  	return *item.node
    23  }
    24  
    25  // Stats returns the connection stat for current node.
    26  func (item *localStatsItem) Stats() sql.DBStats {
    27  	return item.stats
    28  }
    29  
    30  // Stats retrieves and returns the pool stat for all nodes that have been established.
    31  func (c *Core) Stats(ctx context.Context) []StatsItem {
    32  	var items = make([]StatsItem, 0)
    33  	c.links.Iterator(func(k, v any) bool {
    34  		var (
    35  			node  = k.(ConfigNode)
    36  			sqlDB = v.(*sql.DB)
    37  		)
    38  		items = append(items, &localStatsItem{
    39  			node:  &node,
    40  			stats: sqlDB.Stats(),
    41  		})
    42  		return true
    43  	})
    44  	return items
    45  }