Skip to content

Commit 21ea545

Browse files
committed
added endpoint and first js for displaying better statistics
1 parent 0995d78 commit 21ea545

File tree

4 files changed

+70
-17
lines changed

4 files changed

+70
-17
lines changed

config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func read_config() (Config, error) {
3333
viper.SetDefault("shutdown_timeout", time.Second*10)
3434
viper.SetDefault("users", map[string]string{})
3535
viper.SetDefault("checkpoint_interval", 2*time.Hour)
36-
viper.SetDefault("checkpoint_interval", 1*time.Minute)
36+
viper.SetDefault("checkpoint_timeout", 1*time.Minute)
3737

3838
viper.SetEnvPrefix("FFS")
3939
viper.AutomaticEnv()

main.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"database/sql"
66
"errors"
77
"fmt"
8-
"html/template"
98
"io"
109
"log/slog"
1110
"net/http"
@@ -63,12 +62,6 @@ func checkpoint_ticker(ctx context.Context, db *sql.DB) {
6362
var (
6463
config Config
6564

66-
selectSongsHtml = template.Must(template.ParseFiles("select_songs.gohtml"))
67-
selectPlaylistHtml = template.Must(template.ParseFiles("select_playlist.gohtml"))
68-
winnerHtml = template.Must(template.ParseFiles("winner.gohtml"))
69-
statsHtml = template.Must(template.ParseFiles("stats.gohtml"))
70-
errorHtml = template.Must(template.ParseFiles("error.gohtml"))
71-
7265
ctx = context.Background()
7366
db_conn *sql.DB
7467
queries *db.Queries
@@ -162,6 +155,7 @@ func main() {
162155
api.POST("/select_session", selectSessionHandler)
163156
api.POST("/select_song", selectSongHandler)
164157
api.GET("/select_new_playlist", selectNewPlaylistHandler)
158+
api.GET("/playlist_statistics", playlistStatisticsHandler)
165159
}
166160
{
167161
health.GET("", healthcheckHandler)

playlist_statistics.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,12 @@ func playlistStatisticsHandler(c *gin.Context) {
1515
return
1616
}
1717

18-
playlistUrl := c.Query("playlist")
19-
if playlistUrl == "" {
18+
playlistId := c.Query("playlist")
19+
if playlistId == "" {
2020
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("no playlist given"))
2121
return
2222
}
2323

24-
playlistId, err := getPlaylistIdFromURL(playlistUrl)
25-
if err != nil {
26-
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("could not extract playlist id from url"))
27-
return
28-
}
29-
3024
result, err := queries.GetStatistics1(c, db.GetStatistics1Params{
3125
User: user.ID,
3226
Playlist: playlistId,
@@ -36,5 +30,27 @@ func playlistStatisticsHandler(c *gin.Context) {
3630
return
3731
}
3832

39-
c.JSON(http.StatusOK, result)
33+
c.JSON(http.StatusOK, Statistics1ToJson(result))
34+
}
35+
36+
type GetStatisticsJsonResult struct {
37+
ID string `json:"id"`
38+
Title string `json:"title"`
39+
Artists string `json:"artists"`
40+
Image string `json:"image"`
41+
Points int64 `json:"points"`
42+
}
43+
44+
func Statistics1ToJson(result []db.GetStatistics1Row) []GetStatisticsJsonResult {
45+
ret := make([]GetStatisticsJsonResult, len(result))
46+
for i, result := range result {
47+
ret[i] = GetStatisticsJsonResult{
48+
ID: result.ID,
49+
Title: result.Title.String,
50+
Artists: result.Artists.String,
51+
Image: result.Image.String,
52+
Points: result.Points,
53+
}
54+
}
55+
return ret
4056
}

stats.gohtml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,46 @@
33

44
<head>
55
<title>Find Favourite Song</title>
6+
<script>
7+
async function fill_in_new_stats(playlist_id) {
8+
const resp = await fetch(`/api/playlist_statistics?playlist=${playlist_id}`);
9+
if (!resp.ok) {
10+
console.error(`error fetching stats: ${resp.status}`)
11+
return
12+
}
13+
14+
const json = await resp.json();
15+
console.log(json);
16+
17+
const points_map = Map.groupBy(json, ({ points }) => points);
18+
const new_stats = document.getElementById('new_statistics');
19+
[...points_map.keys()].sort((a, b) => a-b).reverse().forEach(points => {
20+
const points_div = document.createElement('div');
21+
const points_h2 = document.createElement('h2');
22+
points_h2.innerText = `${points}`;
23+
24+
points_div.appendChild(points_h2);
25+
26+
for (const item of points_map.get(points)) {
27+
item_div = document.createElement('div');
28+
item_div.innerHTML = `
29+
<div class="wrapper">
30+
<div class="left">
31+
<img src="${item.image}" />
32+
</div>
33+
<div class="right">
34+
<h3>${item.title}</h3>
35+
<h4>${item.artists}</h4>
36+
</div>
37+
</div>
38+
`;
39+
points_div.appendChild(item_div);
40+
}
41+
42+
new_stats.appendChild(points_div);
43+
});
44+
}
45+
</script>
646
</head>
747

848
<body>
@@ -21,6 +61,9 @@
2161
</div>
2262
</div>
2363
{{ end }}
64+
<h1>New Statistics</h1>
65+
<div id="new_statistics">
66+
</div>
2467
</main>
2568
</body>
2669

0 commit comments

Comments
 (0)