Skip to content

Commit

Permalink
API: Add user online stats (#3637)
Browse files Browse the repository at this point in the history
* add statsUserOnline bool to policy

* add OnlineMap struct to stats

* apply UserOnline functionality to dispatcher

* add statsonline api command

* fix comments

* Update app/stats/online_map.go

Co-authored-by: mmmray <[email protected]>

* improve AddIP

* regenerate pb

---------

Co-authored-by: mmmray <[email protected]>
  • Loading branch information
hossinasaadi and mmmray authored Nov 3, 2024
1 parent e3276df commit 2c72864
Show file tree
Hide file tree
Showing 15 changed files with 575 additions and 177 deletions.
12 changes: 12 additions & 0 deletions app/dispatcher/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,18 @@ func (d *DefaultDispatcher) getLink(ctx context.Context) (*transport.Link, *tran
}
}
}

if p.Stats.UserOnline {
name := "user>>>" + user.Email + ">>>online"
if om, _ := stats.GetOrRegisterOnlineMap(d.stats, name); om != nil {
sessionInbounds := session.InboundFromContext(ctx)
userIP := sessionInbounds.Source.Address.String()
om.AddIP(userIP)
// log Online user with ips
// errors.LogDebug(ctx, "user>>>" + user.Email + ">>>online", om.Count(), om.List())

}
}
}

return inboundLink, outboundLink
Expand Down
1 change: 1 addition & 0 deletions app/policy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func (p *Policy) ToCorePolicy() policy.Session {
if p.Stats != nil {
cp.Stats.UserUplink = p.Stats.UserUplink
cp.Stats.UserDownlink = p.Stats.UserDownlink
cp.Stats.UserOnline = p.Stats.UserOnline
}
if p.Buffer != nil {
cp.Buffer.PerConnection = p.Buffer.Connection
Expand Down
192 changes: 150 additions & 42 deletions app/policy/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/policy/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ message Policy {
message Stats {
bool user_uplink = 1;
bool user_downlink = 2;
bool user_online = 3;
}

message Buffer {
Expand Down
14 changes: 14 additions & 0 deletions app/stats/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ func (s *statsServer) GetStats(ctx context.Context, request *GetStatsRequest) (*
}, nil
}

func (s *statsServer) GetStatsOnline(ctx context.Context, request *GetStatsRequest) (*GetStatsResponse, error) {
c := s.stats.GetOnlineMap(request.Name)
if c == nil {
return nil, errors.New(request.Name, " not found.")
}
value := int64(c.Count())
return &GetStatsResponse{
Stat: &Stat{
Name: request.Name,
Value: value,
},
}, nil
}

func (s *statsServer) QueryStats(ctx context.Context, request *QueryStatsRequest) (*QueryStatsResponse, error) {
matcher, err := strmatcher.Substr.New(request.Pattern)
if err != nil {
Expand Down
Loading

0 comments on commit 2c72864

Please sign in to comment.