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

Skip to content

Commit 0349ccf

Browse files
committed
根据用户访问网站的情况动态更新活跃用户数据
1 parent 1138430 commit 0349ccf

File tree

7 files changed

+142
-9
lines changed

7 files changed

+142
-9
lines changed
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"domain": "dev.studygolang.com",
3-
"host": "127.0.0.1:8080",
4-
"wshost": "127.0.0.1:6090",
5-
"drive_name": "mysql",
6-
"dsn": "root:@tcp(localhost:3306)/studygolang?charset=utf8",
7-
"cookie_secret": "2fwefefwf",
8-
"smtp_username": "example_username",
2+
"domain": "dev.studygolang.com",
3+
"host": "127.0.0.1:8080",
4+
"wshost": "127.0.0.1:6090",
5+
"drive_name": "mysql",
6+
"dsn": "root:@tcp(localhost:3306)/studygolang?charset=utf8",
7+
"cookie_secret": "2fwefefwf",
8+
"smtp_username": "example_username",
99
"smtp_password": "example_password",
1010
"smtp_host": "smtp.example.com",
1111
"smtp_addr": "smtp.example.com:25",
1212
"from_email": "[email protected]",
13-
"data": "data/max_online_num",
14-
"pid": "pid/studygolang.pid"
13+
"data": "data/max_online_num",
14+
"pid": "pid/studygolang.pid"
1515
}

websites/code/studygolang/src/model/dao.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ func (this *Dao) FindAll(selectCol ...string) (*sql.Rows, error) {
192192
this.selectCols = "`" + strings.Join(selectCol, "`,`") + "`"
193193
strSql := util.SelectSql(this)
194194
logger.Debugln("FindAll sql:", strSql)
195+
logger.Debugln("FindAll bind params:", this.whereVal)
195196
err := this.Open()
196197
if err != nil {
197198
return nil, err
@@ -219,6 +220,16 @@ func (this *Dao) Scan(rows *sql.Rows, colNum int, colFieldMap map[string]interfa
219220
return rows.Scan(scanInterface...)
220221
}
221222

223+
// 执行 insert、update、delete 等操作
224+
func (this *Dao) Exec(strSql string, args ...interface{}) (sql.Result, error) {
225+
err := this.Open()
226+
if err != nil {
227+
return nil, err
228+
}
229+
defer this.Close()
230+
return this.DB.Exec(strSql, args...)
231+
}
232+
222233
/*
223234
// FindAllEx 查找多条数据
224235
func (this *Dao) FindAllEx(selectCol ...string) (reflect.Value, error) {

websites/code/studygolang/src/model/user.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ func (this *UserLogin) Find(selectCol ...string) error {
4545
return this.Dao.Find(this.colFieldMap(), selectCol...)
4646
}
4747

48+
func (this *UserLogin) FindAll(selectCol ...string) ([]*UserLogin, error) {
49+
if len(selectCol) == 0 {
50+
selectCol = util.MapKeys(this.colFieldMap())
51+
}
52+
rows, err := this.Dao.FindAll(selectCol...)
53+
if err != nil {
54+
return nil, err
55+
}
56+
// TODO:
57+
userList := make([]*UserLogin, 0, 10)
58+
logger.Debugln("selectCol", selectCol)
59+
colNum := len(selectCol)
60+
for rows.Next() {
61+
user := NewUserLogin()
62+
err = this.Scan(rows, colNum, user.colFieldMap(), selectCol...)
63+
if err != nil {
64+
logger.Errorln("UserLogin FindAll Scan Error:", err)
65+
continue
66+
}
67+
userList = append(userList, user)
68+
}
69+
return userList, nil
70+
}
71+
4872
// 为了支持连写
4973
func (this *UserLogin) Where(condition string) *UserLogin {
5074
this.Dao.Where(condition)

websites/code/studygolang/src/service/user.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,24 @@ func IncUserWeight(where string, weight int) {
351351
logger.Errorln("UserActive update Error:", err)
352352
}
353353
}
354+
355+
func DecrUserWeight(where string, divide int) {
356+
if divide <= 0 {
357+
return
358+
}
359+
360+
strSql := "UPDATE user_active SET weight=weight/" + strconv.Itoa(divide) + " WHERE " + where
361+
if result, err := model.NewUserActive().Exec(strSql); err != nil {
362+
logger.Errorln("UserActive update Error:", err)
363+
} else {
364+
n, _ := result.RowsAffected()
365+
logger.Debugln(strSql, "affected num:", n)
366+
}
367+
}
368+
369+
// 获取 loginTime 之前没有登录的用户
370+
func FindNotLoginUsers(loginTime string) (userList []*model.UserLogin, err error) {
371+
userLogin := model.NewUserLogin()
372+
userList, err = userLogin.Where("login_time<" + loginTime).FindAll()
373+
return
374+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2013 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 main
8+
9+
import (
10+
"logger"
11+
"service"
12+
"time"
13+
"util"
14+
)
15+
16+
// 后台运行的任务
17+
func ServeBackGround() {
18+
for {
19+
go decrUserActiveWeight()
20+
time.Sleep(24 * time.Hour)
21+
}
22+
}
23+
24+
func decrUserActiveWeight() {
25+
logger.Debugln("start decr user active weight...")
26+
27+
loginTime := time.Now().Add(-72 * time.Hour)
28+
userList, err := service.FindNotLoginUsers(loginTime.Format(util.TIME_LAYOUT_OFTEN))
29+
if err != nil {
30+
logger.Errorln("获取最近未登录用户失败:", err)
31+
return
32+
}
33+
34+
logger.Debugln("need dealing users:", len(userList))
35+
36+
for _, user := range userList {
37+
divide := 5
38+
39+
lastLoginTime, err := util.TimeParseOften(user.LoginTime)
40+
if err == nil {
41+
hours := (loginTime.Sub(lastLoginTime) / 24).Hours()
42+
if hours < 24 {
43+
divide = 2
44+
} else if hours < 48 {
45+
divide = 3
46+
} else if hours < 72 {
47+
divide = 4
48+
}
49+
}
50+
51+
logger.Debugln("decr user weight, username:", user.Username, "divide:", divide)
52+
service.DecrUserWeight("username='"+user.Username+"'", divide)
53+
}
54+
55+
logger.Debugln("end decr user active weight...")
56+
}

websites/code/studygolang/src/studygolang/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ func main() {
3232

3333
go ServeWebSocket()
3434

35+
go ServeBackGround()
36+
3537
router := initRouter()
3638
http.Handle("/", router)
3739
log.Fatal(http.ListenAndServe(Config["host"], nil))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2013 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 util
8+
9+
import (
10+
"time"
11+
)
12+
13+
const TIME_LAYOUT_OFTEN = "2006-01-02 15:04:05"
14+
15+
// 解析常用的日期时间格式:2014-01-11 16:18:00,东八区
16+
func TimeParseOften(value string) (time.Time, error) {
17+
local, _ := time.LoadLocation("Local")
18+
return time.ParseInLocation(TIME_LAYOUT_OFTEN, value, local)
19+
}

0 commit comments

Comments
 (0)