Skip to content

Commit eb1da78

Browse files
committed
add user info metric
1 parent 2a5c0bf commit eb1da78

File tree

5 files changed

+77
-4
lines changed

5 files changed

+77
-4
lines changed

app/app.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ type APP struct {
3535
userMap map[string]time.Time
3636
recordMap map[string]time.Time
3737

38+
userInfoMu sync.Mutex
39+
userInfo map[string]time.Time
40+
3841
wg tools.WaitGroupWrapper
3942
shutdown chan struct{}
4043
shutdownComplete chan struct{}
@@ -49,6 +52,7 @@ func New(cfg Config) *APP {
4952
group: new(singleflight.Group),
5053
userMap: make(map[string]time.Time),
5154
recordMap: make(map[string]time.Time),
55+
userInfo: make(map[string]time.Time),
5256
shutdown: make(chan struct{}),
5357
shutdownComplete: make(chan struct{}),
5458
}
@@ -89,6 +93,9 @@ func (a *APP) Run() error {
8993
a.wg.Wrap(func() {
9094
exitFunc(a.runHTTP())
9195
})
96+
a.wg.Wrap(func() {
97+
exitFunc(a.runInterHTTP())
98+
})
9299

93100
if enable {
94101
a.Cron(CronSpec)
@@ -111,12 +118,17 @@ func (a *APP) runHTTP() error {
111118

112119
r := mux.NewRouter()
113120
Router(r, a, ioutil.Discard)
121+
handle := handlers.RecoveryHandler()(handlers.CompressHandler(r))
122+
return a.runhttp(address, handle)
123+
}
124+
125+
func (a *APP) runhttp(address string, handle http.Handler) error {
114126

115127
srv := &http.Server{
116128
Addr: address,
117129
WriteTimeout: 120 * time.Second,
118130
ReadTimeout: 120 * time.Second,
119-
Handler: handlers.RecoveryHandler()(handlers.CompressHandler(r)),
131+
Handler: handle,
120132
}
121133

122134
go func() {

app/config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package app
22

33
type Config struct {
4-
Address string
5-
SqlitePath string
6-
EnableCron bool
4+
Address string
5+
DebugAddress int
6+
SqlitePath string
7+
EnableCron bool
78
}
89

910
type RedisConfig struct {

app/metrics.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package app
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"sort"
8+
"time"
9+
10+
"github.com/gorilla/mux"
11+
)
12+
13+
func (a *APP) addUser(name string, isCN bool) {
14+
t := time.Now()
15+
16+
key := name + "_" + fmt.Sprintf("%v", isCN)
17+
18+
a.userInfoMu.Lock()
19+
a.userInfo[key] = t
20+
a.userInfoMu.Unlock()
21+
}
22+
23+
func (a *APP) runInterHTTP() error {
24+
var (
25+
port = a.config.DebugAddress
26+
)
27+
28+
r := mux.NewRouter()
29+
r.HandleFunc("/user_info", a.ShowUserInfo)
30+
31+
return a.runhttp(fmt.Sprintf("127.0.0.1:%d", port), r)
32+
}
33+
34+
type MetricUserInfo struct {
35+
Name string `json:"name"`
36+
// IsCN string `json:"is_cn"`
37+
Time string `json:"last_time"`
38+
}
39+
40+
func (a *APP) ShowUserInfo(w http.ResponseWriter, r *http.Request) {
41+
42+
a.userInfoMu.Lock()
43+
list := make([]MetricUserInfo, 0, len(a.userInfo))
44+
for k, v := range a.userInfo {
45+
list = append(list, MetricUserInfo{
46+
Name: k,
47+
Time: v.Format("2006-01-02 15:04:05"),
48+
})
49+
}
50+
a.userInfoMu.Unlock()
51+
52+
sort.Slice(list, func(i, j int) bool {
53+
return list[i].Name < list[j].Name
54+
})
55+
56+
body, _ := json.Marshal(list)
57+
w.Write(body)
58+
}

app/router.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ func (a *APP) HandlerFunc(badgeType BadgeType, isCN bool) http.Handler {
119119
w.WriteHeader(http.StatusNotFound)
120120
return
121121
}
122+
a.addUser(name, isCN)
122123
f(badgeType, name, isCN, w, r)
123124
})
124125
}

cmd/run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func NewRunCommand() *cobra.Command {
3232
flag := cmd.Flags()
3333

3434
flag.StringVarP(&opt.Address, "address", "", ":8080", "http listen address")
35+
flag.IntVarP(&opt.DebugAddress, "debug-address", "", 18080, "http debug address, only localhost")
3536
flag.StringVarP(&opt.SqlitePath, "sqlite-path", "", "./lc.db", "sqlite3 file path")
3637
flag.BoolVarP(&opt.EnableCron, "enable-cron", "", false, "if enable cron")
3738

0 commit comments

Comments
 (0)