From 33e6d54fd26f33d223d471ce7607735031655070 Mon Sep 17 00:00:00 2001 From: Jason Fischer Date: Sat, 17 Sep 2022 09:17:03 -0700 Subject: [PATCH] Fix issues with incorrect values in widgets associated: #180 associated: #194 --- src/pages/api/services/proxy.js | 36 +++++++++++++++++---------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/pages/api/services/proxy.js b/src/pages/api/services/proxy.js index bc9c9c8ec..889b3849d 100644 --- a/src/pages/api/services/proxy.js +++ b/src/pages/api/services/proxy.js @@ -6,22 +6,24 @@ import npmProxyHandler from "utils/proxies/npm"; import transmissionProxyHandler from "utils/proxies/transmission"; import qbittorrentProxyHandler from "utils/proxies/qbittorrent"; -function jsonArrayMapper(data, map) { +function asJson(data) { if (data?.length > 0) { const json = JSON.parse(data.toString()); - if (json instanceof Array) { - return json.map(map); - } + return json; } return data; } -function asJson(data) { - if (data?.length > 0) { - const json = JSON.parse(data.toString()); - return json; +function jsonArrayTransform(data, transform) { + const json = asJson(data); + if (json instanceof Array) { + return transform(json); } - return data; + return json; +} + +function jsonArrayFilter(data, filter) { + return jsonArrayTransform(data, items => items.filter(filter)); } const serviceProxyHandlers = { @@ -33,16 +35,16 @@ const serviceProxyHandlers = { proxy: genericProxyHandler, maps: { movie: (data) => ({ - wanted: jsonArrayMapper(data, (item) => item.isAvailable === false).length, - have: jsonArrayMapper(data, (item) => item.isAvailable === true).length, - }), + wanted: jsonArrayFilter(data, (item) => item.isAvailable === false).length, + have: jsonArrayFilter(data, (item) => item.isAvailable === true).length + }) }, }, sonarr: { proxy: genericProxyHandler, maps: { series: (data) => ({ - total: asJson(data.toString()).length, + total: asJson(data).length, }), }, }, @@ -50,7 +52,7 @@ const serviceProxyHandlers = { proxy: genericProxyHandler, maps: { album: (data) => ({ - have: jsonArrayMapper(data, (item) => item.statistics.percentOfTracks === 100).length, + have: jsonArrayFilter(data, (item) => item.statistics.percentOfTracks === 100).length, }), }, }, @@ -58,7 +60,7 @@ const serviceProxyHandlers = { proxy: genericProxyHandler, maps: { book: (data) => ({ - have: jsonArrayMapper(data, (item) => item.statistics.bookFileCount > 0).length, + have: jsonArrayFilter(data, (item) => item.statistics.bookFileCount > 0).length, }), }, }, @@ -66,10 +68,10 @@ const serviceProxyHandlers = { proxy: genericProxyHandler, maps: { movies: (data) => ({ - total: asJson(data.toString()).total, + total: asJson(data).total, }), episodes: (data) => ({ - total: asJson(data.toString()).total, + total: asJson(data).total, }), }, },