Created
October 28, 2022 15:53
-
-
Save WoolDoughnut310/877772f71ecb2fde36574d6f53f61d9c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const getFavouriteCluster = async (songIds: ObjectId[]) => { | |
const db = getDb(); | |
// Since there are 8 total clusters for the model | |
const clusters: number[] = new Array(8).fill(0); | |
const collection = db.collection<Song>("song"); | |
// Get the cluster from each favourite song | |
const songs = await collection | |
.find({ _id: { $in: songIds } }, { projection: { cluster: 1 } }) | |
.toArray(); | |
// Keep track of the number of songs with a certain cluster | |
for (const song of songs) { | |
clusters[song.cluster]++; | |
} | |
// Remember that each array index corresponds to the cluster number | |
const favouriteCluster = clusters.indexOf(Math.max(...clusters)); | |
return favouriteCluster; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment