-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuser.go
237 lines (208 loc) · 4.46 KB
/
user.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"github.com/hoisie/mustache"
"strings"
"net/http"
"log"
"io"
"fmt"
"net/url"
"path/filepath"
"time"
)
type userHistoryS struct {
watch,watchPath string
dur float32
time time.Time
}
type userHistory struct {
m []*userHistoryS
dur float32
}
type user struct {
name string
cpuinfo,sysinfo,meminfo string
app,appver string
watch,watchPath string
history userHistory
}
type usermap struct {
m map[string]*user
}
func loadUsermap(test bool) (m usermap) {
m.m = map[string]*user{}
if test {
m.m["xieran"] = &user{
name: "xieran",
history: userHistory{
m: []*userHistoryS{
&userHistoryS{watch:"xx", watchPath:"m3u8/menu/youku/0", dur:33.0, time:time.Now()},
},
dur:33.0,
},
}
return
}
return
}
func (m usermap) shotone(name string) (u *user) {
var ok bool
u, ok = m.m[name]
if !ok {
return nil
}
return
}
func (u *user) addHistory(watch,watchPath string, dur float32) {
var h *userHistoryS
if len(u.history.m) > 0 && u.history.m[len(u.history.m)-1].watch == watch {
h = u.history.m[len(u.history.m)-1]
} else {
h = &userHistoryS{watch:watch, watchPath:watchPath, dur:dur, time:time.Now()}
u.history.m = append(u.history.m, h)
}
h.dur += dur
u.history.dur += dur
}
func (h *userHistoryS) TimeStr() string {
return h.time.Format("15:04:05")
}
func (h *userHistoryS) DescHtml() string {
return getWatchHtml(h.watch, h.watchPath)
}
func (h *userHistoryS) DurStr() string {
return durstr(h.dur)
}
func getWatchPath(_url string) string {
u, _ := url.Parse(_url)
path := filepath.Dir(u.Path)
path = strings.Trim(path, "/")
return path
}
func (m usermap) interim(r *http.Request) {
var name string
name = r.FormValue("name")
if name == "" {
return
}
log.Printf("user %s: interim", name)
var u *user
var ok bool
u, ok = m.m[name]
if !ok {
u = &user{}
u.name = name
m.m[name] = u
}
if r.FormValue("cpuinfo") != "" {
u.cpuinfo = r.FormValue("cpuinfo")
}
if r.FormValue("meminfo") != "" {
u.meminfo = r.FormValue("meminfo")
}
if r.FormValue("sysinfo") != "" {
u.sysinfo = r.FormValue("sysinfo")
}
watch := r.FormValue("watch")
watchPath := ""
if watch != "" {
watchPath = getWatchPath(watch)
u.watch = watch
u.watchPath = watchPath
log.Printf("user %s: watch %s", name, u.watchPath)
}
if r.FormValue("app") != "" {
u.app = r.FormValue("app")
}
if r.FormValue("appver") != "" {
u.appver = r.FormValue("appver")
}
if r.FormValue("interval") != "" {
var i float32
fmt.Sscanf(r.FormValue("interval"), "%f", &i)
if i > 0 && watch != "" {
u.addHistory(watch,watchPath, i)
}
}
}
func (m usermap) shotall() (ret usermap) {
return m
}
func userPage(w io.Writer, path string) {
u := global.user.shotone(path)
if u == nil {
return
}
renderIndex(w, "user",
mustache.RenderFile("tpl/userPage.html", map[string]interface{} {
"name": u.name,
"watch": fmt.Sprintf(`<a target=_blank href="%s">%s</a>`, u.watch, u.watch),
"app": u.app,
"appver": u.appver,
"cpuinfo": u.cpuinfo,
"meminfo": u.meminfo,
"sysinfo": u.sysinfo,
"hisDur": durstr(u.history.dur),
"history": u.history.m,
}))
}
type usersS struct {
NameHref, Name string
WatchHtml, Watch string
Time string
}
func userlistPage(users []usersS) string {
return mustache.RenderFile("tpl/usersPage.html", map[string]interface{} {
"livenr":len(users),
"users":users,
})
}
func getWatchHtml(watch,watchPath string) (html string) {
html = ""
if strings.HasPrefix(watchPath, "m3u8/menu") {
menupath := pathsplit(watchPath, 2)
menu := global.menu.get(menupath, nil)
log.Printf("pages: %s", menupath)
if menu != nil {
html = fmt.Sprintf(`<a target=_blank href="/menu/%s">%s</a>`, menu.path, menu.Desc)
}
}
if html == "" {
html = fmt.Sprintf(`<a href="%s">%s</a>`, watch, watch)
}
return
}
func (u user) getPageS() (pu usersS) {
pu = usersS{
Name: u.name,
NameHref: "/user/"+u.name,
Watch: u.watch,
WatchHtml: getWatchHtml(u.watch, u.watchPath),
}
return
}
func usersPage(w io.Writer, path string) {
m := global.user.shotall()
users := []usersS{}
for _, u := range m.m {
users = append(users, u.getPageS())
}
renderIndex(w, "user", userlistPage(users))
}
func (m usermap) listPlayers(path string) (users []usersS) {
for _, u := range m.m {
if u.watchPath == path {
users = append(users, u.getPageS())
}
}
return
}
func (m usermap) countPlayers(path string) (n int) {
log.Printf("count %s", path)
for _, u := range m.m {
if u.watchPath == path {
n++
}
}
return
}