forked from lonng/nanoserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
105 lines (79 loc) · 2.1 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package web
import (
"github.com/lonng/nex"
log "github.com/sirupsen/logrus"
"time"
"github.com/yearnfar/nanoserver/db"
"github.com/yearnfar/nanoserver/protocol"
"github.com/yearnfar/nanoserver/pkg/errutil"
)
var dayInternal = 24 * 60 * 60
//注册用户数
func registerUsersHandler(query *nex.Form) (interface{}, error) {
begin := query.Int64OrDefault("from", 0)
end := query.Int64OrDefault("to", -1)
if end < 0 {
end = time.Now().Unix()
}
log.Infof("获取注册用户信息: begin=%s, end=%s", time.Unix(begin, 0).String(), time.Unix(end, 0).String())
c, err := db.QueryRegisterUsers(begin, end)
if err != nil {
return nil, err
}
return protocol.CommonResponse{
Data: c,
}, nil
}
//实时在线人数
func onlineLiteHandler() (interface{}, error) {
c, err := db.OnlineStatsLite()
if err != nil {
return nil, err
}
return protocol.CommonResponse{
Data: c,
}, nil
}
//某日的n日留存
func retentionHandler(query *nex.Form) (interface{}, error) {
from := query.IntOrDefault("from", -1)
to := query.IntOrDefault("to", -1)
if from < 0 || to < 0 || to < from {
return nil, errutil.ErrIllegalParameter
}
list := []*protocol.Retention{}
for i := from; i <= to; i += dayInternal {
ret, err := db.RetentionList(i)
if err != nil {
return nil, err
}
list = append(list, ret)
}
return &protocol.RetentionResponse{Data: list}, nil
}
//从指定日开始到当前的每日房卡消耗
func cardConsumeStatsHandler(query *nex.Form) (interface{}, error) {
from := query.Int64OrDefault("from", 0)
to := query.Int64OrDefault("to", 0)
if to == 0 {
from = time.Now().Unix()
}
ret, err := db.ConsumeStats(from, to)
if err != nil {
return nil, err
}
return &protocol.RetentionResponse{Data: ret}, nil
}
//从指定日开始到当前的每日活跃人数
func activationUsersHandler(query *nex.Form) (interface{}, error) {
from := query.Int64OrDefault("from", 0)
to := query.Int64OrDefault("to", 0)
if to == 0 {
to = time.Now().Unix()
}
ret, err := db.QueryActivationUser(from, to)
if err != nil {
return nil, err
}
return &protocol.RetentionResponse{Data: ret}, nil
}