github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/sessionctx/variable/statusvar.go (about)

     1  // Copyright 2015 PingCAP, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package variable
    15  
    16  import (
    17  	"github.com/insionng/yougam/libraries/juju/errors"
    18  )
    19  
    20  var statisticsList []Statistics
    21  var globalStatusScopes = make(map[string]ScopeFlag)
    22  
    23  // DefaultScopeFlag is the status default scope.
    24  var DefaultScopeFlag = ScopeGlobal | ScopeSession
    25  
    26  // StatusVal is the value of the corresponding status variable.
    27  type StatusVal struct {
    28  	Scope ScopeFlag
    29  	Value interface{}
    30  }
    31  
    32  // Statistics is the interface of statistics.
    33  type Statistics interface {
    34  	// GetScope gets the status variables scope.
    35  	GetScope(status string) ScopeFlag
    36  	// Stats returns the statistics status variables.
    37  	Stats() (map[string]interface{}, error)
    38  }
    39  
    40  // RegisterStatistics registers statistics.
    41  func RegisterStatistics(s Statistics) {
    42  	statisticsList = append(statisticsList, s)
    43  }
    44  
    45  // GetStatusVars gets registered statistics status variables.
    46  func GetStatusVars() (map[string]*StatusVal, error) {
    47  	statusVars := make(map[string]*StatusVal)
    48  
    49  	for _, statistics := range statisticsList {
    50  		vals, err := statistics.Stats()
    51  		if err != nil {
    52  			return nil, errors.Trace(err)
    53  		}
    54  
    55  		for name, val := range vals {
    56  			scope := statistics.GetScope(name)
    57  			statusVars[name] = &StatusVal{Value: val, Scope: scope}
    58  		}
    59  	}
    60  
    61  	return statusVars, nil
    62  }