Skip to content

Commit

Permalink
Adding randomness when selecting oldest provider
Browse files Browse the repository at this point in the history
  • Loading branch information
dhakalaashish committed May 6, 2024
1 parent 7fbf805 commit 3f50be7
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions apps/server/src/pages/api/cron/geo-event-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default async function alertFetcher(req: NextApiRequest, res: NextApiResp

let newSiteAlertCount = 0;
let processedProviders = 0;
const fetchCount = Math.max(5, limit * 2);
// while (processedProviders <= limit) {
const activeProviders: GeoEventProvider[] = await prisma.$queryRaw`
SELECT *
Expand All @@ -63,8 +64,18 @@ export default async function alertFetcher(req: NextApiRequest, res: NextApiResp
AND "fetchFrequency" IS NOT NULL
AND ("lastRun" + ("fetchFrequency" || ' minutes')::INTERVAL) < (current_timestamp AT TIME ZONE 'UTC')
ORDER BY (current_timestamp AT TIME ZONE 'UTC' - "lastRun") DESC
LIMIT ${limit};
LIMIT ${fetchCount};
`;
function shuffleArray(array: GeoEventProvider[]) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]]; // swap elements
}
return array;
}
const shuffledProviders = shuffleArray([...activeProviders]);
const selectedProviders = shuffledProviders.slice(0, limit);

// Filter out those active providers whose last (run date + fetchFrequency (in minutes) > current time
// Break the loop if there are no active providers

Expand All @@ -73,7 +84,7 @@ export default async function alertFetcher(req: NextApiRequest, res: NextApiResp
// break;
// }

logger(`Running Geo Event Fetcher. Taking ${activeProviders.length} eligible providers.`, "info");
logger(`Running Geo Event Fetcher. Taking ${selectedProviders.length} eligible providers.`, "info");

// Define Chunk Size for processGeoEvents
const chunkSize = 2000;
Expand All @@ -88,7 +99,7 @@ export default async function alertFetcher(req: NextApiRequest, res: NextApiResp
}

// Loop for each active provider and fetch geoEvents
const promises = activeProviders.map(async (provider) => {
const promises = selectedProviders.map(async (provider) => {
const { config, id: geoEventProviderId, clientId: geoEventProviderClientId, clientApiKey, lastRun } = provider
// For GOES-16, geoEventProviderId is 55, geoEventProviderClientId is GEOSTATIONARY, and clientApiKey is GOES-16
const parsedConfig: GeoEventProviderConfig = JSON.parse(JSON.stringify(config))
Expand Down

0 comments on commit 3f50be7

Please sign in to comment.