From 24a13c12cf97ec729ddce762de40715c2668cabc Mon Sep 17 00:00:00 2001 From: Calvin Montgomery Date: Sun, 30 Sep 2018 21:02:52 -0700 Subject: [PATCH] Minor fixes, logging, metrics --- src/channel/library.js | 31 +++++++++++++++----------- src/channel/playlist.js | 48 +++++++++-------------------------------- src/io/ioserver.js | 10 ++++++++- 3 files changed, 38 insertions(+), 51 deletions(-) diff --git a/src/channel/library.js b/src/channel/library.js index c8c4d7ca..e7e8a763 100644 --- a/src/channel/library.js +++ b/src/channel/library.js @@ -3,7 +3,7 @@ var Flags = require("../flags"); var util = require("../utilities"); var InfoGetter = require("../get-info"); var db = require("../database"); -var Media = require("../media"); +import { Counter, Summary } from 'prom-client'; const LOGGER = require('@calzoneman/jsli')('channel/library'); const TYPE_UNCACHE = { @@ -45,17 +45,6 @@ LibraryModule.prototype.cacheMediaList = function (list) { } }; -LibraryModule.prototype.getItem = function (id, cb) { - db.channels.getLibraryItem(this.channel.name, id, function (err, row) { - if (err) { - cb(err, null); - } else { - var meta = JSON.parse(row.meta || "{}"); - cb(null, new Media(row.id, row.title, row.seconds, row.type, meta)); - } - }); -}; - LibraryModule.prototype.handleUncache = function (user, data) { if (!this.channel.is(Flags.C_REGISTERED)) { return; @@ -81,11 +70,24 @@ LibraryModule.prototype.handleUncache = function (user, data) { }); }; +const librarySearchQueryCount = new Counter({ + name: 'cytube_library_search_query_count', + help: 'Counter for number of channel library searches', + labelNames: ['source'] +}); +const librarySearchResultSize = new Summary({ + name: 'cytube_library_search_results_size', + help: 'Summary for number of channel library results returned', + labelNames: ['source'] +}); LibraryModule.prototype.handleSearchMedia = function (user, data) { var query = data.query.substring(0, 100); var searchYT = function () { + librarySearchQueryCount.labels('yt').inc(1, new Date()); InfoGetter.Getters.ytSearch(query, function (e, vids) { if (!e) { + librarySearchResultSize.labels('yt') + .observe(vids.length, new Date()); user.socket.emit("searchResults", { source: "yt", results: vids @@ -98,11 +100,16 @@ LibraryModule.prototype.handleSearchMedia = function (user, data) { !this.channel.modules.permissions.canSeePlaylist(user)) { searchYT(); } else { + librarySearchQueryCount.labels('library').inc(1, new Date()); + db.channels.searchLibrary(this.channel.name, query, function (err, res) { if (err) { res = []; } + librarySearchResultSize.labels('library') + .observe(res.length, new Date()); + if (res.length === 0) { return searchYT(); } diff --git a/src/channel/playlist.js b/src/channel/playlist.js index 7e30ade0..257042b2 100644 --- a/src/channel/playlist.js +++ b/src/channel/playlist.js @@ -394,10 +394,6 @@ PlaylistModule.prototype.handleQueue = function (user, data) { var id = data.id; var type = data.type; - if (type === "lib") { - LOGGER.warn("Outdated client: IP %s emitting queue with type=lib", - user.realip); - } if (data.pos !== "next" && data.pos !== "end") { return; @@ -522,42 +518,18 @@ PlaylistModule.prototype.queueStandard = function (user, data) { this.channel.refCounter.ref("PlaylistModule::queueStandard"); counters.add("playlist:queue:count", 1); this.semaphore.queue(function (lock) { - var lib = self.channel.modules.library; - if (lib && self.channel.is(Flags.C_REGISTERED) && !util.isLive(data.type)) { - // TODO: remove this check entirely once metrics are collected. - lib.getItem(data.id, function (err, item) { - if (err && err !== "Item not in library") { - LOGGER.error("Failed to query for library item: %s", String(err)); - } else if (err === "Item not in library") { - counters.add("playlist:queue:library:miss", 1); - } else { - // temp hack until all clients are updated. - // previously, library search results would queue with - // type "lib"; this has now been changed. - data.type = item.type; - counters.add("playlist:queue:library:hit", 1); - } + InfoGetter.getMedia(data.id, data.type, function (err, media) { + if (err) { + error(XSS.sanitizeText(String(err))); + self.channel.refCounter.unref("PlaylistModule::queueStandard"); + return lock.release(); + } - handleLookup(); + self._addItem(media, data, user, function () { + lock.release(); + self.channel.refCounter.unref("PlaylistModule::queueStandard"); }); - } else { - handleLookup(); - } - - function handleLookup() { - InfoGetter.getMedia(data.id, data.type, function (err, media) { - if (err) { - error(XSS.sanitizeText(String(err))); - self.channel.refCounter.unref("PlaylistModule::queueStandard"); - return lock.release(); - } - - self._addItem(media, data, user, function () { - lock.release(); - self.channel.refCounter.unref("PlaylistModule::queueStandard"); - }); - }); - } + }); }); }; diff --git a/src/io/ioserver.js b/src/io/ioserver.js index e69c14d8..5e8bcf83 100644 --- a/src/io/ioserver.js +++ b/src/io/ioserver.js @@ -229,7 +229,15 @@ class IOServer { LOGGER.info('Accepted socket from %s', socket.context.ipAddress); counters.add('socket.io:accept', 1); - socket.once('disconnect', () => counters.add('socket.io:disconnect', 1)); + socket.once('disconnect', (reason, reasonDetail) => { + LOGGER.info( + '%s disconnected (%s%s)', + socket.context.ipAddress, + reason, + reasonDetail ? ` - ${reasonDetail}` : '' + ); + counters.add('socket.io:disconnect', 1); + }); const user = new User(socket, socket.context.ipAddress, socket.context.user); if (socket.context.user) {