generated from fun-stack/example
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstoryplot-data.go
127 lines (97 loc) · 3.15 KB
/
storyplot-data.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
package main
import (
"database/sql"
"github.com/pkg/errors"
)
func maxSampleTime(ndb newsDatabase, storyID int) (int, error) {
var n int
err := ndb.db.QueryRow(`
select max(sampleTime) from dataset
where id = ?
`, storyID).Scan(&n)
return n, errors.Wrap(err, "QueryRow count: select max(sampleTime)")
}
func rankDatapoints(ndb newsDatabase, storyID int) ([][]any, error) {
var n int
if err := ndb.db.QueryRow("select count(*) from dataset where id = ?", storyID).Scan(&n); err != nil {
return nil, errors.Wrap(err, "QueryRow: select count")
}
if n == 0 {
return nil, ErrStoryIDNotFound
}
var submissionTime int64
if err := ndb.db.QueryRow("select timestamp from stories where id = ?", storyID).Scan(&submissionTime); err != nil {
return nil, errors.Wrap(err, "QueryRow: select submissionTime")
}
ranks := make([][]any, n)
// rows, err := ndb.db.Query("select sampleTime, (case when qnRank > 90 then 91 else qnRank end) as qnRank, topRank, newRank, bestRank, askRank, showRank, rawRank from dataset where id = ?", storyID)
rows, err := ndb.db.Query("select sampleTime, rawRank, topRank, newRank, bestRank, askRank, showRank from dataset where id = ?", storyID)
if err != nil {
return nil, errors.Wrap(err, "Query: select ranks")
}
defer rows.Close()
// rawRank, top, new, bet, ask, show
const nRanks = 6
i := 0
for rows.Next() {
var sampleTime int64
var nullableRanks [nRanks]sql.NullInt32
err = rows.Scan(&sampleTime, &nullableRanks[0], &nullableRanks[1], &nullableRanks[2], &nullableRanks[3], &nullableRanks[4], &nullableRanks[5])
if err != nil {
return nil, errors.Wrap(err, "rows.Scan")
}
ranks[i] = make([]any, nRanks+1)
ranks[i][0] = sampleTime
for j, rank := range nullableRanks {
if rank.Valid {
ranks[i][j+1] = rank.Int32
} else {
ranks[i][j+1] = 91
}
}
i++
}
err = rows.Err()
return ranks, errors.Wrap(err, "rows.Err")
}
func upvotesDatapoints(ndb newsDatabase, storyID int, modelParams ModelParams) ([][]any, error) {
var n int
if err := ndb.db.QueryRow("select count(*) from dataset where id = ?", storyID).Scan(&n); err != nil {
return nil, errors.Wrap(err, "QueryRow: select count")
}
if n == 0 {
return nil, ErrStoryIDNotFound
}
var submissionTime int64
if err := ndb.db.QueryRow("select timestamp from stories where id = ?", storyID).Scan(&submissionTime); err != nil {
return nil, errors.Wrap(err, "QueryRow: select submissionTime")
}
upvotesData := make([][]any, n)
rows, err := ndb.db.Query(`select sampleTime, cumulativeUpvotes, cumulativeExpectedUpvotes
from dataset where id = ?`, storyID)
if err != nil {
return nil, errors.Wrap(err, "Query: select upvotes")
}
defer rows.Close()
i := 0
for rows.Next() {
var sampleTime int64
var upvotes int
var expectedUpvotes float64
// var upvoteRate float64
err = rows.Scan(&sampleTime, &upvotes, &expectedUpvotes)
if err != nil {
return nil, errors.Wrap(err, "rows.Scan")
}
upvotesData[i] = []any{
sampleTime,
int32(upvotes),
expectedUpvotes,
modelParams.upvoteRate(upvotes, expectedUpvotes),
// upvoteRate,
}
i++
}
err = rows.Err()
return upvotesData, errors.Wrap(err, "rows.Err")
}