mirror of
https://github.com/SrIzan10/flight-slack.git
synced 2026-06-06 00:56:52 +00:00
chore: improve poll intervals
This commit is contained in:
@@ -75,43 +75,56 @@ export class FlightUpdater {
|
||||
this.flightIntervals.set(flight.id, interval);
|
||||
}
|
||||
|
||||
// next function partly based off code generated by openrouter horizon beta
|
||||
// some checks modified and/or deleted
|
||||
// n * 60 * 1000 = n minutes (in milliseconds for setInterval)
|
||||
private calculatePollInterval(flight: Flight): number {
|
||||
const now = new Date();
|
||||
const scheduledOff = new Date(flight.scheduledOff);
|
||||
const scheduledOn = new Date(flight.scheduledOn);
|
||||
|
||||
// pre-departure phase (more than 2 hours before takeoff)
|
||||
if (now < new Date(scheduledOff.getTime() - 2 * 60 * 60 * 1000)) {
|
||||
return 30 * 60 * 1000; // 30 minutes
|
||||
// far out (> 24h)
|
||||
if (now < new Date(scheduledOff.getTime() - 24 * 60 * 60 * 1000)) {
|
||||
return 3 * 60 * 60 * 1000;
|
||||
}
|
||||
|
||||
// pre-departure phase (less than 2 hours before takeoff)
|
||||
|
||||
// 24h to 4h pre-departure
|
||||
if (now < new Date(scheduledOff.getTime() - 4 * 60 * 60 * 1000)) {
|
||||
return 2 * 60 * 60 * 1000;
|
||||
}
|
||||
|
||||
// 4h to 1h pre-departure
|
||||
if (now < new Date(scheduledOff.getTime() - 60 * 60 * 1000)) {
|
||||
return 30 * 60 * 1000;
|
||||
}
|
||||
|
||||
// 60m to 15m pre-departure
|
||||
if (now < new Date(scheduledOff.getTime() - 15 * 60 * 1000)) {
|
||||
return 10 * 60 * 1000;
|
||||
}
|
||||
|
||||
if (now < scheduledOff) {
|
||||
return 5 * 60 * 1000; // 5 minutes
|
||||
return 3 * 60 * 1000;
|
||||
}
|
||||
|
||||
// In-flight phase
|
||||
if (!flight.actualOn && now < new Date(scheduledOn.getTime() + 2 * 60 * 60 * 1000)) {
|
||||
// frequent updates during takeoff and initial 5 minute climb
|
||||
if (now < new Date(scheduledOff.getTime() + 5 * 60 * 1000)) {
|
||||
return 2 * 60 * 1000; // 2 minutes
|
||||
|
||||
const inflightWindowEnd = new Date(scheduledOn.getTime() + 2 * 60 * 60 * 1000);
|
||||
if (!flight.actualOn && now < inflightWindowEnd) {
|
||||
if (flight.progressPercent) {
|
||||
if (flight.progressPercent < 10) return 3 * 60 * 1000;
|
||||
if (flight.progressPercent > 90) return 5 * 60 * 1000;
|
||||
} else {
|
||||
// first 15 minutes after scheduledOff
|
||||
if (now < new Date(scheduledOff.getTime() + 15 * 60 * 1000)) {
|
||||
return 4 * 60 * 1000;
|
||||
}
|
||||
}
|
||||
|
||||
// Medium frequency during cruise
|
||||
const flightDuration = scheduledOn.getTime() - scheduledOff.getTime();
|
||||
const timeInFlight = now.getTime() - scheduledOff.getTime();
|
||||
const progressPercent = (timeInFlight / flightDuration) * 100;
|
||||
|
||||
// frequent updates during landing
|
||||
if (progressPercent > 80) {
|
||||
return 3 * 60 * 1000; // 3 minutes
|
||||
}
|
||||
|
||||
return 8 * 60 * 1000; // 8 minutes during cruise
|
||||
|
||||
// cruisin
|
||||
return 12 * 60 * 1000;
|
||||
}
|
||||
|
||||
// flight arrived
|
||||
return 15 * 60 * 1000;
|
||||
|
||||
// post arrival (eventually deleted)
|
||||
return 10 * 60 * 1000;
|
||||
}
|
||||
|
||||
private async updateSingleFlight(flight: Flight) {
|
||||
|
||||
Reference in New Issue
Block a user