Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add probe stats scripts #377

Merged
merged 4 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ data/IP_BLACKLIST.json
data/AWS_IP_RANGES.json
data/GCP_IP_RANGES.json
.eslintcache
probes-stats/known-result.csv
probes-stats/known-result.json
probes-stats/all-result.csv
probes-stats/all-result.json
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@
"clean": "rimraf coverage",
"download:jsons": "tsx src/lib/download-jsons.ts",
"prepare": "npm run download:jsons; husky install || echo 'Failed to install husky'",
"stats:known": "NEW_RELIC_ENABLED=false NEW_RELIC_LOG_ENABLED=false tsx probes-stats/known.ts",
"stats:all": "NEW_RELIC_ENABLED=false NEW_RELIC_LOG_ENABLED=false tsx probes-stats/all.ts",
"start": "NODE_ENV=production node --max_old_space_size=3584 --max-semi-space-size=128 --experimental-loader newrelic/esm-loader.mjs dist/index.js",
"start:dev": "NODE_ENV=development FAKE_PROBE_IP=1 NEW_RELIC_ENABLED=false tsx src/index.ts",
"start:test": "NODE_ENV=test FAKE_PROBE_IP=1 NEW_RELIC_ENABLED=false tsx src/index.ts",
Expand Down
73 changes: 73 additions & 0 deletions probes-stats/all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* How to run:
* 1. Run redis instance
* 2. Run with fulfilled keys: `ADMIN_KEY= IPINFO_API_KEY= MAXMIND_ACCOUNT_ID= MAXMIND_LICENSE_KEY= npm run stats:all`
*/

import got from 'got';
import fs from 'node:fs';
import { getRedisClient, initRedis } from '../src/lib/redis/client.js';
import { LocationInfo, createGeoipClient } from '../src/lib/geoip/client.js';
import { FastlyBundledResponse, fastlyLookup } from '../src/lib/geoip/providers/fastly.js';
import { ipinfoLookup } from '../src/lib/geoip/providers/ipinfo.js';
import { maxmindLookup } from '../src/lib/geoip/providers/maxmind.js';
import { populateMemList as populateIpWhiteList } from '../src/lib/geoip/whitelist.js';

await populateIpWhiteList();
await initRedis();
const geoIpClient = createGeoipClient();

type ResultItem = {
ip: string,
ipinfo: string,
maxmind: string,
fastly: string,
algorithm: string,
};

const getData = async () => {
const result: ResultItem[] = [];
const probes = await got(`https://api.globalping.io/v1/probes?adminkey=${process.env['ADMIN_KEY']}`).json<{ipAddress: string}[]>();

await Promise.all(probes.map(async (probe) => {
const ip = probe.ipAddress;

const ipinfo = await geoIpClient.lookupWithCache<LocationInfo>(`geoip:ipinfo:${ip}`, async () => ipinfoLookup(ip)).catch(err => ({ normalizedCity: err.message.toUpperCase() }));
const maxmind = await geoIpClient.lookupWithCache<LocationInfo>(`geoip:maxmind:${ip}`, async () => maxmindLookup(ip)).catch(err => ({ normalizedCity: err.message.toUpperCase() }));
const fastly = await geoIpClient.lookupWithCache<FastlyBundledResponse>(`geoip:fastly:${ip}`, async () => fastlyLookup(ip)).catch(err => ({ location: { normalizedCity: err.message.toUpperCase() } }));

const updatedRow = {
ip,
ipinfo: ipinfo.normalizedCity,
maxmind: maxmind.normalizedCity,
fastly: fastly.location.normalizedCity,
} as ResultItem;

try {
const location = await geoIpClient.lookup(ip);
updatedRow['algorithm'] = location.normalizedCity;
} catch (err) {
updatedRow['algorithm'] = err.message.toUpperCase();
}

result.push(updatedRow);
}));

return result;
};

const generateFiles = (data: ResultItem[]) => {
const csvContent = [
'ip,ipinfo,maxmind,fastly,algorithm',
...data.map(row => `${row.ip},${row.ipinfo},${row.maxmind},${row.fastly},${row.algorithm}`),
].join('\n');
fs.writeFileSync('./probes-stats/all-result.json', JSON.stringify(data, null, 2));
fs.writeFileSync('./probes-stats/all-result.csv', csvContent);
};

(async () => {
const data = await getData();
generateFiles(data);
const redis = getRedisClient();
await redis.disconnect();
})();
8 changes: 8 additions & 0 deletions probes-stats/known-probes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"ip": "exampleIp",
"city": "exampleCity",
"country": "exampleCountry",
"note": "exampleNote"
}
]
93 changes: 93 additions & 0 deletions probes-stats/known.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* How to run:
* 1. Run redis instance
* 2. Replace content of the known-probes.json with the known probes info in the same format
* 3. Run with fulfilled keys: `IPINFO_API_KEY= MAXMIND_ACCOUNT_ID= MAXMIND_LICENSE_KEY= npm run stats:known`
*/

import fs from 'node:fs';
import { getRedisClient, initRedis } from '../src/lib/redis/client.js';
import { LocationInfo, createGeoipClient } from '../src/lib/geoip/client.js';
import { normalizeCityName } from '../src/lib/geoip/utils.js';
import { FastlyBundledResponse, fastlyLookup } from '../src/lib/geoip/providers/fastly.js';
import { ipinfoLookup } from '../src/lib/geoip/providers/ipinfo.js';
import { maxmindLookup } from '../src/lib/geoip/providers/maxmind.js';
import { populateMemList as populateIpWhiteList } from '../src/lib/geoip/whitelist.js';
import sheet from './known-probes.json' assert { type: 'json' };

await populateIpWhiteList();
await initRedis();
const geoIpClient = createGeoipClient();

type ResultItem = {
ip: string,
city: string,
ipinfo: string,
maxmind: string,
fastly: string,
algorithm: string,
country: string,
note: string,
};

const getData = async () => {
const result: ResultItem[] = [];

await Promise.all(sheet.map(async (row) => {
const normalizedCity = normalizeCityName(row.city);

const ipinfo = await geoIpClient.lookupWithCache<LocationInfo>(`geoip:ipinfo:${row.ip}`, async () => ipinfoLookup(row.ip));
const maxmind = await geoIpClient.lookupWithCache<LocationInfo>(`geoip:maxmind:${row.ip}`, async () => maxmindLookup(row.ip));
const fastly = await geoIpClient.lookupWithCache<FastlyBundledResponse>(`geoip:fastly:${row.ip}`, async () => fastlyLookup(row.ip));

const updatedRow = {
...row,
city: normalizedCity,
ipinfo: ipinfo.normalizedCity,
maxmind: maxmind.normalizedCity,
fastly: fastly.location.normalizedCity,
} as ResultItem;

try {
const location = await geoIpClient.lookup(row.ip);
updatedRow['algorithm'] = location.normalizedCity;
} catch (err) {
updatedRow['algorithm'] = err.message.toUpperCase();
}

result.push(updatedRow);
}));

return result;
};

const getAccuracy = (result) => {
const ipinfoTrueCount = result.filter(row => row.ipinfo === row.city).length;
const maxmindTrueCount = result.filter(row => row.maxmind === row.city).length;
const fastlyTrueCount = result.filter(row => row.fastly === row.city).length;
const algorithmTrueCount = result.filter(row => row.algorithm === row.city).length;
return {
ipinfo: (ipinfoTrueCount / result.length).toFixed(2),
maxmind: (maxmindTrueCount / result.length).toFixed(2),
fastly: (fastlyTrueCount / result.length).toFixed(2),
algorithm: (algorithmTrueCount / result.length).toFixed(2),
};
};

const generateFiles = (data: ResultItem[], acc: {ipinfo: string, maxmind: string, fastly: string, algorithm: string}) => {
const csvContent = [
'ip,real city,ipinfo,maxmind,fastly,algorithm',
`accuracy:,1,${acc.ipinfo},${acc.maxmind},${acc.fastly},${acc.algorithm}`,
...data.map(row => `${row.ip},${row.city},${row.ipinfo},${row.maxmind},${row.fastly},${row.algorithm}`),
].join('\n');
fs.writeFileSync('./probes-stats/known-result.json', JSON.stringify(data, null, 2));
fs.writeFileSync('./probes-stats/known-result.csv', csvContent);
};

(async () => {
const data = await getData();
const acc = getAccuracy(data);
generateFiles(data, acc);
const redis = getRedisClient();
await redis.disconnect();
})();
2 changes: 1 addition & 1 deletion src/lib/geoip/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export default class GeoipClient {
return _.omit(best, 'provider');
}

private async lookupWithCache<T> (key: string, fn: () => Promise<T>): Promise<T> {
public async lookupWithCache<T> (key: string, fn: () => Promise<T>): Promise<T> {
const cached = await this.cache.get<T>(key);

if (cached) {
Expand Down