Snippets

Piotr Szrajber Unique icao codes

Created by Piotr Szrajber
/**
 * Można to na przykład wkleić w konsolę javascript (F12 w Chrome)
 *
 * Przykładowy output na konsoli:
 *
 * VM575:40: Received 821 records with 36 unique icao codes
 * VM575:40 In air: 36; removed: 0; added:0
 * VM575:41 Last timestamp contains similar number of objects than the previous one
 * VM575:42 Icaos by timestamp: (30) [28, 29, 28, 27, 27, 27, 27, 28, 27, 27, 28, 29, 28, 28, 28, 28, 25, 28, 28, 27, 24, 25, 26, 27, 27, 28, 31, 29, 28, 24]
 * VM575:39 Received 808 records with 35 unique icao codes
 * VM575:40 In air: 35; removed: 1; added:0
 * VM575:41 Last timestamp contains significantly less icaos than the previous one
 * VM575:42 Icaos by timestamp: (30) [29, 28, 27, 27, 27, 27, 28, 27, 27, 28, 29, 28, 28, 28, 28, 25, 28, 28, 27, 24, 25, 26, 27, 27, 28, 31, 29, 28, 26, 13]
 * VM575:39 Received 806 records with 35 unique icao codes
 * VM575:40 In air: 35; removed: 0; added:0
 * VM575:41 Last timestamp contains similar number of objects than the previous one
 * VM575:42 Icaos by timestamp: (30) [28, 27, 27, 27, 27, 28, 27, 27, 28, 29, 28, 28, 28, 28, 25, 28, 28, 27, 24, 25, 26, 27, 27, 28, 31, 29, 28, 26, 13, 27]
 */
let memoryStore = new Set();
getData = async function() {
    const response = await fetch("https://airspac.eu/adsb-sources/getCreotechV2AdsbTicks");
    const data = await response.json();
    const icaos = new Set(data.map(item => item.icao));
    const timestamps = new Set(data.map(item => item.timestamp));
    const icaosByTimestamp = {};
    for (let ts of timestamps) {
        icaosByTimestamp[ts] = new Set(data.filter(item => item.timestamp === ts).map(item => item.icao)).size;
    }
    let toRemove = 0;
    let toAdd = 0;
    for (let prevKey of memoryStore) {
        if (!icaos.has(prevKey)) {
            toRemove++;
            memoryStore.delete(prevKey);
        }
    }
    for (let nextKey of icaos) {
        if (!memoryStore.has(nextKey)) {
            toAdd++;
            memoryStore.add(nextKey);
        }
    }

    const icaosByTimestampValues = Object.values(icaosByTimestamp);
    const lastSignificantlyLower = icaosByTimestampValues.length > 2 && icaosByTimestampValues[icaosByTimestampValues.length - 1] / icaosByTimestampValues[icaosByTimestampValues.length - 2] < .8;

    console.log(`Received ${data.length} records with ${icaos.size} unique icao codes`);
    console.log(`In air: ${memoryStore.size}; removed: ${toRemove}; added:${toAdd}`);
    console.log(lastSignificantlyLower ? `Last timestamp contains significantly less icaos than the previous one` : `Last timestamp contains similar number of objects than the previous one`);
    console.log(`Icaos by timestamp:`, icaosByTimestampValues);
}
var handle = setInterval(getData, 1000);

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.