Browse Source

chore: Re-added debug_station to backend

Owen Diffey 2 years ago
parent
commit
a5634b7339
2 changed files with 123 additions and 1 deletions
  1. 122 0
      backend/logic/api.js
  2. 1 1
      backend/logic/ws.js

+ 122 - 0
backend/logic/api.js

@@ -1,14 +1,17 @@
 import config from "config";
 
 import async from "async";
+import crypto from "crypto";
 
 import CoreClass from "../core";
 
 let AppModule;
+let DBModule;
 let PlaylistsModule;
 let UtilsModule;
 let PunishmentsModule;
 let CacheModule;
+let NotificationsModule;
 
 class _APIModule extends CoreClass {
 	// eslint-disable-next-line require-jsdoc
@@ -24,10 +27,12 @@ class _APIModule extends CoreClass {
 	initialize() {
 		return new Promise((resolve, reject) => {
 			AppModule = this.moduleManager.modules.app;
+			DBModule = this.moduleManager.modules.db;
 			PlaylistsModule = this.moduleManager.modules.playlists;
 			UtilsModule = this.moduleManager.modules.utils;
 			PunishmentsModule = this.moduleManager.modules.punishments;
 			CacheModule = this.moduleManager.modules.cache;
+			NotificationsModule = this.moduleManager.modules.notifications;
 
 			const SIDname = config.get("cookie.SIDname");
 
@@ -132,6 +137,123 @@ class _APIModule extends CoreClass {
 							});
 					});
 
+					if (config.get("debug.stationIssue")) {
+						response.app.get("/debug_station", async (req, res) => {
+							const responseObject = {};
+
+							const stationModel = await DBModule.runJob("GET_MODEL", {
+								modelName: "station"
+							});
+
+							async.waterfall(
+								[
+									next => {
+										stationModel.find({}, next);
+									},
+
+									(stations, next) => {
+										responseObject.mongo = {
+											stations
+										};
+										next();
+									},
+
+									next => {
+										CacheModule.runJob("HGETALL", { table: "stations" })
+											.then(stations => {
+												next(null, stations);
+											})
+											.catch(err => {
+												console.log(err);
+												next(err);
+											});
+									},
+
+									(stations, next) => {
+										responseObject.redis = {
+											stations
+										};
+										next();
+									},
+
+									next => {
+										responseObject.cryptoExamples = {};
+										responseObject.mongo.stations.forEach(station => {
+											const payloadName = `stations.nextSong?id=${station._id}`;
+											responseObject.cryptoExamples[station._id] = crypto
+												.createHash("md5")
+												.update(`_notification:${payloadName}_`)
+												.digest("hex");
+										});
+										next();
+									},
+
+									next => {
+										NotificationsModule.pub.keys("*", next);
+									},
+
+									(redisKeys, next) => {
+										responseObject.redis = {
+											...redisKeys,
+											ttl: {}
+										};
+										async.eachLimit(
+											redisKeys,
+											1,
+											(redisKey, next) => {
+												NotificationsModule.pub.ttl(redisKey, (err, ttl) => {
+													responseObject.redis.ttl[redisKey] = ttl;
+													next(err);
+												});
+											},
+											next
+										);
+									},
+
+									next => {
+										responseObject.debugLogs = this.moduleManager.debugLogs.stationIssue;
+										next();
+									},
+
+									next => {
+										responseObject.debugJobs = this.moduleManager.debugJobs;
+										next();
+									}
+								],
+								err => {
+									if (err) {
+										console.log(err);
+										return res.json({
+											error: err,
+											objectSoFar: responseObject
+										});
+									}
+
+									const responseJson = JSON.stringify(responseObject, (key, value) => {
+										if (
+											key === "module" ||
+											key === "task" ||
+											key === "onFinish" ||
+											key === "server" ||
+											key === "nsp" ||
+											key === "socket" ||
+											key === "res" ||
+											key === "client" ||
+											key === "_idleNext" ||
+											key === "_idlePrev"
+										) {
+											return undefined;
+										}
+										if (key === "parentJob" && value) return value.toString();
+										return value;
+									});
+
+									return res.end(responseJson);
+								}
+							);
+						});
+					}
+
 					resolve();
 				})
 				.catch(err => {

+ 1 - 1
backend/logic/ws.js

@@ -4,7 +4,7 @@
 
 import config from "config";
 import async from "async";
-import { WebSocket, WebSocketServer } from "ws";
+import { WebSocketServer } from "ws";
 import { EventEmitter } from "events";
 
 import CoreClass from "../core";