fix: new stuff

This commit is contained in:
Saahil 2025-03-27 11:18:10 -04:00
parent dbf8d79d60
commit e908cea93c
No known key found for this signature in database
GPG key ID: 8A8B64515254CFC6
2 changed files with 46 additions and 1 deletions

View file

@ -30,7 +30,7 @@ app.dbs = {};
app.dbs.ddm = new JSONdb("data/discord-datamining.json");
app.dbs.memdebug = new JSONdb("data/memdb.json");
app.dbs.seven39 = new JSONdb("data/739.json");
app.dbs.flightly = new JSONdb("data/flightly.json");
app.dbs.anondm = new EncryptedJsonDb("data/anondm.json", {
password: process.env.ANONDM_PASSWORD,
});

View file

@ -1,5 +1,6 @@
import dayjs from "dayjs";
import ms from "ms";
import { ModifiedApp } from "./slackapp";
export interface FlightData {
flight: Flight;
flightPosition: FlightPosition[];
@ -159,7 +160,51 @@ export function getFlightData(flightId: string): Promise<Flight> {
}
});
}
function detectChanges(newData, previousData) {
if (!previousData) {
console.log("Initial flight data loaded.");
previousData = newData;
return;
}
let changes = [];
function compareObjects(oldObj, newObj, path = "") {
for (let key in newObj) {
let newPath = path ? `${path}.${key}` : key;
if (typeof newObj[key] === "object" && newObj[key] !== null) {
compareObjects(oldObj[key] || {}, newObj[key], newPath);
} else if (oldObj[key] !== newObj[key]) {
changes.push(`${newPath}: ${oldObj[key]}${newObj[key]}`);
}
}
}
compareObjects(previousData, newData);
if (changes.length > 0) {
return changes;
console.log("Flight data changed:");
console.log(changes.join("\n"));
} else {
// console.log("No changes detected.");
}
// previousData = newData;
}
export async function cronForTrackingData(app: ModifiedApp) {
const IdsToTrack = app.dbs.flightly.get("flightly-ids")
for(const {flightId, userId} of IdsToTrack){
const flightD = await getFlightData(flightId);
const changes = await detectChanges(flightD, app.dbs.flightly.get(userId));
await app.client.chat.postMessage({
channel: userId,
text: changes.join("\n")
})
app.dbs.flightly.set(userId, flightD);
await new Promise(r => setTimeout(r, 1000));
}
}
export async function getTextVersionOfData(flightId: string) {
const flightD = await getFlightData(flightId);
return `