Skip to content

Instantly share code, notes, and snippets.

@WoolDoughnut310
Created October 28, 2022 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WoolDoughnut310/877772f71ecb2fde36574d6f53f61d9c to your computer and use it in GitHub Desktop.
Save WoolDoughnut310/877772f71ecb2fde36574d6f53f61d9c to your computer and use it in GitHub Desktop.
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