Skip to content

Commit

Permalink
feat: sync system tags with dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Sep 24, 2024
1 parent f55cf19 commit 24c6af3
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/lib/override/adopted-probes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import _ from 'lodash';
import config from 'config';
import { scopedLogger } from '../logger.js';
import type { fetchProbesWithAdminData as serverFetchProbesWithAdminData } from '../ws/server.js';
import type { Probe, ProbeLocation } from '../../probe/types.js';
import type { Probe, ProbeLocation, Tag } from '../../probe/types.js';
import { normalizeFromPublicName } from '../geoip/utils.js';
import { getIndex } from '../location/location.js';

Expand All @@ -23,6 +23,7 @@ export type AdoptedProbe = {
type: 'user';
value: string;
}[];
systemTags: string[];
isCustomCity: boolean;
status: string;
isIPv4Supported: boolean,
Expand All @@ -40,18 +41,27 @@ export type AdoptedProbe = {
network: string | null;
}

type Row = Omit<AdoptedProbe, 'isCustomCity' | 'tags'> & {
type Row = Omit<AdoptedProbe, 'isCustomCity' | 'tags' | 'systemTags' | 'altIps' | 'isIPv4Supported' | 'isIPv6Supported'> & {
altIps: string;
tags: string;
systemTags: string;
isCustomCity: number;
isIPv4Supported: number;
isIPv6Supported: number;
}

type AdoptedFieldDescription = {
connectedField: string,
shouldUpdateIfCustomCity: boolean,
formatter?: (connectedValue: unknown) => unknown
}

export class AdoptedProbes {
private connectedIpToProbe: Map<string, Probe> = new Map();
private connectedUuidToProbe: Map<string, Probe> = new Map();
private adoptedProbes: AdoptedProbe[] = [];
private adoptedIpToProbe: Map<string, AdoptedProbe> = new Map();
private readonly adoptedFieldToConnectedField = {
private readonly adoptedFieldToConnectedField: Record<string, AdoptedFieldDescription> = {
status: {
connectedField: 'status',
shouldUpdateIfCustomCity: true,
Expand All @@ -76,6 +86,11 @@ export class AdoptedProbes {
connectedField: 'hardwareDevice',
shouldUpdateIfCustomCity: true,
},
systemTags: {
connectedField: 'tags',
shouldUpdateIfCustomCity: true,
formatter: (connectedTags: Tag[]) => connectedTags.filter(({ type }) => type === 'system').map(({ value }) => value),

Check failure on line 92 in src/lib/override/adopted-probes.ts

View workflow job for this annotation

GitHub Actions / Run e2e tests

Type '(connectedTags: Tag[]) => string[]' is not assignable to type '(connectedValue: unknown) => unknown'.

Check failure on line 92 in src/lib/override/adopted-probes.ts

View workflow job for this annotation

GitHub Actions / Run tests

Type '(connectedTags: Tag[]) => string[]' is not assignable to type '(connectedValue: unknown) => unknown'.
},
asn: {
connectedField: 'location.asn',
shouldUpdateIfCustomCity: true,
Expand Down Expand Up @@ -201,7 +216,10 @@ export class AdoptedProbes {
altIps: JSON.parse(row.altIps) as string[],
tags: (JSON.parse(row.tags) as { prefix: string; value: string; }[])
.map(({ prefix, value }) => ({ type: 'user' as const, value: `u-${prefix}-${value}` })),
systemTags: JSON.parse(row.systemTags) as string[],
isCustomCity: Boolean(row.isCustomCity),
isIPv4Supported: Boolean(row.isIPv4Supported),
isIPv6Supported: Boolean(row.isIPv6Supported),
}));

this.adoptedProbes = adoptedProbes;
Expand Down Expand Up @@ -269,18 +287,22 @@ export class AdoptedProbes {
return;
}

const updateObject: Record<string, string | number> = {};
const updateObject: Record<string, unknown> = {};

Object.entries(this.adoptedFieldToConnectedField).forEach(([ adoptedField, { connectedField, shouldUpdateIfCustomCity }]) => {
Object.entries(this.adoptedFieldToConnectedField).forEach(([ adoptedField, { connectedField, shouldUpdateIfCustomCity, formatter }]) => {
if (isCustomCity && !shouldUpdateIfCustomCity) {
return;
}

const adoptedValue = _.get(adoptedProbe, adoptedField) as string | number;
const connectedValue = _.get(connectedProbe, connectedField) as string | number;
const adoptedValue = _.get(adoptedProbe, adoptedField) as unknown;
let connectedValue = _.get(connectedProbe, connectedField) as unknown;

if (formatter) {
connectedValue = formatter(connectedValue);
}

if (adoptedValue !== connectedValue) {
updateObject[adoptedField] = connectedValue;
if (!_.isEqual(adoptedValue, connectedValue)) {
updateObject[adoptedField] = _.isObject(connectedValue) ? JSON.stringify(connectedValue) : connectedValue;
}
});

Expand Down Expand Up @@ -331,11 +353,11 @@ export class AdoptedProbes {
}
}

private async updateProbeData (adoptedProbe: AdoptedProbe, updateObject: Record<string, string | number>) {
private async updateProbeData (adoptedProbe: AdoptedProbe, updateObject: Record<string, unknown>) {
await this.sql(ADOPTED_PROBES_TABLE).where({ ip: adoptedProbe.ip }).update(updateObject);

for (const [ field, value ] of Object.entries(updateObject)) {
(adoptedProbe as unknown as Record<string, string | number>)[field] = value;
(adoptedProbe as Record<string, unknown>)[field] = value;
}
}

Expand Down

0 comments on commit 24c6af3

Please sign in to comment.