Ver Fonte

feat: added button on admin/songs to recalculate all song ratings

Kristian Vos há 3 anos atrás
pai
commit
4b43b0afc0

+ 36 - 1
backend/logic/actions/songs.js

@@ -207,7 +207,7 @@ export default {
 	 * @param {object} session - the session object automatically added by the websocket
 	 * @param cb
 	 */
-	updateAll: isAdminRequired(async function length(session, cb) {
+	updateAll: isAdminRequired(async function updateAll(session, cb) {
 		async.waterfall(
 			[
 				next => {
@@ -232,6 +232,41 @@ export default {
 		);
 	}),
 
+	/**
+	 * Recalculates all song ratings
+	 *
+	 * @param {object} session - the session object automatically added by the websocket
+	 * @param cb
+	 */
+	recalculateAllRatings: isAdminRequired(async function recalculateAllRatings(session, cb) {
+		async.waterfall(
+			[
+				next => {
+					SongsModule.runJob("RECALCULATE_ALL_SONG_RATINGS", {}, this)
+						.then(() => {
+							next();
+						})
+						.catch(err => {
+							next(err);
+						});
+				}
+			],
+			async err => {
+				if (err) {
+					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
+					this.log(
+						"ERROR",
+						"SONGS_RECALCULATE_ALL_RATINGS",
+						`Failed to recalculate all song ratings. "${err}"`
+					);
+					return cb({ status: "error", message: err });
+				}
+				this.log("SUCCESS", "SONGS_RECALCULATE_ALL_RATINGS", `Recalculated all song ratings successfully.`);
+				return cb({ status: "success", message: "Successfully recalculated all song ratings." });
+			}
+		);
+	}),
+
 	/**
 	 * Gets a song from the Musare song id
 	 *

+ 44 - 0
backend/logic/songs.js

@@ -754,6 +754,50 @@ class _SongsModule extends CoreClass {
 		});
 	}
 
+	/**
+	 * Recalculates dislikes and likes for all songs
+	 *
+	 * @returns {Promise} - returns a promise (resolve, reject)
+	 */
+	RECALCULATE_ALL_SONG_RATINGS() {
+		return new Promise((resolve, reject) => {
+			async.waterfall(
+				[
+					next => {
+						SongsModule.SongModel.find({}, { _id: true }, next);
+					},
+
+					(songs, next) => {
+						let index = 0;
+						const { length } = songs;
+						async.eachLimit(
+							songs,
+							2,
+							(song, next) => {
+								index += 1;
+								console.log(`Recalculating ratings for song #${index} out of ${length}: ${song._id}`);
+								SongsModule.runJob("RECALCULATE_SONG_RATINGS", { songId: song._id }, this)
+									.then(() => {
+										next();
+									})
+									.catch(err => {
+										next(err);
+									});
+							},
+							err => {
+								next(err);
+							}
+						);
+					}
+				],
+				err => {
+					if (err) return reject(new Error(err));
+					return resolve();
+				}
+			);
+		});
+	}
+
 	/**
 	 * Gets an array of all genres
 	 *

+ 17 - 0
frontend/src/pages/Admin/tabs/Songs.vue

@@ -32,6 +32,14 @@
 				<confirm placement="bottom" @confirm="updateAllSongs()">
 					<button class="button is-danger">Update all songs</button>
 				</confirm>
+				<confirm
+					placement="bottom"
+					@confirm="recalculateAllSongRatings()"
+				>
+					<button class="button is-danger">
+						Recalculate all song ratings
+					</button>
+				</confirm>
 			</div>
 			<br />
 			<div class="box">
@@ -546,6 +554,15 @@ export default {
 				else new Toast(res.message);
 			});
 		},
+		recalculateAllSongRatings() {
+			new Toast(
+				"Recalculating all song ratings, this could take a bit of time."
+			);
+			this.socket.dispatch("songs.recalculateAllRatings", res => {
+				if (res.status === "success") new Toast(res.message);
+				else new Toast(res.message);
+			});
+		},
 		getSet() {
 			if (this.isGettingSet) return;
 			if (this.position >= this.maxPosition) return;