Skip to content

Commit bfd85b7

Browse files
feat: add AdminData class
1 parent ff6dc9c commit bfd85b7

File tree

5 files changed

+80
-21
lines changed

5 files changed

+80
-21
lines changed

src/lib/admin-data.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { Knex } from 'knex';
2+
import type { Probe } from '../probe/types.js';
3+
import { scopedLogger } from './logger.js';
4+
5+
const logger = scopedLogger('admin-data');
6+
7+
export class AdminData {
8+
constructor (private readonly sql: Knex) {}
9+
10+
scheduleSync () {
11+
setTimeout(() => {
12+
this.syncDashboardData()
13+
.finally(() => this.scheduleSync())
14+
.catch(error => logger.error(error));
15+
}, 60_000).unref();
16+
}
17+
18+
async syncDashboardData () {
19+
// Implement
20+
}
21+
22+
getUpdatedLocation (probe: Probe) {
23+
// Implement
24+
return probe.location;
25+
}
26+
}

src/lib/adopted-probes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Knex } from 'knex';
22
import Bluebird from 'bluebird';
33
import _ from 'lodash';
44
import { scopedLogger } from './logger.js';
5-
import type { fetchRawProbes as serverFetchRawProbes } from './ws/server.js';
5+
import type { fetchProbesWithAdminData as serverFetchProbesWithAdminData } from './ws/server.js';
66
import type { Probe } from '../probe/types.js';
77
import { normalizeFromPublicName } from './geoip/utils.js';
88

@@ -88,7 +88,7 @@ export class AdoptedProbes {
8888

8989
constructor (
9090
private readonly sql: Knex,
91-
private readonly fetchRawProbes: typeof serverFetchRawProbes,
91+
private readonly fetchProbesWithAdminData: typeof serverFetchProbesWithAdminData,
9292
) {}
9393

9494
getByIp (ip: string) {
@@ -134,7 +134,7 @@ export class AdoptedProbes {
134134
}
135135

136136
async syncDashboardData () {
137-
const allProbes = await this.fetchRawProbes();
137+
const allProbes = await this.fetchProbesWithAdminData();
138138
this.connectedIpToProbe = new Map(allProbes.map(probe => [ probe.ipAddress, probe ]));
139139
this.connectedUuidToIp = new Map(allProbes.map(probe => [ probe.uuid, probe.ipAddress ]));
140140

src/lib/probe-override.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,38 @@
11
import { getIndex } from '../probe/builder.js';
22
import type { Probe } from '../probe/types.js';
33
import type { AdoptedProbes } from './adopted-probes.js';
4+
import type { AdminData } from './admin-data.js';
45

56
export class ProbeOverride {
6-
constructor (private readonly adoptedProbes: AdoptedProbes) {}
7+
constructor (
8+
private readonly adoptedProbes: AdoptedProbes,
9+
private readonly adminData: AdminData,
10+
) {}
711

812
async syncDashboardData () {
9-
await this.adoptedProbes.syncDashboardData();
13+
await Promise.all([
14+
this.adoptedProbes.syncDashboardData(),
15+
this.adminData.syncDashboardData(),
16+
]);
1017
}
1118

1219
scheduleSync () {
1320
this.adoptedProbes.scheduleSync();
21+
this.adminData.scheduleSync();
1422
}
1523

1624
getUpdatedLocation (probe: Probe) {
17-
return this.adoptedProbes.getUpdatedLocation(probe);
25+
const adminLocation = this.adminData.getUpdatedLocation(probe);
26+
const adoptedLocation = this.adoptedProbes.getUpdatedLocation(probe);
27+
return { ...adminLocation, ...adoptedLocation };
1828
}
1929

20-
addOverrideData (probes: Probe[]) {
30+
addAdminData (probes: Probe[]) {
31+
// Implement
32+
return probes;
33+
}
34+
35+
addAdoptedData (probes: Probe[]) {
2136
return probes.map((probe) => {
2237
const adopted = this.adoptedProbes.getByIp(probe.ipAddress);
2338

src/lib/ws/server.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { client } from '../sql/client.js';
99
import { ProbeOverride } from '../probe-override.js';
1010
import { ProbeIpLimit } from './helper/probe-ip-limit.js';
1111
import { AdoptedProbes } from '../adopted-probes.js';
12+
import { AdminData } from '../admin-data.js';
1213

1314
export type SocketData = {
1415
probe: Probe;
@@ -76,24 +77,34 @@ export const fetchRawSockets = async () => {
7677
return io.of(PROBES_NAMESPACE).fetchSockets();
7778
};
7879

79-
export const fetchProbes = async ({ allowStale = true } = {}): Promise<Probe[]> => {
80+
export const fetchRawProbes = async (): Promise<Probe[]> => {
8081
if (!syncedProbeList) {
8182
throw new Error('WS server not initialized yet');
8283
}
8384

84-
return allowStale ? syncedProbeList.getProbes() : syncedProbeList.fetchProbes();
85+
return syncedProbeList.getRawProbes();
8586
};
8687

87-
export const fetchRawProbes = async (): Promise<Probe[]> => {
88+
export const fetchProbesWithAdminData = async (): Promise<Probe[]> => {
8889
if (!syncedProbeList) {
8990
throw new Error('WS server not initialized yet');
9091
}
9192

92-
return syncedProbeList.getRawProbes();
93+
return syncedProbeList.getProbesWithAdminData();
94+
};
95+
96+
export const fetchProbes = async ({ allowStale = true } = {}): Promise<Probe[]> => {
97+
if (!syncedProbeList) {
98+
throw new Error('WS server not initialized yet');
99+
}
100+
101+
return allowStale ? syncedProbeList.getProbes() : syncedProbeList.fetchProbes();
93102
};
94103

95104
export const adoptedProbes = new AdoptedProbes(client, fetchRawProbes);
96105

97-
export const probeOverride = new ProbeOverride(adoptedProbes);
106+
export const adminData = new AdminData(client);
107+
108+
export const probeOverride = new ProbeOverride(adoptedProbes, adminData);
98109

99110
export const probeIpLimit = new ProbeIpLimit(fetchProbes, fetchRawSockets);

src/lib/ws/synced-probe-list.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export class SyncedProbeList extends EventEmitter {
4545

4646
private logger: winston.Logger;
4747
private rawProbes: Probe[];
48+
private probesWithAdminData: Probe[];
4849
private probes: Probe[];
4950
private oldest: number;
5051
private pushTimer: NodeJS.Timeout | undefined;
@@ -59,6 +60,7 @@ export class SyncedProbeList extends EventEmitter {
5960
this.nodeId = randomBytes(8).toString('hex');
6061
this.logger = scopedLogger('synced-probe-list', this.nodeId);
6162
this.rawProbes = [];
63+
this.probesWithAdminData = [];
6264
this.probes = [];
6365
this.oldest = Infinity;
6466
this.lastReadEventId = Date.now().toString();
@@ -72,6 +74,18 @@ export class SyncedProbeList extends EventEmitter {
7274
});
7375
}
7476

77+
getRawProbes (): Probe[] {
78+
return this.rawProbes.slice();
79+
}
80+
81+
getProbesWithAdminData (): Probe[] {
82+
return this.probesWithAdminData.slice();
83+
}
84+
85+
getProbes (): Probe[] {
86+
return this.probes.slice();
87+
}
88+
7589
async fetchProbes (): Promise<Probe[]> {
7690
const start = Date.now();
7791

@@ -87,14 +101,6 @@ export class SyncedProbeList extends EventEmitter {
87101
});
88102
}
89103

90-
getProbes (): Probe[] {
91-
return this.probes.slice();
92-
}
93-
94-
getRawProbes (): Probe[] {
95-
return this.rawProbes.slice();
96-
}
97-
98104
private updateProbes () {
99105
const probes = [];
100106
let oldest = Infinity;
@@ -108,7 +114,8 @@ export class SyncedProbeList extends EventEmitter {
108114
}
109115

110116
this.rawProbes = probes;
111-
this.probes = this.probeOverride.addOverrideData(probes);
117+
this.probesWithAdminData = this.probeOverride.addAdminData(probes);
118+
this.probes = this.probeOverride.addAdoptedData(this.probesWithAdminData);
112119
this.oldest = oldest;
113120

114121
this.emit(this.localUpdateEvent);

0 commit comments

Comments
 (0)