-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathshow.go
88 lines (80 loc) · 2.09 KB
/
show.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
package main
import (
"encoding/json"
"fmt"
"os"
"regexp"
"sort"
"strings"
"github.com/aclements/go-moremath/stats"
"github.com/olekukonko/tablewriter"
)
func show(id int, format string, ssort string, reverse bool, tag string) {
f, err := os.Open(fmt.Sprintf("papercall.%d.json", id))
check(err)
var subs []*Submission
dec := json.NewDecoder(f)
err = dec.Decode(&subs)
check(err)
rev := func(a, b int) (int, int) {
if reverse {
a, b = b, a
}
return a, b
}
switch strings.ToLower(ssort) {
case "updated":
sort.Slice(subs, func(i, j int) bool { i, j = rev(i, j); return subs[i].Updated.After(subs[j].Updated) })
case "trust":
sort.Slice(subs, func(i, j int) bool { i, j = rev(i, j); return subs[i].Trust > subs[j].Trust })
case "stddev":
sort.Slice(subs, func(i, j int) bool {
i, j = rev(i, j)
var s1, s2 stats.Sample
for _, r := range subs[i].Ratings {
s1.Xs = append(s1.Xs, float64(r.Value))
}
for _, r := range subs[j].Ratings {
s2.Xs = append(s2.Xs, float64(r.Value))
}
return s1.StdDev() < s2.StdDev()
})
case "rating":
fallthrough
default:
sort.Slice(subs, func(i, j int) bool { i, j = rev(i, j); return subs[i].Rating > subs[j].Rating })
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"title", "format", "tags", "rating", "trust", "url"})
format = strings.ToUpper(format)
var rows int
for _, s := range subs {
f := strings.SplitN(strings.ToUpper(s.Format), " ", -1)[0]
match, err := regexp.MatchString(format, f)
check(err)
if !match {
continue
}
tags := strings.Join(s.Tags, " ")
match, err = regexp.MatchString(tag, tags)
check(err)
if !match {
continue
}
rows++
var samp stats.Sample
for _, r := range s.Ratings {
samp.Xs = append(samp.Xs, float64(r.Value))
}
table.Append([]string{
s.Title,
f,
tags,
fmt.Sprintf("%0.2f (%0.2f)", samp.Mean(), samp.StdDev()),
fmt.Sprintf("%0.2f", s.Trust),
fmt.Sprintf("https://papercall.io/cfps/%d/submissions/%d", id, s.Id),
})
}
table.SetFooter([]string{"Count", fmt.Sprintf("%d", rows), "", "", "", ""})
table.Render()
}