-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats.go
373 lines (311 loc) · 8.94 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package frontend
import (
"encoding/json"
"log"
"math"
"strconv"
"strings"
"go.savla.dev/littr/pkg/models"
"github.com/maxence-charriere/go-app/v9/pkg/app"
)
type StatsPage struct {
app.Compo
}
type statsContent struct {
app.Compo
flowStats map[string]int
userStats map[string]userStat
nicknames []string
users map[string]models.User
searchString string
loaderShow bool
toastShow bool
toastText string
}
type userStat struct {
// PostCount is a number of posts of such user.
PostCount int `default:0`
// ReactionCount tells the number of interactions (stars given).
ReactionCount int `default:0`
// FlowerCount is basically a number of followers.
FlowerCount int `default:0`
// ShadeCount is basically a number of blockers.
ShadeCount int `default:0`
// Searched is a special boolean used by the search engine to mark who is to be shown in search results.
Searched bool `default:true`
}
func (p *StatsPage) OnNav(ctx app.Context) {
ctx.Page().SetTitle("stats / littr")
}
func (p *StatsPage) Render() app.UI {
return app.Div().Body(
&header{},
&footer{},
&statsContent{},
)
}
func (c *statsContent) onClickUserFlow(ctx app.Context, e app.Event) {
key := ctx.JSSrc().Get("id").String()
//c.buttonDisabled = true
ctx.Navigate("/flow/user/" + key)
}
func (c *statsContent) onSearch(ctx app.Context, e app.Event) {
val := ctx.JSSrc().Get("value").String()
//if c.searchString == "" {
//if val == "" {
// return
//}
if len(val) > 20 {
return
}
ctx.NewActionWithValue("search", val)
}
func (c *statsContent) handleSearch(ctx app.Context, a app.Action) {
matchedList := []string{}
val, ok := a.Value.(string)
if !ok {
return
}
ctx.Async(func() {
users := c.userStats
// iterate over calculated stats' "rows" and find matchings
for key, user := range users {
//user := users[key]
user.Searched = false
// use lowecase to search across UNICODE letters
lval := strings.ToLower(val)
lkey := strings.ToLower(key)
if strings.Contains(lkey, lval) {
user.Searched = true
//matchedList = append(matchedList, key)
}
users[key] = user
}
ctx.Dispatch(func(ctx app.Context) {
c.userStats = users
c.nicknames = matchedList
c.loaderShow = false
log.Println("search dispatch")
})
return
})
}
func (c *statsContent) dismissToast(ctx app.Context, e app.Event) {
c.toastShow = false
c.toastText = ""
}
func (c *statsContent) OnMount(ctx app.Context) {
c.loaderShow = true
ctx.Handle("search", c.handleSearch)
}
func (c *statsContent) OnNav(ctx app.Context) {
toastText := ""
ctx.Async(func() {
payload := struct {
FlowStats map[string]int `json:"flow_stats"`
UserStats map[string]userStat `json:"user_stats"`
Users map[string]models.User `json:"users"`
Code int `json:"code"`
}{}
// fetch the stats
if byteData, _ := litterAPI("GET", "/api/v1/stats", nil, "", 0); byteData != nil {
err := json.Unmarshal(*byteData, &payload)
if err != nil {
log.Println(err.Error())
ctx.Dispatch(func(ctx app.Context) {
c.toastText = "backend error: " + err.Error()
c.toastShow = (c.toastText != "")
})
return
}
} else {
log.Println("cannot fetch stats")
ctx.Dispatch(func(ctx app.Context) {
c.toastText = "cannot fetch stats"
c.toastShow = (c.toastText != "")
})
return
}
if payload.Code == 401 {
toastText = "please log-in again"
ctx.LocalStorage().Set("user", "")
ctx.LocalStorage().Set("authGranted", false)
ctx.Dispatch(func(ctx app.Context) {
c.toastText = toastText
c.toastShow = (toastText != "")
})
return
}
ctx.Dispatch(func(ctx app.Context) {
c.users = payload.Users
c.flowStats = payload.FlowStats
c.userStats = payload.UserStats
c.loaderShow = false
log.Println("dispatch ends")
})
return
})
}
func (c *statsContent) Render() app.UI {
users := c.userStats
flowStats := c.flowStats
return app.Main().Class("responsive").Body(
app.Div().Class("row").Body(
app.Div().Class("max padding").Body(
app.H5().Text("user stats"),
//app.P().Text("wanna know your flow stats? how many you got in the flow and vice versa? yo"),
),
),
app.Div().Class("space"),
// snackbar
app.A().OnClick(c.dismissToast).Body(
app.If(c.toastText != "",
app.Div().Class("snackbar red10 white-text top active").Body(
app.I().Text("error"),
app.Span().Text(c.toastText),
),
),
),
app.Div().Class("field prefix round fill").Style("border-radius", "8px").Body(
app.I().Class("front").Text("search"),
//app.Input().Type("search").OnChange(c.ValueTo(&c.searchString)).OnSearch(c.onSearch),
//app.Input().ID("search").Type("text").OnChange(c.ValueTo(&c.searchString)).OnSearch(c.onSearch),
app.Input().ID("search").Type("text").OnChange(c.onSearch).OnSearch(c.onSearch),
),
app.Table().Class("border left-align").ID("table-stats-flow").Body(
// table header
app.THead().Body(
app.Tr().Body(
app.Th().Class("align-left").Body(
app.Span().Style("writing-mode", "vertical-lr").Text("nickname"),
),
app.Th().Class("align-left").Body(
app.Span().Style("writing-mode", "vertical-lr").Text("posts"),
),
app.Th().Class("align-left").Body(
app.Span().Style("writing-mode", "vertical-lr").Text("stars"),
),
app.Th().Class("align-left").Body(
app.Span().Style("writing-mode", "vertical-lr").Text("flowers"),
),
app.Th().Class("align-left").Body(
app.Span().Style("writing-mode", "vertical-lr").Text("shades"),
),
app.Th().Class("align-left").Body(
app.Span().Style("writing-mode", "vertical-lr").Text("ratio"),
),
),
),
// table body
app.TBody().Body(
app.Range(users).Map(func(key string) app.UI {
// calculate the ratio
ratio := func() float64 {
if users[key].PostCount <= 0 {
return 0
}
stars := float64(users[key].ReactionCount)
posts := float64(users[key].PostCount)
shades := float64(users[key].ShadeCount)
users := float64(flowStats["users"])
baseRatio := stars / posts
shadeCoeff := 1.0
if users > 1 && shades > 1 {
shadeCoeff = 1 - math.Log(shades)/math.Log(users)
}
return baseRatio * shadeCoeff
}()
// filter out unmatched keys
//log.Printf("%s: %t\n", key, users[key].Searched)
if !users[key].Searched || c.users[key].Nickname == "system" {
return app.P().Text("")
}
return app.Tr().Body(
app.Td().Class("align-left").Body(
app.P().Body(
app.P().Body(
//app.B().Text(key).Class("deep-orange-text"),
app.A().Class("bold deep-orange-text").OnClick(c.onClickUserFlow).Text(key).ID(key),
//app.A().Class("bold deep-orange-text").OnClick(nil).Text(key).ID(key),
),
),
),
app.Td().Class("align-left").Body(
app.P().Body(
app.Text(strconv.FormatInt(int64(users[key].PostCount), 10)),
),
),
app.Td().Class("align-left").Body(
app.P().Body(
app.Text(strconv.FormatInt(int64(users[key].ReactionCount), 10)),
),
),
app.Td().Class("align-left").Body(
app.P().Body(
app.Text(strconv.FormatInt(int64(users[key].FlowerCount), 10)),
),
),
app.Td().Class("align-left").Body(
app.P().Body(
app.Text(strconv.FormatInt(int64(users[key].ShadeCount), 10)),
),
),
app.Td().Class("align-left").Body(
app.P().Body(
app.Text(strconv.FormatFloat(ratio, 'f', 2, 64)),
),
),
)
}),
),
),
app.If(c.loaderShow,
app.Div().Class("small-space"),
app.Progress().Class("circle center large deep-orange-border active"),
),
app.Div().Class("large-space"),
app.Div().Class("row").Body(
app.Div().Class("max padding").Body(
app.H5().Text("system stats"),
//app.P().Text("pop in to see how much this instance lit nocap"),
),
),
//app.P().Body(
//app.A().Class("deep-orange-text bold").Href("https://umami.gscloud.cz/share/NAA3vF0M8uBpeARj/LITTER").Text("web analytics (external link)"),
//),
app.Div().Class("space"),
app.Table().Class("border left-align").ID("table-stats-system").Body(
// table header
app.THead().Body(
app.Tr().Body(
app.Th().Class("align-left").Text("property"),
app.Th().Class("align-left").Text("value"),
),
),
// table body
app.TBody().Body(
app.Range(flowStats).Map(func(key string) app.UI {
return app.Tr().Body(
app.Td().Class("align-left").Body(
app.P().Body(
app.P().Body(
app.B().Text(key).Class("deep-orange-text"),
),
),
),
app.Td().Class("align-left").Body(
app.P().Body(
app.Text(strconv.FormatInt(int64(flowStats[key]), 10)),
),
),
)
}),
),
),
app.Div().ID("page-end-anchor"),
app.If(c.loaderShow,
app.Div().Class("small-space"),
app.Progress().Class("circle center large deep-orange-border active"),
),
)
}