1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 09:56:18 -05:00
v2fly/app/stats/command/command.go

98 lines
2.0 KiB
Go
Raw Normal View History

2019-02-01 14:08:21 -05:00
// +build !confonly
2018-03-31 04:30:12 -04:00
package command
2018-09-30 17:08:41 -04:00
//go:generate errorgen
2018-03-31 04:30:12 -04:00
import (
"context"
grpc "google.golang.org/grpc"
2018-07-10 17:40:58 -04:00
2018-03-31 04:30:12 -04:00
"v2ray.com/core"
2018-07-10 17:40:58 -04:00
"v2ray.com/core/app/stats"
2018-03-31 04:30:12 -04:00
"v2ray.com/core/common"
2018-07-10 17:40:58 -04:00
"v2ray.com/core/common/strmatcher"
feature_stats "v2ray.com/core/features/stats"
2018-03-31 04:30:12 -04:00
)
2018-07-10 17:40:58 -04:00
// statsServer is an implementation of StatsService.
2018-03-31 04:30:12 -04:00
type statsServer struct {
stats feature_stats.Manager
2018-03-31 04:30:12 -04:00
}
func NewStatsServer(manager feature_stats.Manager) StatsServiceServer {
2018-07-10 17:40:58 -04:00
return &statsServer{stats: manager}
}
2018-03-31 04:30:12 -04:00
func (s *statsServer) GetStats(ctx context.Context, request *GetStatsRequest) (*GetStatsResponse, error) {
c := s.stats.GetCounter(request.Name)
if c == nil {
return nil, newError(request.Name, " not found.")
}
var value int64
if request.Reset_ {
value = c.Set(0)
} else {
value = c.Value()
}
return &GetStatsResponse{
Stat: &Stat{
Name: request.Name,
Value: value,
},
}, nil
}
2018-07-10 17:40:58 -04:00
func (s *statsServer) QueryStats(ctx context.Context, request *QueryStatsRequest) (*QueryStatsResponse, error) {
matcher, err := strmatcher.Substr.New(request.Pattern)
if err != nil {
return nil, err
}
response := &QueryStatsResponse{}
manager, ok := s.stats.(*stats.Manager)
if !ok {
return nil, newError("QueryStats only works its own stats.Manager.")
}
manager.Visit(func(name string, c feature_stats.Counter) bool {
2018-07-10 17:40:58 -04:00
if matcher.Match(name) {
var value int64
if request.Reset_ {
value = c.Set(0)
} else {
value = c.Value()
}
response.Stat = append(response.Stat, &Stat{
Name: name,
Value: value,
})
}
return true
})
return response, nil
}
2018-03-31 04:30:12 -04:00
type service struct {
2018-10-22 05:26:22 -04:00
statsManager feature_stats.Manager
2018-03-31 04:30:12 -04:00
}
func (s *service) Register(server *grpc.Server) {
2018-10-22 05:26:22 -04:00
RegisterStatsServiceServer(server, NewStatsServer(s.statsManager))
2018-03-31 04:30:12 -04:00
}
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, cfg interface{}) (interface{}, error) {
2018-10-22 05:26:22 -04:00
s := new(service)
core.RequireFeatures(ctx, func(sm feature_stats.Manager) {
s.statsManager = sm
})
return s, nil
2018-03-31 04:30:12 -04:00
}))
}