/
who_first.go
85 lines (64 loc) · 2.09 KB
/
who_first.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
package analysis
import (
"github.com/go-pg/pg/v10"
"github.com/jivison/gowon-indexer/lib/customerrors"
"github.com/jivison/gowon-indexer/lib/db"
"github.com/jivison/gowon-indexer/lib/graph/model"
"github.com/jivison/gowon-indexer/lib/helpers/inputparser"
)
// WhoFirstArtist returns a list of who first listened to an artist
func (a Analysis) WhoFirstArtist(artist *db.Artist, settings *model.WhoKnowsSettings, whoLast bool, excludeIDs []int64) ([]db.Scrobble, error) {
var whoFirst []db.Scrobble
sort := "asc"
aggFunc := "min"
if whoLast {
sort = "desc"
}
if whoLast {
aggFunc = "max"
}
query := db.Db.Model(&whoFirst).
ColumnExpr(aggFunc+"(scrobbled_at) as scrobbled_at").
Column("scrobble.user_id").
Where("scrobble.artist_id = ?", artist.ID).
Where("scrobbled_at > '2002-03-19 00:00:00'::date").
Group("scrobble.user_id").OrderExpr("1 " + sort)
if len(excludeIDs) != 0 {
query = query.Where("scrobble.user_id NOT IN (?)", pg.In(excludeIDs))
}
err := inputparser.CreateParser(query).ParseWhoKnowsSettings(settings, &inputparser.InputSettings{
UserIDPath: `scrobble"."user_id`,
}).GetQuery().Select()
if err != nil {
return whoFirst, customerrors.DatabaseUnknownError()
}
whoFirst, err = a.AddUserToPlays(whoFirst)
if err != nil {
return whoFirst, nil
}
return whoFirst, nil
}
func (a Analysis) WhoHasUndatedArtist(artist *db.Artist, settings *model.WhoKnowsSettings, whoLast bool) ([]db.Scrobble, error) {
var whoFirst []db.Scrobble
sort := "asc"
if whoLast {
sort = "desc"
}
query := db.Db.Model(&whoFirst).
Relation("Track._").
Column("scrobble.user_id").
Where("scrobble.artist_id = ?", artist.ID).
Where("scrobbled_at < '2002-03-19 00:00:00'::date").
Group("scrobble.user_id").OrderExpr("1 " + sort)
err := inputparser.CreateParser(query).ParseWhoKnowsSettings(settings, &inputparser.InputSettings{
UserIDPath: `scrobble"."user_id`,
}).GetQuery().Select()
if err != nil {
return whoFirst, customerrors.DatabaseUnknownError()
}
whoFirst, err = a.AddUserToPlays(whoFirst)
if err != nil {
return whoFirst, nil
}
return whoFirst, nil
}