import (
"context"
"encoding/json"
- "fmt"
"net/http"
"github.com/redis/go-redis/v9"
)
type DBSong struct {
+ Key string
+ ArtistName string
+ TrackName string
+ TrackViewURL string
+ PreviewURL string
+ ArtworkURL60 string
+ ArtworkURL100 string
+ DisplayArtistNames string
+ DisplayTrackName string
+}
+type DBRoom struct {
+ Name string
+ AssignedSongKeys []string
}
type DBStatusMessage struct {
TotalSongCount int
+ AllSongs []DBSong
+ Rooms []DBRoom
}
type DBWrapper struct {
if err != nil {
panic(err)
}
- fmt.Println(keys)
key_count := len(keys)
+ // all zset type keys (because zsets are just keys)
+ // not the best way to scan this, and it's a weird
+ // way to get room information in the first place but it will have to do
+ allRooms := make([]DBRoom, 0)
+
+ all_keys, _, err := dbw.RedisDb.Scan(dbw.ctx, 0, "*", int64(key_count)).Result()
+ if err != nil {
+ panic(err)
+ }
+ for _, k := range all_keys {
+ t, err := dbw.RedisDb.Type(dbw.ctx, k).Result()
+ if err != nil {
+ panic(err)
+ }
+
+ if t == "zset" {
+ assignedIds, err := dbw.RedisDb.ZRange(dbw.ctx, k, 0, -1).Result()
+ if err != nil {
+ panic(err)
+ }
+
+ allRooms = append(allRooms, DBRoom{
+ Name: k,
+ AssignedSongKeys: assignedIds,
+ })
+ }
+ }
+
+
+ // honestly, the more i read the docs here maybe the guy used
+ // the hmset, hmget wrong or something, idk, i am not redoing the
+ // the database unless there ends up being significant performance implications
+ allSongs := make([]DBSong, key_count)
+ for i, k := range keys {
+ intf, err := dbw.RedisDb.HMGet(dbw.ctx, k, "artistName", "trackName", "trackViewUrl", "previewUrl", "artworkUrl60", "artworkUrl100", "displayArtistNames", "displayTrackName").Result()
+ if err != nil {
+ panic(err)
+ }
+ song := DBSong{
+ Key: k,
+ ArtistName: intf[0].(string),
+ TrackName: intf[1].(string),
+ TrackViewURL: intf[2].(string),
+ PreviewURL: intf[3].(string),
+ ArtworkURL60: intf[4].(string),
+ ArtworkURL100: intf[5].(string),
+ DisplayArtistNames: intf[6].(string),
+ DisplayTrackName: intf[7].(string),
+ }
+ allSongs[i] = song
+ }
+
m := DBStatusMessage{
TotalSongCount: key_count,
+ AllSongs: allSongs,
+ Rooms: allRooms,
}
b, err := json.Marshal(m)
if err != nil {
--- /dev/null
+:root {
+ font-family: sans-serif;
+}
<h1>Current Status of db:</h1>
<button type="button" onclick="get_updated_status()">Get Status</button>
<h2>List of songs in the database:</h2>
- <ul id="db_status_song_list">
+ <p>Filter songs here:</p>
+ <input id="allSongFilterList" type="text"></input>
+ <ul style="max-height: 200px; overflow-y: auto" id="db_status_song_list">
</ul>
<h2>Which rooms these songs are applied to</h2>
+ <ul id="list_of_rooms">
+
+ </ul>
</div>
<div>
-function get_updated_status() {
+let AllSongs = null
+let dbSongList = document.getElementById("db_status_song_list")
+
+
+let allSongFilterList = document.getElementById("allSongFilterList")
+allSongFilterList.oninput = (event) => {filterAllSongs(event)}
+
+function filterSongByAllAttrs(filter, song) {
+ return song.Key.includes(filter) || song.ArtistName.includes(filter) ||
+ song.TrackName.includes(filter)
+}
+
+function filterAllSongs(event) {
+ console.log("Firing input trigger")
+ let filterBy = allSongFilterList.value
+ console.log("Filtering by:" + filterBy)
+
+ if (AllSongs == null) {
+ console.log("all songs was null")
+ return
+ }
+
+ dbSongList.innerHTML = ""
+
+ for (let song of AllSongs) {
+ if (filterSongByAllAttrs(filterBy, song)) {
+ let li = document.createElement("li")
+ li.innerText = song.Key
+ dbSongList.appendChild(li)
+ }
+ }
+
+ console.log("function done")
+}
+
+
+async function get_updated_status() {
console.log("updating database status...")
- fetch("http://localhost:8000/api/db_status_update").then((response) => {
- console.log(response)
+ fetch("http://localhost:8000/api/db_status_update", {'Accept': 'application/json', 'Content-type': 'application/json'}).then(async (response) => {
+ const body = await response.json()
+
+ // update total song list
+ AllSongs = body.AllSongs
+ for (let song of body.AllSongs) {
+ let li = document.createElement("li")
+ li.innerText = song.Key
+ dbSongList.appendChild(li)
+ }
+
+ // update list of rooms
+ let room_list = document.getElementById("list_of_rooms")
+ for (let room of body.Rooms) {
+ let header = document.createElement("h3")
+ header.innerText = room.Name
+ let new_list = document.createElement("ul")
+ new_list.id = "room:" + room.Name
+
+ room_list.appendChild(header)
+ room_list.appendChild(new_list)
+ }
})
}