Skip to content

Commit 3673bbf

Browse files
committed
Refactor ActivityMonitor.js for improved error handling and code readability #360
1 parent 67d14d5 commit 3673bbf

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

backend/tasks/ActivityMonitor.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ const configClass = require("../classes/config");
77
const API = require("../classes/api-loader");
88
const { sendUpdate } = require("../ws");
99
const { isNumber } = require("@mui/x-data-grid/internals");
10-
const MINIMUM_SECONDS_TO_INCLUDE_PLAYBACK = process.env.MINIMUM_SECONDS_TO_INCLUDE_PLAYBACK ? Number(process.env.MINIMUM_SECONDS_TO_INCLUDE_PLAYBACK) : 1;
10+
const MINIMUM_SECONDS_TO_INCLUDE_PLAYBACK = process.env.MINIMUM_SECONDS_TO_INCLUDE_PLAYBACK
11+
? Number(process.env.MINIMUM_SECONDS_TO_INCLUDE_PLAYBACK)
12+
: 1;
1113

1214
async function getSessionsInWatchDog(SessionData, WatchdogData) {
1315
let existingData = await WatchdogData.filter((wdData) => {
@@ -170,18 +172,26 @@ async function ActivityMonitor(interval) {
170172
/////get data from jf_playback_activity within the last hour with progress of <=80% for current items in session
171173

172174
const ExistingRecords = await db
173-
.query(`SELECT * FROM jf_recent_playback_activity(1)`)
174-
.then((res) =>
175-
res.rows.filter(
176-
(row) =>
177-
playbackToInsert.some((pbi) => pbi.NowPlayingItemId === row.NowPlayingItemId && pbi.EpisodeId === row.EpisodeId) &&
178-
row.Progress <= 80.0
179-
)
180-
);
175+
.query(`SELECT * FROM jf_recent_playback_activity(1) limit 0`)
176+
.then((res) => {
177+
if (res.rows && Array.isArray(res.rows) && res.rows.length > 0) {
178+
return res.rows.filter(
179+
(row) =>
180+
playbackToInsert.some(
181+
(pbi) => pbi.NowPlayingItemId === row.NowPlayingItemId && pbi.EpisodeId === row.EpisodeId
182+
) && row.Progress <= 80.0
183+
);
184+
} else {
185+
return [];
186+
}
187+
})
188+
.catch((err) => {
189+
console.error("Error fetching existing records:", err);
190+
});
181191
let ExistingDataToUpdate = [];
182192

183193
//for each item in playbackToInsert, check if it exists in the recent playback activity and update accordingly. insert new row if updating existing exceeds the runtime
184-
if (playbackToInsert.length >0 && ExistingRecords.length >0) {
194+
if (playbackToInsert.length > 0 && ExistingRecords.length > 0) {
185195
ExistingDataToUpdate = playbackToInsert.filter((playbackData) => {
186196
const existingrow = ExistingRecords.find((existing) => {
187197
let newDurationWithingRunTime = true;

0 commit comments

Comments
 (0)