Skip to content

Commit

Permalink
feat(source): Add relative time since last activity to logging
Browse files Browse the repository at this point in the history
Makes logs easier to read when looking for stale source/player
  • Loading branch information
FoxxMD committed Jan 21, 2025
1 parent 1110af8 commit 973efde
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/backend/sources/AbstractSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
sortByNewestPlayDate,
sortByOldestPlayDate,
} from "../utils.js";
import { comparePlayTemporally, temporalAccuracyIsAtLeast, todayAwareFormat } from "../utils/TimeUtils.js";
import { comparePlayTemporally, temporalAccuracyIsAtLeast, timeToHumanTimestamp, todayAwareFormat } from "../utils/TimeUtils.js";
import { getRoot } from '../ioc.js';
import { componentFileLogger } from '../common/logging.js';
import { WebhookPayload } from '../common/infrastructure/config/health/webhooks.js';
Expand Down Expand Up @@ -440,6 +440,8 @@ export default abstract class AbstractSource extends AbstractComponent implement

const activeThreshold = this.lastActivityAt.add(checkActiveFor, 's');
const inactiveFor = dayjs.duration(Math.abs(activeThreshold.diff(dayjs(), 'millisecond'))).humanize(false);
const relativeActivity = dayjs.duration(this.lastActivityAt.diff(dayjs(), 'ms'));
const humanRelativeActivity = relativeActivity.asSeconds() > -3 ? '' : ` (${timeToHumanTimestamp(relativeActivity)} ago)`;
let friendlyInterval = '';
const friendlyLastFormat = todayAwareFormat(this.lastActivityAt);
if (activeThreshold.isBefore(dayjs())) {
Expand All @@ -452,12 +454,12 @@ export default abstract class AbstractSource extends AbstractComponent implement
sleepTime = interval + backoff;
}
if(isDebugMode()) {
debugMsgs.push(`Last activity ${friendlyLastFormat} is ${inactiveFor} outside of polling period (last activity + ${checkActiveFor}s)`);
debugMsgs.push(`Last activity ${friendlyLastFormat}${humanRelativeActivity} is ${inactiveFor} outside of polling period (last activity + ${checkActiveFor}s)`);
} else {
debugMsgs.push(`Last activity was at ${friendlyLastFormat}`);
debugMsgs.push(`Last activity was at ${friendlyLastFormat}${humanRelativeActivity}`);
}
} else {
debugMsgs.push(`Last activity was at ${friendlyLastFormat}`);
debugMsgs.push(`Last activity was at ${friendlyLastFormat}${humanRelativeActivity}`);
friendlyInterval = `${formatNumber(sleepTime)}s`;
}
debugMsgs.push(`Next check in ${friendlyInterval}`);
Expand Down
2 changes: 1 addition & 1 deletion src/backend/utils/TimeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export const parseDurationFromTimestamp = (timestamp: any) => {
export type Milliseconds = number;

export const timeToHumanTimestamp = (val: ReturnType<typeof dayjs.duration> | Milliseconds): string => {
const ms = dayjs.isDuration(val) ? val.asMilliseconds() : val;
const ms = dayjs.isDuration(val) ? Math.abs(val.asMilliseconds()) : val;

// less than one hour
if(ms < 3600000) {
Expand Down

0 comments on commit 973efde

Please sign in to comment.