Screen all older fallback candidates
This commit is contained in:
1 parent
83b5f1f5b6
commit
5c9839926d
2 files changed
+17
-23
No files matched your search
+11
-17
@@ -19,7 +19,6 @@ interface ListingScreeningSummary {
|
|||||||
eligible: number;
|
eligible: number;
|
||||||
outsideRecentWindow: number;
|
outsideRecentWindow: number;
|
||||||
deferred: number;
|
deferred: number;
|
||||||
deferredReason: 'selected-recent-candidate' | 'older-than-fallback' | null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Coordinates permanent polling disablement across account runtimes. */
|
/** Coordinates permanent polling disablement across account runtimes. */
|
||||||
@@ -52,9 +51,10 @@ export class MonitorService {
|
|||||||
const recentCutoffSec = prioritizeRecent
|
const recentCutoffSec = prioritizeRecent
|
||||||
? Math.floor(Date.now() / 1000) - this.config.reddit.recentWindowMinutes * 60
|
? Math.floor(Date.now() / 1000) - this.config.reddit.recentWindowMinutes * 60
|
||||||
: 0;
|
: 0;
|
||||||
// Older posts found while prioritizing recent ones — used as a fallback
|
// Older posts found while prioritizing recent ones. Screen every eligible
|
||||||
// so the bot still has something to do if no recent posts exist anywhere.
|
// older candidate so seller and recipient filters are applied consistently,
|
||||||
let fallback: RedditPost | null = null;
|
// then select the newest one only if no recent post exists anywhere.
|
||||||
|
const fallbackCandidates: RedditPost[] = [];
|
||||||
|
|
||||||
for (const subreddit of subreddits) {
|
for (const subreddit of subreddits) {
|
||||||
try {
|
try {
|
||||||
@@ -70,7 +70,6 @@ export class MonitorService {
|
|||||||
eligible: 0,
|
eligible: 0,
|
||||||
outsideRecentWindow: 0,
|
outsideRecentWindow: 0,
|
||||||
deferred: 0,
|
deferred: 0,
|
||||||
deferredReason: null,
|
|
||||||
};
|
};
|
||||||
let selected: RedditPost | null = null;
|
let selected: RedditPost | null = null;
|
||||||
|
|
||||||
@@ -140,18 +139,15 @@ export class MonitorService {
|
|||||||
if (!prioritizeRecent || post.created_utc >= recentCutoffSec) {
|
if (!prioritizeRecent || post.created_utc >= recentCutoffSec) {
|
||||||
selected = post;
|
selected = post;
|
||||||
summary.deferred = newestFirst.length - index - 1;
|
summary.deferred = newestFirst.length - index - 1;
|
||||||
summary.deferredReason = 'selected-recent-candidate';
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is the newest eligible post in this subreddit. Keep it as a
|
// Keep every eligible older post. We deliberately finish screening
|
||||||
// possible fallback, then move to the next subreddit to look for a
|
// the listing rather than stopping at its newest fallback candidate:
|
||||||
// recent candidate. Older posts in this batch cannot improve it.
|
// later posts may be rejected, and this keeps the fallback decision
|
||||||
|
// based on the full set of available posts.
|
||||||
summary.outsideRecentWindow += 1;
|
summary.outsideRecentWindow += 1;
|
||||||
if (!fallback || post.created_utc > fallback.created_utc) fallback = post;
|
fallbackCandidates.push(post);
|
||||||
summary.deferred = newestFirst.length - index - 1;
|
|
||||||
summary.deferredReason = 'older-than-fallback';
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logListingSummary(subreddit.name, posts.length, summary, selected !== null);
|
this.logListingSummary(subreddit.name, posts.length, summary, selected !== null);
|
||||||
@@ -179,6 +175,7 @@ export class MonitorService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fallback = fallbackCandidates.sort((a, b) => b.created_utc - a.created_utc)[0];
|
||||||
if (fallback) {
|
if (fallback) {
|
||||||
this.logger.info(
|
this.logger.info(
|
||||||
`No posts within last ${this.config.reddit.recentWindowMinutes}m — falling back to newest available (r/${fallback.subreddit})`,
|
`No posts within last ${this.config.reddit.recentWindowMinutes}m — falling back to newest available (r/${fallback.subreddit})`,
|
||||||
@@ -259,10 +256,7 @@ export class MonitorService {
|
|||||||
`${summary.outsideRecentWindow} outside recent window`,
|
`${summary.outsideRecentWindow} outside recent window`,
|
||||||
];
|
];
|
||||||
if (summary.deferred > 0) {
|
if (summary.deferred > 0) {
|
||||||
const reason = summary.deferredReason === 'older-than-fallback'
|
counts.push(`${summary.deferred} not evaluated after selected recent candidate`);
|
||||||
? 'older than eligible fallback'
|
|
||||||
: 'not evaluated after selected recent candidate';
|
|
||||||
counts.push(`${summary.deferred} ${reason}`);
|
|
||||||
}
|
}
|
||||||
this.logger.info(`r/${subreddit} listing: ${counts.join('; ')}`);
|
this.logger.info(`r/${subreddit} listing: ${counts.join('; ')}`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -345,7 +345,7 @@ describe('MonitorService', () => {
|
|||||||
expect(result[0].id).toBe('t3_old');
|
expect(result[0].id).toBe('t3_old');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('labels deferred listing posts as older than the eligible fallback', async () => {
|
it('screens every older fallback candidate before selecting the newest one', async () => {
|
||||||
const now = Math.floor(Date.now() / 1000);
|
const now = Math.floor(Date.now() / 1000);
|
||||||
const fallback: RedditPost = {
|
const fallback: RedditPost = {
|
||||||
id: 't3_fallback', subreddit: 'sub1', author: 'u_fallback', title: 'Fallback',
|
id: 't3_fallback', subreddit: 'sub1', author: 'u_fallback', title: 'Fallback',
|
||||||
@@ -360,14 +360,14 @@ describe('MonitorService', () => {
|
|||||||
recentWindowMinutes: 60,
|
recentWindowMinutes: 60,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
monitor = new MonitorService(
|
const redditApi = createMockRedditApi([fallback, older]);
|
||||||
createMockRedditApi([fallback, older]), createMockPostsRepo(), createMockMessagesRepo(), config, logger, 0,
|
monitor = new MonitorService(redditApi, createMockPostsRepo(), createMockMessagesRepo(), config, logger, 0);
|
||||||
);
|
|
||||||
|
|
||||||
await monitor.getUnprocessedPosts();
|
await expect(monitor.getUnprocessedPosts()).resolves.toEqual([fallback]);
|
||||||
|
|
||||||
|
expect(redditApi.isSellerProfile).toHaveBeenCalledTimes(2);
|
||||||
expect(logger.logs).toContainEqual(expect.objectContaining({
|
expect(logger.logs).toContainEqual(expect.objectContaining({
|
||||||
message: expect.stringContaining('1 older than eligible fallback'),
|
message: expect.stringContaining('2 eligible; 2 outside recent window'),
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user