/
song_tracker2.go
465 lines (422 loc) · 12 KB
/
song_tracker2.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
* provide aspects of functionality relating to my song_tracker project.
* I intend to move some/all of the functionality from PHP to Go.
*
* TODO:
* - all 'api' type requests should always respond with json - even errors.
*/
package main
import (
"database/sql"
"encoding/json"
"errors"
"flag"
"fmt"
"log"
"net"
"net/http"
"net/http/fcgi"
"os"
"regexp"
"strconv"
"github.com/horgh/config"
_ "github.com/lib/pq"
)
// Config holds our config keys.
type Config struct {
ListenHost string
ListenPort uint64
DbUser string
DbPass string
DbName string
DbHost string
DbPort uint64
UriPrefix string
}
// HttpHandler is an object implementing the http.Handler interface
// for serving requests.
type HttpHandler struct {
settings *Config
}
// RequestHandlerFunc is a function that services a specific request.
type RequestHandlerFunc func(http.ResponseWriter, *http.Request,
*Config)
// RequestHandler defines requests we service.
type RequestHandler struct {
Method string
// regex patter on the path to match.
PathPattern string
// handler function.
Func RequestHandlerFunc
}
// TopResult holds row data for a 'top artist' or 'top song' request.
type TopResult struct {
Count int64
Label string
}
// global db connection.
// this is so we try to share a single connection for multiple requests.
// NOTE: according to the database/sql documentation, the DB type
// is indeed safe for concurrent use by multiple goroutines.
var Db *sql.DB
// TopLimitMax defines the maximum number of 'top' results we respond to.
var TopLimitMax = 100
// connectToDb opens a new connection to the database.
func connectToDb(settings *Config) (*sql.DB, error) {
// connect to the database.
dsn := fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%d",
settings.DbUser, settings.DbPass, settings.DbName, settings.DbHost,
settings.DbPort)
db, err := sql.Open("postgres", dsn)
if err != nil {
log.Print("Failed to connect to the database: " + err.Error())
return nil, err
}
log.Print("Opened new connection to the database.")
return db, nil
}
// getDb connects us to the database if necessary, and returns an active
// database connection.
// we use the global Db variable to try to ensure we use a single connection.
func getDb(settings *Config) (*sql.DB, error) {
// if we have a db connection, ensure that it is still available
// so that we reconnect if it is not.
if Db != nil {
err := Db.Ping()
if err != nil {
log.Printf("Database ping failed: %s", err.Error())
// continue on, but set us so that we attempt to reconnect.
Db.Close()
Db = nil
}
}
// connect to the database if necessary.
if Db == nil {
db, err := connectToDb(settings)
if err != nil {
log.Printf("Failed to connect to the database: %s", err.Error())
return nil, err
}
Db = db
}
return Db, nil
}
// send500Error sends an internal server error with the given message in the
// body.
func send500Error(rw http.ResponseWriter, message string) {
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte(message))
}
// getParametersTopRequest retrieves and validates parameters to a
// top artists/songs request.
// we return: user_id, limit (limit of top count), days back to build
// the top artists count for. if days back is -1, we find the count
// for all time.
func getParametersTopRequest(request *http.Request) (int64, int64, int64, error) {
// pull the parameters out and convert and validate them.
err := request.ParseForm()
if err != nil {
return 0, 0, 0, err
}
// user_id. required.
userIdStr, exists := request.Form["user_id"]
if !exists || len(userIdStr) != 1 {
return 0, 0, 0, errors.New("No user ID given")
}
userId, err := strconv.ParseInt(userIdStr[0], 10, 64)
if err != nil {
return 0, 0, 0, err
}
if userId < 0 {
return 0, 0, 0, errors.New("Invalid user ID")
}
// limit. required.
limitStr, exists := request.Form["limit"]
if !exists || len(limitStr) != 1 {
return 0, 0, 0, errors.New("No limit given")
}
limit, err := strconv.ParseInt(limitStr[0], 10, 64)
if err != nil {
return 0, 0, 0, err
}
if limit < 1 || int(limit) > TopLimitMax {
return 0, 0, 0, errors.New("Invalid limit")
}
// days_back. optional.
daysBackStr, exists := request.Form["days_back"]
var daysBack int64 = -1
if exists && len(daysBackStr) == 1 {
daysBack, err = strconv.ParseInt(daysBackStr[0], 10, 64)
if err != nil {
return 0, 0, 0, err
}
if daysBack < 1 {
return 0, 0, 0, errors.New("Invalid days back")
}
}
log.Printf("Parameters: user_id [%d] limit [%d] days_back [%d]",
userId, limit, daysBack)
return userId, limit, daysBack, nil
}
// retrieveTopArtists retrieves the top artist counts.
// we find the top 'limit' artists for the given user.
// we do this for the specified number of days back. if the given
// days back is set as -1, we find the top artists of all time.
func retrieveTopArtists(settings *Config, userId int64, limit int64,
daysBack int64) ([]TopResult, error) {
// we need a database connection.
// TODO: we could try a cache first.
db, err := getDb(settings)
if err != nil {
return nil, err
}
query := `
SELECT
COUNT(s.id) AS count,
s.artist AS label
FROM play p
LEFT JOIN song s
ON p.song_id = s.id
WHERE
p.user_id = $1
AND s.artist != 'N/A'
AND p.create_time > current_timestamp - CAST($2 AS INTERVAL)
GROUP BY s.artist
ORDER BY count DESC
LIMIT $3
`
interval := fmt.Sprintf("%d days", daysBack)
if daysBack == -1 {
// arbitrary. another alternative is to take out the create_time
// comparison, but that means having a separate query (or messing
// around with parameters more than I want)
interval = "1000 years"
}
log.Printf("Using interval [%s]", interval)
rows, err := db.Query(query, userId, interval, limit)
if err != nil {
return nil, err
}
var results []TopResult
for rows.Next() {
var result TopResult
err := rows.Scan(&result.Count, &result.Label)
if err != nil {
return nil, err
}
results = append(results, result)
}
return results, nil
}
// retrieveTopSongs retrieves the top song counts.
// we find the top 'limit' artists for the given user.
// we do this for the specified number of days back. if the given
// days back is set as -1, we find the top songs of all time.
func retrieveTopSongs(settings *Config, userId int64, limit int64,
daysBack int64) ([]TopResult, error) {
// we need a database connection.
// TODO: we could try a cache first.
db, err := getDb(settings)
if err != nil {
return nil, err
}
query := `
SELECT
COUNT(1) AS count,
CONCAT(s.artist, ' - ', s.title) AS label
FROM play p
LEFT JOIN song s
ON p.song_id = s.id
WHERE
p.user_id = $1
AND p.create_time > current_timestamp - CAST($2 AS INTERVAL)
GROUP BY label
ORDER BY count DESC
LIMIT $3
`
interval := fmt.Sprintf("%d days", daysBack)
if daysBack == -1 {
// arbitrary. another alternative is to take out the create_time
// comparison, but that means having a separate query (or messing
// around with parameters more than I want)
interval = "1000 years"
}
log.Printf("Using interval [%s]", interval)
rows, err := db.Query(query, userId, interval, limit)
if err != nil {
return nil, err
}
var results []TopResult
for rows.Next() {
var result TopResult
err := rows.Scan(&result.Count, &result.Label)
if err != nil {
return nil, err
}
results = append(results, result)
}
return results, nil
}
// responseTopCount sends the response to a top artists or songs request.
func responseTopCount(rw http.ResponseWriter, counts []TopResult) error {
type TopResponse struct {
Counts []TopResult
}
topResponse := TopResponse{Counts: counts}
b, err := json.Marshal(topResponse)
if err != nil {
return err
}
rw.Header().Set("Content-Type", "application/json; charset=utf8")
rw.Write(b)
return nil
}
// handlerTopArtists looks up the top artists for a user.
func handlerTopArtists(rw http.ResponseWriter, request *http.Request,
settings *Config) {
// find our parameters.
userId, limit, daysBack, err := getParametersTopRequest(request)
if err != nil {
msg := fmt.Sprintf("Failed to retrieve parameters: %s", err.Error())
log.Printf(msg)
send500Error(rw, msg)
return
}
// find the counts.
counts, err := retrieveTopArtists(settings, userId, limit, daysBack)
if err != nil {
msg := fmt.Sprintf("Failed to retrieve top artists: %s", err.Error())
log.Printf(msg)
send500Error(rw, msg)
return
}
// build and send the response.
err = responseTopCount(rw, counts)
if err != nil {
msg := fmt.Sprintf("Failed to generate response: %s", err.Error())
log.Printf(msg)
send500Error(rw, msg)
return
}
}
// handlerTopSongs looks up the top songs for a user.
func handlerTopSongs(rw http.ResponseWriter, request *http.Request,
settings *Config) {
// find our parameters.
userId, limit, daysBack, err := getParametersTopRequest(request)
if err != nil {
msg := fmt.Sprintf("Failed to retrieve parameters: %s", err.Error())
log.Printf(msg)
send500Error(rw, msg)
return
}
// find the counts.
counts, err := retrieveTopSongs(settings, userId, limit, daysBack)
if err != nil {
msg := fmt.Sprintf("Failed to retrieve top artists: %s", err.Error())
log.Printf(msg)
send500Error(rw, msg)
return
}
// build and send the response.
err = responseTopCount(rw, counts)
if err != nil {
msg := fmt.Sprintf("Failed to generate response: %s", err.Error())
log.Printf(msg)
send500Error(rw, msg)
return
}
}
// ServeHTTP is a function to implement the http.Handler interface.
// we service http requests.
func (handler HttpHandler) ServeHTTP(rw http.ResponseWriter,
request *http.Request) {
log.Printf("Serving new [%s] request from [%s] to path [%s]",
request.Method, request.RemoteAddr, request.URL.Path)
// define our handlers.
var handlers = []RequestHandler{
RequestHandler{
Method: "GET",
PathPattern: "^" + handler.settings.UriPrefix + "/top/artists",
Func: handlerTopArtists,
},
RequestHandler{
Method: "GET",
PathPattern: "^" + handler.settings.UriPrefix + "/top/songs",
Func: handlerTopSongs,
},
}
// find a matching handler.
for _, actionHandler := range handlers {
if actionHandler.Method != request.Method {
continue
}
matched, err := regexp.MatchString(actionHandler.PathPattern,
request.URL.Path)
if err != nil {
log.Printf("Error matching regex: %s", err.Error())
continue
}
if matched {
actionHandler.Func(rw, request, handler.settings)
return
}
}
// there was no matching handler - send a 404.
log.Printf("No handler for this request.")
rw.WriteHeader(http.StatusNotFound)
rw.Write([]byte("404 Not Found"))
}
// main is the entry point of the program.
func main() {
log.SetFlags(log.Ldate | log.Ltime)
// command line arguments.
configPath := flag.String("config-file", "",
"Path to a configuration file.")
logPath := flag.String("log-file", "",
"Path to a log file.")
flag.Parse()
// config file is required.
if len(*configPath) == 0 {
log.Print("You must specify a configuration file.")
flag.PrintDefaults()
os.Exit(1)
}
if len(*logPath) == 0 {
log.Print("You must specify a log file.")
flag.PrintDefaults()
os.Exit(1)
}
// open log file.
// don't use os.Create() because that truncates.
logFh, err := os.OpenFile(*logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
log.Printf("Failed to open log file: %s: %s", *logPath, err.Error())
os.Exit(1)
}
log.SetOutput(logFh)
// load up our settings.
var settings Config
err = config.GetConfig(*configPath, &settings)
if err != nil {
log.Printf("Failed to retrieve config: %s", err.Error())
os.Exit(1)
}
// start listening.
var listenHostPort = fmt.Sprintf("%s:%d", settings.ListenHost,
settings.ListenPort)
listener, err := net.Listen("tcp", listenHostPort)
if err != nil {
log.Print("Failed to open port: " + err.Error())
os.Exit(1)
}
httpHandler := HttpHandler{settings: &settings}
// XXX: this will serve requests forever - should we have a signal
// or a method to cause this to gracefully stop?
log.Print("Starting to serve requests.")
err = fcgi.Serve(listener, httpHandler)
if err != nil {
log.Print("Failed to start serving HTTP: " + err.Error())
os.Exit(1)
}
}