-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_config.go
186 lines (161 loc) · 3.71 KB
/
server_config.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
package main
import (
"database/sql"
"time"
)
type sqliteBackend struct {
db *sql.DB
}
var opToColumn = map[string]string{
"++": "plusplus",
"--": "minusminus",
"+-": "plusminus",
}
func (s sqliteBackend) incr(key string) (err error) {
identifier, operation := key[:len(key)-2], key[len(key)-2:]
txn, err := s.db.Begin()
if err != nil {
return err
}
defer func(txn *sql.Tx) {
if err != nil {
txn.Rollback()
}
}(txn)
statement := "UPDATE karma SET " + opToColumn[operation] + "=" + opToColumn[operation] + " + 1 WHERE identifier = ?"
stmt, err := txn.Prepare(statement)
if err != nil {
return err
}
result, err := stmt.Exec(identifier)
if err != nil {
return err
}
rows, err := result.RowsAffected()
if err != nil {
return err
}
// successful update
if rows == 1 {
txn.Commit()
return nil
}
plusplus := 0
minusminus := 0
plusminus := 0
switch operation {
case "++":
plusplus += 1
case "--":
minusminus += 1
case "+-":
plusminus += 1
}
stmt, err = txn.Prepare("INSERT INTO karma (identifier, plusplus, minusminus, plusminus) VALUES (?, ?, ?, ?)")
if err != nil {
return err
}
result, err = stmt.Exec(identifier, plusplus, minusminus, plusminus)
if err != nil {
return err
}
txn.Commit()
return nil
}
func (s sqliteBackend) addChannelKarma(mutation karmaMutation, channel string) (err error) {
txn, err := s.db.Begin()
if err != nil {
return err
}
defer func(txn *sql.Tx) {
if err != nil {
txn.Rollback()
}
}(txn)
dateBucket := time.Now().Format("2006-01-02")
statement := "UPDATE channel_karma SET " + opToColumn[mutation.op] + "=" + opToColumn[mutation.op] + " + 1 WHERE identifier = ? AND channel = ? AND date_bucket = ?"
stmt, err := txn.Prepare(statement)
if err != nil {
return err
}
result, err := stmt.Exec(mutation.identifier, channel, dateBucket)
if err != nil {
return err
}
rows, err := result.RowsAffected()
if err != nil {
return err
}
// successful update
if rows == 1 {
txn.Commit()
return nil
}
plusplus := 0
minusminus := 0
plusminus := 0
switch mutation.op {
case "++":
plusplus += 1
case "--":
minusminus += 1
case "+-":
plusminus += 1
}
stmt, err = txn.Prepare("INSERT INTO channel_karma (identifier, channel, date_bucket, plusplus, minusminus, plusminus) VALUES (?, ?, ?, ?, ?, ?)")
if err != nil {
return err
}
result, err = stmt.Exec(
mutation.identifier,
channel,
dateBucket,
plusplus,
minusminus,
plusminus,
)
if err != nil {
return err
}
txn.Commit()
return nil
}
// TODO use channel_karma for this
func (s sqliteBackend) getInt(key string) (value int) {
identifier, operation := key[:len(key)-2], key[len(key)-2:]
stmt, err := s.db.Prepare("SELECT " + opToColumn[operation] + " FROM karma WHERE identifier = ?")
if err != nil {
panic(err)
}
err = stmt.QueryRow(identifier).Scan(&value)
if err != nil {
return 0
}
return value
}
type karmaWithId struct {
identifier string
karma karmaSet
}
func (s sqliteBackend) getChannelTop3(channel string, minimumDate string) (karmaList []karmaWithId) {
stmt, err := s.db.Prepare("SELECT identifier, SUM(plusplus) as plusplus_sum, SUM(minusminus) as minusminus_sum, SUM(plusminus) FROM channel_karma WHERE channel = ? AND date_bucket > ? GROUP BY identifier ORDER BY (plusplus_sum - minusminus_sum) DESC LIMIT 3")
if err != nil {
panic(err)
}
rows, err := stmt.Query(channel, minimumDate)
if err != nil {
panic(err)
}
defer rows.Close()
for rows.Next() {
var karma karmaWithId
if err := rows.Scan(&karma.identifier, &karma.karma.plusplus, &karma.karma.minusminus, &karma.karma.plusminus); err != nil {
panic(err)
}
karmaList = append(karmaList, karma)
}
if err := rows.Err(); err != nil {
panic(err)
}
return karmaList
}