0.1.3 Add date ranges

This commit is contained in:
Daniel Mason 2022-01-06 11:30:15 +13:00
parent 1298bf9a7a
commit b87ad426d6
6 changed files with 41 additions and 38 deletions

View File

@ -2,7 +2,7 @@ stages:
- build
variables:
VERSION: 0.1.2
VERSION: 0.1.3
build-go:
image: golang:1.17

View File

@ -1,3 +1,6 @@
# 0.1.3
-
# 0.1.2
- Add docker-compose file for local dev
- Implemented top listeners for artist/album endpoints to match track

View File

@ -6,9 +6,13 @@ cd /var/www/goscrobble-api
git pull
echo 'Building backend'
go build -o goscrobble cmd/go-scrobble/*.go
go build -o goscrobble cmd/goscrobble/*.go
echo 'Fetching lastest frontend commit'
cd /var/www/goscrobble-web
git pull
echo 'Installing frontend packages'
npm install --production

View File

@ -126,15 +126,21 @@ func getArtistByUUID(uuid string) (Artist, error) {
return artist, nil
}
func getTopArtists(userUuid string) (TopArtists, error) {
func getTopArtists(userUuid string, dayRange string) (TopArtists, error) {
var topArtist TopArtists
dateClause := ""
if dayRange != "" {
dateClause = " AND DATE(created_at) > SUBDATE(CURRENT_DATE, " + dayRange + ") "
}
rows, err := db.Query("SELECT BIN_TO_UUID(`artists`.`uuid`, true), `artists`.`name`, IFNULL(BIN_TO_UUID(`artists`.`uuid`, true),''), count(*) "+
"FROM `scrobbles` "+
"JOIN `tracks` ON `tracks`.`uuid` = `scrobbles`.`track` "+
"JOIN track_artist ON track_artist.track = tracks.uuid "+
"JOIN artists ON track_artist.artist = artists.uuid "+
"WHERE `scrobbles`.`user` = UUID_TO_BIN(?, true) "+
dateClause+
"GROUP BY `artists`.`uuid` "+
"ORDER BY count(*) DESC "+
"LIMIT 14;",

View File

@ -77,17 +77,18 @@ func HandleRequests(port string) {
v1.HandleFunc("/profile/{username}", limitMiddleware(getProfile, lightLimiter)).Methods("GET")
v1.HandleFunc("/artists/top/{uuid}", limitMiddleware(getArtists, lightLimiter)).Methods("GET")
v1.HandleFunc("/artists/top/{uuid}/{days:[0-9]+}", limitMiddleware(getArtists, lightLimiter)).Methods("GET")
v1.HandleFunc("/artists/{uuid}", limitMiddleware(getArtist, lightLimiter)).Methods("GET")
v1.HandleFunc("/artists/{uuid}/top", limitMiddleware(getTopUsersForArtist, lightLimiter)).Methods("GET")
v1.HandleFunc("/albums/top/{uuid}", limitMiddleware(getArtists, lightLimiter)).Methods("GET")
v1.HandleFunc("/albums/{uuid}", limitMiddleware(getAlbum, lightLimiter)).Methods("GET")
v1.HandleFunc("/albums/{uuid}/top", limitMiddleware(getTopUsersForAlbum, lightLimiter)).Methods("GET")
v1.HandleFunc("/tracks/top/{uuid}", limitMiddleware(getTracks, lightLimiter)).Methods("GET") // User UUID - Top Tracks
v1.HandleFunc("/tracks/top/{uuid}/{days:[0-9]+}", limitMiddleware(getTracks, lightLimiter)).Methods("GET") // User UUID - Top Tracks (With Date)
v1.HandleFunc("/tracks/{uuid}", limitMiddleware(getTrack, lightLimiter)).Methods("GET") // Track UUID
v1.HandleFunc("/tracks/{uuid}/top", limitMiddleware(getTopUsersForTrack, lightLimiter)).Methods("GET") // TrackUUID - Top Listeners
v1.HandleFunc("/albums/{uuid}", limitMiddleware(getAlbum, lightLimiter)).Methods("GET")
v1.HandleFunc("/albums/{uuid}/top", limitMiddleware(getTopUsersForAlbum, lightLimiter)).Methods("GET")
v1.HandleFunc("/register", limitMiddleware(handleRegister, heavyLimiter)).Methods("POST")
v1.HandleFunc("/login", limitMiddleware(handleLogin, standardLimiter)).Methods("POST")
v1.HandleFunc("/sendreset", limitMiddleware(handleSendReset, heavyLimiter)).Methods("POST")
@ -594,18 +595,21 @@ func getTrack(w http.ResponseWriter, r *http.Request) {
// getArtists - Returns artist data for a user
func getArtists(w http.ResponseWriter, r *http.Request) {
var uuid string
dayRange := ""
for k, v := range mux.Vars(r) {
if k == "uuid" {
uuid = v
} else if k == "days" {
dayRange = v
}
}
if uuid == "" {
throwOkError(w, "Invalid UUID")
return
}
track, err := getTopArtists(uuid)
track, err := getTopArtists(uuid, dayRange)
if err != nil {
throwOkError(w, err.Error())
return
@ -616,37 +620,16 @@ func getArtists(w http.ResponseWriter, r *http.Request) {
w.Write(json)
}
// getAlbums - Returns album data for a user
func getAlbums(w http.ResponseWriter, r *http.Request) {
var uuid string
for k, v := range mux.Vars(r) {
if k == "uuid" {
uuid = v
}
}
if uuid == "" {
throwOkError(w, "Invalid UUID")
return
}
album, err := getAlbumByUUID(uuid)
if err != nil {
throwOkError(w, err.Error())
return
}
json, _ := json.Marshal(&album)
w.WriteHeader(http.StatusOK)
w.Write(json)
}
// getTracks - Returns track data for a user
func getTracks(w http.ResponseWriter, r *http.Request) {
var uuid string
dayRange := ""
for k, v := range mux.Vars(r) {
if k == "uuid" {
uuid = v
} else if k == "days" {
dayRange = v
}
}
@ -655,7 +638,7 @@ func getTracks(w http.ResponseWriter, r *http.Request) {
return
}
track, err := getTopTracks(uuid)
track, err := getTopTracks(uuid, dayRange)
if err != nil {
throwOkError(w, err.Error())
return
@ -838,7 +821,7 @@ func getServerInfo(w http.ResponseWriter, r *http.Request) {
}
info := ServerInfo{
Version: "0.1.2",
Version: "0.1.3",
RegistrationEnabled: registrationEnabled,
}

View File

@ -184,19 +184,26 @@ func getTrackByUUID(uuid string) (Track, error) {
return track, nil
}
func getTopTracks(userUuid string) (TopTracks, error) {
func getTopTracks(userUuid string, dayRange string) (TopTracks, error) {
var topTracks TopTracks
dateClause := ""
if dayRange != "" {
dateClause = " AND DATE(created_at) > SUBDATE(CURRENT_DATE, " + dayRange + ") "
}
rows, err := db.Query("SELECT BIN_TO_UUID(`tracks`.`uuid`, true), `tracks`.`name`, IFNULL(BIN_TO_UUID(`albums`.`uuid`, true),''), count(*) "+
"FROM `scrobbles` "+
"JOIN `tracks` ON `tracks`.`uuid` = `scrobbles`.`track` "+
"JOIN track_album ON track_album.track = tracks.uuid "+
"JOIN albums ON track_album.album = albums.uuid "+
"WHERE `user` = UUID_TO_BIN(?, true) "+
dateClause+
"GROUP BY `scrobbles`.`track` "+
"ORDER BY count(*) DESC "+
"LIMIT 14",
userUuid)
if err != nil {
log.Printf("Failed to fetch top tracks: %+v", err)
return topTracks, errors.New("Failed to fetch top tracks")