Skip to content

Commit

Permalink
refactor: user last active time
Browse files Browse the repository at this point in the history
  • Loading branch information
lanthora committed Oct 27, 2024
1 parent 3607060 commit 2accb18
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
11 changes: 11 additions & 0 deletions candy/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sync"
"time"

mapset "github.com/deckarep/golang-set/v2"
"github.com/lanthora/cacao/logger"
"github.com/lanthora/cacao/model"
"github.com/lanthora/cacao/storage"
Expand Down Expand Up @@ -43,15 +44,25 @@ var idNetMapMutex sync.RWMutex
func flush() {
idNetMapMutex.RLock()
defer idNetMapMutex.RUnlock()

refreshedUsers := mapset.NewSet[uint]()

for _, n := range idNetMap {
n.ipWsMapMutex.RLock()
defer n.ipWsMapMutex.RUnlock()
hasDeviceOnline := false
for _, ws := range n.ipWsMap {
if ws.dev.model.Online {
hasDeviceOnline = true
ws.dev.model.Save()
}
}
if hasDeviceOnline && !refreshedUsers.ContainsOne(n.model.UserID) {
model.RefreshUserLastActiveTimeByUserID(n.model.UserID)
refreshedUsers.Add(n.model.UserID)
}
}
refreshedUsers.Clear()
}

func autoFlush() {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/deckarep/golang-set/v2 v2.6.0
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQ
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM=
github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
Expand Down
24 changes: 12 additions & 12 deletions model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,24 @@ func DeleteUserByUserID(userid uint) {
db.Delete(&User{Model: gorm.Model{ID: userid}})
}

func GetLastActiveTimeByUserID(userid uint) (activeTime time.Time) {
func GetLastActiveTimeByUserID(userid uint) time.Time {
if userid != 0 {
db := storage.Get()

u := &User{Model: gorm.Model{ID: userid}}
db.Model(u).Take(&u)

result := db.Unscoped().Model(&Device{}).Select("devices.updated_at").Joins("left join nets on devices.net_id = nets.id").Where("nets.user_id = ?", userid).Order("devices.updated_at desc").Take(&activeTime)
if result.Error != nil {
logger.Debug("get user last active time failed: %v", result.Error)
activeTime = u.UpdatedAt
return
if result := db.Model(u).Take(&u); result.Error == nil {
return u.UpdatedAt
}
}
return time.Now()
}

if u.UpdatedAt.Before(activeTime) {
u.UpdatedAt = activeTime
func RefreshUserLastActiveTimeByUserID(userid uint) {
if userid != 0 {
db := storage.Get()
u := &User{Model: gorm.Model{ID: userid}}
if result := db.Model(u).Take(&u); result.Error == nil {
u.UpdatedAt = time.Now()
u.Save()
}
}
return
}

0 comments on commit 2accb18

Please sign in to comment.