Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b9e77d2

Browse files
committed
增加 expvar,监控数据
1 parent 0064757 commit b9e77d2

File tree

9 files changed

+105
-5
lines changed

9 files changed

+105
-5
lines changed

src/http/controller/admin/metrics.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2017 The StudyGolang Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
// http://studygolang.com
5+
// Author: polaris [email protected]
6+
7+
package admin
8+
9+
import (
10+
"expvar"
11+
"global"
12+
"logic"
13+
"time"
14+
15+
"github.com/labstack/echo"
16+
17+
. "http"
18+
)
19+
20+
var (
21+
onlineStats = expvar.NewMap("online_stats")
22+
loginUserNum expvar.Int
23+
visitorUserNum expvar.Int
24+
)
25+
26+
type MetricsController struct{}
27+
28+
// 注册路由
29+
func (self MetricsController) RegisterRoute(g *echo.Group) {
30+
g.GET("/debug/vars", self.DebugExpvar)
31+
}
32+
33+
func (self MetricsController) DebugExpvar(ctx echo.Context) error {
34+
loginUserNum.Set(int64(logic.Book.LoginLen()))
35+
visitorUserNum.Set(int64(logic.Book.Len()))
36+
37+
onlineStats.Set("login_user_num", &loginUserNum)
38+
onlineStats.Set("visitor_user_num", &visitorUserNum)
39+
onlineStats.Set("uptime", expvar.Func(self.calculateUptime))
40+
onlineStats.Set("login_user_data", logic.Book.LoginUserData())
41+
42+
handler := expvar.Handler()
43+
handler.ServeHTTP(ResponseWriter(ctx), Request(ctx))
44+
return nil
45+
}
46+
47+
func (self MetricsController) calculateUptime() interface{} {
48+
return time.Since(global.App.LaunchTime).String()
49+
}

src/http/controller/admin/routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ func RegisterRoutes(g *echo.Group) {
1616
new(ReadingController).RegisterRoute(g)
1717
new(ToolController).RegisterRoute(g)
1818
new(SettingController).RegisterRoute(g)
19+
new(MetricsController).RegisterRoute(g)
1920
}

src/http/http.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@ func executeTpl(ctx echo.Context, tpl *template.Template, data map[string]interf
311311

312312
data["setting"] = logic.WebsiteSetting
313313

314+
// 记录处理时间
315+
data["resp_time"] = time.Since(ctx.Get("req_start_time").(time.Time))
316+
314317
buf := new(bytes.Buffer)
315318
err := tpl.Execute(buf, data)
316319
if err != nil {

src/http/middleware/login.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"net/url"
1515
"strconv"
1616
"strings"
17+
"time"
1718
"util"
1819

1920
. "http"
@@ -29,6 +30,8 @@ func AutoLogin() echo.MiddlewareFunc {
2930
// github.com/gorilla/sessions 要求必须 Clear
3031
defer context.Clear(Request(ctx))
3132

33+
ctx.Set("req_start_time", time.Now())
34+
3235
var getCurrentUser = func(usernameOrId interface{}) {
3336
if db.MasterDB != nil {
3437
// TODO: 考虑缓存,或延迟查询,避免每次都查询

src/logic/book.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package logic
88

99
import (
10+
"encoding/json"
1011
"io/ioutil"
1112
"os"
1213
"path/filepath"
@@ -18,6 +19,7 @@ import (
1819
"github.com/polaris1119/config"
1920
"github.com/polaris1119/goutils"
2021
"github.com/polaris1119/logger"
22+
"github.com/polaris1119/times"
2123
)
2224

2325
const (
@@ -86,6 +88,23 @@ func (this *UserData) SendMessage(message *Message) {
8688
}
8789
}
8890

91+
// 用于 expvar 统计信息
92+
type LoginUser struct {
93+
Uid int `json:"uid"`
94+
LastAccessTime string `json:"last_access_time"`
95+
OnlineDuartion string `json:"online_duration"`
96+
}
97+
type LoginUserSlice []*LoginUser
98+
99+
func (self LoginUserSlice) String() string {
100+
b, err := json.Marshal(self)
101+
if err != nil {
102+
return "[]"
103+
}
104+
105+
return string(b)
106+
}
107+
89108
var Book = &book{users: make(map[int]*UserData), uids: make(map[int]struct{})}
90109

91110
type book struct {
@@ -187,6 +206,23 @@ func (this *book) LoginLen() int {
187206
return len(this.uids)
188207
}
189208

209+
func (this *book) LoginUserData() LoginUserSlice {
210+
this.rwMutex.RLock()
211+
defer this.rwMutex.RUnlock()
212+
213+
loginUserData := LoginUserSlice(make([]*LoginUser, 0, len(this.uids)))
214+
for uid := range this.uids {
215+
user := this.users[uid]
216+
loginUserData = append(loginUserData, &LoginUser{
217+
Uid: uid,
218+
LastAccessTime: times.Format("Y-m-d H:i:s", user.lastAccessTime),
219+
OnlineDuartion: user.onlineDuartion.String(),
220+
})
221+
}
222+
223+
return loginUserData
224+
}
225+
190226
// 给某个用户发送一条消息
191227
func (this *book) PostMessage(uid int, message *Message) {
192228
this.rwMutex.RLock()

src/logic/email.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ func (self EmailLogic) EmailNotice() {
203203
continue
204204
}
205205

206+
if user.IsThird == 1 && strings.HasSuffix(user.Email, "github.com") {
207+
logger.Infoln("the email is not exists:", user)
208+
continue
209+
}
210+
206211
data["email"] = user.Email
207212
data["token"] = self.GenUnsubscribeToken(user)
208213

src/model/user.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ func (this *User) TableName() string {
9191

9292
func (this *User) String() string {
9393
buffer := goutils.NewBuffer()
94-
buffer.Append(this.Username).Append(this.Email).Append(this.Uid).Append(this.Mtime)
94+
buffer.Append(this.Username).Append(" ").
95+
Append(this.Email).Append(" ").
96+
Append(this.Uid).Append(" ").
97+
Append(this.Mtime)
9598

9699
return buffer.String()
97100
}

template/articles/detail.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ <h1>
208208
});
209209

210210
var len = '{{.article.Txt}}'.length;
211-
var readTime = Math.round(len / 1200);
211+
var readTime = Math.round(len / 900);
212212
if (readTime >= 1) {
213213
$('.read-time').text('预计阅读时间 '+readTime+' 分钟');
214214
} else {

template/common/layout.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@
142142
</strong>
143143
&nbsp;<span class="cc">最高记录 <span id="maxonline">{{.online_users.maxonline}}</span></span>
144144
<div class="sep20"></div>
145-
{{.setting.Slogan}}
145+
&copy;{{.app.Copyright}} {{.setting.Slogan}}
146146
<div class="sep5"></div>
147-
Powered by StudyGolang(Golang + MySQL) &nbsp;<span class="snow"></span>&nbsp;服务器由 <a href="http://www.ucai.cn" class="dark" target="_blank">优才学院</a> 赞助 &nbsp;<span class="snow">·</span>&nbsp;CDN 由 <a href="https://portal.qiniu.com/signup?code=3lfz4at7pxfma" title="七牛云" class="dark" target="_blank">七牛云</a> 赞助
147+
Powered by <a href="https://github.com/studygolang/studygolang">StudyGolang(Golang + MySQL)</a> &nbsp;<span class="snow"></span>&nbsp;服务器由 <a href="http://www.ucai.cn" class="dark" target="_blank">优才学院</a> 赞助 &nbsp;<span class="snow">·</span>&nbsp;CDN 由 <a href="https://portal.qiniu.com/signup?code=3lfz4at7pxfma" title="七牛云" class="dark" target="_blank">七牛云</a> 赞助
148148
<div class="sep20"></div>
149-
<span class="small cc">VERSION: {{.app.Version}} &nbsp;&nbsp; <strong>为了更好的体验,本站推荐使用 Chrome 或 Firefox 浏览器</strong></span>
149+
<span class="small cc">VERSION: {{.app.Version}}&nbsp;<span class="snow">·</span>&nbsp;{{.resp_time}}&nbsp;<span class="snow">·</span>&nbsp;<strong>为了更好的体验,本站推荐使用 Chrome 或 Firefox 浏览器</strong></span>
150150
<div class="sep20"></div>
151151
<span class="f12 c9"><a href="http://www.miibeian.gov.cn/" target="_blank" rel="nofollow">{{.setting.Beian}}</a></span>
152152
<div class="sep10"></div>

0 commit comments

Comments
 (0)